Skip to content

Commit

Permalink
Split as two tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Sep 24, 2024
1 parent dae1e48 commit 103c956
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 3 deletions.
10 changes: 8 additions & 2 deletions web/packages/operations/ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
],
Expand Down
3 changes: 2 additions & 1 deletion web/packages/operations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
89 changes: 89 additions & 0 deletions web/packages/operations/src/transfer_to_ethereum.ts
Original file line number Diff line number Diff line change
@@ -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)
}
103 changes: 103 additions & 0 deletions web/packages/operations/src/transfer_to_polkadot.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 103c956

Please sign in to comment.