chore: remove duplicated addDecimal function (#1908)

* chore: remove duplicated addDecimal function

* style: lint

* test: fix e2e test
This commit is contained in:
Dexter Edwards 2022-11-01 01:56:01 +00:00 committed by GitHub
parent da6a3755db
commit ac03ad8e3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 21 additions and 57 deletions

View File

@ -102,7 +102,7 @@ context(
.contains(2.0, epochTimeout)
.should('be.visible');
cy.get(nominatedStake).should('have.text', 2); // 2001-STKE-017 2002-SINC-007
cy.get(nominatedStake).should('have.text', '2.000000000000000000'); // 2001-STKE-017 2002-SINC-007
cy.navigate_to('staking');

View File

@ -8,7 +8,6 @@ import { useTranslation } from 'react-i18next';
import noIcon from '../../images/token-no-icon.png';
import vegaBlack from '../../images/vega_black.png';
import { BigNumber } from '../../lib/bignumber';
import { addDecimal } from '../../lib/decimals';
import type { WalletCardAssetProps } from '../wallet-card';
import type {
Delegations,
@ -20,6 +19,7 @@ import { useContracts } from '../../contexts/contracts/contracts-context';
import type { ERC20Asset } from '@vegaprotocol/assets';
import { isAssetTypeERC20 } from '@vegaprotocol/assets';
import { AccountType } from '@vegaprotocol/types';
import { addDecimal } from '@vegaprotocol/react-helpers';
const DELEGATIONS_QUERY = gql`
query Delegations($partyId: ID!) {
@ -130,7 +130,7 @@ export const usePollForDelegations = () => {
symbol: a.asset.symbol,
decimals: a.asset.decimals,
balance: new BigNumber(
addDecimal(new BigNumber(a.balance), a.asset.decimals)
addDecimal(a.balance, a.asset.decimals)
),
image: isVega ? vegaBlack : noIcon,
border: isVega,

View File

@ -2,11 +2,10 @@ import type { FieldFunctionOptions, Reference } from '@apollo/client';
import { ApolloClient, from, HttpLink, InMemoryCache } from '@apollo/client';
import { onError } from '@apollo/client/link/error';
import { RetryLink } from '@apollo/client/link/retry';
import { addDecimal } from '@vegaprotocol/react-helpers';
import sortBy from 'lodash/sortBy';
import uniqBy from 'lodash/uniqBy';
import { BigNumber } from './bignumber';
import { addDecimal } from './decimals';
import { deterministicShuffle } from './deterministic-shuffle';
// Create seed in memory. Validator list order will remain the same
@ -20,7 +19,7 @@ export function createClient(base?: string) {
throw new Error('Base must be passed into createClient!');
}
const formatUintToNumber = (amount: string, decimals = 18) =>
addDecimal(new BigNumber(amount), decimals).toString();
addDecimal(amount, decimals).toString();
const createReadField = (fieldName: string) => ({
[`${fieldName}Formatted`]: {

View File

@ -1,25 +0,0 @@
import { BigNumber } from './bignumber';
import { addDecimal, removeDecimal } from './decimals';
it('Do not pad numbers with 0s when the number length is less than the specified DPs', () => {
expect(addDecimal(new BigNumber(10000), 10)).toEqual('0.000001');
});
it('Handles large numbers correctly', () => {
const claimCode = new BigNumber('20000000000000000000000000');
const decimals = 18;
const decimalised = addDecimal(claimCode, decimals);
expect(decimalised.toString()).toEqual('20000000');
const undecimalised = removeDecimal(claimCode, decimals);
expect(undecimalised.toString()).toEqual(
'20000000000000000000000000000000000000000000'
);
const mangled = removeDecimal(
new BigNumber(addDecimal(claimCode, decimals)),
decimals
);
expect(mangled.toString()).toEqual('20000000000000000000000000');
});

View File

@ -1,12 +0,0 @@
import type { BigNumber } from '../lib/bignumber';
export function addDecimal(value: BigNumber, decimals: number): string {
return value
.dividedBy(Math.pow(10, decimals))
.decimalPlaces(decimals)
.toString();
}
export function removeDecimal(value: BigNumber, decimals: number): string {
return value.times(Math.pow(10, decimals)).toFixed(0);
}

View File

@ -1,6 +1,6 @@
import { BigNumber } from '../../lib/bignumber';
import { addDecimal } from '../../lib/decimals';
import type { IClaimTokenParams } from '@vegaprotocol/smart-contracts';
import { addDecimal } from '@vegaprotocol/react-helpers';
export enum ClaimStatus {
Ready,
@ -107,7 +107,6 @@ export function claimReducer(
error: new Error('Invalid code'),
};
} else {
const denomination = new BigNumber(action.data.amount);
return {
...state,
claimData: {
@ -118,7 +117,9 @@ export function claimReducer(
v: Number(action.data.v),
},
claim: {
amount: new BigNumber(addDecimal(denomination, action.decimals)),
amount: new BigNumber(
addDecimal(action.data.amount, action.decimals)
),
target: action.data.target ?? null,
tranche: Number(action.data.trancheId),
expiry: Number(action.data.expiry),

View File

@ -3,10 +3,10 @@ import * as Sentry from '@sentry/react';
import { Button, Callout, Intent, Loader } from '@vegaprotocol/ui-toolkit';
import { useTranslation } from 'react-i18next';
import { useAppState } from '../../../contexts/app-state/app-state-context';
import { BigNumber } from '../../../lib/bignumber';
import { removeDecimal } from '../../../lib/decimals';
import type { BigNumber } from '../../../lib/bignumber';
import type { UndelegateSubmissionBody } from '@vegaprotocol/wallet';
import { useVegaWallet } from '@vegaprotocol/wallet';
import { removeDecimal } from '@vegaprotocol/react-helpers';
interface PendingStakeProps {
pendingAmount: BigNumber;
@ -37,10 +37,7 @@ export const PendingStake = ({
const command: UndelegateSubmissionBody = {
undelegateSubmission: {
nodeId,
amount: removeDecimal(
new BigNumber(pendingAmount),
appState.decimals
),
amount: removeDecimal(pendingAmount.toString(), appState.decimals),
method: 'METHOD_NOW',
},
};

View File

@ -8,7 +8,6 @@ import { TokenInput } from '../../../components/token-input';
import { useAppState } from '../../../contexts/app-state/app-state-context';
import { useSearchParams } from '../../../hooks/use-search-params';
import { BigNumber } from '../../../lib/bignumber';
import { addDecimal, removeDecimal } from '../../../lib/decimals';
import type {
PartyDelegations,
PartyDelegationsVariables,
@ -29,7 +28,12 @@ import type {
UndelegateSubmissionBody,
} from '@vegaprotocol/wallet';
import { useVegaWallet } from '@vegaprotocol/wallet';
import { useNetworkParam, NetworkParams } from '@vegaprotocol/react-helpers';
import {
useNetworkParam,
NetworkParams,
removeDecimal,
addDecimal,
} from '@vegaprotocol/react-helpers';
export const PARTY_DELEGATIONS_QUERY = gql`
query PartyDelegations($partyId: ID!) {
@ -107,7 +111,7 @@ export const StakingForm = ({
);
const minTokensWithDecimals = React.useMemo(() => {
const minTokens = new BigNumber(minAmount !== null ? minAmount : '');
const minTokens = minAmount !== null ? minAmount : '';
return addDecimal(minTokens, appState.decimals);
}, [appState.decimals, minAmount]);
@ -128,13 +132,13 @@ export const StakingForm = ({
const delegateInput: DelegateSubmissionBody = {
delegateSubmission: {
nodeId,
amount: removeDecimal(new BigNumber(amount), appState.decimals),
amount: removeDecimal(amount, appState.decimals),
},
};
const undelegateInput: UndelegateSubmissionBody = {
undelegateSubmission: {
nodeId,
amount: removeDecimal(new BigNumber(amount), appState.decimals),
amount: removeDecimal(amount, appState.decimals),
method:
removeType === RemoveType.Now
? 'METHOD_NOW'