From ac03ad8e3d1d5a6035305d6c070e06f3d60b6c4e Mon Sep 17 00:00:00 2001 From: Dexter Edwards Date: Tue, 1 Nov 2022 01:56:01 +0000 Subject: [PATCH] chore: remove duplicated addDecimal function (#1908) * chore: remove duplicated addDecimal function * style: lint * test: fix e2e test --- .../src/integration/flow/staking-flow.cy.js | 2 +- .../token/src/components/vega-wallet/hooks.ts | 4 +-- apps/token/src/lib/apollo-client.ts | 5 ++-- apps/token/src/lib/decimals.test.ts | 25 ------------------- apps/token/src/lib/decimals.ts | 12 --------- apps/token/src/routes/claim/claim-reducer.ts | 7 +++--- .../src/routes/staking/node/pending-stake.tsx | 9 +++---- .../src/routes/staking/node/staking-form.tsx | 14 +++++++---- 8 files changed, 21 insertions(+), 57 deletions(-) delete mode 100644 apps/token/src/lib/decimals.test.ts delete mode 100644 apps/token/src/lib/decimals.ts diff --git a/apps/token-e2e/src/integration/flow/staking-flow.cy.js b/apps/token-e2e/src/integration/flow/staking-flow.cy.js index 3a89a8584..11f329545 100644 --- a/apps/token-e2e/src/integration/flow/staking-flow.cy.js +++ b/apps/token-e2e/src/integration/flow/staking-flow.cy.js @@ -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'); diff --git a/apps/token/src/components/vega-wallet/hooks.ts b/apps/token/src/components/vega-wallet/hooks.ts index 156b3f69f..7801c031b 100644 --- a/apps/token/src/components/vega-wallet/hooks.ts +++ b/apps/token/src/components/vega-wallet/hooks.ts @@ -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, diff --git a/apps/token/src/lib/apollo-client.ts b/apps/token/src/lib/apollo-client.ts index ec0677147..14c57794c 100644 --- a/apps/token/src/lib/apollo-client.ts +++ b/apps/token/src/lib/apollo-client.ts @@ -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`]: { diff --git a/apps/token/src/lib/decimals.test.ts b/apps/token/src/lib/decimals.test.ts deleted file mode 100644 index 92f16f5d7..000000000 --- a/apps/token/src/lib/decimals.test.ts +++ /dev/null @@ -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'); -}); diff --git a/apps/token/src/lib/decimals.ts b/apps/token/src/lib/decimals.ts deleted file mode 100644 index 7f46611ce..000000000 --- a/apps/token/src/lib/decimals.ts +++ /dev/null @@ -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); -} diff --git a/apps/token/src/routes/claim/claim-reducer.ts b/apps/token/src/routes/claim/claim-reducer.ts index b4aa544a8..6ac740f3e 100644 --- a/apps/token/src/routes/claim/claim-reducer.ts +++ b/apps/token/src/routes/claim/claim-reducer.ts @@ -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), diff --git a/apps/token/src/routes/staking/node/pending-stake.tsx b/apps/token/src/routes/staking/node/pending-stake.tsx index 79ec0d542..8a140efe5 100644 --- a/apps/token/src/routes/staking/node/pending-stake.tsx +++ b/apps/token/src/routes/staking/node/pending-stake.tsx @@ -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', }, }; diff --git a/apps/token/src/routes/staking/node/staking-form.tsx b/apps/token/src/routes/staking/node/staking-form.tsx index 48d355cdd..4628dfe99 100644 --- a/apps/token/src/routes/staking/node/staking-form.tsx +++ b/apps/token/src/routes/staking/node/staking-form.tsx @@ -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'