Skip to content

Commit

Permalink
feat(js) add ability to get GasInfo after gas calculation (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zewasik authored Dec 22, 2024
1 parent 896a081 commit ee8781a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 13 additions & 4 deletions js/src/transaction-builder.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -34,6 +34,7 @@ export class TransactionBuilder<ResponseType> {
private _signerOptions: Partial<SignerOptions>;
private _tx: SubmittableExtrinsic<'promise', ISubmittableResult>;
private _voucher: string;
private _gasInfo: GasInfo;
public readonly programId: HexString;

constructor(
Expand Down Expand Up @@ -159,9 +160,11 @@ export class TransactionBuilder<ResponseType> {
? 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(),
Expand All @@ -174,7 +177,7 @@ export class TransactionBuilder<ResponseType> {
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(),
Expand All @@ -187,7 +190,7 @@ export class TransactionBuilder<ResponseType> {
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(),
Expand All @@ -204,6 +207,8 @@ export class TransactionBuilder<ResponseType> {
}
}

this._gasInfo = gas;

return this;
}

Expand Down Expand Up @@ -357,4 +362,8 @@ export class TransactionBuilder<ResponseType> {
},
};
}

get gasInfo() {
return this._gasInfo;
}
}
12 changes: 12 additions & 0 deletions js/test/demo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('Ping', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand All @@ -62,6 +63,7 @@ describe('Ping', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand Down Expand Up @@ -91,6 +93,7 @@ describe('Ping', () => {

const result = await response();

expect(transaction.gasInfo).toBeDefined();
expect(result).toHaveProperty('ok', 'pong');
});
});
Expand All @@ -104,6 +107,7 @@ describe('Counter', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand All @@ -121,6 +125,7 @@ describe('Counter', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand All @@ -143,6 +148,7 @@ describe('Counter', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand Down Expand Up @@ -171,6 +177,7 @@ describe('Dog', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand All @@ -188,6 +195,7 @@ describe('Dog', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand All @@ -212,6 +220,7 @@ describe('Dog', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand Down Expand Up @@ -242,6 +251,7 @@ describe('ThisThat', () => {

const { msgId, blockHash, response } = await transaction.signAndSend();

expect(transaction.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand All @@ -256,6 +266,7 @@ describe('ThisThat', () => {

const { msgId, blockHash, response } = await tx.signAndSend();

expect(tx.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand All @@ -272,6 +283,7 @@ describe('ThisThat', () => {

const { msgId, blockHash, response } = await tx.signAndSend();

expect(tx.gasInfo).toBeDefined();
expect(msgId).toBeDefined();
expect(blockHash).toBeDefined();

Expand Down

0 comments on commit ee8781a

Please sign in to comment.