From a84631024cf78341c022a182c8e3cb564a9b206c Mon Sep 17 00:00:00 2001 From: Burnt Val Date: Fri, 2 Aug 2024 14:48:14 -0400 Subject: [PATCH] extrapolate logic to function to reduce redundancy --- packages/signers/src/signers/utils/client.ts | 91 +++++++++----------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/packages/signers/src/signers/utils/client.ts b/packages/signers/src/signers/utils/client.ts index 5f755670..411f125b 100644 --- a/packages/signers/src/signers/utils/client.ts +++ b/packages/signers/src/signers/utils/client.ts @@ -100,36 +100,25 @@ export class AAClient extends SigningCosmWasmClient { } /** - * Create and a cosmwasm add authenticator msg to the abstract account - * @param msg the message to be sent - * @returns + * Simulates a transaction to estimate the gas and calculates the default fee. + * + * @param {string} sender - The address of the sender. + * @param {readonly EncodeObject[]} messages - An array of messages to include in the transaction. + * @param {string | undefined} memo - An optional memo to include in the transaction. + * @returns {Promise} - The calculated default fee for the transaction. */ - public async addAbstractAccountAuthenticator( - msg: AddAuthenticator, - memo = "", - fee?: StdFee, - ): Promise { - if (!this.abstractSigner.abstractAccount) { - throw new Error("Abstract account address not set in signer"); - } - const sender = this.abstractSigner.abstractAccount; - const addMsg: MsgExecuteContractEncodeObject = { - typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", - value: MsgExecuteContract.fromPartial({ - sender, - contract: sender, - msg: Buffer.from(JSON.stringify(msg), "utf-8"), - funds: [], - }), - }; - + private async simulateDefaultFee( + sender: string, + messages: readonly EncodeObject[], + memo: string | undefined, + ): Promise { const { gasPrice: gasPriceString, gasAdjustment, gasAdjustmentMargin, } = xionGasValues; - const simmedGas = await this.simulate(sender, [addMsg], memo); + const simmedGas = await this.simulate(sender, messages, memo); const gasPrice = GasPrice.fromString(gasPriceString); const calculatedFee: StdFee = calculateFee( simmedGas * gasAdjustment, @@ -150,6 +139,35 @@ export class AAClient extends SigningCosmWasmClient { defaultFee = { amount: calculatedFee.amount, gas: gas }; } + return defaultFee; + } + + /** + * Create and a cosmwasm add authenticator msg to the abstract account + * @param msg the message to be sent + * @returns + */ + public async addAbstractAccountAuthenticator( + msg: AddAuthenticator, + memo = "", + fee?: StdFee, + ): Promise { + if (!this.abstractSigner.abstractAccount) { + throw new Error("Abstract account address not set in signer"); + } + const sender = this.abstractSigner.abstractAccount; + const addMsg: MsgExecuteContractEncodeObject = { + typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", + value: MsgExecuteContract.fromPartial({ + sender, + contract: sender, + msg: Buffer.from(JSON.stringify(msg), "utf-8"), + funds: [], + }), + }; + + const defaultFee = await this.simulateDefaultFee(sender, [addMsg], memo); + const tx = await this.sign(sender, [addMsg], fee || defaultFee, memo); return this.broadcastTx(TxRaw.encode(tx).finish()); } @@ -178,32 +196,7 @@ export class AAClient extends SigningCosmWasmClient { }), }; - const { - gasPrice: gasPriceString, - gasAdjustment, - gasAdjustmentMargin, - } = xionGasValues; - - const simmedGas = await this.simulate(sender, [addMsg], memo); - const gasPrice = GasPrice.fromString(gasPriceString); - const calculatedFee: StdFee = calculateFee( - simmedGas * gasAdjustment, - gasPrice, - ); - - let defaultFee: StdFee; - let gas = ( - parseInt(calculatedFee.gas) * gasAdjustment + - gasAdjustmentMargin - ).toString(); - - const chainId = await this.getChainId(); - - if (/testnet/.test(chainId)) { - defaultFee = { amount: [{ amount: "0", denom: "uxion" }], gas: gas }; - } else { - defaultFee = { amount: calculatedFee.amount, gas: gas }; - } + const defaultFee = await this.simulateDefaultFee(sender, [addMsg], memo); const tx = await this.sign(sender, [addMsg], fee || defaultFee, memo); return this.broadcastTx(TxRaw.encode(tx).finish());