Skip to content

Commit

Permalink
Added gas estimation helper function.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdc-Hitesh committed Oct 15, 2021
1 parent 6e30524 commit c2e6c8d
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
111 changes: 111 additions & 0 deletions lib/src/client/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
import { expect } from 'chai';
import nock from 'nock';
import { fuzzyDescribe } from '../test/mocha-fuzzy/suite';
Expand Down Expand Up @@ -121,6 +122,116 @@ describe('CroClient', function () {
nock.cleanAll();
});

it('Should return `gas_info` for gas estimation', async function () {
nock.disableNetConnect();
const mockSimulatedResponse = {
gas_info: {
gas_wanted: '0',
gas_used: '462867',
},
result: {
data: 'CiEKHy9jaGFpbm1haW4ubmZ0LnYxLk1zZ0lzc3VlRGVub20=',
log:
'[{"events":[{"type":"issue_denom","attributes":[{"key":"denom_id","value":"alpha"},{"key":"denom_name","value":"nft_name"},{"key":"creator","value":"tcro1rg4g3saewjdeadvalj427p735xy07s0ng6m4yu"}]},{"type":"message","attributes":[{"key":"action","value":"/chainmain.nft.v1.MsgIssueDenom"},{"key":"module","value":"nft"},{"key":"sender","value":"tcro1rg4g3saewjdeadvalj427p735xy07s0ng6m4yu"}]}]}]',
events: [
{
type: 'message',
attributes: [
{
key: 'YWN0aW9u',
value: 'L2NoYWlubWFpbi5uZnQudjEuTXNnSXNzdWVEZW5vbQ==',
index: false,
},
],
},
{
type: 'issue_denom',
attributes: [
{
key: 'ZGVub21faWQ=',
value: 'YWxwaGE=',
index: false,
},
{
key: 'ZGVub21fbmFtZQ==',
value: 'bmZ0X25hbWU=',
index: false,
},
{
key: 'Y3JlYXRvcg==',
value: 'dGNybzFyZzRnM3NhZXdqZGVhZHZhbGo0MjdwNzM1eHkwN3Mwbmc2bTR5dQ==',
index: false,
},
],
},
{
type: 'message',
attributes: [
{
key: 'bW9kdWxl',
value: 'bmZ0',
index: false,
},
{
key: 'c2VuZGVy',
value: 'dGNybzFyZzRnM3NhZXdqZGVhZHZhbGo0MjdwNzM1eHkwN3Mwbmc2bTR5dQ==',
index: false,
},
],
},
],
},
};
const cosmosTxObject = {
body: {
messages: [
{
'@type': '/cosmos.bank.v1beta1.MsgSend',
amount: [{ denom: 'basetcro', amount: '1000' }],
from_address: 'tcro1fzcrza3j4f2677jfuxulkg33z6852qsqs8hx50',
to_address: 'tcro1fzcrza3j4f2677jfuxulkg33z6852qsqs8hx50',
},
],
memo: 'amino test',
timeout_height: '0',
extension_options: [],
non_critical_extension_options: [],
},
auth_info: {
signer_infos: [
{
public_key: {
'@type': '/cosmos.crypto.secp256k1.PubKey',
key: 'AiPJOV1BAT5kcMjSfai3WFBVT6raP+PoEmYMvfRTSoXX',
},
mode_info: { single: { mode: 'SIGN_MODE_DIRECT' } },
sequence: '1',
},
],
fee: {
amount: [{ denom: 'basetcro', amount: '10000' }],
gas_limit: '100000',
payer: '',
granter: '',
},
},
signatures: [],
};

const rpcUrl: string = `${CroNetwork.Mainnet.defaultNodeUrl}:1317`;

nock.cleanAll();

nock(rpcUrl).persist(true).post('/cosmos/tx/v1beta1/simulate').reply(200, mockSimulatedResponse);

const croHttpClient = await cro.CroClient.estimateGasLimit(cosmosTxObject);

expect(croHttpClient.gas_used).to.be.equal('462867');
expect(croHttpClient.gas_wanted).to.be.equal('0');

nock.cleanAll();
});

function remockNockForAbciQuery() {
const rpcUrl: string = CroNetwork.Testnet.rpcUrl ?? '';
nock.cleanAll();
Expand Down
25 changes: 25 additions & 0 deletions lib/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import {
StargateClient,
Expand All @@ -18,8 +19,24 @@ import { Coin } from '@cosmjs/stargate/build/codec/cosmos/base/v1beta1/coin';
import { SearchTxFilter, SearchTxQuery } from '@cosmjs/stargate/build/search';
import { Block, BroadcastTxResponse, IndexedTx, SequenceResponse } from '@cosmjs/stargate/build/stargateclient';
import ow from 'ow';
import axios from 'axios';
import { InitConfigurations } from '../core/cro';
import { owUrl } from './ow.types';
import { CosmosTx } from '../cosmos/v1beta1/types/cosmostx';

export interface GasInfo {
gas_wanted: string;
gas_used: string;
}

export interface GasEstimateResponse {
gas_info: GasInfo;
result: {
data: string;
log: string;
events: { type: string; attributes: { key: string; value: string }[] }[];
};
}

export interface ICroClient {
query():
Expand Down Expand Up @@ -122,5 +139,13 @@ export const croClient = function (config: InitConfigurations) {
public async broadcastTx(tx: Uint8Array): Promise<BroadcastTxResponse> {
return this.txClient.broadcastTx(tx);
}

public static async estimateGasLimit(txBody: CosmosTx): Promise<GasInfo> {
const COSMOS_REST_PORT = 1317;
const requestUrl = `${config.network.defaultNodeUrl}:${COSMOS_REST_PORT}/cosmos/tx/v1beta1/simulate`;
const postData = { tx: txBody };
const gasEstimatedResponse = await axios.post<GasEstimateResponse>(requestUrl, postData);
return gasEstimatedResponse.data.gas_info;
}
};
};

0 comments on commit c2e6c8d

Please sign in to comment.