-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreatePool.js
88 lines (73 loc) · 2.17 KB
/
createPool.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { ethers } from 'ethers'
import { Pool } from '@uniswap/v3-sdk'
import { Token } from '@uniswap/sdk-core'
import { abi as IUniswapV3PoolABI } from '@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json'
const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/<YOUR-ENDPOINT-HERE>')
const poolAddress = '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
const poolContract = new ethers.Contract(poolAddress, IUniswapV3PoolABI, provider)
interface Immutables {
factory: string
token0: string
token1: string
fee: number
tickSpacing: number
maxLiquidityPerTick: ethers.BigNumber
}
interface State {
liquidity: ethers.BigNumber
sqrtPriceX96: ethers.BigNumber
tick: number
observationIndex: number
observationCardinality: number
observationCardinalityNext: number
feeProtocol: number
unlocked: boolean
}
async function getPoolImmutables() {
const [factory, token0, token1, fee, tickSpacing, maxLiquidityPerTick] = await Promise.all([
poolContract.factory(),
poolContract.token0(),
poolContract.token1(),
poolContract.fee(),
poolContract.tickSpacing(),
poolContract.maxLiquidityPerTick(),
])
const immutables: Immutables = {
factory,
token0,
token1,
fee,
tickSpacing,
maxLiquidityPerTick,
}
return immutables
}
async function getPoolState() {
const [liquidity, slot] = await Promise.all([poolContract.liquidity(), poolContract.slot0()])
const PoolState: State = {
liquidity,
sqrtPriceX96: slot[0],
tick: slot[1],
observationIndex: slot[2],
observationCardinality: slot[3],
observationCardinalityNext: slot[4],
feeProtocol: slot[5],
unlocked: slot[6],
}
return PoolState
}
async function main() {
const [immutables, state] = await Promise.all([getPoolImmutables(), getPoolState()])
const TokenA = new Token(3, immutables.token0, 6, 'USDC', 'USD Coin')
const TokenB = new Token(3, immutables.token1, 18, 'WETH', 'Wrapped Ether')
const poolExample = new Pool(
TokenA,
TokenB,
immutables.fee,
state.sqrtPriceX96.toString(),
state.liquidity.toString(),
state.tick
)
console.log(poolExample)
}
main()