From 95060890dd6523dbba05b477f5c3a415593601f0 Mon Sep 17 00:00:00 2001 From: Zewasik Date: Fri, 20 Dec 2024 19:50:14 +0200 Subject: [PATCH 1/2] add ability to get GasInfo after gas calculation --- js/src/transaction-builder.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/js/src/transaction-builder.ts b/js/src/transaction-builder.ts index 4902c4c7..3a88190b 100644 --- a/js/src/transaction-builder.ts +++ b/js/src/transaction-builder.ts @@ -1,4 +1,4 @@ -import { GearApi, HexString, ICallOptions, MessageQueuedData, decodeAddress } from '@gear-js/api'; +import { GasInfo, GearApi, HexString, ICallOptions, MessageQueuedData, decodeAddress } from '@gear-js/api'; import { SignerOptions, SubmittableExtrinsic } from '@polkadot/api/types'; import { IKeyringPair, ISubmittableResult } from '@polkadot/types/types'; import { TypeRegistry, u128, u64 } from '@polkadot/types'; @@ -34,6 +34,7 @@ export class TransactionBuilder { private _signerOptions: Partial; private _tx: SubmittableExtrinsic<'promise', ISubmittableResult>; private _voucher: string; + private _gasInfo: GasInfo; public readonly programId: HexString; constructor( @@ -159,9 +160,11 @@ export class TransactionBuilder { ? decodeAddress(typeof this._account === 'string' ? this._account : this._account.address) : ZERO_ADDRESS; + let gas: GasInfo; + switch (this._tx.method.method) { case 'uploadProgram': { - const gas = await this._api.program.calculateGas.initUpload( + gas = await this._api.program.calculateGas.initUpload( source, this._tx.args[0].toHex(), this._tx.args[2].toHex(), @@ -174,7 +177,7 @@ export class TransactionBuilder { break; } case 'createProgram': { - const gas = await this._api.program.calculateGas.initCreate( + gas = await this._api.program.calculateGas.initCreate( source, this._tx.args[0].toHex(), this._tx.args[2].toHex(), @@ -187,7 +190,7 @@ export class TransactionBuilder { break; } case 'sendMessage': { - const gas = await this._api.program.calculateGas.handle( + gas = await this._api.program.calculateGas.handle( source, this._tx.args[0].toHex(), this._tx.args[1].toHex(), @@ -204,6 +207,8 @@ export class TransactionBuilder { } } + this._gasInfo = gas; + return this; } @@ -357,4 +362,8 @@ export class TransactionBuilder { }, }; } + + get gasInfo() { + return this._gasInfo; + } } From 5caa358e508cc9bae094653a4f72147d7b4b5f5d Mon Sep 17 00:00:00 2001 From: Zewasik Date: Sun, 22 Dec 2024 10:26:42 +0200 Subject: [PATCH 2/2] add tests for gas info --- js/test/demo.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/js/test/demo.test.ts b/js/test/demo.test.ts index 2f74ed1a..ed342f93 100644 --- a/js/test/demo.test.ts +++ b/js/test/demo.test.ts @@ -50,6 +50,7 @@ describe('Ping', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -62,6 +63,7 @@ describe('Ping', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -91,6 +93,7 @@ describe('Ping', () => { const result = await response(); + expect(transaction.gasInfo).toBeDefined(); expect(result).toHaveProperty('ok', 'pong'); }); }); @@ -104,6 +107,7 @@ describe('Counter', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -121,6 +125,7 @@ describe('Counter', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -143,6 +148,7 @@ describe('Counter', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -171,6 +177,7 @@ describe('Dog', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -188,6 +195,7 @@ describe('Dog', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -212,6 +220,7 @@ describe('Dog', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -242,6 +251,7 @@ describe('ThisThat', () => { const { msgId, blockHash, response } = await transaction.signAndSend(); + expect(transaction.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -256,6 +266,7 @@ describe('ThisThat', () => { const { msgId, blockHash, response } = await tx.signAndSend(); + expect(tx.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined(); @@ -272,6 +283,7 @@ describe('ThisThat', () => { const { msgId, blockHash, response } = await tx.signAndSend(); + expect(tx.gasInfo).toBeDefined(); expect(msgId).toBeDefined(); expect(blockHash).toBeDefined();