import express from "express"; import CommonUtil from "../../utils/commonUtil.js"; import {Contract, ethers, utils} from "ethers"; import RouterEthAbi from './contract/routerEth.js' import mathUtil from "../../utils/mathUtil.js"; const router = express.Router(); // const ethChainId = 101; // const wrapperEth = "0x72E2F4830b9E45d52F80aC08CB2bEC0FeF72eD9c"; // const routerAddress = "0x8731d54E9D02c286767d56ac03e8037C07e01e98"; // const routerEthAddress = "0x150f94B44927F078737562f0fcF3C95c01Cc2376"; // const network = "https://eth-mainnet.g.alchemy.com/v2/I0grWRjLbj_Rf1WP3QMJ-z3mKp71Hb4L"; const networks = { eth: { chain_id: 101, wrapper_eth: "0x72E2F4830b9E45d52F80aC08CB2bEC0FeF72eD9c", router_address: "0x8731d54E9D02c286767d56ac03e8037C07e01e98", router_eth_address: "0x150f94B44927F078737562f0fcF3C95c01Cc2376", network: "https://eth-mainnet.g.alchemy.com/v2/I0grWRjLbj_Rf1WP3QMJ-z3mKp71Hb4L" }, arb: { chain_id: 110, wrapper_eth: "0x82cbecf39bee528b5476fe6d1550af59a9db6fc0", router_address: "0x53bf833a5d6c4dda888f69c22c88c9f356a41614", router_eth_address: "0xbf22f0f184bCcbeA268dF387a49fF5238dD23E40", network: "https://arb-mainnet.g.alchemy.com/v2/8hbABA2L3eaR7VWGY8yk-5p16Aeurf2Z" }, opt: { chain_id: 111, wrapper_eth: "0xb69c8cbcd90a39d8d3d3ccf0a3e968511c3856a0", router_address: "0xB0D502E938ed5f4df2E681fE6E419ff29631d62b", router_eth_address: "0xB49c4e680174E331CB0A7fF3Ab58afC9738d5F8b", network: "https://opt-mainnet.g.alchemy.com/v2/-JQnqchpVew7O9KBdK_lkbXl_iHtfrwS" } } // const arbChainId = 110; // const wrapperArbEthAddress = "0x82cbecf39bee528b5476fe6d1550af59a9db6fc0"; // const routerArbAddress = "0x53bf833a5d6c4dda888f69c22c88c9f356a41614"; // const routerArbEthAddress = "0xbf22f0f184bCcbeA268dF387a49fF5238dD23E40"; // const arbNetwork = "https://arb-mainnet.g.alchemy.com/v2/8hbABA2L3eaR7VWGY8yk-5p16Aeurf2Z"; router.post("/", async (req, res) => { let {wallet, params} = CommonUtil.getParamsAndWallet(req); let dist_chain_id = networks[params.dist_chain].chain_id; let source_chain_id = networks[params.source_chain].chain_id; let wrap_addr = networks[params.source_chain].wrapper_eth; let rt_addr = networks[params.source_chain].router_address; let rt_eth_addr = networks[params.source_chain].router_eth_address; let net_work = networks[params.source_chain].network; // if (params.distChain === "eth") { // distChainId = ethChainId; // wrap_addr = wrapperEth; // rt_addr = routerAddress; // rt_eth_addr = routerEthAddress; // net_work = network; // } else if (params.distChain === "arb") { // distChainId = arbChainId; // wrap_addr = wrapperArbEthAddress; // rt_addr = routerArbAddress; // rt_eth_addr = routerArbEthAddress; // net_work = arbNetwork; // } const provider = ethers.getDefaultProvider(net_work); const signer = new ethers.Wallet(wallet.privateKey, provider); // const router = new Contract(rt_addr, RouterAbi, signer); const routerEth = new Contract(rt_eth_addr, RouterEthAbi, signer); let amountNumber = mathUtil.getRandomNumber(params.minAmount, params.maxAmount, params.accuracy); amountNumber = utils.parseEther(`${amountNumber}`); let crossGas = utils.parseEther(params.crossGas); const amountOut = amountNumber.mul(995).div(1000); let tx = await routerEth.swapETH( dist_chain_id, wallet.address, wallet.address, amountNumber, amountOut, { value: amountNumber.add(crossGas) } ) await tx.wait(); tx = await provider.getTransaction(tx.hash); const txReceipt = await provider.getTransactionReceipt(tx.hash); const gas = utils.formatEther((txReceipt.gasUsed * tx.gasPrice).toFixed(0)); const balance = await signer.getBalance() const balanceStr = utils.formatEther(balance); const value = utils.formatEther(tx.value) res.send({ code: 0, msg: "", data: { txId: tx.hash, gas: gas, currentBalance: balanceStr, status: txReceipt.status, value: value } }); }); export default router;