diff --git a/apps/wallet-mobile/src/TxHistory/BalanceBanner.tsx b/apps/wallet-mobile/src/TxHistory/BalanceBanner.tsx index 9195fe46d3..2c53aa69da 100644 --- a/apps/wallet-mobile/src/TxHistory/BalanceBanner.tsx +++ b/apps/wallet-mobile/src/TxHistory/BalanceBanner.tsx @@ -10,7 +10,6 @@ import {PairedBalance} from '../components/PairedBalance/PairedBalance' import {usePrimaryBalance} from '../features/Portfolio/common/hooks/usePrimaryBalance' import {usePrivacyMode} from '../features/Settings/PrivacyMode/PrivacyMode' import {useSelectedWallet} from '../features/WalletManager/Context' -import {asQuantity} from '../yoroi-wallets/utils' export const BalanceBanner = React.forwardRef((_, ref) => { const wallet = useSelectedWallet() @@ -30,20 +29,14 @@ export const BalanceBanner = React.forwardRef((_, ref) => { togglePrivacyMode()} style={styles.button}> - + @@ -52,13 +45,17 @@ export const BalanceBanner = React.forwardRef((_, ref) => { ) }) -type PrimaryBalanceProps = {isPrivacyOff: boolean; primaryBalance: Portfolio.Token.Amount; privacyPlaceholder: string} -const PrimaryBalance = ({isPrivacyOff, primaryBalance, privacyPlaceholder}: PrimaryBalanceProps) => { +type BalanceProps = {isPrivacyOff: boolean; amount: Portfolio.Token.Amount; privacyPlaceholder: string} +const Balance = ({isPrivacyOff, amount, privacyPlaceholder}: BalanceProps) => { const styles = useStyles() - const balance = isPrivacyOff - ? amountFormatter({template: '{{value}} {{symbol}}'})(primaryBalance) - : amountFormatter({template: `${privacyPlaceholder} {{symbol}}`})(primaryBalance) + const balance = React.useMemo( + () => + !isPrivacyOff + ? amountFormatter({template: '{{value}} {{ticker}}'})(amount) + : amountFormatter({template: `${privacyPlaceholder} {{ticker}}`})(amount), + [amount, isPrivacyOff, privacyPlaceholder], + ) return ( diff --git a/apps/wallet-mobile/src/components/PairedBalance/PairedBalance.tsx b/apps/wallet-mobile/src/components/PairedBalance/PairedBalance.tsx index e11fdeab8b..b012a1683e 100644 --- a/apps/wallet-mobile/src/components/PairedBalance/PairedBalance.tsx +++ b/apps/wallet-mobile/src/components/PairedBalance/PairedBalance.tsx @@ -1,5 +1,5 @@ import {useTheme} from '@yoroi/theme' -import {Balance} from '@yoroi/types' +import {Portfolio} from '@yoroi/types' import * as React from 'react' import {defineMessages, useIntl} from 'react-intl' import {StyleSheet, Text, TextStyle} from 'react-native' @@ -10,13 +10,15 @@ import {useExchangeRate} from '../../yoroi-wallets/hooks' import {CurrencySymbol} from '../../yoroi-wallets/types' import {Quantities} from '../../yoroi-wallets/utils' import {Boundary, ResetError, ResetErrorRef} from '..' +import { splitBigInt } from '@yoroi/common' type Props = { - amount: Balance.Amount + amount: Portfolio.Token.Amount + privacyPlaceholder: string isPrivacyOff?: boolean textStyle?: TextStyle } -export const PairedBalance = React.forwardRef(({isPrivacyOff, amount, textStyle}, ref) => { +export const PairedBalance = React.forwardRef(({isPrivacyOff, amount, textStyle, privacyPlaceholder}, ref) => { const {currency} = useCurrencyContext() return ( @@ -31,13 +33,12 @@ export const PairedBalance = React.forwardRef(({isPrivacyO ), }} > - + ) }) -const hiddenPairedTotal = '*.**' -const Amount = ({isPrivacyOff, amount, textStyle}: Props) => { +const Price = ({isPrivacyOff, amount, textStyle, privacyPlaceholder}: Props) => { const wallet = useSelectedWallet() const styles = useStyles() const {currency, config} = useCurrencyContext() @@ -46,20 +47,15 @@ const Amount = ({isPrivacyOff, amount, textStyle}: Props) => { // hide pairing when set to the primary token if (currency === 'ADA') return null - // hide pairing when the amount is not primary - if (wallet.primaryTokenInfo.id !== amount.tokenId) return null - if (rate == null) return ( - + ... {currency} ) - const primaryExchangeQuantity = Quantities.quotient( - amount.quantity, - `${10 ** wallet.primaryToken.metadata.numberOfDecimals}`, - ) + const price = splitBigInt() + const secondaryExchangeQuantity = Quantities.decimalPlaces( Quantities.product([primaryExchangeQuantity, `${rate}`]), config.decimals, @@ -78,7 +74,7 @@ const BalanceError = ({textStyle}: {textStyle?: TextStyle}) => { const {currency} = useCurrencyContext() return ( - + {strings.pairedBalanceError(currency)} ) diff --git a/apps/wallet-mobile/src/features/Portfolio/common/hooks/usePortfolioTokenManager.ts b/apps/wallet-mobile/src/features/Portfolio/common/hooks/usePortfolioTokenManager.ts index e2e6652c4e..30990bb61b 100644 --- a/apps/wallet-mobile/src/features/Portfolio/common/hooks/usePortfolioTokenManager.ts +++ b/apps/wallet-mobile/src/features/Portfolio/common/hooks/usePortfolioTokenManager.ts @@ -9,9 +9,9 @@ export const usePortfolioTokenManager = ({network}: {network: Chain.Network}) => } export const buildPortfolioTokenManager = ({network}: {network: Chain.Network}) => { - const networkStorageMounted = mountMMKVStorage({path: '/', id: `${network}.token-manager`}) - const tokenDiscoveryStorageMounted = networkStorageMounted.join('token-discovery/') - const tokenInfoStorageMounted = networkStorageMounted.join('token-info/') + const storage = mountMMKVStorage({path: '/', id: `${network}.token-manager`}) + const tokenDiscoveryStorageMounted = storage.join('token-discovery/') + const tokenInfoStorageMounted = storage.join('token-info/') const tokenStorage = portfolioTokenStorageMaker({ tokenDiscoveryStorage: observableStorageMaker(tokenDiscoveryStorageMounted), diff --git a/apps/wallet-mobile/translations/messages/src/components/PairedBalance/PairedBalance.json b/apps/wallet-mobile/translations/messages/src/components/PairedBalance/PairedBalance.json index 6350d75886..f08483a17b 100644 --- a/apps/wallet-mobile/translations/messages/src/components/PairedBalance/PairedBalance.json +++ b/apps/wallet-mobile/translations/messages/src/components/PairedBalance/PairedBalance.json @@ -4,14 +4,14 @@ "defaultMessage": "!!!Error obtaining {currency} pairing", "file": "src/components/PairedBalance/PairedBalance.tsx", "start": { - "line": 88, + "line": 84, "column": 22, - "index": 2781 + "index": 2633 }, "end": { - "line": 91, + "line": 87, "column": 3, - "index": 2913 + "index": 2765 } } ] \ No newline at end of file diff --git a/packages/common/src/storage/adapters/mmkv-storage.test.ts b/packages/common/src/storage/adapters/mmkv-storage.test.ts index ac5b8e1d6e..380bf6ccc2 100644 --- a/packages/common/src/storage/adapters/mmkv-storage.test.ts +++ b/packages/common/src/storage/adapters/mmkv-storage.test.ts @@ -4,9 +4,9 @@ import {parseSafe} from '../../utils/parsers' import {mountMMKVMultiStorage, mountMMKVStorage} from './mmkv-storage' import {MMKV} from 'react-native-mmkv' -const mmkv = new MMKV({id: 'default.mmkv'}) +const mmkv = new MMKV({id: 'test.mmkv'}) const rootStorage = mountMMKVStorage( - {path: '/', id: 'default.mmkv'}, + {path: '/', id: 'test.mmkv'}, {instance: mmkv}, ) @@ -129,12 +129,12 @@ describe('prefixed storage', () => { rootStorage.setItem('item', item, (data: unknown) => { expect(data).toBe(item) return storedItem - }) // overrides JSON.stringify + }) const parsedResult = rootStorage.getItem('item', (data: unknown) => { expect(data).toBe(storedItem) return item - }) // overrides JSON.parse + }) expect(parsedResult).toBe(item) }) @@ -152,7 +152,7 @@ describe('prefixed storage', () => { rootStorage.multiSet(tuples, (data: unknown) => { expect([item1, item2]).toContain(data) return `${data}-modified` - }) // overrides JSON.stringify + }) const parsedResult = rootStorage.multiGet( ['item1', 'item2'], @@ -160,7 +160,7 @@ describe('prefixed storage', () => { expect([storedItem1, storedItem2]).toContain(data) return data?.slice(0, 5) }, - ) // overrides JSON.parse + ) expect(parsedResult).toEqual(tuples) }) diff --git a/packages/common/src/storage/adapters/mmkv-storage.ts b/packages/common/src/storage/adapters/mmkv-storage.ts index c83964f4a0..9d0bac2599 100644 --- a/packages/common/src/storage/adapters/mmkv-storage.ts +++ b/packages/common/src/storage/adapters/mmkv-storage.ts @@ -5,8 +5,6 @@ import {parseSafe} from '../../utils/parsers' import {isFolderKey} from '../helpers/is-folder-key' import {isFileKey} from '../helpers/is-file-key' -// ------- -// FACTORY export const mountMMKVStorage = ( { path, @@ -17,10 +15,7 @@ export const mountMMKVStorage = ( }, {instance}: {instance?: MMKV} = {}, ): App.Storage => { - // mmkv uses id as file and the path is irrelevant if sharing content amongst app (iOS) - // therefore the client needs to know all the ids to delete all data - // which means that it works differently than AsyncStorage - // think of the id as the volume + // think of the id as the filename and the path as filter const storage = instance ?? new MMKV({id}) const withPath = (key: string) => diff --git a/packages/portfolio/src/helpers/amount-breakdown.test.ts b/packages/portfolio/src/helpers/amount-breakdown.test.ts new file mode 100644 index 0000000000..3ddb185ee2 --- /dev/null +++ b/packages/portfolio/src/helpers/amount-breakdown.test.ts @@ -0,0 +1,18 @@ +import BigNumber from 'bignumber.js' +import {tokenBalanceMocks} from '../adapters/token-balance.mocks' +import {amountBreakdown} from './amount-breakdown' + +describe('amountBreakdown', () => { + it('should correctly split the quantity based on decimals', () => { + const result = amountBreakdown(tokenBalanceMocks.primaryETH) + + expect(result).toEqual({ + bi: 1000000n, + bn: new BigNumber('1e-12'), + decimalPlaces: 18, + fraction: '000000000001000000', + integer: '0', + str: '0.000000000001000000', + }) + }) +}) diff --git a/packages/portfolio/src/helpers/amount-breakdown.ts b/packages/portfolio/src/helpers/amount-breakdown.ts new file mode 100644 index 0000000000..44dec963b3 --- /dev/null +++ b/packages/portfolio/src/helpers/amount-breakdown.ts @@ -0,0 +1,6 @@ +import {splitBigInt} from '@yoroi/common' +import {Portfolio} from '@yoroi/types' + +export function amountBreakdown({quantity, info}: Portfolio.Token.Amount) { + return splitBigInt(quantity, info.decimals) +} diff --git a/packages/portfolio/src/index.ts b/packages/portfolio/src/index.ts index 3f65cd78b7..1e5258e023 100644 --- a/packages/portfolio/src/index.ts +++ b/packages/portfolio/src/index.ts @@ -1,4 +1,5 @@ export * from './helpers/amount-formatter' +export * from './helpers/amount-breakdown' export * from './helpers/create-primary-token-info' export * from './helpers/sorting'