Skip to content

Commit

Permalink
extrapolate logic to function to reduce redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntVal committed Aug 2, 2024
1 parent 7938d31 commit a846310
Showing 1 changed file with 42 additions and 49 deletions.
91 changes: 42 additions & 49 deletions packages/signers/src/signers/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<StdFee>} - The calculated default fee for the transaction.
*/
public async addAbstractAccountAuthenticator(
msg: AddAuthenticator,
memo = "",
fee?: StdFee,
): Promise<DeliverTxResponse> {
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<StdFee> {
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,
Expand All @@ -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<DeliverTxResponse> {
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());
}
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit a846310

Please sign in to comment.