Skip to content

Commit

Permalink
Add prefix when simulate by chain
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Dec 2, 2022
1 parent 11b25fa commit 1f11a14
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
32 changes: 21 additions & 11 deletions src/simulate/safe.simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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,
),
),
);

Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down
22 changes: 13 additions & 9 deletions src/simulate/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -38,6 +38,7 @@ export class SimulateUtils {
safeAddress: string,
safePubkey: any,
totalOwner: number,
denom: string,
): Promise<Uint8Array> {
const indexerClient = new IndexerClient();
let sequence = 0;
Expand All @@ -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 = {
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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(),
});
Expand Down
3 changes: 2 additions & 1 deletion src/simulate/wallet.simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,6 +74,7 @@ export class WalletSimulate {
messages,
safeInfo.safeAddress,
safePubkey,
this.chain.prefix,
);

// build txBytes
Expand Down

0 comments on commit 1f11a14

Please sign in to comment.