Browse Source

Merge remote-tracking branch 'origin/master'

million 1 year ago
parent
commit
adbb17ce2e

+ 15 - 15
js-runtime/src/script/zksync2/mute/swapUsdcForEth.js

@@ -8,7 +8,7 @@ import swapAbi from "../abi/swapAbi.js";
 const router = express.Router();
 
 async function sendSwapTx(amountNumber, router, signer, provider, wallet, approveGas) {
-    const amountIn = amountNumber * 1000000;
+    const amountIn = amountNumber;
     const path = ["0x3355df6D4c9C3035724Fd0e3914dE96A5a83aaf4", "0x5AEa5775959fBC2557Cc8789bC1bf90A239D9a91"];
     const stable = [false, false]
     let timestamp = new Date().getTime();
@@ -41,7 +41,7 @@ router.post("/", async (req, res) => {
     const amountMin = params.amountMin;
     const amountMax = params.amountMax;
     const accuracy = params.accuracy;
-    const amountNumber = mathUtil.getRandomNumber(amountMin, amountMax, accuracy);
+    const amountNumber = mathUtil.getRandomNumber(amountMin, amountMax, accuracy) * 1000000;
     const signer = req.body.wallet;
     const provider = new Provider('https://mainnet.era.zksync.io');
 
@@ -49,23 +49,23 @@ router.post("/", async (req, res) => {
     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);
+    // const allowance = await usdc.allowance(signer.address, router);
 
     let result = {};
-    if (allowance <= 0) {
-        const approveTx = await usdc.approve(router, 0xFFFFFFFF);
-        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");
-        }
-
+    // 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 {
-        result = await sendSwapTx(amountNumber, routerContract, signer, provider, wallet, 0);
+        throw new Error("approve error");
     }
+
+    // } else {
+    //     result = await sendSwapTx(amountNumber, routerContract, signer, provider, wallet, 0);
+    // }
     res.send(result);
 })
 

+ 12 - 12
js-runtime/src/script/zksync2/sync/swapUsdcForEth.js

@@ -1,7 +1,7 @@
 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 {tokenSwap, getPoolAddress, wETHAddress, usdcAddress, routerAddress} from "./swap.js";
 import {utils} from "ethers";
 import erc20 from "../abi/erc20.js";
 
@@ -11,24 +11,24 @@ router.post("/", async (req, res) => {
     const amountMin = params.amountMin;
     const amountMax = params.amountMax;
     const accuracy = params.accuracy;
-    const amountNumber = mathUtil.getRandomNumber(amountMin, amountMax, 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);
+    // const allowance = await usdc.allowance(signer.address, routerAddress);
     let approveGas = 0;
-    if (allowance <= 0) {
-        const approveTx = await usdc.approve(routerAddress, 0xFFFFFFFF);
-        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");
-        }
+    // 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, parseFloat(amountNumber) * 1000000, wallet);
+    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));