import express from "express"; import mathUtil from "../../utils/mathUtil.js"; import {Contract, Provider, Wallet} from "zksync-web3"; import erc20 from "../abi/erc20.js"; import {utils} from "ethers"; import swapAbi from "../abi/swapAbi.js"; const router = express.Router(); async function sendSwapTx(amountNumber, router, signer, provider, wallet, approveGas) { const amountIn = amountNumber; const path = ["0x3355df6D4c9C3035724Fd0e3914dE96A5a83aaf4", "0x5AEa5775959fBC2557Cc8789bC1bf90A239D9a91"]; const stable = [false, false] let timestamp = new Date().getTime(); timestamp = (timestamp / 1000 + 20 * 60).toFixed(0); const amountsOut = await router.getAmountsOut(amountIn, path, stable); const amountOut = (amountsOut[1].mul(995).div(1000)); const swapTx = await router.swapExactTokensForETHSupportingFeeOnTransferTokens(amountIn, amountOut, path, signer.address, timestamp, stable) await swapTx.wait(); const swapReceipt = await provider.getTransactionReceipt(swapTx.hash); const gas = utils.formatEther((swapReceipt.gasUsed * swapTx.gasPrice).toFixed(0)); const balance = await wallet.getBalance() const balanceStr = utils.formatEther(balance); const value = utils.formatEther(swapTx.value) return { code: 0, msg: "", data: { txId: swapTx.hash, gas: `${parseFloat(approveGas) + parseFloat(gas)}`, currentBalance: balanceStr, status: swapReceipt.status, value: value } } } router.post("/", async (req, res) => { const params = req.body.params; const amountMin = params.amountMin; const amountMax = params.amountMax; const accuracy = params.accuracy; const amountNumber = mathUtil.getRandomNumber(amountMin, amountMax, accuracy) * 1000000; const signer = req.body.wallet; const provider = new Provider('https://mainnet.era.zksync.io'); const wallet = new Wallet(signer.privateKey, provider); const router = "0x8b791913eb07c32779a16750e3868aa8495f5964"; const usdc = new Contract("0x3355df6d4c9c3035724fd0e3914de96a5a83aaf4", erc20, wallet); const routerContract = new Contract(router, swapAbi, wallet); // const allowance = await usdc.allowance(signer.address, router); let result = {}; // if (allowance <= 0) { const approveTx = await usdc.approve(router, amountNumber); await approveTx.wait(); const txReceipt = await provider.getTransactionReceipt(approveTx.hash); const approveGas = utils.formatEther((txReceipt.gasUsed * approveTx.gasPrice).toFixed(0)); if (txReceipt.status === 1) { result = await sendSwapTx(amountNumber, routerContract, signer, provider, wallet, approveGas); } else { throw new Error("approve error"); } // } else { // result = await sendSwapTx(amountNumber, routerContract, signer, provider, wallet, 0); // } res.send(result); }) export default router;