From 9041ccd22c76dfa8db1823f41d098aa46e0f6310 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Thu, 9 Sep 2021 13:34:43 -0400 Subject: [PATCH] feat: add support for getGenesisHash RPC (#19732) --- web3.js/src/connection.ts | 12 ++++++++++++ web3.js/test/connection.test.ts | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 3a4978b6e3b56b..ba947633d98fed 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -2963,6 +2963,18 @@ export class Connection { return res.result; } + /** + * Fetch the genesis hash + */ + async getGenesisHash(): Promise { + const unsafeRes = await this._rpcRequest('getGenesisHash', []); + const res = create(unsafeRes, jsonRpcResult(string())); + if ('error' in res) { + throw new Error('failed to get genesis hash: ' + res.error.message); + } + return res.result; + } + /** * Fetch a processed block from the cluster. */ diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index 344c89bb5c1540..91c2a248cdfeb5 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -2668,6 +2668,17 @@ describe('Connection', () => { expect(version['solana-core']).to.be.ok; }); + it('getGenesisHash', async () => { + await mockRpcResponse({ + method: 'getGenesisHash', + params: [], + value: 'GH7ome3EiwEr7tu9JuTh2dpYWBJK3z69Xm1ZE3MEE6JC', + }); + + const genesisHash = await connection.getGenesisHash(); + expect(genesisHash).not.to.be.empty; + }); + it('request airdrop', async () => { const account = Keypair.generate();