swap2.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 { Wallet, Provider } from "zksync-web3";
  8. const rpc = "https://zksync2-testnet.zksync.dev/";
  9. const privateKey = "0xd768b0b3f8dedcb465ad680268453391f7da6ec4e1942a13fdfcdea8773aab3e";
  10. const quoterAddress = "0xE93D1d35a63f7C6b51ef46a27434375761a7Db28";
  11. const swapAddress = "0x3040EE148D09e5B92956a64CDC78b49f48C0cDdc";
  12. const fromTokenAddress = "0x0faF6df7054946141266420b43783387A78d82A9";
  13. const swapAmount = 100;
  14. const chain = base.initialChainTable[ChainId.ZkSyncAlphaTest];
  15. console.log("rpc: ", rpc);
  16. const web3 = new Web3(new Web3.providers.HttpProvider(rpc));
  17. const account = web3.eth.accounts.privateKeyToAccount(privateKey);
  18. console.log("address: ", account.address);
  19. const fromToken = await token.fetchToken(fromTokenAddress, chain, web3);
  20. console.log("fromToken: ", fromToken);
  21. const toToken = base.getGasToken(base.ChainId.ZkSyncAlphaTest);
  22. console.log("toToken: ", toToken);
  23. const quoterContract = quoter.getQuoterContract(quoterAddress, web3);
  24. const payAmount = new BigNumber(swapAmount).times(10 ** fromToken.decimal);
  25. const fee = 2000; // 2000 means 0.2%
  26. const params = {
  27. // pay testA to buy testB
  28. tokenChain: [fromToken, toToken],
  29. feeChain: [fee],
  30. inputAmount: payAmount.toFixed(0),
  31. };
  32. const { outputAmount } = await quoter.quoterSwapChainWithExactInput(quoterContract, params);
  33. const receiveAmount = outputAmount;
  34. const receiveAmountDecimal = token.amount2Decimal(new BigNumber(receiveAmount), toToken);
  35. console.log(fromToken.symbol + " to pay: ", swapAmount);
  36. console.log(toToken.symbol + " to acquire: ", receiveAmountDecimal);
  37. const swapContract = swap.getSwapContract(swapAddress, web3);
  38. const swapParams = {
  39. ...params,
  40. // slippery is 1.5%
  41. minOutputAmount: new BigNumber(receiveAmount).times(0.985).toFixed(0),
  42. strictERC20Token: false,
  43. };
  44. const { swapCalling, options } = swap.getSwapChainWithExactInputCall(swapContract, account.address, chain, swapParams);
  45. const gasLimit = await swapCalling.estimateGas(options);
  46. console.log("gas limit: ", gasLimit);
  47. const provider = new Provider(rpc);
  48. const wallet = new Wallet(privateKey).connect(provider);
  49. const tx = await wallet.sendTransaction({
  50. value: Web3.utils.numberToHex(options.value),
  51. data: swapCalling.arguments[0][0],
  52. to: swapAddress,
  53. gasLimit,
  54. });
  55. console.log(JSON.stringify(tx, null, 4));