Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Spl Token 2022 in getBalances #728

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions platforms/solana/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
} from '@wormhole-foundation/sdk-connect';
import { SolanaChain } from './chain.js';

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

const splParsedTokenAccounts = await rpc.getParsedTokenAccountsByOwner(
new PublicKey(walletAddress),
{
programId: new PublicKey(TOKEN_PROGRAM_ID),
},
);
const splParsedTokenAccounts = (await Promise.all(
[TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID]
.map(pid => new PublicKey(pid))
.map(programId => rpc.getParsedTokenAccountsByOwner(new PublicKey(walletAddress), { programId })
))).reduce<{
pubkey: PublicKey;
account: AccountInfo<ParsedAccountData>;
}[]
>((acc, val) => {
return acc.concat(val.value);
}, []);

const balancesArr = tokens.map((token) => {
if (isNative(token)) {
return { ['native']: native };
}
const addrString = new SolanaAddress(token).toString();
const amount = splParsedTokenAccounts.value.find(
const amount = splParsedTokenAccounts.find(
(v) => v?.account.data.parsed?.info?.mint === token,
)?.account.data.parsed?.info?.tokenAmount?.amount;
if (!amount) return { [addrString]: null };
Expand Down
Loading