From 02057d891ac8bbb0fb76858f3041f0e3a6d92801 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Fri, 23 Aug 2024 12:33:47 +0530 Subject: [PATCH] Add config option and arg to set gas price --- config.example.yml | 1 + package.json | 1 + src/cmds/registry-cmds/bond-cmds/create.ts | 6 +- src/cmds/registry-cmds/tokens-cmds/send.ts | 7 +- src/cmds/registry.ts | 3 +- src/util/common.ts | 5 +- src/util/fees.ts | 38 +++++++---- yarn.lock | 75 ++++++++++++++++++---- 8 files changed, 103 insertions(+), 33 deletions(-) diff --git a/config.example.yml b/config.example.yml index c83f987..f2be2b9 100644 --- a/config.example.yml +++ b/config.example.yml @@ -7,3 +7,4 @@ services: chainId: laconic_9000-1 gas: 200000 fees: 200000alnt + gasPrice: 1alnt diff --git a/package.json b/package.json index 0e5d681..2d01a95 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ }, "dependencies": { "@cerc-io/registry-sdk": "^0.2.6", + "@cosmjs/stargate": "^0.32.2", "fs-extra": "^10.1.0", "js-yaml": "^3.14.1", "lodash": "^4.17.21", diff --git a/src/cmds/registry-cmds/bond-cmds/create.ts b/src/cmds/registry-cmds/bond-cmds/create.ts index bb2bc13..2385d94 100644 --- a/src/cmds/registry-cmds/bond-cmds/create.ts +++ b/src/cmds/registry-cmds/bond-cmds/create.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import assert from 'assert'; import { Registry } from '@cerc-io/registry-sdk'; -import { getConfig, getConnectionInfo, getGasAndFees, txOutput } from '../../../util'; +import { getConfig, getConnectionInfo, getGasAndFees, getGasPrice, txOutput } from '../../../util'; export const command = 'create'; @@ -32,7 +32,9 @@ export const handler = async (argv: Arguments) => { assert(privateKey, 'Invalid Transaction Key.'); assert(chainId, 'Invalid registry Chain ID.'); - const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId); + const gasPrice = getGasPrice(argv, registryConfig); + const registry = new Registry(gqlEndpoint, rpcEndpoint, { chainId, gasPrice }); + const fee = getGasAndFees(argv, registryConfig); const bondId = await registry.getNextBondId(privateKey); const result = await registry.createBond({ denom, amount }, privateKey, fee); diff --git a/src/cmds/registry-cmds/tokens-cmds/send.ts b/src/cmds/registry-cmds/tokens-cmds/send.ts index 7523ccf..7183f80 100644 --- a/src/cmds/registry-cmds/tokens-cmds/send.ts +++ b/src/cmds/registry-cmds/tokens-cmds/send.ts @@ -1,9 +1,10 @@ import { Arguments } from 'yargs'; import assert from 'assert'; -import { Account, Registry } from '@cerc-io/registry-sdk'; + +import { Account, Registry, DEFAULT_GAS_ESTIMATION_MULTIPLIER } from '@cerc-io/registry-sdk'; +import { DeliverTxResponse } from '@cosmjs/stargate'; import { getConfig, getConnectionInfo, getGasAndFees, queryOutput } from '../../../util'; -import { DeliverTxResponse } from '@cosmjs/stargate'; export const command = 'send'; @@ -51,7 +52,7 @@ export const handler = async (argv: Arguments) => { amount } ], - fee); + fee || DEFAULT_GAS_ESTIMATION_MULTIPLIER); assert(txResponse.code === 0, `TX Failed - Hash: ${txResponse.transactionHash}, Code: ${txResponse.code}, Message: ${txResponse.rawLog}`); diff --git a/src/cmds/registry.ts b/src/cmds/registry.ts index 5097edd..54bbc39 100644 --- a/src/cmds/registry.ts +++ b/src/cmds/registry.ts @@ -15,7 +15,8 @@ exports.builder = (yargs: yargs.Argv) => { id: { type: 'string' }, address: { type: 'string' }, gas: { type: 'string' }, - fees: { type: 'string' } + fees: { type: 'string' }, + gasPrice: { type: 'string' } }) .commandDir('registry-cmds') .demandCommand() diff --git a/src/util/common.ts b/src/util/common.ts index cc0bdd6..e5559c3 100644 --- a/src/util/common.ts +++ b/src/util/common.ts @@ -2,7 +2,7 @@ import { Arguments } from 'yargs'; import clean from 'lodash-clean'; export const getConnectionInfo = (argv: Arguments, config: any) => { - const { server, userKey, bondId, txKey, chainId, fees, gas } = argv; + const { server, userKey, bondId, txKey, chainId, fees, gas, gasPrice } = argv; const result = { ...config, @@ -10,7 +10,8 @@ export const getConnectionInfo = (argv: Arguments, config: any) => { ...clean({ server, userKey, bondId, txKey, chainId }), privateKey: stripHexPrefix(txKey || userKey || config.userKey), gas: String(gas || config.gas), - fees: String(fees || config.fees) + fees: String(fees || config.fees), + gasPrice: String(gasPrice || config.gasPrice) }; return result; diff --git a/src/util/fees.ts b/src/util/fees.ts index d0a07ef..589810f 100644 --- a/src/util/fees.ts +++ b/src/util/fees.ts @@ -2,23 +2,35 @@ import assert from 'assert'; import { Arguments } from 'yargs'; -export const parseGasAndFees = (gas: string, fees = '') => { - assert(gas, 'Invalid gas.'); +import { StdFee, GasPrice, parseCoins } from '@cosmjs/stargate'; - const [{ amount, denom }] = fees.trim().split(',') - .map(fee => fee.trim().split(/(\d+)/)) - .filter(([_, amount, denom]) => (denom && amount)) - .map(([_, amount, denom]) => ({ denom, amount })); +export const parseGasAndFees = (gas?: string, fees?: string): StdFee | number | undefined => { + const isFeesANumber = !isNaN(Number(fees)); - return { - amount: [{ denom, amount }], - gas - }; + // If fees is a number or not given, treat it as a gas estimation multiplier + if (fees == null) { + return undefined; + } else if (isFeesANumber) { + return Number(fees); + } else { + // If fees is not a gas estimation multiplier, gas is required + assert(gas, 'Invalid gas.'); + + return { + amount: parseCoins(String(fees)), + gas: String(gas) + }; + } }; -export const getGasAndFees = (argv: Arguments, config: any = {}) => { +export const getGasAndFees = (argv: Arguments, config: any = {}): StdFee | number | undefined => { return parseGasAndFees( - String(argv.gas || config.gas), - String(argv.fees || config.fees) + argv.gas || config.gas, + argv.fees || config.fees ); }; + +export const getGasPrice = (argv: Arguments, config: any = {}): GasPrice | undefined => { + const gasPriceString = argv.gasPrice || config.gasPrice; + return gasPriceString != null ? GasPrice.fromString(String(gasPriceString)) : undefined; +}; diff --git a/yarn.lock b/yarn.lock index 62ac42b..a2e74b7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -365,6 +365,16 @@ "@cosmjs/math" "0.28.4" "@cosmjs/utils" "0.28.4" +"@cosmjs/amino@^0.32.2": + version "0.32.4" + resolved "https://registry.yarnpkg.com/@cosmjs/amino/-/amino-0.32.4.tgz#3908946c0394e6d431694c8992c5147079a1c860" + integrity sha512-zKYOt6hPy8obIFtLie/xtygCkH9ZROiQ12UHfKsOkWaZfPQUvVbtgmu6R4Kn1tFLI/SRkw7eqhaogmW/3NYu/Q== + dependencies: + "@cosmjs/crypto" "^0.32.4" + "@cosmjs/encoding" "^0.32.4" + "@cosmjs/math" "^0.32.4" + "@cosmjs/utils" "^0.32.4" + "@cosmjs/amino@^0.32.3": version "0.32.3" resolved "https://registry.yarnpkg.com/@cosmjs/amino/-/amino-0.32.3.tgz#b81d4a2b8d61568431a1afcd871e1344a19d97ff" @@ -417,6 +427,19 @@ elliptic "^6.5.4" libsodium-wrappers-sumo "^0.7.11" +"@cosmjs/crypto@^0.32.4": + version "0.32.4" + resolved "https://registry.yarnpkg.com/@cosmjs/crypto/-/crypto-0.32.4.tgz#5d29633b661eaf092ddb3e7ea6299cfd6f4507a2" + integrity sha512-zicjGU051LF1V9v7bp8p7ovq+VyC91xlaHdsFOTo2oVry3KQikp8L/81RkXmUIT8FxMwdx1T7DmFwVQikcSDIw== + dependencies: + "@cosmjs/encoding" "^0.32.4" + "@cosmjs/math" "^0.32.4" + "@cosmjs/utils" "^0.32.4" + "@noble/hashes" "^1" + bn.js "^5.2.0" + elliptic "^6.5.4" + libsodium-wrappers-sumo "^0.7.11" + "@cosmjs/encoding@0.27.1": version "0.27.1" resolved "https://registry.yarnpkg.com/@cosmjs/encoding/-/encoding-0.27.1.tgz#3cd5bc0af743485eb2578cdb08cfa84c86d610e1" @@ -435,6 +458,15 @@ bech32 "^1.1.4" readonly-date "^1.0.0" +"@cosmjs/encoding@^0.32.2", "@cosmjs/encoding@^0.32.4": + version "0.32.4" + resolved "https://registry.yarnpkg.com/@cosmjs/encoding/-/encoding-0.32.4.tgz#646e0e809f7f4f1414d8fa991fb0ffe6c633aede" + integrity sha512-tjvaEy6ZGxJchiizzTn7HVRiyTg1i4CObRRaTRPknm5EalE13SV+TCHq38gIDfyUeden4fCuaBVEdBR5+ti7Hw== + dependencies: + base64-js "^1.3.0" + bech32 "^1.1.4" + readonly-date "^1.0.0" + "@cosmjs/encoding@^0.32.3": version "0.32.3" resolved "https://registry.yarnpkg.com/@cosmjs/encoding/-/encoding-0.32.3.tgz#e245ff511fe4a0df7ba427b5187aab69e3468e5b" @@ -479,6 +511,13 @@ dependencies: bn.js "^5.2.0" +"@cosmjs/math@^0.32.2", "@cosmjs/math@^0.32.4": + version "0.32.4" + resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.32.4.tgz#87ac9eadc06696e30a30bdb562a495974bfd0a1a" + integrity sha512-++dqq2TJkoB8zsPVYCvrt88oJWsy1vMOuSOKcdlnXuOA/ASheTJuYy4+oZlTQ3Fr8eALDLGGPhJI02W2HyAQaw== + dependencies: + bn.js "^5.2.0" + "@cosmjs/math@^0.32.3": version "0.32.3" resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.32.3.tgz#16e4256f4da507b9352327da12ae64056a2ba6c9" @@ -486,7 +525,7 @@ dependencies: bn.js "^5.2.0" -"@cosmjs/proto-signing@^0.32.2", "@cosmjs/proto-signing@^0.32.3": +"@cosmjs/proto-signing@^0.32.2": version "0.32.3" resolved "https://registry.yarnpkg.com/@cosmjs/proto-signing/-/proto-signing-0.32.3.tgz#91ae149b747d18666a6ccc924165b306431f7c0d" integrity sha512-kSZ0ZUY0DwcRT0NcIn2HkadH4NKlwjfZgbLj1ABwh/4l0RgeT84QCscZCu63tJYq3K6auwqTiZSZERwlO4/nbg== @@ -509,21 +548,28 @@ xstream "^11.14.0" "@cosmjs/stargate@^0.32.2": - version "0.32.3" - resolved "https://registry.yarnpkg.com/@cosmjs/stargate/-/stargate-0.32.3.tgz#5a92b222ada960ebecea72cc9f366370763f4b66" - integrity sha512-OQWzO9YWKerUinPIxrO1MARbe84XkeXJAW0lyMIjXIEikajuXZ+PwftiKA5yA+8OyditVmHVLtPud6Pjna2s5w== + version "0.32.2" + resolved "https://registry.yarnpkg.com/@cosmjs/stargate/-/stargate-0.32.2.tgz#73718c5c6a3ae138682ee9987333d911eca22a13" + integrity sha512-AsJa29fT7Jd4xt9Ai+HMqhyj7UQu7fyYKdXj/8+/9PD74xe6lZSYhQPcitUmMLJ1ckKPgXSk5Dd2LbsQT0IhZg== dependencies: "@confio/ics23" "^0.6.8" - "@cosmjs/amino" "^0.32.3" - "@cosmjs/encoding" "^0.32.3" - "@cosmjs/math" "^0.32.3" - "@cosmjs/proto-signing" "^0.32.3" - "@cosmjs/stream" "^0.32.3" - "@cosmjs/tendermint-rpc" "^0.32.3" - "@cosmjs/utils" "^0.32.3" + "@cosmjs/amino" "^0.32.2" + "@cosmjs/encoding" "^0.32.2" + "@cosmjs/math" "^0.32.2" + "@cosmjs/proto-signing" "^0.32.2" + "@cosmjs/stream" "^0.32.2" + "@cosmjs/tendermint-rpc" "^0.32.2" + "@cosmjs/utils" "^0.32.2" cosmjs-types "^0.9.0" xstream "^11.14.0" +"@cosmjs/stream@^0.32.2": + version "0.32.4" + resolved "https://registry.yarnpkg.com/@cosmjs/stream/-/stream-0.32.4.tgz#83e1f2285807467c56d9ea0e1113f79d9fa63802" + integrity sha512-Gih++NYHEiP+oyD4jNEUxU9antoC0pFSg+33Hpp0JlHwH0wXhtD3OOKnzSfDB7OIoEbrzLJUpEjOgpCp5Z+W3A== + dependencies: + xstream "^11.14.0" + "@cosmjs/stream@^0.32.3": version "0.32.3" resolved "https://registry.yarnpkg.com/@cosmjs/stream/-/stream-0.32.3.tgz#7522579aaf18025d322c2f33d6fb7573220395d6" @@ -531,7 +577,7 @@ dependencies: xstream "^11.14.0" -"@cosmjs/tendermint-rpc@^0.32.2", "@cosmjs/tendermint-rpc@^0.32.3": +"@cosmjs/tendermint-rpc@^0.32.2": version "0.32.3" resolved "https://registry.yarnpkg.com/@cosmjs/tendermint-rpc/-/tendermint-rpc-0.32.3.tgz#f0406b9f0233e588fb924dca8c614972f9038aff" integrity sha512-xeprW+VR9xKGstqZg0H/KBZoUp8/FfFyS9ljIUTLM/UINjP2MhiwncANPS2KScfJVepGufUKk0/phHUeIBSEkw== @@ -557,6 +603,11 @@ resolved "https://registry.yarnpkg.com/@cosmjs/utils/-/utils-0.28.4.tgz#ecbc72458cdaffa6eeef572bc691502b3151330f" integrity sha512-lb3TU6833arPoPZF8HTeG9V418CpurvqH5Aa/ls0I0wYdPDEMO6622+PQNQhQ8Vw8Az2MXoSyc8jsqrgawT84Q== +"@cosmjs/utils@^0.32.2", "@cosmjs/utils@^0.32.4": + version "0.32.4" + resolved "https://registry.yarnpkg.com/@cosmjs/utils/-/utils-0.32.4.tgz#a9a717c9fd7b1984d9cefdd0ef6c6f254060c671" + integrity sha512-D1Yc+Zy8oL/hkUkFUL/bwxvuDBzRGpc4cF7/SkdhxX4iHpSLgdOuTt1mhCh9+kl6NQREy9t7SYZ6xeW5gFe60w== + "@cosmjs/utils@^0.32.3": version "0.32.3" resolved "https://registry.yarnpkg.com/@cosmjs/utils/-/utils-0.32.3.tgz#5dcaee6dd7cc846cdc073e9a7a7f63242f5f7e31"