Skip to content

Commit bee7973

Browse files
authored
Merge pull request #182 from nowooj/fix/gas-auto
fix: gas auto
2 parents 0a7d26e + d972f3d commit bee7973

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

networks/cosmos/src/workflows/plugins/fee-calculation.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { INPUT_VALIDATION_STAGING_KEYS } from './input-validation';
66
import { MESSAGE_ENCODING_STAGING_KEYS } from './message-encoding';
77
import { SIGNER_INFO_STAGING_KEYS } from './signer-info';
88
import { Price } from '@interchainjs/types';
9+
import Decimal from 'decimal.js';
910

1011
/**
1112
* Staging keys created by FeeCalculationPlugin
@@ -63,30 +64,20 @@ export class FeeCalculationPlugin extends BaseWorkflowBuilderPlugin<
6364
const multiplier = params.options?.multiplier ?? 1.5;
6465
const gasLimit = BigInt(Math.ceil(Number(gasUsed) * multiplier));
6566

66-
// Default gas price (this should be configurable)
67+
// Default gas price ( Price | 'average' | 'high' | 'low' | undefined)
6768
let denom = "uatom";
68-
let gasPriceValue: string;
69+
let gasPriceValue = new Decimal('0.025');
6970

70-
if (params.options?.gasPrice &&
71-
typeof params.options.gasPrice === 'object' &&
72-
'amount' in params.options.gasPrice &&
73-
'denom' in params.options.gasPrice &&
74-
typeof (params.options.gasPrice as any).denom === 'string' &&
75-
(params.options.gasPrice as any).amount) {
76-
// type Price
77-
const price = params.options.gasPrice as Price;
78-
denom = price.denom;
79-
gasPriceValue = price.amount.toString();
80-
} else if (typeof params.options?.gasPrice === 'string' &&
81-
!isNaN(Number(params.options.gasPrice))) {
82-
// type string (number only)
83-
gasPriceValue = params.options.gasPrice;
84-
} else {
85-
// 'average' | 'high' | 'low' | undefined
86-
gasPriceValue = '0.025';
71+
// ex) 123.123uatom -> gasPriceValue: "123.123", denom: "uatom")
72+
if (typeof params.options?.gasPrice === 'string') {
73+
const numberMatch = params.options.gasPrice.match(/^(\d+(?:\.\d+)?)(.*)$/);
74+
if (numberMatch && numberMatch.length === 3) {
75+
gasPriceValue = new Decimal(numberMatch[1]);
76+
denom = numberMatch[2];
77+
}
8778
}
8879

89-
const amount = BigInt(Math.ceil(Number(gasLimit) * Number(gasPriceValue)));
80+
const amount = gasPriceValue.mul(gasLimit.toString()).ceil();
9081

9182
finalFee = {
9283
amount: [{ denom, amount: amount.toString() }],

0 commit comments

Comments
 (0)