Skip to content

Commit

Permalink
feat: ✨ reduce network request for signAndBroadcast
Browse files Browse the repository at this point in the history
reduced the number of network request for signAndBroadcast, now fetch account info only once instead of two.
  • Loading branch information
DavideSegullo committed Sep 6, 2024
1 parent e3c764e commit efd062d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 14 additions & 2 deletions packages/cosmwasm-stargate/src/signingcosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,27 @@ export class SigningCosmWasmClient extends CosmWasmClient {
timeoutHeight?: bigint,
): Promise<DeliverTxResponse> {
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);
}
Expand Down
16 changes: 14 additions & 2 deletions packages/stargate/src/signingstargateclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,27 @@ export class SigningStargateClient extends StargateClient {
timeoutHeight?: bigint,
): Promise<DeliverTxResponse> {
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);
}
Expand Down

0 comments on commit efd062d

Please sign in to comment.