Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getMultipleParsedAccounts method #28414

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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