approve.mjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import base, { ChainId } from "iziswap-sdk/lib/base/index.js";
  2. import Web3 from "web3";
  3. import { BigNumber } from "bignumber.js";
  4. import quoter from "iziswap-sdk/lib/quoter/index.js";
  5. import swap from "iziswap-sdk/lib/swap/index.js";
  6. import token from "iziswap-sdk/lib/base/token/index.js";
  7. import liquidityManager from "iziswap-sdk/lib/liquidityManager/index.js";
  8. import pool from "iziswap-sdk/lib/pool/index.js";
  9. import { Wallet, Provider } from "zksync-web3";
  10. const chainId = base.ChainId.ZkSyncAlphaTest;
  11. const rpc = "https://zksync2-testnet.zksync.dev/";
  12. const walletAddress = "0x93626e5d46772652a8100a3ffc34d7bad8b29e00";
  13. const privateKey = "0xd768b0b3f8dedcb465ad680268453391f7da6ec4e1942a13fdfcdea8773aab3e";
  14. const quoterAddress = "0xE93D1d35a63f7C6b51ef46a27434375761a7Db28";
  15. const swapAddress = "0x3040EE148D09e5B92956a64CDC78b49f48C0cDdc";
  16. const toTokenAddress = "0xA5900cce51c45Ab9730039943B3863C822342034";
  17. const liquidityManagerAddress = "0x25727b360604E1e6B440c3B25aF368F54fc580B6";
  18. const tokenAAddress = "0x00";
  19. const tokenBAddress = "0xA5900cce51c45Ab9730039943B3863C822342034";
  20. const provider = new Provider(rpc);
  21. const wallet = new Wallet(privateKey).connect(provider);
  22. const chain = base.initialChainTable[chainId];
  23. console.log("rpc: ", rpc);
  24. const web3 = new Web3(new Web3.providers.HttpProvider(rpc));
  25. const account = web3.eth.accounts.privateKeyToAccount(privateKey);
  26. console.log("address: ", account.address);
  27. const liquidityManagerContract = liquidityManager.getLiquidityManagerContract(liquidityManagerAddress, web3);
  28. console.log("liquidity manager address: ", liquidityManagerAddress);
  29. const tokenA = base.getGasToken(chainId);
  30. console.log("tokenA: ", tokenA);
  31. const tokenB = await token.fetchToken(tokenBAddress, chain, web3);
  32. console.log("tokenB: ", tokenB);
  33. async function approve(tokenAddress) {
  34. const erc20ABI = [
  35. {
  36. inputs: [
  37. {
  38. internalType: "address",
  39. name: "spender",
  40. type: "address",
  41. },
  42. {
  43. internalType: "uint256",
  44. name: "amount",
  45. type: "uint256",
  46. },
  47. ],
  48. name: "approve",
  49. outputs: [
  50. {
  51. internalType: "bool",
  52. name: "",
  53. type: "bool",
  54. },
  55. ],
  56. stateMutability: "nonpayable",
  57. type: "function",
  58. },
  59. ];
  60. const tokenAContract = new web3.eth.Contract(erc20ABI, tokenAddress);
  61. // you could approve a very large amount (much more greater than amount to transfer),
  62. // and don't worry about that because liquidityManager only transfer your token to pool with amount you specified and your token is safe
  63. // then you do not need to approve next time for this user's address
  64. const approveCalling = tokenAContract.methods.approve(
  65. liquidityManagerAddress,
  66. "0xffffffffffffffffffffffffffffffff"
  67. );
  68. // estimate gas
  69. let gasLimit = await approveCalling.estimateGas({ from: account.address });
  70. console.log("approve gas limit: ", gasLimit);
  71. // then send transaction to approve
  72. // you could simply use followiing line if you use metamask in your frontend code
  73. // otherwise, you should use the function "web3.eth.accounts.signTransaction"
  74. // notice that, sending transaction for approve may fail if you have approved the token to liquidityManager before
  75. // if you want to enlarge approve amount, you should refer to interface of erc20 token
  76. const tx0 = await wallet.sendTransaction({
  77. from: account.address,
  78. to: tokenAddress,
  79. data: approveCalling.encodeABI(),
  80. gasLimit,
  81. });
  82. console.log("approve tx: ", JSON.stringify(tx0, null, 4));
  83. }
  84. await approve(tokenA.address);