feat: add getMultipleAccountsInfo which uses the getMultipleAccounts RPC method (#18736)

* feat(web3): add getMultipleAccountsInfo which uses the getMultipleAccounts RPC method

* fix: add airdrop to get multiple accounts info

Co-authored-by: Josh Hundley <josh.hundley@gmail.com>
This commit is contained in:
Tommy Johnson 2021-07-20 12:42:14 -04:00 committed by GitHub
parent 207c90bd8b
commit c8442fd476
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 0 deletions

View File

@ -2407,6 +2407,28 @@ export class Connection {
}
}
/**
* Fetch all the account info for multiple accounts specified by an array of public keys
*/
async getMultipleAccountsInfo(
publicKeys: PublicKey[],
commitment?: Commitment,
): Promise<AccountInfo<Buffer>[] | null> {
const keys = publicKeys.map(key => key.toBase58());
const args = this._buildArgs([keys], commitment, 'base64');
const unsafeRes = await this._rpcRequest('getMultipleAccounts', args);
const res = create(
unsafeRes,
jsonRpcResultAndContext(nullable(array(AccountInfoResult))),
);
if ('error' in res) {
throw new Error(
'failed to get info for accounts ' + keys + ': ' + res.error.message,
);
}
return res.result.value;
}
/**
* Returns epoch activation information for a stake account that has been delegated
*/

View File

@ -155,6 +155,76 @@ describe('Connection', () => {
.be.null;
});
it('get multiple accounts info', async () => {
const account1 = Keypair.generate();
const account2 = Keypair.generate();
{
await helpers.airdrop({
connection,
address: account1.publicKey,
amount: LAMPORTS_PER_SOL,
});
await helpers.airdrop({
connection,
address: account2.publicKey,
amount: LAMPORTS_PER_SOL,
});
}
const value = [
{
owner: '11111111111111111111111111111111',
lamports: LAMPORTS_PER_SOL,
data: ['', 'base64'],
executable: false,
rentEpoch: 0,
},
{
owner: '11111111111111111111111111111111',
lamports: LAMPORTS_PER_SOL,
data: ['', 'base64'],
executable: false,
rentEpoch: 0,
},
];
await mockRpcResponse({
method: 'getMultipleAccounts',
params: [
[account1.publicKey.toBase58(), account2.publicKey.toBase58()],
{encoding: 'base64'},
],
value: value,
withContext: true,
});
const res = await connection.getMultipleAccountsInfo(
[account1.publicKey, account2.publicKey],
'confirmed',
);
const expectedValue = [
{
owner: new PublicKey('11111111111111111111111111111111'),
lamports: LAMPORTS_PER_SOL,
data: Buffer.from([]),
executable: false,
rentEpoch: 0,
},
{
owner: new PublicKey('11111111111111111111111111111111'),
lamports: LAMPORTS_PER_SOL,
data: Buffer.from([]),
executable: false,
rentEpoch: 0,
},
];
expect(res).to.eql(expectedValue);
});
it('get program accounts', async () => {
const account0 = Keypair.generate();
const account1 = Keypair.generate();