1
1
import { Keypair as HeliumKeypair , Mnemonic } from '@helium/crypto'
2
- import { Asset , truthy } from '@helium/spl-utils'
3
2
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'
9
10
import axios from 'axios'
10
11
import * as bip39 from 'bip39'
11
12
import { Buffer } from 'buffer'
@@ -14,6 +15,7 @@ import { useEffect, useMemo, useState } from 'react'
14
15
import Config from 'react-native-config'
15
16
import { retryWithBackoff } from '@utils/retryWithBackoff'
16
17
import { useSolana } from '@features/solana/SolanaProvider'
18
+ import { AccountLayout , getAssociatedTokenAddress } from '@solana/spl-token'
17
19
18
20
export const solanaDerivation = ( account = - 1 , change : number | undefined ) => {
19
21
if ( account === - 1 ) {
@@ -53,12 +55,7 @@ export type ResolvedPath = {
53
55
derivationPath : string
54
56
keypair : Keypair
55
57
balance ?: number
56
- tokens ?: RpcResponseAndContext <
57
- Array < {
58
- pubkey : PublicKey
59
- account : AccountInfo < Buffer >
60
- } >
61
- >
58
+ tokens ?: { mint : PublicKey ; amount : bigint } [ ]
62
59
nfts ?: Asset [ ]
63
60
needsMigrated ?: boolean
64
61
}
@@ -131,27 +128,33 @@ export const useDerivationAccounts = ({ mnemonic }: { mnemonic?: string }) => {
131
128
132
129
if ( keypair ) {
133
130
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 ( [
135
138
retryWithBackoff ( ( ) =>
136
139
connection . getBalance ( keypair . publicKey ) ,
137
140
) ,
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
+ ) ,
155
158
] )
156
159
157
160
if ( derivationPath === heliumDerivation ( - 1 ) ) {
@@ -167,6 +170,7 @@ export const useDerivationAccounts = ({ mnemonic }: { mnemonic?: string }) => {
167
170
derivationPath,
168
171
keypair,
169
172
balance,
173
+ tokens,
170
174
needsMigrated,
171
175
} as ResolvedPath
172
176
}
0 commit comments