import express from "express"; import mathUtil from "../../utils/mathUtil.js"; import {Contract, Provider, Wallet} from "zksync-web3"; import {tokenSwap, getPoolAddress, wETHAddress, usdcAddress, routerAddress} from "./swap.js"; import {utils} from "ethers"; import erc20 from "../abi/erc20.js"; const router = express.Router(); 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 usdc = new Contract(usdcAddress, erc20, wallet); // const allowance = await usdc.allowance(signer.address, routerAddress); let approveGas = 0; // if (allowance <= 0) { const approveTx = await usdc.approve(routerAddress, amountNumber); await approveTx.wait(); const txReceipt = await provider.getTransactionReceipt(approveTx.hash); approveGas = utils.formatEther((txReceipt.gasUsed * approveTx.gasPrice).toFixed(0)); if (txReceipt.status !== 1) { throw new Error("approve error"); } // } const pool = await getPoolAddress(usdcAddress, wETHAddress); const txR = await tokenSwap(pool, usdcAddress, amountNumber, wallet); const tx = await provider.getTransaction(txR.transactionHash); // const swapReceipt = await provider.getTransactionReceipt(tx.hash); const gas = utils.formatEther((txR.gasUsed * tx.gasPrice).toFixed(0)); const balance = await wallet.getBalance() const balanceStr = utils.formatEther(balance); const value = utils.formatEther(tx.value) res.send({ code: 0, msg: "", data: { txId: tx.hash, gas: `${parseFloat(approveGas + "") + parseFloat(gas)}`, currentBalance: balanceStr, status: txR.status, value: value } }) }) export default router;