From 879c212da645d3d102298fa4227089b9f8339a73 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Mon, 12 Jul 2021 20:59:28 -0700 Subject: [PATCH] Don't show aux accounts (#70) --- package.json | 2 +- src/context/Token.tsx | 21 +++++++++++++-------- src/utils/tokens.ts | 28 ++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index e4e57d6..5868a18 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@project-serum/swap-ui", - "version": "0.2.0", + "version": "0.2.1", "main": "dist/index.js", "types": "dist/index.d.ts", "homepage": "https://github.com/project-serum/swap-ui", diff --git a/src/context/Token.tsx b/src/context/Token.tsx index f1f22fd..e381dc6 100644 --- a/src/context/Token.tsx +++ b/src/context/Token.tsx @@ -9,7 +9,10 @@ import { Token, TOKEN_PROGRAM_ID, } from "@solana/spl-token"; -import { getOwnedTokenAccounts, parseTokenAccountData } from "../utils/tokens"; +import { + getOwnedAssociatedTokenAccounts, + parseTokenAccountData, +} from "../utils/tokens"; import { SOL_MINT } from "../utils/pubkeys"; export type TokenContext = { @@ -29,14 +32,16 @@ export function TokenContextProvider(props: any) { return; } // Fetch SPL tokens. - getOwnedTokenAccounts(provider.connection, provider.wallet.publicKey).then( - (accs) => { - if (accs) { - _OWNED_TOKEN_ACCOUNTS_CACHE.push(...accs); - setRefresh((r) => r + 1); - } + getOwnedAssociatedTokenAccounts( + provider.connection, + provider.wallet.publicKey + ).then((accs) => { + if (accs) { + // @ts-ignore + _OWNED_TOKEN_ACCOUNTS_CACHE.push(...accs); + setRefresh((r) => r + 1); } - ); + }); // Fetch SOL balance. provider.connection .getAccountInfo(provider.wallet.publicKey) diff --git a/src/utils/tokens.ts b/src/utils/tokens.ts index 2890c44..9583c6c 100644 --- a/src/utils/tokens.ts +++ b/src/utils/tokens.ts @@ -5,12 +5,14 @@ import * as BufferLayout from "buffer-layout"; import { BN } from "@project-serum/anchor"; import { TOKEN_PROGRAM_ID, + ASSOCIATED_TOKEN_PROGRAM_ID, + Token, AccountInfo as TokenAccount, } from "@solana/spl-token"; import { Connection, PublicKey } from "@solana/web3.js"; import * as bs58 from "bs58"; -export async function getOwnedTokenAccounts( +export async function getOwnedAssociatedTokenAccounts( connection: Connection, publicKey: PublicKey ) { @@ -31,7 +33,7 @@ export async function getOwnedTokenAccounts( resp.error.message ); } - return resp.result + const accs = resp.result .map(({ pubkey, account: { data, executable, owner, lamports } }: any) => ({ publicKey: new PublicKey(pubkey), accountInfo: { @@ -61,6 +63,28 @@ export async function getOwnedTokenAccounts( .map(({ publicKey, accountInfo }: any) => { return { publicKey, account: parseTokenAccountData(accountInfo.data) }; }); + + return ( + ( + await Promise.all( + accs + // @ts-ignore + .map(async (ta) => { + const ata = await Token.getAssociatedTokenAddress( + ASSOCIATED_TOKEN_PROGRAM_ID, + TOKEN_PROGRAM_ID, + ta.account.mint, + publicKey + ); + return [ta, ata]; + }) + ) + ) + // @ts-ignore + .filter(([ta, ata]) => ta.publicKey.equals(ata)) + // @ts-ignore + .map(([ta]) => ta) + ); } const ACCOUNT_LAYOUT = BufferLayout.struct([