Skip to content

Commit c83b17d

Browse files
committed
Merge branch 'main' into feat/disable-feerefunder-fee
2 parents f4ec49c + 6aa65be commit c83b17d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+938
-722
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ START_BLOCK_HEIGHT - how many blocks we wait before start first test
105105
NO_DOCKER - do not start cosmopark for tests
106106
NO_PRINT_VERSIONS - do not print contract versions in console
107107
DEBUG_SUBMIT_TX - log submitted txs to stdout
108+
WALLETS_SIGN_METHOD - use 'secp256k1' to sign messages using direct sign, 'eip191' to sign using ethereum like signature,
109+
'random' to pick method randomly each time you request for a new wallet
108110
```
109111

110112
## Config

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@
5151
"@cosmjs/cosmwasm-stargate": "^0.32.4",
5252
"@cosmjs/stargate": "0.32.4",
5353
"@cosmjs/tendermint-rpc": "^0.32.4",
54-
"@neutron-org/neutronjs": "https://github.com/neutron-org/neutronjs.git#b33eea8fbae746f2eb238fa1fb54c878d8d977c3",
55-
"@neutron-org/neutronjsplus": "https://github.com/neutron-org/neutronjsplus.git#8e5baa16dcaa0da2f9a4c9125b3b0ddde44634a4",
54+
"@neutron-org/neutronjs": "https://github.com/neutron-org/neutronjs.git#b9cbc1f31621584219f46a49783704c51e2f2abc",
55+
"@neutron-org/neutronjsplus": "https://github.com/neutron-org/neutronjsplus.git#74d630354411fd7d838642d51bd3bdfe7f0e0298",
5656
"@types/lodash": "^4.14.182",
5757
"axios": "1.6.0",
5858
"commander": "^10.0.0",
5959
"cosmjs-types": "^0.9.0",
6060
"date-fns": "^2.16.1",
61+
"ethers": "^6.13.5",
6162
"express": "^4.18.2",
6263
"jest-extended": "^4.0.2",
6364
"lodash": "^4.17.21",
@@ -96,4 +97,4 @@
9697
"engines": {
9798
"node": ">=20.0"
9899
}
99-
}
100+
}

setup/scripts/2_init_genesis.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ DEMO_MNEMONIC_3="obscure canal because tomorrow tribe sibling describe satoshi k
2020
RLY_MNEMONIC_1="alley afraid soup fall idea toss can goose become valve initial strong forward bright dish figure check leopard decide warfare hub unusual join cart"
2121
RLY_MNEMONIC_2="record gift you once hip style during joke field prize dust unique length more pencil transfer quit train device arrive energy sort steak upset"
2222

23+
ETH_DEMO_1_ADDRESS="neutron165cyjk6ujhjy3cyxkj2wdqw3fj3k69kqkaqnrm"
24+
ETH_DEMO_2_ADDRESS="neutron1lx5vlcwz78zp4g24qne4mrsutvkkh5ffj674q5"
25+
ETH_DEMO_3_ADDRESS="neutron1m2arw2gnr5n3n0g2yg40y6qzj0lclw9jxuth9e"
2326

2427
MASTER_CHAIN_DIR="${CHAIN_DIR}/node-1"
2528

@@ -33,12 +36,15 @@ echo "$DEMO_MNEMONIC_2" | $BINARY keys add demowallet2 --home "$MASTER_CHAIN_DIR
3336
echo "$DEMO_MNEMONIC_3" | $BINARY keys add demowallet3 --home "$MASTER_CHAIN_DIR" --recover --keyring-backend=test
3437
echo "$RLY_MNEMONIC_1" | $BINARY keys add rly1 --home "$MASTER_CHAIN_DIR" --recover --keyring-backend=test
3538
echo "$RLY_MNEMONIC_2" | $BINARY keys add rly2 --home "$MASTER_CHAIN_DIR" --recover --keyring-backend=test
36-
#$BINARY $GENESIS_PREFIX add-genesis-account "$($BINARY --home "$MASTER_CHAIN_DIR" keys show val1 --keyring-backend test -a --home "$MASTER_CHAIN_DIR")" "100000000000000$STAKEDENOM" --home "$MASTER_CHAIN_DIR"
3739
$BINARY $GENESIS_PREFIX add-genesis-account "$($BINARY --home "$MASTER_CHAIN_DIR" keys show val1 --keyring-backend test -a --home "$MASTER_CHAIN_DIR")" "100000000000000$STAKEDENOM" --home "$MASTER_CHAIN_DIR"
3840
$BINARY $GENESIS_PREFIX add-genesis-account "$($BINARY --home "$MASTER_CHAIN_DIR" keys show val2 --keyring-backend test -a --home "$MASTER_CHAIN_DIR")" "100000000000000$STAKEDENOM" --home "$MASTER_CHAIN_DIR"
3941
$BINARY $GENESIS_PREFIX add-genesis-account "$($BINARY --home "$MASTER_CHAIN_DIR" keys show demowallet1 --keyring-backend test -a --home "$MASTER_CHAIN_DIR")" "100000000000000$STAKEDENOM,100000000000000$IBCATOMDENOM,100000000000000$IBCUSDCDENOM" --home "$MASTER_CHAIN_DIR"
4042
$BINARY $GENESIS_PREFIX add-genesis-account "$($BINARY --home "$MASTER_CHAIN_DIR" keys show demowallet2 --keyring-backend test -a --home "$MASTER_CHAIN_DIR")" "100000000000000$STAKEDENOM,100000000000000$IBCATOMDENOM,100000000000000$IBCUSDCDENOM" --home "$MASTER_CHAIN_DIR"
4143
$BINARY $GENESIS_PREFIX add-genesis-account "$($BINARY --home "$MASTER_CHAIN_DIR" keys show demowallet3 --keyring-backend test -a --home "$MASTER_CHAIN_DIR")" "100000000000000$STAKEDENOM,100000000000000$IBCATOMDENOM,100000000000000$IBCUSDCDENOM" --home "$MASTER_CHAIN_DIR"
44+
# eth like demo1, demo2, demo3 accounts
45+
$BINARY $GENESIS_PREFIX add-genesis-account $ETH_DEMO_1_ADDRESS "100000000000000$STAKEDENOM,100000000000000$IBCATOMDENOM,100000000000000$IBCUSDCDENOM" --home "$MASTER_CHAIN_DIR"
46+
$BINARY $GENESIS_PREFIX add-genesis-account $ETH_DEMO_2_ADDRESS "100000000000000$STAKEDENOM,100000000000000$IBCATOMDENOM,100000000000000$IBCUSDCDENOM" --home "$MASTER_CHAIN_DIR"
47+
$BINARY $GENESIS_PREFIX add-genesis-account $ETH_DEMO_3_ADDRESS "100000000000000$STAKEDENOM,100000000000000$IBCATOMDENOM,100000000000000$IBCUSDCDENOM" --home "$MASTER_CHAIN_DIR"
4248
$BINARY $GENESIS_PREFIX add-genesis-account "$($BINARY --home "$MASTER_CHAIN_DIR" keys show rly1 --keyring-backend test -a --home "$MASTER_CHAIN_DIR")" "100000000000000$STAKEDENOM" --home "$MASTER_CHAIN_DIR"
4349
$BINARY $GENESIS_PREFIX add-genesis-account "$($BINARY --home "$MASTER_CHAIN_DIR" keys show rly2 --keyring-backend test -a --home "$MASTER_CHAIN_DIR")" "100000000000000$STAKEDENOM" --home "$MASTER_CHAIN_DIR"
4450
$BINARY gentx val1 "100000000$STAKEDENOM" --home "$MASTER_CHAIN_DIR" --chain-id "$CHAINID" --keyring-backend test

setup/scripts/4_update-config.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ for i in `seq 1 ${NODES}`; do
4040
sed -i -e 's/enabled-unsafe-cors = false/enabled-unsafe-cors = true/g' "${CHAIN_DIR}/node-${i}/config/app.toml"
4141
sed -i -e 's/persistent_peers = ""/persistent_peers = "'"$NODE1@$PERSISTENT_PEER:$P2PPORT"'"/g' "${CHAIN_DIR}/node-${i}/config/config.toml"
4242
sed -i -e 's/allow_duplicate_ip = false/allow_duplicate_ip = true/g' "${CHAIN_DIR}/node-${i}/config/config.toml"
43+
sed -i -e 's#cors_allowed_origins = \[\]#cors_allowed_origins = ["*"]#g' "${CHAIN_DIR}/node-${i}/config/config.toml"
4344
done;

src/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"VAL_MNEMONIC_1": "clock post desk civil pottery foster expand merit dash seminar song memory figure uniform spice circle try happy obvious trash crime hybrid hood cushion",
3+
"VAL_MNEMONIC_2": "angry twist harsh drastic left brass behave host shove marriage fall update business leg direct reward object ugly security warm tuna model broccoli choice",
34
"DEMO_MNEMONIC_1": "banner spread envelope side kite person disagree path silver will brother under couch edit food venture squirrel civil budget number acquire point work mass",
45
"DEMO_MNEMONIC_2": "veteran try aware erosion drink dance decade comic dawn museum release episode original list ability owner size tuition surface ceiling depth seminar capable only",
56
"DEMO_MNEMONIC_3": "obscure canal because tomorrow tribe sibling describe satoshi kiwi upgrade bless empty math trend erosion oblige donate label birth chronic hazard ensure wreck shine",

src/global_setup.ts

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defaultRegistryTypes, SigningStargateClient } from '@cosmjs/stargate';
22
import { DirectSecp256k1HdWallet, Registry } from '@cosmjs/proto-signing';
33
import { generateMnemonic } from 'bip39';
44
import { setup } from './helpers/setup';
5+
import { pathToString } from '@cosmjs/crypto';
56
import { MsgMultiSend } from '@neutron-org/neutronjs/cosmos/bank/v1beta1/tx';
67
import { GlobalSetupContext } from 'vitest/node';
78
import { Input, Output } from '@neutron-org/neutronjs/cosmos/bank/v1beta1/bank';
@@ -13,13 +14,18 @@ import {
1314
NEUTRON_DENOM,
1415
NEUTRON_PREFIX,
1516
NEUTRON_RPC,
17+
IBC_ATOM_DENOM,
18+
IBC_USDC_DENOM,
1619
} from './helpers/constants';
1720

1821
import config from './config.json';
22+
import { ethers } from 'ethers';
23+
import { ACC_PATH, ethToNeutronBechAddress } from './helpers/metamask_emulator';
24+
import { stringToPath } from '@cosmjs/crypto/build/slip10';
1925

2026
let teardownHappened = false;
2127

22-
const WALLET_COUNT = 1000;
28+
const MNEMONICS_COUNT = 1000;
2329

2430
export default async function ({ provide }: GlobalSetupContext) {
2531
const host1 = process.env.NODE1_URL || 'http://localhost:1317';
@@ -28,15 +34,31 @@ export default async function ({ provide }: GlobalSetupContext) {
2834
await setup(host1, host2);
2935
}
3036

37+
// generate lots of mnemonics for test wallets
3138
const mnemonics: string[] = [];
32-
for (let i = 0; i < WALLET_COUNT; i++) {
39+
for (let i = 0; i < MNEMONICS_COUNT; i++) {
3340
mnemonics.push(generateMnemonic());
3441
}
3542

36-
// fund a lot or preallocated wallets for testing purposes
37-
await fundWallets(mnemonics, NEUTRON_RPC, NEUTRON_PREFIX, NEUTRON_DENOM);
38-
await fundWallets(mnemonics, GAIA_RPC, COSMOS_PREFIX, COSMOS_DENOM);
43+
const denomsToFund = [NEUTRON_DENOM, IBC_ATOM_DENOM, IBC_USDC_DENOM];
44+
for (let i = 0; i < denomsToFund.length; i++) {
45+
await fundWallets(
46+
mnemonics,
47+
NEUTRON_RPC,
48+
NEUTRON_PREFIX,
49+
NEUTRON_DENOM,
50+
denomsToFund[i],
51+
);
52+
}
53+
await fundWallets(
54+
mnemonics,
55+
GAIA_RPC,
56+
COSMOS_PREFIX,
57+
COSMOS_DENOM,
58+
COSMOS_DENOM,
59+
);
3960

61+
// make mnemonics fetchable in test
4062
provide('mnemonics', mnemonics);
4163

4264
return async () => {
@@ -52,25 +74,29 @@ export default async function ({ provide }: GlobalSetupContext) {
5274
};
5375
}
5476

55-
// Funds a lots of new wallets from one wallet.
77+
// Funds lots of new wallets from one wallet.
5678
async function fundWallets(
5779
mnemonics: string[],
5880
rpc: string,
5981
prefix: string,
82+
feeDenom: string,
6083
denom: string,
6184
): Promise<void> {
62-
const directwallet = await DirectSecp256k1HdWallet.fromMnemonic(
85+
const richguyWallet = await DirectSecp256k1HdWallet.fromMnemonic(
6386
config.DEMO_MNEMONIC_1,
6487
{ prefix: prefix },
6588
);
66-
const client = await SigningStargateClient.connectWithSigner(
89+
const richguy = await SigningStargateClient.connectWithSigner(
6790
rpc,
68-
directwallet,
91+
richguyWallet,
6992
{ registry: new Registry(defaultRegistryTypes) },
7093
);
7194

72-
const richguy = (await directwallet.getAccounts())[0].address;
73-
const pooramount = '10000000000';
95+
const richguyAddress = (await richguyWallet.getAccounts())[0].address;
96+
// amount to be transferred to each new wallet
97+
const poorAmount = '10000000000';
98+
99+
let outputs: Output[];
74100
const values: Promise<Output>[] = mnemonics.map((mnemonic) =>
75101
DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
76102
prefix: prefix,
@@ -79,32 +105,54 @@ async function fundWallets(
79105
.then((accounts) => accounts[0])
80106
.then((account) => ({
81107
address: account.address,
82-
coins: [{ denom: denom, amount: pooramount }],
108+
coins: [{ denom: denom, amount: poorAmount }],
83109
})),
84110
);
85-
const outputs: Output[] = await Promise.all(values);
111+
outputs = await Promise.all(values);
112+
113+
if (prefix === NEUTRON_PREFIX) {
114+
// fund both addresses derived from ethereum and cosmos-sdk for a given mnemonic.
115+
// this will allow us to use either one in any test.
116+
const cosmosHdPath = stringToPath(ACC_PATH);
117+
const ethDerivedOutputs = mnemonics.map((mnemonic) => {
118+
const hdNode = ethers.HDNodeWallet.fromMnemonic(
119+
ethers.Mnemonic.fromPhrase(mnemonic),
120+
pathToString(cosmosHdPath),
121+
);
122+
const wallet = new ethers.Wallet(hdNode.privateKey);
123+
const neutronAddress = ethToNeutronBechAddress(wallet.address);
124+
return {
125+
address: neutronAddress,
126+
coins: [{ denom: denom, amount: poorAmount }],
127+
};
128+
});
129+
outputs = outputs.concat(ethDerivedOutputs);
130+
}
131+
const amount =
132+
prefix === NEUTRON_PREFIX
133+
? +poorAmount * MNEMONICS_COUNT * 2
134+
: +poorAmount * MNEMONICS_COUNT;
135+
86136
const inputs: Input[] = [
87137
{
88-
address: richguy,
89-
coins: [
90-
{ denom: denom, amount: (+pooramount * WALLET_COUNT).toString() },
91-
],
138+
address: richguyAddress,
139+
coins: [{ denom: denom, amount: amount.toString() }],
92140
},
93141
];
94142
const value: MsgMultiSend = {
95-
inputs: inputs,
96-
outputs: outputs,
143+
inputs,
144+
outputs,
97145
};
98146
const msg: any = {
99147
typeUrl: MsgMultiSend.typeUrl,
100148
value: value,
101149
};
102150
const fee = {
103-
gas: '30000000',
104-
amount: [{ denom: denom, amount: '75000' }],
151+
gas: '50000000',
152+
amount: [{ denom: feeDenom, amount: '125000' }],
105153
};
106-
const result = await client.signAndBroadcast(richguy, [msg], fee, '');
107-
const resultTx = await client.getTx(result.transactionHash);
154+
const result = await richguy.signAndBroadcast(richguyAddress, [msg], fee, '');
155+
const resultTx = await richguy.getTx(result.transactionHash);
108156
if (resultTx.code !== 0) {
109157
throw (
110158
'could not setup test wallets; rawLog = ' +

src/helpers/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const NEUTRON_REST = process.env.NODE1_URL || 'http://localhost:1317';
8585
export const GAIA_REST = process.env.NODE2_URL || 'http://localhost:1316';
8686
export const IBC_WEB_HOST = process.env.ICQ_WEB_HOST || 'http://localhost:9999';
8787
export const GAIA_CONNECTION = 'connection-0';
88+
export const WALLETS_SIGN_METHOD = process.env.WALLETS_SIGN_METHOD || 'random';
8889

8990
export const STAKING_VAULT =
9091
'neutron1jarq7kgdyd7dcfu2ezeqvg4w4hqdt3m5lv364d8mztnp9pzmwwwqjw7fvg';

src/helpers/dao.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import { DaoContracts } from '@neutron-org/neutronjsplus/dist/dao_types';
1010
import { NEUTRON_DENOM, CONTRACTS } from './constants';
1111
import { waitBlocks } from '@neutron-org/neutronjsplus/dist/wait';
1212
import { addSubdaoProposal } from '@neutron-org/neutronjsplus/dist/proposal';
13-
import { SigningNeutronClient } from './signing_neutron_client';
13+
import { NeutronTestClient } from './neutron_test_client';
1414

1515
export const deploySubdao = async (
16-
client: SigningNeutronClient,
16+
client: NeutronTestClient,
1717
mainDaoCoreAddress: string,
1818
overrulePreProposeAddress: string,
1919
securityDaoAddr: string,
@@ -152,7 +152,7 @@ export const deploySubdao = async (
152152

153153
export const setupSubDaoTimelockSet = async (
154154
user: string,
155-
client: SigningNeutronClient,
155+
client: NeutronTestClient,
156156
mainDaoAddress: string,
157157
securityDaoAddr: string,
158158
mockMainDao: boolean,
@@ -180,7 +180,7 @@ export const setupSubDaoTimelockSet = async (
180180

181181
export const deployNeutronDao = async (
182182
user: string,
183-
client: SigningNeutronClient,
183+
client: NeutronTestClient,
184184
): Promise<DaoContracts> => {
185185
const coreCodeId = await client.upload(CONTRACTS.DAO_CORE);
186186
const proposeSingleCodeId = await client.upload(

src/helpers/fake_eip191_signer.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { AccountData } from '@cosmjs/proto-signing';
2+
import { MetaMaskEmulator } from './metamask_emulator';
3+
import { serializeSignDoc, StdSignDoc } from '@cosmjs/amino/build/signdoc';
4+
import { Buffer } from 'buffer';
5+
import { Eip191Signer } from '@neutron-org/neutronjsplus/dist/eip191_signer';
6+
7+
/**
8+
* Implementation of Eip191Signer that uses MetaMaskEmulator (ether.js inside) for signing
9+
*/
10+
export class FakeMetaMaskEip191Signer implements Eip191Signer {
11+
constructor(private readonly metamaskEmulator: MetaMaskEmulator) {}
12+
13+
// Get accounts from the MetaMask emulator.
14+
async getAccounts(): Promise<readonly AccountData[]> {
15+
// Use the new method to get both addresses and public keys
16+
return this.metamaskEmulator.getAccountsWithPubkeys();
17+
}
18+
19+
// Sign a document using EIP-191 format.
20+
// This implementation uses the personal_sign method from MetaMask.
21+
async signEip191(
22+
signerAddress: string,
23+
signDoc: StdSignDoc,
24+
): Promise<{ signature: { signature: Buffer }; signed: any }> {
25+
const messageToSign = serializeSignDoc(signDoc);
26+
27+
// Sign the message using the MetaMask emulator
28+
const signature = await this.metamaskEmulator.personalSign(
29+
messageToSign,
30+
signerAddress,
31+
);
32+
const signatureHex = Buffer.from(signature.replace('0x', ''), 'hex');
33+
34+
// Return the signature and the original document
35+
return {
36+
signature: {
37+
signature: signatureHex,
38+
},
39+
signed: signDoc,
40+
};
41+
}
42+
}

0 commit comments

Comments
 (0)