syncswap.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Web3, { eth } from "web3";
  2. import { BigNumber } from "bignumber.js";
  3. import { Wallet, Provider } from "zksync-web3";
  4. import SyncSwapPoolFactoryAbi from "./SyncSwapClassicPoolFactory.json" assert { type: "json" };
  5. import SyncSwapRouterAbi from "./SyncSwapRouter.json" assert { type: "json" };
  6. import { ethers } from "ethers";
  7. const rpc = "https://zksync2-testnet.zksync.dev/";
  8. const walletAddress = "0x93626e5d46772652a8100a3ffc34d7bad8b29e00";
  9. const privateKey = "0xd768b0b3f8dedcb465ad680268453391f7da6ec4e1942a13fdfcdea8773aab3e";
  10. const poolFactoryAddress = "0xf2FD2bc2fBC12842aAb6FbB8b1159a6a83E72006";
  11. const routerAddress = "0xB3b7fCbb8Db37bC6f572634299A58f51622A847e";
  12. const wethAddress = "0x20b28b1e4665fff290650586ad76e977eab90c5d";
  13. const usdcAddress = "0x0faF6df7054946141266420b43783387A78d82A9";
  14. const provider = new Provider(rpc);
  15. const wallet = new Wallet(privateKey).connect(provider);
  16. console.log("rpc: ", rpc);
  17. const web3 = new Web3(new Web3.providers.HttpProvider(rpc));
  18. const account = web3.eth.accounts.privateKeyToAccount(privateKey);
  19. console.log("address: ", account.address);
  20. const poolFactory = new web3.eth.Contract(SyncSwapPoolFactoryAbi, poolFactoryAddress);
  21. const poolAddress = await poolFactory.methods.getPool(wethAddress, usdcAddress).call();
  22. console.log("poolAddress: ", res);
  23. const router = new web3.eth.Contract(SyncSwapRouterAbi, routerAddress);
  24. console.log(ethers.utils.parseEther("0.001", 18).toString());
  25. const addLiquidity = router.methods.addLiquidity2(
  26. poolAddress,
  27. [
  28. [ethers.constants.AddressZero, ethers.utils.parseEther("0.01", 18).toString()],
  29. [usdcAddress, ethers.utils.parseEther("0", 6).toString()],
  30. ],
  31. //encode abi
  32. ethers.utils.defaultAbiCoder.encode(["uint256"], [walletAddress]),
  33. 0,
  34. "0x0000000000000000000000000000000000000000",
  35. "0x"
  36. );
  37. console.log(addLiquidity.encodeABI());
  38. const gasLimit = await addLiquidity.estimateGas({
  39. from: walletAddress,
  40. value: ethers.utils.parseEther("0.01", 18).toString(),
  41. });
  42. console.log("gasLimit: ", gasLimit);
  43. const tx = await wallet.sendTransaction({
  44. // from: wallet.address,
  45. to: routerAddress,
  46. data: addLiquidity.encodeABI(),
  47. gasLimit,
  48. });
  49. console.log("tx: ", tx);