diff --git a/src/simulate/safe.simulate.ts b/src/simulate/safe.simulate.ts index fd69aaa7..34e3ae0a 100644 --- a/src/simulate/safe.simulate.ts +++ b/src/simulate/safe.simulate.ts @@ -12,6 +12,7 @@ import { fromBase64, toBase64 } from '@cosmjs/encoding'; import { SimulateUtils } from './utils'; import { IndexerClient } from 'src/utils/apis/IndexerClient'; import { TX_TYPE_URL } from 'src/common/constants/app.constant'; +import { Chain } from 'src/entities'; export class SafeSimulate { signature: string; @@ -22,16 +23,14 @@ export class SafeSimulate { constructor( public ownerSimulates: OwnerSimulate[], public threshold: number, - public prefix: string, - public chainId: string = 'aura-testnet-2', - public tendermintUrl: string = 'https://rpc.dev.aura.network/', + public chain: Chain, ) { // create pubkey and address this.pubkey = createMultisigThresholdPubkey( ownerSimulates.map((owner) => owner.pubkey), threshold, ); - this.address = pubkeyToAddress(this.pubkey, this.prefix); + this.address = pubkeyToAddress(this.pubkey, this.chain.prefix); } /** @@ -43,21 +42,27 @@ export class SafeSimulate { if (this.signature) return; // create simple msgs, fee - const msgs = SimulateUtils.getDefaultMsgs(this.address); - const fee = SimulateUtils.getDefaultFee(); + const msgs = SimulateUtils.getDefaultMsgs(this.address, this.chain.denom); + const fee = SimulateUtils.getDefaultFee(this.chain.denom); // get account number and sequence const indexerClient = new IndexerClient(); const { accountNumber, sequence } = await indexerClient.getAccountNumberAndSequence( - this.chainId, + this.chain.chainId, this.address, ); // sign with all owners const result = await Promise.all( this.ownerSimulates.map(async (ownerSimulate) => - ownerSimulate.sign(msgs, fee, accountNumber, sequence, this.chainId), + ownerSimulate.sign( + msgs, + fee, + accountNumber, + sequence, + this.chain.chainId, + ), ), ); @@ -86,11 +91,12 @@ export class SafeSimulate { messages: any[], safeAddress: string, safePubkey: any, + prefix: string, ) { let simulateAuthInfo; // get simulate msgs base typeUrl and the messages given by user - const encodeMsgs = SimulateUtils.anyToEncodeMsgs(messages); + const encodeMsgs = SimulateUtils.anyToEncodeMsgs(messages, prefix); encodeMsgs.map((msg) => { switch (msg.typeUrl) { case TX_TYPE_URL.SEND: @@ -110,12 +116,16 @@ export class SafeSimulate { const authInfoBytes = simulateAuthInfo ? fromBase64(simulateAuthInfo) : await SimulateUtils.makeAuthInfoBytes( - this.chainId, + this.chain.chainId, safeAddress, safePubkey, this.threshold, + this.chain.denom, ); - const bodyBytes = SimulateUtils.makeBodyBytes(encodeMsgs); + const bodyBytes = SimulateUtils.makeBodyBytes( + encodeMsgs, + this.chain.prefix, + ); return { authInfoBytes, bodyBytes, diff --git a/src/simulate/utils.ts b/src/simulate/utils.ts index 7a12895f..f2807b22 100644 --- a/src/simulate/utils.ts +++ b/src/simulate/utils.ts @@ -20,9 +20,9 @@ import { coins } from '@cosmjs/amino'; import { IndexerClient } from 'src/utils/apis/IndexerClient'; export class SimulateUtils { - public static makeBodyBytes(messages: any[]): Uint8Array { + public static makeBodyBytes(messages: any[], prefix: string): Uint8Array { const signedTxBody = { - messages: this.anyToEncodeMsgs(messages), + messages: this.anyToEncodeMsgs(messages, prefix), memo: '', }; const signedTxBodyEncodeObject: TxBodyEncodeObject = { @@ -38,6 +38,7 @@ export class SimulateUtils { safeAddress: string, safePubkey: any, totalOwner: number, + denom: string, ): Promise { const indexerClient = new IndexerClient(); let sequence = 0; @@ -49,7 +50,7 @@ export class SimulateUtils { console.log(error); } - const defaultFee = SimulateUtils.getDefaultFee(); + const defaultFee = SimulateUtils.getDefaultFee(denom); const signers: boolean[] = Array(totalOwner).fill(false); const signerInfo: SignerInfo = { @@ -88,11 +89,14 @@ export class SimulateUtils { return Uint8Array.from(TxRaw.encode(newTxRaw).finish()); } - public static getDefaultMsgs(safeAddress: string): MsgSendEncodeObject[] { + public static getDefaultMsgs( + safeAddress: string, + denom: string, + ): MsgSendEncodeObject[] { const msgSend: MsgSend = { fromAddress: safeAddress, - toAddress: 'aura1522aavcagyrahayuspe47ndje7s694dkzcup6x', - amount: coins(1, 'utaura'), + toAddress: safeAddress, + amount: coins(1, denom), }; const msg: MsgSendEncodeObject = { typeUrl: '/cosmos.bank.v1beta1.MsgSend', @@ -101,7 +105,7 @@ export class SimulateUtils { return [msg]; } - static getDefaultFee(denom = 'utaura'): StdFee { + static getDefaultFee(denom): StdFee { return { amount: coins(1, denom), gas: '200000', @@ -126,10 +130,10 @@ export class SimulateUtils { }, ] */ - static anyToEncodeMsgs(messages: any[]): EncodeObject[] { + static anyToEncodeMsgs(messages: any[], prefix: string): EncodeObject[] { const aminoTypes = new AminoTypes({ ...createBankAminoConverters(), - ...createStakingAminoConverters('aura'), + ...createStakingAminoConverters(prefix), ...createDistributionAminoConverters(), ...createGovAminoConverters(), }); diff --git a/src/simulate/wallet.simulate.ts b/src/simulate/wallet.simulate.ts index 5fe3822a..9118c4cb 100644 --- a/src/simulate/wallet.simulate.ts +++ b/src/simulate/wallet.simulate.ts @@ -42,7 +42,7 @@ export class WalletSimulate { const safe = new SafeSimulate( this.ownerWallets.slice(0, i), i, - this.chain.prefix, + this.chain, ); // save to map @@ -74,6 +74,7 @@ export class WalletSimulate { messages, safeInfo.safeAddress, safePubkey, + this.chain.prefix, ); // build txBytes