Switch to web3.js getProgramAccounts: Fix for devnet and ultimately mainnet (#352)
This commit is contained in:
parent
0c7da6e442
commit
eda316d30b
|
@ -17,52 +17,25 @@ import {
|
||||||
transferChecked,
|
transferChecked,
|
||||||
} from './instructions';
|
} from './instructions';
|
||||||
import { ACCOUNT_LAYOUT, getOwnedAccountsFilters, MINT_LAYOUT } from './data';
|
import { ACCOUNT_LAYOUT, getOwnedAccountsFilters, MINT_LAYOUT } from './data';
|
||||||
import bs58 from 'bs58';
|
|
||||||
|
|
||||||
export async function getOwnedTokenAccounts(connection, publicKey) {
|
export async function getOwnedTokenAccounts(connection, publicKey) {
|
||||||
let filters = getOwnedAccountsFilters(publicKey);
|
let filters = getOwnedAccountsFilters(publicKey);
|
||||||
let resp = await connection._rpcRequest('getProgramAccounts', [
|
let resp = await connection.getProgramAccounts(
|
||||||
TOKEN_PROGRAM_ID.toBase58(),
|
TOKEN_PROGRAM_ID,
|
||||||
{
|
{
|
||||||
commitment: connection.commitment,
|
|
||||||
filters,
|
filters,
|
||||||
},
|
},
|
||||||
]);
|
);
|
||||||
if (resp.error) {
|
return resp
|
||||||
throw new Error(
|
|
||||||
'failed to get token accounts owned by ' +
|
|
||||||
publicKey.toBase58() +
|
|
||||||
': ' +
|
|
||||||
resp.error.message,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return resp.result
|
|
||||||
.map(({ pubkey, account: { data, executable, owner, lamports } }) => ({
|
.map(({ pubkey, account: { data, executable, owner, lamports } }) => ({
|
||||||
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 }) => {
|
|
||||||
// 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(
|
export async function signAndSendTransaction(
|
||||||
|
|
Loading…
Reference in New Issue