From eda316d30bb7be4250dc622e41b6fda6f54ca7d8 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 31 Jul 2021 04:38:44 +1000 Subject: [PATCH] Switch to web3.js getProgramAccounts: Fix for devnet and ultimately mainnet (#352) --- src/utils/tokens/index.js | 37 +++++-------------------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/src/utils/tokens/index.js b/src/utils/tokens/index.js index 11acd61..7c254d0 100644 --- a/src/utils/tokens/index.js +++ b/src/utils/tokens/index.js @@ -17,52 +17,25 @@ import { transferChecked, } from './instructions'; import { ACCOUNT_LAYOUT, getOwnedAccountsFilters, MINT_LAYOUT } from './data'; -import bs58 from 'bs58'; export async function getOwnedTokenAccounts(connection, publicKey) { let filters = getOwnedAccountsFilters(publicKey); - let resp = await connection._rpcRequest('getProgramAccounts', [ - TOKEN_PROGRAM_ID.toBase58(), + let resp = await connection.getProgramAccounts( + TOKEN_PROGRAM_ID, { - commitment: connection.commitment, filters, }, - ]); - if (resp.error) { - throw new Error( - 'failed to get token accounts owned by ' + - publicKey.toBase58() + - ': ' + - resp.error.message, - ); - } - return resp.result + ); + return resp .map(({ pubkey, account: { data, executable, owner, lamports } }) => ({ publicKey: new PublicKey(pubkey), accountInfo: { - data: bs58.decode(data), + data, executable, owner: new PublicKey(owner), lamports, }, })) - .filter(({ accountInfo }) => { - // 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; - }); - }); } export async function signAndSendTransaction(