Skip to content

Commit

Permalink
feat: add getMultipleParsedAccounts method (solana-labs#28414)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored and nickfrosty committed Jan 4, 2023
1 parent dc68283 commit 0f60644
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
26 changes: 26 additions & 0 deletions web3.js/src/connection.ts
Expand Up @@ -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
*/
Expand Down
53 changes: 51 additions & 2 deletions web3.js/test/connection.test.ts
Expand Up @@ -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;
Expand All @@ -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),
);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0f60644

Please sign in to comment.