From efd062d00a2cd662be7ae2d87f0830fa9eaded71 Mon Sep 17 00:00:00 2001 From: Davide Segullo Date: Fri, 6 Sep 2024 15:51:10 +0200 Subject: [PATCH] feat: :sparkles: reduce network request for signAndBroadcast reduced the number of network request for signAndBroadcast, now fetch account info only once instead of two. --- .../src/signingcosmwasmclient.ts | 16 ++++++++++++++-- packages/stargate/src/signingstargateclient.ts | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index dfa192d183..9736cc53d0 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -623,15 +623,27 @@ export class SigningCosmWasmClient extends CosmWasmClient { timeoutHeight?: bigint, ): Promise { let usedFee: StdFee; + + const { accountNumber, sequence } = await this.getSequence(signerAddress); + if (fee == "auto" || typeof fee === "number") { assertDefined(this.gasPrice, "Gas price must be set in the client options when auto gas is used."); - const gasEstimation = await this.simulate(signerAddress, messages, memo); + const gasEstimation = await this.simulate(signerAddress, messages, memo, { sequence }); const multiplier = typeof fee === "number" ? fee : this.defaultGasMultiplier; usedFee = calculateFee(Math.round(gasEstimation * multiplier), this.gasPrice); } else { usedFee = fee; } - const txRaw = await this.sign(signerAddress, messages, usedFee, memo, undefined, timeoutHeight); + + const chainId = await this.getChainId(); + + const signerData: SignerData = { + accountNumber: accountNumber, + sequence: sequence, + chainId: chainId, + }; + + const txRaw = await this.sign(signerAddress, messages, usedFee, memo, signerData, timeoutHeight); const txBytes = TxRaw.encode(txRaw).finish(); return this.broadcastTx(txBytes, this.broadcastTimeoutMs, this.broadcastPollIntervalMs); } diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index 8612f45e93..7bf42195a6 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -317,15 +317,27 @@ export class SigningStargateClient extends StargateClient { timeoutHeight?: bigint, ): Promise { let usedFee: StdFee; + + const { accountNumber, sequence } = await this.getSequence(signerAddress); + if (fee == "auto" || typeof fee === "number") { assertDefined(this.gasPrice, "Gas price must be set in the client options when auto gas is used."); - const gasEstimation = await this.simulate(signerAddress, messages, memo); + const gasEstimation = await this.simulate(signerAddress, messages, memo, { sequence }); const multiplier = typeof fee === "number" ? fee : this.defaultGasMultiplier; usedFee = calculateFee(Math.round(gasEstimation * multiplier), this.gasPrice); } else { usedFee = fee; } - const txRaw = await this.sign(signerAddress, messages, usedFee, memo, undefined, timeoutHeight); + + const chainId = await this.getChainId(); + + const signerData: SignerData = { + accountNumber: accountNumber, + sequence: sequence, + chainId: chainId, + }; + + const txRaw = await this.sign(signerAddress, messages, usedFee, memo, signerData, timeoutHeight); const txBytes = TxRaw.encode(txRaw).finish(); return this.broadcastTx(txBytes, this.broadcastTimeoutMs, this.broadcastPollIntervalMs); }