From 88937c4b55f97ac0daed1d5e4b6a7b3aec9e411e Mon Sep 17 00:00:00 2001 From: Rhys Bartels-Waller Date: Sun, 22 Sep 2024 18:29:22 +0800 Subject: [PATCH 1/2] fix: remove fixed precision on Ada values - Ada has a 6 decimal point precision, but currently we're rounding to 2 - Decimals on the CoinId type is dropped to simplify the implementation, as Ada has a constant value and it's not used for any other currencies - Fixes some wonky logic that was working due to the common rounding to 2 decimal points --- .../api/__tests__/input-output-transformer.test.ts | 6 +++--- .../__tests__/wallet-balance-transformer.test.ts | 4 ++-- .../send/helpers/__tests__/transformers.test.ts | 2 +- .../src/utils/constants.ts | 1 - .../src/utils/mocks/test-helpers.tsx | 4 ++-- .../__tests__/pending-tx-transformer.test.ts | 14 ++++++-------- .../activity/helpers/common-tx-transformer.ts | 2 +- .../Form/CoinInput/__tests__/util.test.ts | 10 +++++----- .../components/Form/CoinInput/useSelectedCoins.tsx | 2 +- .../components/Form/CoinInput/util.ts | 2 +- packages/cardano/src/wallet/types.ts | 1 - .../util/__tests__/stake-pool-transformer.test.ts | 1 - .../wallet/util/__tests__/unit-converters.test.ts | 12 ++++++------ .../cardano/src/wallet/util/unit-converters.ts | 7 ++----- .../__tests__/TransactionDetails.test.tsx | 1 - 15 files changed, 30 insertions(+), 39 deletions(-) diff --git a/apps/browser-extension-wallet/src/api/__tests__/input-output-transformer.test.ts b/apps/browser-extension-wallet/src/api/__tests__/input-output-transformer.test.ts index 15fc4657a..6e033ee41 100644 --- a/apps/browser-extension-wallet/src/api/__tests__/input-output-transformer.test.ts +++ b/apps/browser-extension-wallet/src/api/__tests__/input-output-transformer.test.ts @@ -32,7 +32,7 @@ describe('Testing inputOutputTransformer function', () => { expect(result.addr).toEqual( 'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp' ); - expect(result.amount).toBe('1.00'); + expect(result.amount).toBe('1'); expect(result.assetList.length).toEqual(0); }); @@ -46,7 +46,7 @@ describe('Testing inputOutputTransformer function', () => { expect(result.addr).toEqual( 'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp' ); - expect(result.amount).toBe('3.00'); + expect(result.amount).toBe('3'); expect(result.assetList.length).toEqual(0); }); @@ -66,7 +66,7 @@ describe('Testing inputOutputTransformer function', () => { expect(result.addr).toEqual( 'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp' ); - expect(result.amount).toBe('3.00'); + expect(result.amount).toBe('3'); expect(result.assetList.length).toEqual(1); }); }); diff --git a/apps/browser-extension-wallet/src/api/__tests__/wallet-balance-transformer.test.ts b/apps/browser-extension-wallet/src/api/__tests__/wallet-balance-transformer.test.ts index 083ecdb5f..494f9369d 100644 --- a/apps/browser-extension-wallet/src/api/__tests__/wallet-balance-transformer.test.ts +++ b/apps/browser-extension-wallet/src/api/__tests__/wallet-balance-transformer.test.ts @@ -5,13 +5,13 @@ import { walletBalanceTransformer } from '../transformers'; describe('Testing walletBalanceTransformer function', () => { test('given a wallet balance in lovelace should return the balance in ada and undefined for fiat', () => { const result = walletBalanceTransformer('10000000'); - expect(result.coinBalance).toBe('10.00'); + expect(result.coinBalance).toBe('10'); expect(result.fiatBalance).toBeUndefined(); }); test('given a wallet balance in lovelace and a fiat price should return the balance in ada and in fiat', () => { const result = walletBalanceTransformer('10000000', 2); - expect(result.coinBalance).toBe('10.00'); + expect(result.coinBalance).toBe('10'); expect(result.fiatBalance).toBe('20.00'); }); }); diff --git a/apps/browser-extension-wallet/src/features/send/helpers/__tests__/transformers.test.ts b/apps/browser-extension-wallet/src/features/send/helpers/__tests__/transformers.test.ts index c5f736caa..942071007 100644 --- a/apps/browser-extension-wallet/src/features/send/helpers/__tests__/transformers.test.ts +++ b/apps/browser-extension-wallet/src/features/send/helpers/__tests__/transformers.test.ts @@ -15,7 +15,7 @@ describe('Testing availableCoinsTransformer function', () => { const coins = availableCoinsTransformer(totalCoins.toString(), assets); expect(coins).toEqual([ { - balance: '10.00', + balance: '10', id: '1', symbol: 'ADA' }, diff --git a/apps/browser-extension-wallet/src/utils/constants.ts b/apps/browser-extension-wallet/src/utils/constants.ts index 8803cc56a..cec985074 100644 --- a/apps/browser-extension-wallet/src/utils/constants.ts +++ b/apps/browser-extension-wallet/src/utils/constants.ts @@ -12,7 +12,6 @@ export const CARDANO_COIN_SYMBOL: { [key in Wallet.Cardano.NetworkId]: ADASymbol export const cardanoCoin: Wallet.CoinId = { id: '1', name: 'Cardano', - decimals: 6, symbol: CARDANO_COIN_SYMBOL[Wallet.Cardano.NetworkId.Mainnet] }; diff --git a/apps/browser-extension-wallet/src/utils/mocks/test-helpers.tsx b/apps/browser-extension-wallet/src/utils/mocks/test-helpers.tsx index acdcfc79e..07d6ee371 100644 --- a/apps/browser-extension-wallet/src/utils/mocks/test-helpers.tsx +++ b/apps/browser-extension-wallet/src/utils/mocks/test-helpers.tsx @@ -569,7 +569,7 @@ export const cardanoStakePoolSelectedDetails = { status: 'active', ticker: 'STTST', blocks: '20', - pledge: '2000.00' + pledge: '2000' }; export const transformedStakePool = { @@ -585,7 +585,7 @@ export const transformedStakePool = { 'stake_test1uqrw9tjymlm8wrwq7jk68n6v7fs9qz8z0tkdkve26dylmfc2ux2hj', 'stake_test1uq7g7kqeucnqfweqzgxk3dw34e8zg4swnc7nagysug2mm4cm77jrx' ], - pledge: '2000.00ADA', + pledge: '2000ADA', retired: false, saturation: '5.12', size: '- %', diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/pending-tx-transformer.test.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/pending-tx-transformer.test.ts index f837bd340..450168643 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/pending-tx-transformer.test.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/__tests__/pending-tx-transformer.test.ts @@ -117,10 +117,10 @@ describe('Testing tx transformers utils', () => { deposit: undefined, depositReclaim: undefined, direction: 'Outgoing', - fee: '1.00', + fee: '1', fiatAmount: '1.00 USD', id: '6804edf9712d2b619edb6ac86861fe93a730693183a262b165fcc1ba1bc99cad', - amount: '1.00 ADA', + amount: '1 ADA', assets: [ { id: '6b8d07d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7', @@ -141,17 +141,15 @@ describe('Testing tx transformers utils', () => { }); describe('getFormattedFiatAmount', () => { - test('shoud return properly formatted fiat amount', () => { - const amount = new BigNumber('10'); + test('should return properly formatted fiat amount', () => { + const amount = new BigNumber('5500000'); const fiatPrice = 2; const fiatCurrency = { code: 'code', symbol: 'symbol' } as unknown as CurrencyInfo; mockLovelacesToAdaString.mockImplementationOnce((val) => val); - expect(getFormattedFiatAmount({ amount, fiatPrice, fiatCurrency })).toEqual( - `${amount.times(new BigNumber(fiatPrice)).toString()} ${fiatCurrency.code}` - ); + expect(getFormattedFiatAmount({ amount, fiatPrice, fiatCurrency })).toEqual(`11.00 ${fiatCurrency.code}`); }); - test('shoud return properly formatted fiat amount in case there is no fiat price', () => { + test('should return properly formatted fiat amount in case there is no fiat price', () => { const amount = new BigNumber('10'); const fiatPrice = 0; const fiatCurrency = { code: 'code', symbol: 'symbol' } as unknown as CurrencyInfo; diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-tx-transformer.ts b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-tx-transformer.ts index 630e63266..7faa8a1e8 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-tx-transformer.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/activity/helpers/common-tx-transformer.ts @@ -39,7 +39,7 @@ export const getFormattedFiatAmount = ({ fiatCurrency: CurrencyInfo; }): string => { const fiatAmount = fiatPrice - ? Wallet.util.lovelacesToAdaString(amount.times(new BigNumber(fiatPrice)).toString()) + ? Wallet.util.convertLovelaceToFiat({ lovelaces: amount.toString(), fiat: fiatPrice }).toString() : ''; return fiatAmount ? `${fiatAmount} ${fiatCurrency.code}` : '-'; }; diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/util.test.ts b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/util.test.ts index 64c5e159b..760a52fb3 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/util.test.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/__tests__/util.test.ts @@ -31,7 +31,7 @@ describe('CoinInput util', () => { describe('getADACoinProperties', () => { test('returns 0 for availableADA and false for hasMaxBtn when balance is 0', () => { expect(getADACoinProperties('0', '1000000', '0', '0')).toEqual({ - availableADA: '0.00', + availableADA: '0', max: '1', hasMaxBtn: false, hasReachedMaxAmount: false, @@ -40,7 +40,7 @@ describe('CoinInput util', () => { }); test('returns 0 for max and true for hasReachedMaxAmount when spendable coins is 0', () => { expect(getADACoinProperties('1000000', '0', '0', '0')).toEqual({ - availableADA: '1.00', + availableADA: '1', max: '0', hasMaxBtn: true, hasReachedMaxAmount: true, @@ -49,7 +49,7 @@ describe('CoinInput util', () => { }); test('returns formatted balance as availableADA, and the spendable coin in ADA as max when there is no spending', () => { expect(getADACoinProperties('20000000', '10000000', '0', '0')).toEqual({ - availableADA: '20.00', + availableADA: '20', max: '10', hasMaxBtn: true, hasReachedMaxAmount: false, @@ -58,7 +58,7 @@ describe('CoinInput util', () => { }); test('returns the calculated max amount when there is less spent coin than spendable coin', () => { expect(getADACoinProperties('20000000', '10000000', '5', '2')).toEqual({ - availableADA: '20.00', + availableADA: '20', max: '7', hasMaxBtn: true, hasReachedMaxAmount: false, @@ -67,7 +67,7 @@ describe('CoinInput util', () => { }); test('returns max amount as 0 and hasReachedMaxAmount as true when there is more spent coin than spendable coin', () => { expect(getADACoinProperties('20000000', '10000000', '10', '0')).toEqual({ - availableADA: '20.00', + availableADA: '20', max: '0', hasMaxBtn: true, hasReachedMaxAmount: true, diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/useSelectedCoins.tsx b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/useSelectedCoins.tsx index 501aa3f81..61a1bdf24 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/useSelectedCoins.tsx +++ b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/useSelectedCoins.tsx @@ -193,7 +193,7 @@ export const useSelectedCoins = ({ }, formattedFiatValue: `= ${compactNumberWithUnit(fiatValue)} ${fiatCurrency?.code}`, fiatValue: `= ${fiatValue} ${fiatCurrency?.code}`, - maxDecimals: cardanoCoin.decimals + maxDecimals: 6 } as AssetInputListProps['rows'][number]; } diff --git a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/util.ts b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/util.ts index 563a7d363..011d62a53 100644 --- a/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/util.ts +++ b/apps/browser-extension-wallet/src/views/browser-view/features/send-transaction/components/Form/CoinInput/util.ts @@ -47,7 +47,7 @@ export const getADACoinProperties = ( ): ADARow => { // Convert to ADA const availableADA = Wallet.util.lovelacesToAdaString(balance); - const spendableCoinInAda = Wallet.util.lovelacesToAdaString(spendableCoin, undefined, BigNumber.ROUND_DOWN); + const spendableCoinInAda = Wallet.util.lovelacesToAdaString(spendableCoin); // Calculate max amount in ADA const max = getMaxSpendableAmount(spendableCoinInAda, spentCoins, currentSpendingAmount); return { diff --git a/packages/cardano/src/wallet/types.ts b/packages/cardano/src/wallet/types.ts index d75564ccd..4ad23a022 100644 --- a/packages/cardano/src/wallet/types.ts +++ b/packages/cardano/src/wallet/types.ts @@ -29,7 +29,6 @@ export type CoinId = { id: string; symbol: string; name: string; - decimals: number; }; export enum TransactionStatus { diff --git a/packages/cardano/src/wallet/util/__tests__/stake-pool-transformer.test.ts b/packages/cardano/src/wallet/util/__tests__/stake-pool-transformer.test.ts index 99ed36100..25b23e2ed 100644 --- a/packages/cardano/src/wallet/util/__tests__/stake-pool-transformer.test.ts +++ b/packages/cardano/src/wallet/util/__tests__/stake-pool-transformer.test.ts @@ -7,7 +7,6 @@ import { stakePoolTransformer } from '../stake-pool-transformer'; const cardanoCoin: CoinId = { id: '1', name: 'Cardano', - decimals: 6, symbol: 'ADA' }; diff --git a/packages/cardano/src/wallet/util/__tests__/unit-converters.test.ts b/packages/cardano/src/wallet/util/__tests__/unit-converters.test.ts index 1e8692389..fa68d9857 100644 --- a/packages/cardano/src/wallet/util/__tests__/unit-converters.test.ts +++ b/packages/cardano/src/wallet/util/__tests__/unit-converters.test.ts @@ -8,14 +8,14 @@ import { } from '../unit-converters'; describe('Testing lovelacesToAdaString function', () => { - test('should convert lovelaces to ada', async () => { + test('should convert lovelaces to ada - one million', async () => { const result = lovelacesToAdaString('1000000'); - expect(result).toBe('1.00'); + expect(result).toBe('1'); }); - test('should return ada value with 4 decimals', async () => { - const result = lovelacesToAdaString('1000000', 4); - expect(result).toBe('1.0000'); + test('should convert lovelaces to ada - one million and one', async () => { + const result = lovelacesToAdaString('1000001'); + expect(result).toBe('1.000001'); }); }); @@ -63,6 +63,6 @@ describe('Testing getFormattedAmount function', () => { decimals: 2 }; const result = getFormattedAmount({ amount: '2000000', cardanoCoin }); - expect(result).toBe('2.00 symbol'); + expect(result).toBe('2 symbol'); }); }); diff --git a/packages/cardano/src/wallet/util/unit-converters.ts b/packages/cardano/src/wallet/util/unit-converters.ts index b657fcf27..d2ba48055 100644 --- a/packages/cardano/src/wallet/util/unit-converters.ts +++ b/packages/cardano/src/wallet/util/unit-converters.ts @@ -4,11 +4,8 @@ import { CoinId } from '../types'; const LOVELACE_VALUE = 1_000_000; const DEFAULT_DECIMALS = 2; -export const lovelacesToAdaString = ( - lovelaces: string, - decimalValues: number = DEFAULT_DECIMALS, - roudingMode: BigNumber.RoundingMode = BigNumber.ROUND_HALF_UP -): string => new BigNumber(lovelaces).dividedBy(LOVELACE_VALUE).toFixed(decimalValues, roudingMode); +export const lovelacesToAdaString = (lovelaces: string): string => + new BigNumber(lovelaces).dividedBy(LOVELACE_VALUE).toString(); export const adaToLovelacesString = (ada: string): string => new BigNumber(ada).multipliedBy(LOVELACE_VALUE).toString(); diff --git a/packages/core/src/ui/components/ActivityDetail/__tests__/TransactionDetails.test.tsx b/packages/core/src/ui/components/ActivityDetail/__tests__/TransactionDetails.test.tsx index c1bbd7292..4f6a9c7a1 100644 --- a/packages/core/src/ui/components/ActivityDetail/__tests__/TransactionDetails.test.tsx +++ b/packages/core/src/ui/components/ActivityDetail/__tests__/TransactionDetails.test.tsx @@ -11,7 +11,6 @@ const transactionDate = '2021/09/10'; export const cardanoCoin: Wallet.CoinId = { id: '1', name: 'Cardano', - decimals: 6, symbol: 'A' }; From 6eecf095e82e3aa47737f138756082573bc0e3cb Mon Sep 17 00:00:00 2001 From: Rhys Bartels-Waller Date: Sun, 22 Sep 2024 23:50:19 +0800 Subject: [PATCH 2/2] refactor: remove unused constant --- apps/browser-extension-wallet/src/utils/constants.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/browser-extension-wallet/src/utils/constants.ts b/apps/browser-extension-wallet/src/utils/constants.ts index cec985074..8f8bf272b 100644 --- a/apps/browser-extension-wallet/src/utils/constants.ts +++ b/apps/browser-extension-wallet/src/utils/constants.ts @@ -15,8 +15,6 @@ export const cardanoCoin: Wallet.CoinId = { symbol: CARDANO_COIN_SYMBOL[Wallet.Cardano.NetworkId.Mainnet] }; -export const EPOCH_DURATION_DAYS = 5; - export const MIN_COIN_TO_SEND = 1; type StaticBalanceTracker = {