Switch to web3.js getProgramAccounts: Fix for devnet and ultimately mainnet (#352)

This commit is contained in:
Pierre 2021-07-31 04:38:44 +10:00 committed by GitHub
parent 0c7da6e442
commit eda316d30b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 32 deletions

View File

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