Skip to content

Commit

Permalink
custom gas adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
rosepuppy committed Sep 30, 2024
1 parent 2cfef2b commit 8c44143
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
36 changes: 26 additions & 10 deletions v4-client-js/src/clients/modules/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ export class Post {
public useTimestampNonce: boolean = false;
private accountNumberCache: Map<string, Account> = 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();
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -177,6 +177,7 @@ export class Post {
memo?: string,
broadcastMode?: BroadcastMode,
account?: () => Promise<Account>,
gasAdjustment: number = GAS_MULTIPLIER,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msgsPromise = messaging();
const accountPromise = account ? await account() : this.account(wallet.address!);
Expand All @@ -191,6 +192,7 @@ export class Post {
gasPrice,
memo ?? this.defaultClientMemo,
broadcastMode ?? this.defaultBroadcastMode(msgs),
gasAdjustment,
);
}

Expand Down Expand Up @@ -235,6 +237,7 @@ export class Post {
zeroFee: boolean,
gasPrice: GasPrice = this.getGasPrice(),
memo?: string,
gasAdjustment: number = GAS_MULTIPLIER,
): Promise<Uint8Array> {
// protocol expects timestamp nonce in UTC milliseconds, which is the unit returned by Date.now()
const sequence = this.useTimestampNonce ? Date.now() : account.sequence;
Expand All @@ -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,
Expand Down Expand Up @@ -286,6 +296,7 @@ export class Post {
gasPrice: GasPrice = this.getGasPrice(),
memo?: string,
broadcastMode?: BroadcastMode,
gasAdjustment: number = GAS_MULTIPLIER,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const signedTransaction = await this.signTransaction(
wallet,
Expand All @@ -294,6 +305,7 @@ export class Post {
zeroFee,
gasPrice,
memo,
gasAdjustment,
);
return this.sendSignedTransaction(signedTransaction, broadcastMode);
}
Expand Down Expand Up @@ -326,6 +338,7 @@ export class Post {
messages: readonly EncodeObject[],
gasPrice: GasPrice = this.getGasPrice(),
memo?: string,
gasAdjustment: number = GAS_MULTIPLIER,
): Promise<StdFee> {
// Get simulated response.
const encodedMessages: Any[] = messages.map((message: EncodeObject) =>
Expand All @@ -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
Expand Down Expand Up @@ -888,13 +901,16 @@ export class Post {
broadcastMode?: BroadcastMode,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msg = this.registerAffiliateMsg(subaccount.address, affiliate);
const gasAdjustment = 1.8;
return this.send(
subaccount.wallet,
() => Promise.resolve([msg]),
false,
undefined,
undefined,
broadcastMode,
undefined,
gasAdjustment,
);
}

Expand Down
2 changes: 1 addition & 1 deletion v4-client-js/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down

0 comments on commit 8c44143

Please sign in to comment.