approve.mjs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. console.log("rpc: ", rpc);
  23. const web3 = new Web3(new Web3.providers.HttpProvider(rpc));
  24. const account = web3.eth.accounts.privateKeyToAccount(privateKey);
  25. console.log("address: ", account.address);
  26. async function approve(tokenAddress) {
  27. const erc20ABI = [
  28. {
  29. inputs: [
  30. {
  31. internalType: "address",
  32. name: "spender",
  33. type: "address",
  34. },
  35. {
  36. internalType: "uint256",
  37. name: "amount",
  38. type: "uint256",
  39. },
  40. ],
  41. name: "approve",
  42. outputs: [
  43. {
  44. internalType: "bool",
  45. name: "",
  46. type: "bool",
  47. },
  48. ],
  49. stateMutability: "nonpayable",
  50. type: "function",
  51. },
  52. ];
  53. const tokenAContract = new web3.eth.Contract(erc20ABI, tokenAddress);
  54. // you could approve a very large amount (much more greater than amount to transfer),
  55. // and don't worry about that because liquidityManager only transfer your token to pool with amount you specified and your token is safe
  56. // then you do not need to approve next time for this user's address
  57. const approveCalling = tokenAContract.methods.approve(
  58. liquidityManagerAddress,
  59. "0xffffffffffffffffffffffffffffffff"
  60. );
  61. // estimate gas
  62. let gasLimit = await approveCalling.estimateGas({ from: account.address });
  63. console.log("approve gas limit: ", gasLimit);
  64. // then send transaction to approve
  65. // you could simply use followiing line if you use metamask in your frontend code
  66. // otherwise, you should use the function "web3.eth.accounts.signTransaction"
  67. // notice that, sending transaction for approve may fail if you have approved the token to liquidityManager before
  68. // if you want to enlarge approve amount, you should refer to interface of erc20 token
  69. const tx0 = await wallet.sendTransaction({
  70. from: account.address,
  71. to: tokenAddress,
  72. data: approveCalling.encodeABI(),
  73. gasLimit,
  74. });
  75. console.log("approve tx: ", JSON.stringify(tx0, null, 4));
  76. }
  77. await approve(tokenA.address);