From 103c956543a24d2f97693c99fc1fc049f11743b7 Mon Sep 17 00:00:00 2001 From: ron Date: Tue, 24 Sep 2024 15:45:30 +0000 Subject: [PATCH] Split as two tests --- web/packages/operations/ecosystem.config.js | 10 +- web/packages/operations/package.json | 3 +- .../operations/src/transfer_to_ethereum.ts | 89 +++++++++++++++ .../operations/src/transfer_to_polkadot.ts | 103 ++++++++++++++++++ 4 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 web/packages/operations/src/transfer_to_ethereum.ts create mode 100644 web/packages/operations/src/transfer_to_polkadot.ts diff --git a/web/packages/operations/ecosystem.config.js b/web/packages/operations/ecosystem.config.js index 3ebb915382..a7807f8673 100644 --- a/web/packages/operations/ecosystem.config.js +++ b/web/packages/operations/ecosystem.config.js @@ -7,9 +7,15 @@ module.exports = { args: "cron" }, { - name: "transferTest", + name: "transferToPolkadot", node_args: "--require=dotenv/config", - script: "./dist/src/transfer_token.js", + script: "./dist/src/transfer_to_polkadot.js", + args: "cron" + }, + { + name: "transferToEthereum", + node_args: "--require=dotenv/config", + script: "./dist/src/transfer_to_ethereum.js", args: "cron" }, ], diff --git a/web/packages/operations/package.json b/web/packages/operations/package.json index 96ac4491a9..124b588eb6 100644 --- a/web/packages/operations/package.json +++ b/web/packages/operations/package.json @@ -17,7 +17,8 @@ "cron": "npx ts-node src/main.ts cron", "server": "npx ts-node src/server.ts", "smokeTest": "npx ts-node src/transfer_token.ts start", - "cronSmokeTest": "npx ts-node src/transfer_token.ts cron", + "transferToPolkadot": "npx ts-node src/transfer_to_polkadot.ts start", + "transferToEthereum": "npx ts-node src/transfer_to_ethereum.ts start", "format": "prettier src --write" }, "devDependencies": { diff --git a/web/packages/operations/src/transfer_to_ethereum.ts b/web/packages/operations/src/transfer_to_ethereum.ts new file mode 100644 index 0000000000..3c3150113e --- /dev/null +++ b/web/packages/operations/src/transfer_to_ethereum.ts @@ -0,0 +1,89 @@ +import "dotenv/config" +import { Keyring } from "@polkadot/keyring" +import { + contextFactory, + destroyContext, + environment, + toEthereum, +} from "@snowbridge/api" +import { Wallet } from "ethers" +import cron from "node-cron" + +const transfer = async () => { + let env = "local_e2e" + if (process.env.NODE_ENV !== undefined) { + env = process.env.NODE_ENV + } + const snwobridgeEnv = environment.SNOWBRIDGE_ENV[env] + if (snwobridgeEnv === undefined) { + throw Error(`Unknown environment '${env}'`) + } + + const { config } = snwobridgeEnv + + const context = await contextFactory({ + ethereum: { + execution_url: process.env["EXECUTION_NODE_URL"] || config.ETHEREUM_API(process.env.REACT_APP_INFURA_KEY || ""), + beacon_url: config.BEACON_HTTP_API, + }, + polkadot: { + url: { + bridgeHub: config.BRIDGE_HUB_URL, + assetHub: config.ASSET_HUB_URL, + relaychain: config.RELAY_CHAIN_URL, + parachains: config.PARACHAINS, + }, + }, + appContracts: { + gateway: config.GATEWAY_CONTRACT, + beefy: config.BEEFY_CONTRACT, + }, + }) + const polkadot_keyring = new Keyring({ type: "sr25519" }) + + const ETHEREUM_ACCOUNT = new Wallet( + process.env["ETHEREUM_KEY"] || "0x5e002a1af63fd31f1c25258f3082dc889762664cb8f218d86da85dff8b07b342", + context.ethereum.api + ) + const ETHEREUM_ACCOUNT_PUBLIC = await ETHEREUM_ACCOUNT.getAddress() + const POLKADOT_ACCOUNT = process.env["SUBSTRATE_KEY"]?polkadot_keyring.addFromUri(process.env["SUBSTRATE_KEY"]):polkadot_keyring.addFromUri("//Ferdie") + + const amount = 2_000_000_000_000n + + const WETH_CONTRACT = snwobridgeEnv.locations[0].erc20tokensReceivable.find( + (t) => t.id === "WETH" + )!.address + + console.log("# Asset Hub to Ethereum") + { + const plan = await toEthereum.validateSend( + context, + POLKADOT_ACCOUNT, + 1000, + ETHEREUM_ACCOUNT_PUBLIC, + WETH_CONTRACT, + amount + ) + console.log("Plan:", plan, plan.failure?.errors) + const result = await toEthereum.send(context, POLKADOT_ACCOUNT, plan) + console.log("Execute:", result) + } + await destroyContext(context) +} + +if (process.argv.length != 3) { + console.error("Expected one argument with Enum from `start|cron`") + process.exit(1) +} + +if (process.argv[2] == "start") { + transfer() + .then(() => process.exit(0)) + .catch((error) => { + console.error("Error:", error) + process.exit(1) + }) +} else if (process.argv[2] == "cron") { + console.log("running cronjob") + cron.schedule(process.env["CRON_EXPRESSION"] || "0 0 * * *", transfer) +} diff --git a/web/packages/operations/src/transfer_to_polkadot.ts b/web/packages/operations/src/transfer_to_polkadot.ts new file mode 100644 index 0000000000..91c388bc8b --- /dev/null +++ b/web/packages/operations/src/transfer_to_polkadot.ts @@ -0,0 +1,103 @@ +import "dotenv/config" +import { Keyring } from "@polkadot/keyring" +import { + contextFactory, + destroyContext, + environment, + toPolkadot, +} from "@snowbridge/api" +import { WETH9__factory } from "@snowbridge/contract-types" +import { Wallet } from "ethers" +import cron from "node-cron" + +const transfer = async () => { + let env = "local_e2e" + if (process.env.NODE_ENV !== undefined) { + env = process.env.NODE_ENV + } + const snwobridgeEnv = environment.SNOWBRIDGE_ENV[env] + if (snwobridgeEnv === undefined) { + throw Error(`Unknown environment '${env}'`) + } + + const { config } = snwobridgeEnv + + const context = await contextFactory({ + ethereum: { + execution_url: process.env["EXECUTION_NODE_URL"] || config.ETHEREUM_API(process.env.REACT_APP_INFURA_KEY || ""), + beacon_url: config.BEACON_HTTP_API, + }, + polkadot: { + url: { + bridgeHub: config.BRIDGE_HUB_URL, + assetHub: config.ASSET_HUB_URL, + relaychain: config.RELAY_CHAIN_URL, + parachains: config.PARACHAINS, + }, + }, + appContracts: { + gateway: config.GATEWAY_CONTRACT, + beefy: config.BEEFY_CONTRACT, + }, + }) + const polkadot_keyring = new Keyring({ type: "sr25519" }) + + const ETHEREUM_ACCOUNT = new Wallet( + process.env["ETHEREUM_KEY"] || "0x5e002a1af63fd31f1c25258f3082dc889762664cb8f218d86da85dff8b07b342", + context.ethereum.api + ) + const POLKADOT_ACCOUNT = process.env["SUBSTRATE_KEY"]?polkadot_keyring.addFromUri(process.env["SUBSTRATE_KEY"]):polkadot_keyring.addFromUri("//Ferdie") + const POLKADOT_ACCOUNT_PUBLIC = POLKADOT_ACCOUNT.address + + const amount = 2_000_000_000_000n + + const WETH_CONTRACT = snwobridgeEnv.locations[0].erc20tokensReceivable.find( + (t) => t.id === "WETH" + )!.address + + console.log("# Deposit and Approve WETH") + { + const weth9 = WETH9__factory.connect(WETH_CONTRACT, ETHEREUM_ACCOUNT) + const depositResult = await weth9.deposit({ value: amount }) + const depositReceipt = await depositResult.wait() + + const approveResult = await weth9.approve(config.GATEWAY_CONTRACT, amount) + const approveReceipt = await approveResult.wait() + + console.log('deposit tx', depositReceipt?.hash, 'approve tx', approveReceipt?.hash) + } + + console.log("# Ethereum to Asset Hub") + { + const plan = await toPolkadot.validateSend( + context, + ETHEREUM_ACCOUNT, + POLKADOT_ACCOUNT_PUBLIC, + WETH_CONTRACT, + 1000, + amount, + BigInt(0) + ) + console.log("Plan:", plan, plan.failure?.errors) + let result = await toPolkadot.send(context, ETHEREUM_ACCOUNT, plan) + console.log("Execute:", result) + } + await destroyContext(context) +} + +if (process.argv.length != 3) { + console.error("Expected one argument with Enum from `start|cron`") + process.exit(1) +} + +if (process.argv[2] == "start") { + transfer() + .then(() => process.exit(0)) + .catch((error) => { + console.error("Error:", error) + process.exit(1) + }) +} else if (process.argv[2] == "cron") { + console.log("running cronjob") + cron.schedule(process.env["CRON_EXPRESSION"] || "0 0 * * *", transfer) +}