| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import Web3, { eth } from "web3";
- import { BigNumber } from "bignumber.js";
- import { Wallet, Provider } from "zksync-web3";
- import { ethers } from "ethers";
- import spacefiRouterAbi from "./spacefiRouterAbi.json" assert { type: "json" };
- import spacefiFactoryAbi from "./spacefiFactoryAbi.json" assert { type: "json" };
- import spacefiPairAbi from "./spacefiPairAbi.json" assert { type: "json" };
- const rpc = "https://zksync2-testnet.zksync.dev/";
- const privateKey = "0xd768b0b3f8dedcb465ad680268453391f7da6ec4e1942a13fdfcdea8773aab3e";
- const spaceFiRouterAddress = "0x6eeF3310E09DF3aa819Cc2aa364D4f3Ad2E6fFe3";
- const usdcAddress = "0x0faF6df7054946141266420b43783387A78d82A9";
- const provider = new Provider(rpc);
- const wallet = new Wallet(privateKey).connect(provider);
- const web3 = new Web3(new Web3.providers.HttpProvider(rpc));
- const value = ethers.utils.parseEther("0.001");
- const spacefiRouter = new web3.eth.Contract(spacefiRouterAbi, spaceFiRouterAddress);
- const wethAddress = await spacefiRouter.methods.WETH().call();
- console.log(`wethAddress: ${wethAddress}`);
- const d = ethers.BigNumber.from(usdcAddress).gt(ethers.BigNumber.from(wethAddress));
- const inputToken = "eth";
- const inputEth = inputToken === "eth";
- console.log(`d: ${d}`);
- const factoryAddress = await spacefiRouter.methods.factory().call();
- console.log(`factoryAddress: ${factoryAddress}`);
- const factory = new web3.eth.Contract(spacefiFactoryAbi, factoryAddress);
- const pairAddress = await factory.methods.getPair(wethAddress, usdcAddress).call();
- console.log(`pairAddress: ${pairAddress}`);
- const pair = new web3.eth.Contract(spacefiPairAbi, pairAddress);
- const reserves = await pair.methods.getReserves().call();
- const amountOut = await spacefiRouter.methods
- .quote(value.toHexString(), d ^ inputEth ? reserves[1] : reserves[0], d ^ inputEth ? reserves[0] : reserves[1])
- .call();
- console.log(`out: ${ethers.utils.formatUnits(amountOut, 6)}`);
- const addLiquidity = await spacefiRouter.methods.addLiquidityETH(
- usdcAddress,
- amountOut,
- 0,
- 0,
- wallet.address,
- Math.floor(new Date().getTime() / 1000 + 60 * 30) + ""
- );
- const gasLimit = await addLiquidity.estimateGas({ from: wallet.address, value: value.toHexString() });
- console.log(`gasLimit: ${gasLimit}`);
- const tx = await wallet.sendTransaction({
- to: spaceFiRouterAddress,
- data: addLiquidity.encodeABI(),
- value: value.toHexString(),
- gasLimit,
- });
- console.log(tx);
|