Don't show aux accounts (#70)

This commit is contained in:
Armani Ferrante 2021-07-12 20:59:28 -07:00 committed by GitHub
parent 60c53df8b4
commit 879c212da6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 11 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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([