Skip to content

Commit 074b9aa

Browse files
authored
Include Spl Token 2022 in getBalances (#728)
* Include Spl Token 2022 in getBalances * Review change
1 parent cf07b6a commit 074b9aa

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

platforms/solana/src/platform.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import {
1919
} from '@wormhole-foundation/sdk-connect';
2020
import { SolanaChain } from './chain.js';
2121

22-
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
22+
import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
2323
import type {
24+
AccountInfo,
2425
Commitment,
2526
ConnectionConfig,
2627
ParsedAccountData,
@@ -150,19 +151,24 @@ export class SolanaPlatform<N extends Network>
150151
native = BigInt(await rpc.getBalance(new PublicKey(walletAddress)));
151152
}
152153

153-
const splParsedTokenAccounts = await rpc.getParsedTokenAccountsByOwner(
154-
new PublicKey(walletAddress),
155-
{
156-
programId: new PublicKey(TOKEN_PROGRAM_ID),
157-
},
158-
);
154+
const splParsedTokenAccounts = (await Promise.all(
155+
[TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID]
156+
.map(pid => new PublicKey(pid))
157+
.map(programId => rpc.getParsedTokenAccountsByOwner(new PublicKey(walletAddress), { programId })
158+
))).reduce<{
159+
pubkey: PublicKey;
160+
account: AccountInfo<ParsedAccountData>;
161+
}[]
162+
>((acc, val) => {
163+
return acc.concat(val.value);
164+
}, []);
159165

160166
const balancesArr = tokens.map((token) => {
161167
if (isNative(token)) {
162168
return { ['native']: native };
163169
}
164170
const addrString = new SolanaAddress(token).toString();
165-
const amount = splParsedTokenAccounts.value.find(
171+
const amount = splParsedTokenAccounts.find(
166172
(v) => v?.account.data.parsed?.info?.mint === token,
167173
)?.account.data.parsed?.info?.tokenAmount?.amount;
168174
if (!amount) return { [addrString]: null };

0 commit comments

Comments
 (0)