Skip to content

Commit

Permalink
Update token account fetch for rpc node changes
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante committed Aug 8, 2021
1 parent 879c212 commit 318801a
Showing 1 changed file with 7 additions and 35 deletions.
42 changes: 7 additions & 35 deletions src/utils/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,28 @@ import {
AccountInfo as TokenAccount,
} from "@solana/spl-token";
import { Connection, PublicKey } from "@solana/web3.js";
import * as bs58 from "bs58";

export async function getOwnedAssociatedTokenAccounts(
connection: Connection,
publicKey: PublicKey
) {
let filters = getOwnedAccountsFilters(publicKey);
// @ts-ignore
let resp = await connection._rpcRequest("getProgramAccounts", [
TOKEN_PROGRAM_ID.toBase58(),
{
commitment: connection.commitment,
filters,
},
]);
if (resp.error) {
throw new Error(
"failed to get token accounts owned by " +
publicKey.toBase58() +
": " +
resp.error.message
);
}
const accs = resp.result
let resp = await connection.getProgramAccounts(TOKEN_PROGRAM_ID, {
commitment: connection.commitment,
filters,
});

const accs = resp
.map(({ pubkey, account: { data, executable, owner, lamports } }: any) => ({
publicKey: new PublicKey(pubkey),
accountInfo: {
data: bs58.decode(data),
data,
executable,
owner: new PublicKey(owner),
lamports,
},
}))
.filter(({ accountInfo }: any) => {
// TODO: remove this check once mainnet is updated
return filters.every((filter) => {
if (filter.dataSize) {
return accountInfo.data.length === filter.dataSize;
} else if (filter.memcmp) {
let filterBytes = bs58.decode(filter.memcmp.bytes);
return accountInfo.data
.slice(
filter.memcmp.offset,
filter.memcmp.offset + filterBytes.length
)
.equals(filterBytes);
}
return false;
});
})
.map(({ publicKey, accountInfo }: any) => {
return { publicKey, account: parseTokenAccountData(accountInfo.data) };
});
Expand Down

0 comments on commit 318801a

Please sign in to comment.