Expose `getTotalSupply` RPC method (#370)

This commit is contained in:
Trent Nelson 2019-06-25 09:31:22 -06:00 committed by Michael Vines
parent fd2ddef520
commit 034f31d3bc
3 changed files with 38 additions and 0 deletions

View File

@ -93,6 +93,7 @@ declare module '@solana/web3.js' {
signature: TransactionSignature,
): Promise<SignatureSuccess | TransactionError | null>;
getTransactionCount(): Promise<number>;
getTotalSupply(): Promise<number>;
getRecentBlockhash(): Promise<[Blockhash, FeeCalculator]>;
requestAirdrop(
to: PublicKey,

View File

@ -214,6 +214,11 @@ const GetSignatureStatusRpcResult = jsonRpcResult(
*/
const GetTransactionCountRpcResult = jsonRpcResult('number');
/**
* Expected JSON RPC response for the "getTotalSupply" message
*/
const GetTotalSupplyRpcResult = jsonRpcResult('number');
/**
* Expected JSON RPC response for the "getRecentBlockhash" message
*/
@ -524,6 +529,19 @@ export class Connection {
return Number(res.result);
}
/**
* Fetch the current total currency supply of the cluster
*/
async getTotalSupply(): Promise<number> {
const unsafeRes = await this._rpcRequest('getTotalSupply', []);
const res = GetTotalSupplyRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return Number(res.result);
}
/**
* Fetch a recent blockhash from the cluster
*/

View File

@ -199,6 +199,25 @@ test('get transaction count', async () => {
expect(count).toBeGreaterThanOrEqual(0);
});
test('get total supply', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getTotalSupply',
params: [],
},
{
error: null,
result: 1000000,
},
]);
const count = await connection.getTotalSupply();
expect(count).toBeGreaterThanOrEqual(0);
});
test('get recent blockhash', async () => {
const connection = new Connection(url);