Skip to content

Commit

Permalink
Balances Page using getMultipleAccounts
Browse files Browse the repository at this point in the history
Related to issue project-serum#104
  • Loading branch information
jacobcreech committed Jul 14, 2021
1 parent b320f9a commit a595edf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/markets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {useWallet} from './wallet';
import tuple from 'immutable-tuple';
import {notify} from './notifications';
import BN from 'bn.js';
import {getTokenAccountInfo, parseTokenAccountData, useMintInfos,} from './tokens';
import { getMultipleTokenAccountInfo, parseTokenAccountData, useMintInfos } from './tokens';
import {
Balances,
CustomMarketInfo,
Expand Down Expand Up @@ -440,7 +440,7 @@ export function useTokenAccounts(): [
if (!connected || !wallet) {
return null;
}
return await getTokenAccountInfo(connection, wallet.publicKey);
return await getMultipleTokenAccountInfo(connection, wallet.publicKey);
}
return useAsyncData(
getTokenAccounts,
Expand Down
27 changes: 27 additions & 0 deletions src/utils/tokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,33 @@ export async function getOwnedTokenAccounts(
});
}

export async function getMultipleTokenAccountInfo(
connection: Connection,
ownerAddress: PublicKey,
) {
let [splAccounts, account] = await Promise.all([
getOwnedTokenAccounts(connection, ownerAddress),
connection.getAccountInfo(ownerAddress),
]);
let accounts = splAccounts.map(value => {
return value.publicKey;
});
let response = await getMultipleSolanaAccounts(connection, accounts);
const parsedAccounts: TokenAccount[] = Object.entries(response.value).map(([pubKey, accountInfo]) => {
return {
pubkey: new PublicKey(pubKey),
account: accountInfo,
effectiveMint: parseTokenAccountData(accountInfo!.data).mint,
};
}
);
return parsedAccounts.concat({
pubkey: ownerAddress,
account,
effectiveMint: WRAPPED_SOL_MINT,
});
}

export async function getTokenAccountInfo(
connection: Connection,
ownerAddress: PublicKey,
Expand Down

0 comments on commit a595edf

Please sign in to comment.