Update token account fetch for rpc node changes
This commit is contained in:
parent
879c212da6
commit
318801a876
|
@ -10,7 +10,6 @@ import {
|
||||||
AccountInfo as TokenAccount,
|
AccountInfo as TokenAccount,
|
||||||
} from "@solana/spl-token";
|
} from "@solana/spl-token";
|
||||||
import { Connection, PublicKey } from "@solana/web3.js";
|
import { Connection, PublicKey } from "@solana/web3.js";
|
||||||
import * as bs58 from "bs58";
|
|
||||||
|
|
||||||
export async function getOwnedAssociatedTokenAccounts(
|
export async function getOwnedAssociatedTokenAccounts(
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
|
@ -18,48 +17,21 @@ export async function getOwnedAssociatedTokenAccounts(
|
||||||
) {
|
) {
|
||||||
let filters = getOwnedAccountsFilters(publicKey);
|
let filters = getOwnedAccountsFilters(publicKey);
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
let resp = await connection._rpcRequest("getProgramAccounts", [
|
let resp = await connection.getProgramAccounts(TOKEN_PROGRAM_ID, {
|
||||||
TOKEN_PROGRAM_ID.toBase58(),
|
|
||||||
{
|
|
||||||
commitment: connection.commitment,
|
commitment: connection.commitment,
|
||||||
filters,
|
filters,
|
||||||
},
|
});
|
||||||
]);
|
|
||||||
if (resp.error) {
|
const accs = resp
|
||||||
throw new Error(
|
|
||||||
"failed to get token accounts owned by " +
|
|
||||||
publicKey.toBase58() +
|
|
||||||
": " +
|
|
||||||
resp.error.message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const accs = resp.result
|
|
||||||
.map(({ pubkey, account: { data, executable, owner, lamports } }: any) => ({
|
.map(({ pubkey, account: { data, executable, owner, lamports } }: any) => ({
|
||||||
publicKey: new PublicKey(pubkey),
|
publicKey: new PublicKey(pubkey),
|
||||||
accountInfo: {
|
accountInfo: {
|
||||||
data: bs58.decode(data),
|
data,
|
||||||
executable,
|
executable,
|
||||||
owner: new PublicKey(owner),
|
owner: new PublicKey(owner),
|
||||||
lamports,
|
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) => {
|
.map(({ publicKey, accountInfo }: any) => {
|
||||||
return { publicKey, account: parseTokenAccountData(accountInfo.data) };
|
return { publicKey, account: parseTokenAccountData(accountInfo.data) };
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue