Skip to content

Commit

Permalink
fix: cosmos to cosmos e2e message sending script
Browse files Browse the repository at this point in the history
  • Loading branch information
hemz10 committed Dec 15, 2023
1 parent 46a89a7 commit b85b5dd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
33 changes: 17 additions & 16 deletions test/scripts/cosmos/cosmos_e2e_demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import {
getTestAccountWithStake,
} from "./helper";


if (process.argv.length < 3) {
console.error('Usage: ts-node cosmos_e2e_demo.ts <chainName, eg "archway"/"neutron">');
console.error(
'Usage: ts-node cosmos_e2e_demo.ts <chainName, eg "archway"/"neutron">'
);
process.exit(1);
}

const chainName = process.argv[2];


// configure dotenv
dotenv.config();

async function main() {
// Chain Constants, modify as required
let chain1;
let chain2;
if (chainName == "archway"){
if (chainName == "archway") {
chain1 = {
chainId: "archway-node-0",
endpoint: "http://localhost:4564",
prefix: "archway",
};

chain2 = {
chainId: "archway-node-1",
endpoint: "http://localhost:4566",
Expand All @@ -43,16 +43,14 @@ async function main() {
"Demo on Sending token transfer over IBC from archway to archway"
);
console.log("*".repeat(63));

} else{

chain1 = {
} else {
chain1 = {
chainId: "test-chain1",
endpoint: "http://localhost:26669",
prefix: "neutron",
};
chain2 = {

chain2 = {
chainId: "test-chain2",
endpoint: "http://localhost:26653",
prefix: "neutron",
Expand All @@ -65,9 +63,6 @@ async function main() {
console.log("*".repeat(63));
}




// *****************************************************************
// Setting up account on chain 1 by getting mnemonics from env file
const mnemonic1 = process.env.MNEMONIC1 as string;
Expand All @@ -78,13 +73,19 @@ async function main() {
chain1.prefix,
chain1.endpoint
);
console.log("Account on Chain A: ", accountAddress);

// To Check if the client is connected to local chain
getHeight(signingClient, chain1.chainId);

// Get Test Account with stake
const testAccount = await getTestAccountWithStake(chainName);
const testAddress = testAccount.substring(8, testAccount.length).trim();
let testAddress;
if (chainName == "archway") {
testAddress = testAccount.substring(8, testAccount.length).trim();
} else {
testAddress = testAccount;
}

// To Get balance of given account address and transfer balance if 0
const bal = await signingClient.getBalance(accountAddress, "stake");
Expand All @@ -97,7 +98,6 @@ async function main() {
await new Promise((f) => setTimeout(f, 5000));
await getBalance(signingClient, accountAddress);


// *****************************************************************
// Setting up account on chain 1 by getting mnemonics from env file
console.log("Chain 2: " + chain2.chainId);
Expand Down Expand Up @@ -141,6 +141,7 @@ async function main() {
),
},
};

const broadcastResult = await signingClient.signAndBroadcast(
accountAddress,
[msgIBCTransfer],
Expand Down
27 changes: 12 additions & 15 deletions test/scripts/cosmos/helper.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { GasPrice } from "@cosmjs/stargate";
import { GasPrice, SigningStargateClient } from "@cosmjs/stargate";
import { exec } from "child_process";
import fs from "fs";
import { Secp256k1HdWallet } from "@cosmjs/launchpad";

const defaultGasPrice = GasPrice.fromString("0stake");

export async function CreateSigningClient(
mnemonic: string,
prefix: string,
endpoint: string
): Promise<[SigningCosmWasmClient, string]> {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: prefix,
});
): Promise<[SigningStargateClient, string]> {

const wallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, { prefix: prefix });

// Print account address
// Get account address
const accounts = await wallet.getAccounts();
const accountAddress = accounts[0].address;

// Create an signing client with created wallet
const signingClient = await SigningCosmWasmClient.connectWithSigner(
const signingClient = await SigningStargateClient.connectWithSigner(
endpoint,
wallet,
{
Expand All @@ -31,7 +29,7 @@ export async function CreateSigningClient(
}

export async function getHeight(
client: SigningCosmWasmClient,
client: SigningStargateClient,
chainId: string
) {
// To Check if the client is connected to local chain
Expand All @@ -41,7 +39,6 @@ export async function getHeight(

export async function getStake(testaddress: string, destaddress: string, chainName: string) {
const dockerID = await getContainerIdByPartialName(chainName);
console.log(dockerID);

let command = "";

Expand All @@ -50,7 +47,7 @@ export async function getStake(testaddress: string, destaddress: string, chainNa
--chain-id constantine-3 -y`;
} else if (chainName == "neutron"){
command = `docker exec ${dockerID} neutrond tx bank send ${testaddress} ${destaddress} 9000000stake --keyring-backend test \
--chain-id test-chain1 -y`;
--chain-id test-chain1 --home ./data/test-chain1 -y`;
}

exec(command, (error, stdout, stderr) => {
Expand All @@ -71,7 +68,7 @@ export async function getTestAccountWithStake(chainName: string): Promise<string
if (chainName == "archway") {
command = `docker exec ${dockerID} archwayd keys list --keyring-backend test | grep 'address:'`;
} else if (chainName == "neutron"){
command = `docker exec ${dockerID} neutrond keys list --keyring-backend test --home ./data/test-chain1 | grep -A 4 "name: test-key" | awk '/address:/ {print $2}' | cut -d':' -f2`;
command = `docker exec ${dockerID} neutrond keys list --keyring-backend test --home ./data/test-chain1 | grep -A 4 "name: test-key" | awk '/address:/ {print $2}'`;
}

return new Promise<string>((resolve, reject) => {
Expand All @@ -93,7 +90,7 @@ export async function getContainerIdByPartialName(chainName: string): Promise<st
if (chainName == "archway") {
command = 'docker ps -aqf "name=constantine-3"';
} else if (chainName == "neutron"){
command = 'docker ps -aqf "name=neutron"';
command = 'docker ps -aqf "name=neutron-node-test-chain1"';
}

return new Promise<string>((resolve, reject) => {
Expand All @@ -110,7 +107,7 @@ export async function getContainerIdByPartialName(chainName: string): Promise<st
}

export async function getBalance(
client: SigningCosmWasmClient,
client: SigningStargateClient,
address: string
) {
const balance = await client.getBalance(address, "stake");
Expand Down

0 comments on commit b85b5dd

Please sign in to comment.