diff --git a/networks/cosmos/src/workflows/plugins/fee-calculation.ts b/networks/cosmos/src/workflows/plugins/fee-calculation.ts index dd94c15d..3a7dfed8 100644 --- a/networks/cosmos/src/workflows/plugins/fee-calculation.ts +++ b/networks/cosmos/src/workflows/plugins/fee-calculation.ts @@ -6,6 +6,7 @@ import { INPUT_VALIDATION_STAGING_KEYS } from './input-validation'; import { MESSAGE_ENCODING_STAGING_KEYS } from './message-encoding'; import { SIGNER_INFO_STAGING_KEYS } from './signer-info'; import { Price } from '@interchainjs/types'; +import Decimal from 'decimal.js'; /** * Staging keys created by FeeCalculationPlugin @@ -63,30 +64,20 @@ export class FeeCalculationPlugin extends BaseWorkflowBuilderPlugin< const multiplier = params.options?.multiplier ?? 1.5; const gasLimit = BigInt(Math.ceil(Number(gasUsed) * multiplier)); - // Default gas price (this should be configurable) + // Default gas price ( Price | 'average' | 'high' | 'low' | undefined) let denom = "uatom"; - let gasPriceValue: string; + let gasPriceValue = new Decimal('0.025'); - if (params.options?.gasPrice && - typeof params.options.gasPrice === 'object' && - 'amount' in params.options.gasPrice && - 'denom' in params.options.gasPrice && - typeof (params.options.gasPrice as any).denom === 'string' && - (params.options.gasPrice as any).amount) { - // type Price - const price = params.options.gasPrice as Price; - denom = price.denom; - gasPriceValue = price.amount.toString(); - } else if (typeof params.options?.gasPrice === 'string' && - !isNaN(Number(params.options.gasPrice))) { - // type string (number only) - gasPriceValue = params.options.gasPrice; - } else { - // 'average' | 'high' | 'low' | undefined - gasPriceValue = '0.025'; + // ex) 123.123uatom -> gasPriceValue: "123.123", denom: "uatom") + if (typeof params.options?.gasPrice === 'string') { + const numberMatch = params.options.gasPrice.match(/^(\d+(?:\.\d+)?)(.*)$/); + if (numberMatch && numberMatch.length === 3) { + gasPriceValue = new Decimal(numberMatch[1]); + denom = numberMatch[2]; + } } - const amount = BigInt(Math.ceil(Number(gasLimit) * Number(gasPriceValue))); + const amount = gasPriceValue.mul(gasLimit.toString()).ceil(); finalFee = { amount: [{ denom, amount: amount.toString() }],