fix: expose getProgramAccounts RPC method

This commit is contained in:
Tyera Eulberg
2019-06-28 19:28:06 -06:00
committed by Michael Vines
parent c14a44b8b4
commit 0e7c8cd01f
5 changed files with 100 additions and 6 deletions

View File

@ -148,6 +148,13 @@ const ProgramAccountNotificationResult = struct({
result: ProgramAccountInfoResult,
});
/**
* Expected JSON RPC response for the "getProgramAccounts" message
*/
const GetProgramAccountsRpcResult = jsonRpcResult(
struct.list([ProgramAccountInfoResult]),
);
/**
* Expected JSON RPC response for the "confirmTransaction" message
*/
@ -338,6 +345,11 @@ export type TransactionError = {|
*/
type BlockhashAndFeeCalculator = [Blockhash, FeeCalculator]; // This type exists to workaround an esdoc parse error
/**
* @ignore
*/
type PublicKeyAndAccount = [PublicKey, AccountInfo]; // This type exists to workaround an esdoc parse error
/**
* A connection to a fullnode JSON RPC endpoint
*/
@ -435,6 +447,36 @@ export class Connection {
};
}
/**
* Fetch all the accounts owned by the specified program id
*/
async getProgramAccounts(
programId: PublicKey,
): Promise<Array<PublicKeyAndAccount>> {
const unsafeRes = await this._rpcRequest('getProgramAccounts', [
programId.toBase58(),
]);
const res = GetProgramAccountsRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
const {result} = res;
assert(typeof result !== 'undefined');
return result.map(result => {
return [
result[0],
{
executable: result[1].executable,
owner: new PublicKey(result[1].owner),
lamports: result[1].lamports,
data: Buffer.from(result[1].data),
},
];
});
}
/**
* Confirm the transaction identified by the specified signature
*/