diff --git a/src/features/onboarding/import/ImportSubAccountsScreen.tsx b/src/features/onboarding/import/ImportSubAccountsScreen.tsx index acf1312c..246ad461 100644 --- a/src/features/onboarding/import/ImportSubAccountsScreen.tsx +++ b/src/features/onboarding/import/ImportSubAccountsScreen.tsx @@ -18,8 +18,26 @@ import { FlatList } from 'react-native' import ScrollBox from '@components/ScrollBox' import CircleLoader from '@components/CircleLoader' import ForwardButton from '@components/ForwardButton' -import { useOnboarding } from '../OnboardingProvider' +import { PublicKey } from '@solana/web3.js' +import { useMint } from '@helium/helium-react-hooks' +import { useMetaplexMetadata } from '@hooks/useMetaplexMetadata' import { useOnboardingSheet } from '../OnboardingSheet' +import { useOnboarding } from '../OnboardingProvider' + +const TokenItem = ({ mint, amount }: { mint: PublicKey; amount: bigint }) => { + const decimals = useMint(mint).info?.decimals + const { symbol } = useMetaplexMetadata(mint) + + return ( + + {humanReadable(new BN(amount.toString() || 0), decimals)} {symbol} + + ) +} export default () => { const { t } = useTranslation() @@ -38,7 +56,7 @@ export default () => { const derivationAccounts = useMemo(() => { return foundAccounts.filter( (acc) => - (acc.tokens?.value.length || 0) > 0 || + (acc.tokens?.length || 0) > 0 || (acc?.balance || 0) > 0 || (acc.nfts?.length || 0) > 0 || acc.needsMigrated || @@ -126,15 +144,13 @@ export default () => { > {humanReadable(new BN(item?.balance || 0), 9)} SOL - {(item.tokens?.value.length || 0) > 0 ? ( - - {`${item.tokens?.value.length} tokens`} - - ) : null} + {item.tokens?.map((token) => ( + + ))} {(item.nfts?.length || 0) > 0 ? ( { if (account === -1) { @@ -53,12 +55,7 @@ export type ResolvedPath = { derivationPath: string keypair: Keypair balance?: number - tokens?: RpcResponseAndContext< - Array<{ - pubkey: PublicKey - account: AccountInfo - }> - > + tokens?: { mint: PublicKey; amount: bigint }[] nfts?: Asset[] needsMigrated?: boolean } @@ -131,27 +128,33 @@ export const useDerivationAccounts = ({ mnemonic }: { mnemonic?: string }) => { if (keypair) { let needsMigrated = false - const [balance] = await Promise.all([ + const ataMints = [HNT_MINT, MOBILE_MINT, IOT_MINT] + const atas = await Promise.all( + ataMints.map((mint) => + getAssociatedTokenAddress(mint, keypair.publicKey), + ), + ) + const [balance, tokens] = await Promise.all([ retryWithBackoff(() => connection.getBalance(keypair.publicKey), ), - // retryWithBackoff(() => - // connection.getTokenAccountsByOwner( - // keypair.publicKey, - // { - // programId: TOKEN_PROGRAM_ID, - // }, - // ), - // ), - // retryWithBackoff(() => - // getAssetsByOwner( - // connection.rpcEndpoint, - // keypair.publicKey.toBase58(), - // { - // limit: 10, - // }, - // ), - // ), + retryWithBackoff(() => + connection.getMultipleAccountsInfo(atas), + ).then((tokenAccounts) => + tokenAccounts + .map((acc, idx) => { + if (!acc) return null + + const accInfo = AccountLayout.decode(acc.data) + const amount = BigInt(accInfo.amount) + if (amount <= 0n) return null + return { + mint: ataMints[idx], + amount, + } + }) + .filter((account) => account !== null), + ), ]) if (derivationPath === heliumDerivation(-1)) { @@ -167,6 +170,7 @@ export const useDerivationAccounts = ({ mnemonic }: { mnemonic?: string }) => { derivationPath, keypair, balance, + tokens, needsMigrated, } as ResolvedPath }