Skip to content

Commit

Permalink
Merge pull request #118 from bnb-chain/wenty/solana
Browse files Browse the repository at this point in the history
fix: Show solana native token  balance
  • Loading branch information
wenty22 authored Nov 11, 2024
2 parents f2c869d + a67b110 commit 8894d63
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
10 changes: 5 additions & 5 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/canonical-bridge-widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@bnb-chain/space": "workspace:*",
"@emotion/react": "~11.13.0",
"@emotion/styled": "~11.13.0",
"@node-real/walletkit": "2.4.1-alpha.0",
"@node-real/walletkit": "2.4.1-alpha.1",
"@tanstack/react-query": "~5.50.1",
"@types/lodash": "~4.17.7",
"@types/node": "^20",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, Chain, createPublicClient, formatUnits, http } from 'viem';
import { TronWeb } from 'tronweb';
import * as SPLToken from '@solana/spl-token';
import { Connection, PublicKey } from '@solana/web3.js';
import { Connection, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import axios from 'axios';

Expand Down Expand Up @@ -192,24 +192,33 @@ async function getSolanaTokenBalances({
if (!account || !tokens?.length || !connection) {
return {};
}
const balances: Record<string, string | undefined> = {};

// https://stackoverflow.com/questions/69700173/solana-check-all-spl-token-balances-of-a-wallet
const res = await connection.getTokenAccountsByOwner(new PublicKey(account), {
programId: TOKEN_PROGRAM_ID,
});
const [splTokensRes, nativeTokenRes] = await Promise.allSettled([
connection.getTokenAccountsByOwner(new PublicKey(account), {
programId: TOKEN_PROGRAM_ID,
}),
connection.getBalance(new PublicKey(account!)),
]);

res?.value?.forEach((e) => {
const accountInfo = SPLToken.AccountLayout.decode(e.account.data);
const balances: Record<string, string | undefined> = {};
if (splTokensRes.status === 'fulfilled') {
splTokensRes.value?.value?.forEach((e) => {
const accountInfo = SPLToken.AccountLayout.decode(e.account.data);

const token = tokens.find((t) => isSameAddress(t.address, accountInfo.mint.toBase58()));
if (token) {
balances[token.displaySymbol.toUpperCase()] = formatUnits(
accountInfo.amount,
token.decimals,
);
}
});
}

const token = tokens.find((t) => isSameAddress(t.address, accountInfo.mint.toBase58()));
if (token) {
balances[token.displaySymbol.toUpperCase()] = formatUnits(
accountInfo.amount,
token.decimals,
);
}
});
if (nativeTokenRes.status === 'fulfilled') {
balances['SOL'] = String(nativeTokenRes.value / LAMPORTS_PER_SOL);
}

tokens.forEach((t) => {
const key = t.displaySymbol.toUpperCase();
Expand Down

0 comments on commit 8894d63

Please sign in to comment.