From 8c44143884e6433a4b2a05f3fcfa0cfc964c307d Mon Sep 17 00:00:00 2001 From: Bill He Date: Mon, 30 Sep 2024 16:16:17 -0700 Subject: [PATCH] custom gas adjustment --- v4-client-js/src/clients/modules/post.ts | 36 +++++++++++++++++------- v4-client-js/src/lib/constants.ts | 2 +- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/v4-client-js/src/clients/modules/post.ts b/v4-client-js/src/clients/modules/post.ts index 480dd9f7..79ca4889 100644 --- a/v4-client-js/src/clients/modules/post.ts +++ b/v4-client-js/src/clients/modules/post.ts @@ -58,7 +58,13 @@ export class Post { public useTimestampNonce: boolean = false; private accountNumberCache: Map = new Map(); - constructor(get: Get, chainId: string, denoms: DenomConfig, defaultClientMemo?: string, useTimestampNonce?: boolean) { + constructor( + get: Get, + chainId: string, + denoms: DenomConfig, + defaultClientMemo?: string, + useTimestampNonce?: boolean, + ) { this.get = get; this.chainId = chainId; this.registry = generateRegistry(); @@ -129,13 +135,7 @@ export class Post { sequence = msgsAndAccount[1].sequence; } - return this.simulateTransaction( - wallet.pubKey!, - sequence, - msgs, - gasPrice, - memo, - ); + return this.simulateTransaction(wallet.pubKey!, sequence, msgs, gasPrice, memo); } /** @@ -177,6 +177,7 @@ export class Post { memo?: string, broadcastMode?: BroadcastMode, account?: () => Promise, + gasAdjustment: number = GAS_MULTIPLIER, ): Promise { const msgsPromise = messaging(); const accountPromise = account ? await account() : this.account(wallet.address!); @@ -191,6 +192,7 @@ export class Post { gasPrice, memo ?? this.defaultClientMemo, broadcastMode ?? this.defaultBroadcastMode(msgs), + gasAdjustment, ); } @@ -235,6 +237,7 @@ export class Post { zeroFee: boolean, gasPrice: GasPrice = this.getGasPrice(), memo?: string, + gasAdjustment: number = GAS_MULTIPLIER, ): Promise { // protocol expects timestamp nonce in UTC milliseconds, which is the unit returned by Date.now() const sequence = this.useTimestampNonce ? Date.now() : account.sequence; @@ -244,7 +247,14 @@ export class Post { amount: [], gas: '1000000', } - : await this.simulateTransaction(wallet.pubKey!, sequence, messages, gasPrice, memo); + : await this.simulateTransaction( + wallet.pubKey!, + sequence, + messages, + gasPrice, + memo, + gasAdjustment, + ); const txOptions: TransactionOptions = { sequence, @@ -286,6 +296,7 @@ export class Post { gasPrice: GasPrice = this.getGasPrice(), memo?: string, broadcastMode?: BroadcastMode, + gasAdjustment: number = GAS_MULTIPLIER, ): Promise { const signedTransaction = await this.signTransaction( wallet, @@ -294,6 +305,7 @@ export class Post { zeroFee, gasPrice, memo, + gasAdjustment, ); return this.sendSignedTransaction(signedTransaction, broadcastMode); } @@ -326,6 +338,7 @@ export class Post { messages: readonly EncodeObject[], gasPrice: GasPrice = this.getGasPrice(), memo?: string, + gasAdjustment: number = GAS_MULTIPLIER, ): Promise { // Get simulated response. const encodedMessages: Any[] = messages.map((message: EncodeObject) => @@ -347,7 +360,7 @@ export class Post { const gasEstimate: number = Uint53.fromString( simulationResponse.gasInfo.gasUsed.toString(), ).toNumber(); - const fee = calculateFee(Math.floor(gasEstimate * GAS_MULTIPLIER), gasPrice); + const fee = calculateFee(Math.floor(gasEstimate * gasAdjustment), gasPrice); // TODO(TRCL-2550): Temporary workaround before IBC denom is supported in '@cosmjs/stargate'. // The '@cosmjs/stargate' does not support denom with '/', so currently GAS_PRICE is @@ -888,6 +901,7 @@ export class Post { broadcastMode?: BroadcastMode, ): Promise { const msg = this.registerAffiliateMsg(subaccount.address, affiliate); + const gasAdjustment = 1.8; return this.send( subaccount.wallet, () => Promise.resolve([msg]), @@ -895,6 +909,8 @@ export class Post { undefined, undefined, broadcastMode, + undefined, + gasAdjustment, ); } diff --git a/v4-client-js/src/lib/constants.ts b/v4-client-js/src/lib/constants.ts index 05108d25..612f2bb1 100644 --- a/v4-client-js/src/lib/constants.ts +++ b/v4-client-js/src/lib/constants.ts @@ -12,7 +12,7 @@ export const BROADCAST_TIMEOUT_MS: number = 8_000; export const API_TIMEOUT_DEFAULT_MS: number = 5_000; // Gas -export const GAS_MULTIPLIER: number = 1.8; +export const GAS_MULTIPLIER: number = 1.4; export const ZERO_FEE: StdFee = { amount: [],