-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniswapExchange.js
60 lines (43 loc) · 2.15 KB
/
uniswapExchange.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const {ethers} = require("ethers");
const { abi: IUniswapV3PoolABI } = require('@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json');
const { abi: QuoterABI } = require('@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json');
const { getAbi, getPoolImmutables } = require('./helpers');
require('dotenv').config()
const INFURA_URL = process.env.INFURA_URL;
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY;
const provider = new ethers.providers.JsonRpcProvider(INFURA_URL)
const poolAddress = '0xcbcdf9626bc03e24f779434178a73a0b4bad62ed'
const quoterAddress = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6'
const getPrice = async (inputAmount) => {
const poolContract = new ethers.Contract(poolAddress, IUniswapV3PoolABI, provider)
const tokenAddress0 = await poolContract.token0();
const tokenAddress1 = await poolContract.token1();
console.log(tokenAddress0)
console.log(tokenAddress1)
const tokenAbi0 = await getAbi(tokenAddress0);
const tokenAbi1 = await getAbi(tokenAddress1);
const tokenContract0 = new ethers.Contract(tokenAddress0, tokenAbi0, provider)
const tokenContract1 = new ethers.Contract(tokenAddress1, tokenAbi1, provider)
const tokenSymbol0 = await tokenContract0.symbol();
const tokenSymbol1 = await tokenContract1.symbol();
const tokenDecimals0 = await tokenContract0.decimals();
const tokenDecimals1 = await tokenContract1.decimals();
const quoterContract = new ethers.Contract(quoterAddress, QuoterABI, provider)
const immutables = await getPoolImmutables(poolContract)
const amountIn = ethers.utils.parseUnits(
inputAmount.toString(),
tokenDecimals0
)
const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(
immutables.token0,
immutables.token1,
immutables.fee,
amountIn.toString(),
0
)
const amountOut = ethers.utils.formatUnits(quotedAmountOut, tokenDecimals1)
console.log('================')
console.log(`${inputAmount} ${tokenSymbol0} can be swapped for ${amountOut} ${tokenSymbol1}`)
console.log('================')
}
getPrice(1)