From aefedef97f2e5f5a2dc61234f2f5bb99e197eed1 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Mon, 27 Dec 2021 20:16:43 +0300 Subject: [PATCH 1/4] web3.js: add Connection.getFeeForMessage --- web3.js/src/connection.ts | 18 ++++++++++++++++++ web3.js/test/connection.test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index dd4b592d8bc742..edd9e56855eb44 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -2998,6 +2998,24 @@ export class Connection { }; } + /** + * Fetch the fee for a message from the cluster, return with context + */ + async getFeeForMessage( + message: Message, + commitment?: Commitment, + ): Promise> { + const wireMessage = message.serialize().toString('base64'); + const args = this._buildArgs([wireMessage], commitment); + const unsafeRes = await this._rpcRequest('getFeeForMessage', args); + + const res = create(unsafeRes, jsonRpcResultAndContext(nullable(number()))); + if ('error' in res) { + throw new Error('failed to get slot: ' + res.error.message); + } + return res.result; + } + /** * Fetch a recent blockhash from the cluster * @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>} diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index 958eb298dc7a63..e34ccc0f953d26 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -2278,6 +2278,38 @@ describe('Connection', () => { expect(feeCalculator.lamportsPerSignature).to.eq(5000); }); + it('get fee for message', async () => { + const accountFrom = Keypair.generate(); + const accountTo = Keypair.generate(); + + const {blockhash} = await helpers.recentBlockhash({connection}); + + const transaction = new Transaction({ + feePayer: accountFrom.publicKey, + recentBlockhash: blockhash, + }).add( + SystemProgram.transfer({ + fromPubkey: accountFrom.publicKey, + toPubkey: accountTo.publicKey, + lamports: 10, + }), + ); + const message = transaction.compileMessage(); + + await mockRpcResponse({ + method: 'getFeeForMessage', + params: [ + message.serialize().toString('base64'), + {commitment: 'confirmed'}, + ], + value: 5000, + withContext: true, + }); + + const fee = (await connection.getFeeForMessage(message, 'confirmed')).value; + expect(fee).to.eq(5000); + }); + it('get block time', async () => { await mockRpcResponse({ method: 'getBlockTime', From 1362356b46723e1594e650966adf125320157829 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Mon, 10 Jan 2022 18:44:24 +0300 Subject: [PATCH 2/4] throw if value is null --- web3.js/src/connection.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index edd9e56855eb44..8a0b28472e8a15 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -3013,6 +3013,9 @@ export class Connection { if ('error' in res) { throw new Error('failed to get slot: ' + res.error.message); } + if (res.value === null) { + throw new Error('invalid blockhash'); + } return res.result; } From 851b1d5172ef45630aff96fc0d7f7cceec95f78f Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Tue, 11 Jan 2022 12:03:23 +0300 Subject: [PATCH 3/4] fix null value --- web3.js/src/connection.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 8a0b28472e8a15..c8f79a5e0512b1 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -3004,7 +3004,7 @@ export class Connection { async getFeeForMessage( message: Message, commitment?: Commitment, - ): Promise> { + ): Promise> { const wireMessage = message.serialize().toString('base64'); const args = this._buildArgs([wireMessage], commitment); const unsafeRes = await this._rpcRequest('getFeeForMessage', args); @@ -3013,7 +3013,7 @@ export class Connection { if ('error' in res) { throw new Error('failed to get slot: ' + res.error.message); } - if (res.value === null) { + if (res.result === null) { throw new Error('invalid blockhash'); } return res.result; From 481eeb9447d2570d293f91ad7f5cf665389f2a42 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Tue, 11 Jan 2022 12:37:55 +0300 Subject: [PATCH 4/4] fix types --- web3.js/src/connection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index c8f79a5e0512b1..583aaefc324396 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -3016,7 +3016,7 @@ export class Connection { if (res.result === null) { throw new Error('invalid blockhash'); } - return res.result; + return res.result as unknown as RpcResponseAndContext; } /**