Skip to content

Commit

Permalink
Expose getTotalSupply RPC method (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson authored and mvines committed Jun 15, 2020
1 parent fd2ddef commit 034f31d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions web3.js/module.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions web3.js/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
19 changes: 19 additions & 0 deletions web3.js/test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 034f31d

Please sign in to comment.