Skip to content

Instantly share code, notes, and snippets.

@luchenqun
Created November 29, 2022 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luchenqun/13a2d30c306826d21f90001a795f97bb to your computer and use it in GitHub Desktop.
Save luchenqun/13a2d30c306826d21f90001a795f97bb to your computer and use it in GitHub Desktop.
import util from "util";
import path from "path";
import { exec } from "child_process";
import Web3 from "web3";
const execPromis = util.promisify(exec);
const sleep = (time) => {
return new Promise((resolve) => setTimeout(resolve, time));
};
const toWei = (amount) => {
return Web3.utils.toWei(String(amount));
};
const getRandomArrayElements = (arr, count) => {
var shuffled = arr.slice(0),
i = arr.length,
min = i - count,
temp,
index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return count > 1 ? shuffled.slice(min) : shuffled.slice(min)[0];
};
(async () => {
const nodesDir = path.join(process.cwd(), "../nodes");
const out = await execPromis(`./rly q bal ibc-0 --home ./relayer`, { cwd: nodesDir });
console.log(out.stdout);
let loading = false;
const ibcTransfer = async (_ibcSrc, _ibcDst, _denom, _amount) => {
while (loading) {
await sleep(100);
}
loading = true;
const nodesDir = path.join(process.cwd(), "../nodes");
// evmos ibc0, gaia ibc1
const r = Math.random() >= 0.5;
const ibcSrc = _ibcSrc || (r ? "ibc-0" : "ibc-1");
const ibcDst = _ibcDst || (r ? "ibc-1" : "ibc-0");
const denom = _denom || (r ? getRandomArrayElements(["aevmos", "transfer/channel-0/uatom"], 1) : getRandomArrayElements(["uatom", "transfer/channel-0/aevmos"], 1));
const randWei = _amount || toWei(String(Math.random().toFixed(2)));
console.log("ibcTransfer", ibcSrc, ibcDst, denom, randWei);
try {
const cmd0 = `./rly tx link demo --client-tp 500s -d -t 3s --home ./relayer`;
const cmd1 = `./rly tx transfer ${ibcSrc} ${ibcDst} ${randWei}${denom} "$(./rly keys show ${ibcDst} --home ./relayer)" channel-0 -d --home ./relayer`;
const cmd2 = `./rly tx relay-packets demo channel-0 -d --home ./relayer`;
const cmd3 = `./rly tx relay-acknowledgements demo channel-0 -d --home ./relayer`;
const cmds = [cmd1, cmd2, cmd3];
for (const cmd of cmds) {
await execPromis(cmd, { cwd: nodesDir });
await sleep(5000);
}
} catch (error) {
console.log(error);
}
loading = false;
};
await ibcTransfer("ibc-0", "ibc-1", "aevmos", toWei(10));
await ibcTransfer("ibc-1", "ibc-0", "uatom", toWei(10));
setInterval(async () => {
await ibcTransfer();
}, 1000 * 30);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment