diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 3274938d64..6e5db26190 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -93,6 +93,7 @@ declare module '@solana/web3.js' { signature: TransactionSignature, ): Promise; getTransactionCount(): Promise; + getTotalSupply(): Promise; getRecentBlockhash(): Promise<[Blockhash, FeeCalculator]>; requestAirdrop( to: PublicKey, diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index 53df89b095..23ffc07036 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -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 { + 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 */ diff --git a/web3.js/test/connection.test.js b/web3.js/test/connection.test.js index 3ddef10341..7fe5f2472c 100644 --- a/web3.js/test/connection.test.js +++ b/web3.js/test/connection.test.js @@ -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);