swapUsdcForEth.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import express from "express";
  2. import mathUtil from "../../utils/mathUtil.js";
  3. import {Contract, Provider, Wallet} from "zksync-web3";
  4. import erc20 from "../abi/erc20.js";
  5. import {utils} from "ethers";
  6. import swapAbi from "../abi/swapAbi.js";
  7. const router = express.Router();
  8. async function sendSwapTx(amountNumber, router, signer, provider, wallet, approveGas) {
  9. const amountIn = amountNumber;
  10. const path = ["0x3355df6D4c9C3035724Fd0e3914dE96A5a83aaf4", "0x5AEa5775959fBC2557Cc8789bC1bf90A239D9a91"];
  11. const stable = [false, false]
  12. let timestamp = new Date().getTime();
  13. timestamp = (timestamp / 1000 + 20 * 60).toFixed(0);
  14. const amountsOut = await router.getAmountsOut(amountIn, path, stable);
  15. const amountOut = (amountsOut[1].mul(995).div(1000));
  16. const swapTx = await router.swapExactTokensForETHSupportingFeeOnTransferTokens(amountIn, amountOut, path, signer.address, timestamp, stable)
  17. await swapTx.wait();
  18. const swapReceipt = await provider.getTransactionReceipt(swapTx.hash);
  19. const gas = utils.formatEther((swapReceipt.gasUsed * swapTx.gasPrice).toFixed(0));
  20. const balance = await wallet.getBalance()
  21. const balanceStr = utils.formatEther(balance);
  22. const value = utils.formatEther(swapTx.value)
  23. return {
  24. code: 0,
  25. msg: "",
  26. data: {
  27. txId: swapTx.hash,
  28. gas: `${parseFloat(approveGas) + parseFloat(gas)}`,
  29. currentBalance: balanceStr,
  30. status: swapReceipt.status,
  31. value: value
  32. }
  33. }
  34. }
  35. router.post("/", async (req, res) => {
  36. const params = req.body.params;
  37. const amountMin = params.amountMin;
  38. const amountMax = params.amountMax;
  39. const accuracy = params.accuracy;
  40. const amountNumber = mathUtil.getRandomNumber(amountMin, amountMax, accuracy) * 1000000;
  41. const signer = req.body.wallet;
  42. const provider = new Provider('https://mainnet.era.zksync.io');
  43. const wallet = new Wallet(signer.privateKey, provider);
  44. const router = "0x8b791913eb07c32779a16750e3868aa8495f5964";
  45. const usdc = new Contract("0x3355df6d4c9c3035724fd0e3914de96a5a83aaf4", erc20, wallet);
  46. const routerContract = new Contract(router, swapAbi, wallet);
  47. // const allowance = await usdc.allowance(signer.address, router);
  48. let result = {};
  49. // if (allowance <= 0) {
  50. const approveTx = await usdc.approve(router, amountNumber);
  51. await approveTx.wait();
  52. const txReceipt = await provider.getTransactionReceipt(approveTx.hash);
  53. const approveGas = utils.formatEther((txReceipt.gasUsed * approveTx.gasPrice).toFixed(0));
  54. if (txReceipt.status === 1) {
  55. result = await sendSwapTx(amountNumber, routerContract, signer, provider, wallet, approveGas);
  56. } else {
  57. throw new Error("approve error");
  58. }
  59. // } else {
  60. // result = await sendSwapTx(amountNumber, routerContract, signer, provider, wallet, 0);
  61. // }
  62. res.send(result);
  63. })
  64. export default router;