swap.mjs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 chainId = base.ChainId.ZkSyncAlphaTest;
  9. const rpc = "https://zksync2-testnet.zksync.dev/";
  10. const walletAddress = "0x93626e5d46772652a8100a3ffc34d7bad8b29e00";
  11. const privateKey = "0xd768b0b3f8dedcb465ad680268453391f7da6ec4e1942a13fdfcdea8773aab3e";
  12. const quoterAddress = "0xE93D1d35a63f7C6b51ef46a27434375761a7Db28";
  13. const swapAddress = "0x3040EE148D09e5B92956a64CDC78b49f48C0cDdc";
  14. const toTokenAddress = "0xA5900cce51c45Ab9730039943B3863C822342034";
  15. const swapAmount = 2000;
  16. try {
  17. const chain = base.initialChainTable[chainId];
  18. console.log("rpc: ", rpc);
  19. const web3 = new Web3(new Web3.providers.HttpProvider(rpc));
  20. const account = web3.eth.accounts.privateKeyToAccount(privateKey);
  21. console.log("address: ", account.address);
  22. const balance = await web3.eth.getBalance(account.address);
  23. console.log("ethBalance: " + Web3.utils.fromWei(balance, "ether"));
  24. const fromToken = base.getGasToken(base.ChainId.ZkSyncAlphaTest);
  25. console.log("fromToken: ", fromToken);
  26. const toToken = await base.fetchToken(toTokenAddress, chain, web3);
  27. console.log("toToken: ", toToken);
  28. const fee = 2000; // 2000 means 0.2%
  29. const toTokenContract = token.getErc20TokenContract(toToken.address, web3);
  30. const toTokenBalance = await toTokenContract.methods.balanceOf(account.address).call();
  31. console.log(toToken.symbol + " balance: " + token.amount2Decimal(new BigNumber(toTokenBalance), toToken));
  32. console.log("quoter address: ", quoterAddress);
  33. const quoterContract = quoter.getQuoterContract(quoterAddress, web3);
  34. const receiveAmount = new BigNumber(swapAmount).times(10 ** toToken.decimal);
  35. const params = {
  36. // pay testA to buy testB
  37. tokenChain: [fromToken, toToken],
  38. feeChain: [fee],
  39. outputAmount: receiveAmount.toFixed(0),
  40. };
  41. const { inputAmount } = await quoter.quoterSwapChainWithExactOutput(quoterContract, params);
  42. const payAmount = inputAmount;
  43. const payAmountDecimal = token.amount2Decimal(new BigNumber(payAmount), fromToken);
  44. console.log(toToken.symbol + " to desired: ", swapAmount);
  45. console.log(fromToken.symbol + " to pay: ", payAmountDecimal);
  46. const swapContract = swap.getSwapContract(swapAddress, web3);
  47. const swapParams = {
  48. ...params,
  49. // slippery is 1.5%
  50. maxInputAmount: new BigNumber(payAmount).times(1.015).toFixed(0),
  51. strictERC20Token: false,
  52. };
  53. const { swapCalling, options } = swap.getSwapChainWithExactOutputCall(
  54. swapContract,
  55. account.address,
  56. chain,
  57. swapParams
  58. );
  59. console.log(JSON.stringify({ swapCalling, options }, null, 4));
  60. const gasLimit = await swapCalling.estimateGas(options);
  61. console.log("gas limit: ", gasLimit);
  62. const provider = new Provider(rpc);
  63. const wallet = new Wallet(privateKey).connect(provider);
  64. const tx = await wallet.sendTransaction({
  65. value: Web3.utils.numberToHex(options.value),
  66. data: swapCalling.arguments[0][0],
  67. to: swapAddress,
  68. gasLimit,
  69. });
  70. console.log(tx);
  71. } catch (error) {
  72. console.log(error.stack);
  73. }