feat: add getMultipleParsedAccounts method (#28414)
This commit is contained in:
parent
00b1d0930f
commit
831ed96730
|
@ -3170,6 +3170,32 @@ export class Connection {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all the account info for multiple accounts specified by an array of public keys, return with context
|
||||
*/
|
||||
async getMultipleParsedAccounts(
|
||||
publicKeys: PublicKey[],
|
||||
rawConfig?: GetMultipleAccountsConfig,
|
||||
): Promise<
|
||||
RpcResponseAndContext<(AccountInfo<Buffer | ParsedAccountData> | null)[]>
|
||||
> {
|
||||
const {commitment, config} = extractCommitmentFromConfig(rawConfig);
|
||||
const keys = publicKeys.map(key => key.toBase58());
|
||||
const args = this._buildArgs([keys], commitment, 'jsonParsed', config);
|
||||
const unsafeRes = await this._rpcRequest('getMultipleAccounts', args);
|
||||
const res = create(
|
||||
unsafeRes,
|
||||
jsonRpcResultAndContext(array(nullable(ParsedAccountInfoResult))),
|
||||
);
|
||||
if ('error' in res) {
|
||||
throw new SolanaJSONRPCError(
|
||||
res.error,
|
||||
`failed to get info for accounts ${keys}`,
|
||||
);
|
||||
}
|
||||
return res.result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all the account info for multiple accounts specified by an array of public keys, return with context
|
||||
*/
|
||||
|
|
|
@ -3214,8 +3214,9 @@ describe('Connection', function () {
|
|||
if (process.env.TEST_LIVE) {
|
||||
describe('token methods', () => {
|
||||
const connection = new Connection(url, 'confirmed');
|
||||
const newAccount = Keypair.generate().publicKey;
|
||||
const newAccount = PublicKey.unique();
|
||||
|
||||
let payerKeypair = new Keypair();
|
||||
let testTokenMintPubkey: PublicKey;
|
||||
let testOwnerKeypair: Keypair;
|
||||
let testTokenAccountPubkey: PublicKey;
|
||||
|
@ -3225,7 +3226,6 @@ describe('Connection', function () {
|
|||
before(async function () {
|
||||
this.timeout(30 * 1000);
|
||||
|
||||
const payerKeypair = new Keypair();
|
||||
await connection.confirmTransaction(
|
||||
await connection.requestAirdrop(payerKeypair.publicKey, 100000000000),
|
||||
);
|
||||
|
@ -3382,6 +3382,55 @@ describe('Connection', function () {
|
|||
}
|
||||
});
|
||||
|
||||
it('get multiple parsed token accounts', async () => {
|
||||
const accounts = (
|
||||
await connection.getMultipleParsedAccounts([
|
||||
testTokenAccountPubkey,
|
||||
testTokenMintPubkey,
|
||||
payerKeypair.publicKey,
|
||||
newAccount,
|
||||
])
|
||||
).value;
|
||||
expect(accounts.length).to.eq(4);
|
||||
|
||||
const parsedTokenAccount = accounts[0];
|
||||
if (parsedTokenAccount) {
|
||||
const data = parsedTokenAccount.data;
|
||||
if (Buffer.isBuffer(data)) {
|
||||
expect(Buffer.isBuffer(data)).to.eq(false);
|
||||
} else {
|
||||
expect(data.program).to.eq('spl-token');
|
||||
expect(data.parsed).to.be.ok;
|
||||
}
|
||||
} else {
|
||||
expect(parsedTokenAccount).to.be.ok;
|
||||
}
|
||||
|
||||
const parsedTokenMint = accounts[1];
|
||||
if (parsedTokenMint) {
|
||||
const data = parsedTokenMint.data;
|
||||
if (Buffer.isBuffer(data)) {
|
||||
expect(Buffer.isBuffer(data)).to.eq(false);
|
||||
} else {
|
||||
expect(data.program).to.eq('spl-token');
|
||||
expect(data.parsed).to.be.ok;
|
||||
}
|
||||
} else {
|
||||
expect(parsedTokenMint).to.be.ok;
|
||||
}
|
||||
|
||||
const unparsedPayerAccount = accounts[2];
|
||||
if (unparsedPayerAccount) {
|
||||
const data = unparsedPayerAccount.data;
|
||||
expect(Buffer.isBuffer(data)).to.be.true;
|
||||
} else {
|
||||
expect(unparsedPayerAccount).to.be.ok;
|
||||
}
|
||||
|
||||
const unknownAccount = accounts[3];
|
||||
expect(unknownAccount).to.not.be.ok;
|
||||
});
|
||||
|
||||
it('get parsed token program accounts', async () => {
|
||||
const tokenAccounts = await connection.getParsedProgramAccounts(
|
||||
TOKEN_PROGRAM_ID,
|
||||
|
|
Loading…
Reference in New Issue