Skip to content

Commit 054c2f2

Browse files
authored
only fetch ecosystem tokens (#859)
* only fetch ecosystem tokens * update variant
1 parent 35d0583 commit 054c2f2

File tree

2 files changed

+61
-41
lines changed

2 files changed

+61
-41
lines changed

src/features/onboarding/import/ImportSubAccountsScreen.tsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,26 @@ import { FlatList } from 'react-native'
1818
import ScrollBox from '@components/ScrollBox'
1919
import CircleLoader from '@components/CircleLoader'
2020
import ForwardButton from '@components/ForwardButton'
21-
import { useOnboarding } from '../OnboardingProvider'
21+
import { PublicKey } from '@solana/web3.js'
22+
import { useMint } from '@helium/helium-react-hooks'
23+
import { useMetaplexMetadata } from '@hooks/useMetaplexMetadata'
2224
import { useOnboardingSheet } from '../OnboardingSheet'
25+
import { useOnboarding } from '../OnboardingProvider'
26+
27+
const TokenItem = ({ mint, amount }: { mint: PublicKey; amount: bigint }) => {
28+
const decimals = useMint(mint).info?.decimals
29+
const { symbol } = useMetaplexMetadata(mint)
30+
31+
return (
32+
<Text
33+
variant="textSmMedium"
34+
color="secondaryText"
35+
maxFontSizeMultiplier={1.3}
36+
>
37+
{humanReadable(new BN(amount.toString() || 0), decimals)} {symbol}
38+
</Text>
39+
)
40+
}
2341

2442
export default () => {
2543
const { t } = useTranslation()
@@ -38,7 +56,7 @@ export default () => {
3856
const derivationAccounts = useMemo(() => {
3957
return foundAccounts.filter(
4058
(acc) =>
41-
(acc.tokens?.value.length || 0) > 0 ||
59+
(acc.tokens?.length || 0) > 0 ||
4260
(acc?.balance || 0) > 0 ||
4361
(acc.nfts?.length || 0) > 0 ||
4462
acc.needsMigrated ||
@@ -126,15 +144,13 @@ export default () => {
126144
>
127145
{humanReadable(new BN(item?.balance || 0), 9)} SOL
128146
</Text>
129-
{(item.tokens?.value.length || 0) > 0 ? (
130-
<Text
131-
variant="textSmMedium"
132-
color="secondaryText"
133-
maxFontSizeMultiplier={1.3}
134-
>
135-
{`${item.tokens?.value.length} tokens`}
136-
</Text>
137-
) : null}
147+
{item.tokens?.map((token) => (
148+
<TokenItem
149+
key={token.mint.toBase58()}
150+
mint={token.mint}
151+
amount={token.amount}
152+
/>
153+
))}
138154
{(item.nfts?.length || 0) > 0 ? (
139155
<Text
140156
variant="textSmMedium"

src/hooks/useDerivationAccounts.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Keypair as HeliumKeypair, Mnemonic } from '@helium/crypto'
2-
import { Asset, truthy } from '@helium/spl-utils'
32
import {
4-
AccountInfo,
5-
Keypair,
6-
PublicKey,
7-
RpcResponseAndContext,
8-
} from '@solana/web3.js'
3+
Asset,
4+
HNT_MINT,
5+
IOT_MINT,
6+
MOBILE_MINT,
7+
truthy,
8+
} from '@helium/spl-utils'
9+
import { Keypair, PublicKey } from '@solana/web3.js'
910
import axios from 'axios'
1011
import * as bip39 from 'bip39'
1112
import { Buffer } from 'buffer'
@@ -14,6 +15,7 @@ import { useEffect, useMemo, useState } from 'react'
1415
import Config from 'react-native-config'
1516
import { retryWithBackoff } from '@utils/retryWithBackoff'
1617
import { useSolana } from '@features/solana/SolanaProvider'
18+
import { AccountLayout, getAssociatedTokenAddress } from '@solana/spl-token'
1719

1820
export const solanaDerivation = (account = -1, change: number | undefined) => {
1921
if (account === -1) {
@@ -53,12 +55,7 @@ export type ResolvedPath = {
5355
derivationPath: string
5456
keypair: Keypair
5557
balance?: number
56-
tokens?: RpcResponseAndContext<
57-
Array<{
58-
pubkey: PublicKey
59-
account: AccountInfo<Buffer>
60-
}>
61-
>
58+
tokens?: { mint: PublicKey; amount: bigint }[]
6259
nfts?: Asset[]
6360
needsMigrated?: boolean
6461
}
@@ -131,27 +128,33 @@ export const useDerivationAccounts = ({ mnemonic }: { mnemonic?: string }) => {
131128

132129
if (keypair) {
133130
let needsMigrated = false
134-
const [balance] = await Promise.all([
131+
const ataMints = [HNT_MINT, MOBILE_MINT, IOT_MINT]
132+
const atas = await Promise.all(
133+
ataMints.map((mint) =>
134+
getAssociatedTokenAddress(mint, keypair.publicKey),
135+
),
136+
)
137+
const [balance, tokens] = await Promise.all([
135138
retryWithBackoff(() =>
136139
connection.getBalance(keypair.publicKey),
137140
),
138-
// retryWithBackoff(() =>
139-
// connection.getTokenAccountsByOwner(
140-
// keypair.publicKey,
141-
// {
142-
// programId: TOKEN_PROGRAM_ID,
143-
// },
144-
// ),
145-
// ),
146-
// retryWithBackoff(() =>
147-
// getAssetsByOwner(
148-
// connection.rpcEndpoint,
149-
// keypair.publicKey.toBase58(),
150-
// {
151-
// limit: 10,
152-
// },
153-
// ),
154-
// ),
141+
retryWithBackoff(() =>
142+
connection.getMultipleAccountsInfo(atas),
143+
).then((tokenAccounts) =>
144+
tokenAccounts
145+
.map((acc, idx) => {
146+
if (!acc) return null
147+
148+
const accInfo = AccountLayout.decode(acc.data)
149+
const amount = BigInt(accInfo.amount)
150+
if (amount <= 0n) return null
151+
return {
152+
mint: ataMints[idx],
153+
amount,
154+
}
155+
})
156+
.filter((account) => account !== null),
157+
),
155158
])
156159

157160
if (derivationPath === heliumDerivation(-1)) {
@@ -167,6 +170,7 @@ export const useDerivationAccounts = ({ mnemonic }: { mnemonic?: string }) => {
167170
derivationPath,
168171
keypair,
169172
balance,
173+
tokens,
170174
needsMigrated,
171175
} as ResolvedPath
172176
}

0 commit comments

Comments
 (0)