-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3.ts
86 lines (77 loc) · 2.26 KB
/
web3.ts
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
import { ethers } from "ethers";
import BigNumber from "bignumber.js";
import {
CONTROLLER_ADDRESS,
OSQTH_ADDRESS,
UNI_WETH_USDC_POOL_ADDRESS,
UNI_WETH_OSQTH_POOL_ADDRESS,
WETH_ADDRESS,
CRAB_ADDRESS,
QUOTER_ADDRESS,
ORACLE_ADDRESS,
} from "./constants";
import erc20Abi from "human-standard-token-abi";
import { abi as QuoterABI } from "@uniswap/v3-periphery/artifacts/contracts/lens/Quoter.sol/Quoter.json";
import controllerAbi from "./abis/controller.json";
import poolAbi from "./abis/pool-abi.json";
import crabAbi from "./abis/crab.json";
import IOracleAbi from "./abis/ioracle.json";
export const provider = new ethers.providers.JsonRpcProvider(
"http://localhost:8545"
);
export const esprovider = new ethers.providers.EtherscanProvider();
export const controller = new ethers.Contract(
CONTROLLER_ADDRESS,
controllerAbi,
provider
);
export const token = new ethers.Contract(OSQTH_ADDRESS, erc20Abi, provider);
export const weth = new ethers.Contract(WETH_ADDRESS, erc20Abi, provider);
export const crab = new ethers.Contract(CRAB_ADDRESS, crabAbi, provider);
export const uniWethUsdcPool = new ethers.Contract(
UNI_WETH_USDC_POOL_ADDRESS,
poolAbi,
provider
);
export const uniWethOsqthPool = new ethers.Contract(
UNI_WETH_OSQTH_POOL_ADDRESS,
poolAbi,
provider
);
export const quoterContract = new ethers.Contract(
QUOTER_ADDRESS,
QuoterABI,
provider
);
export const IOracle = new ethers.Contract(
ORACLE_ADDRESS,
IOracleAbi,
provider
);
export const getPriceOfSqrtPriceX96 = (
sqrtPriceX96: string,
decDiff: number
) => {
const sqrt = new BigNumber(sqrtPriceX96.toString());
const tokenPrice0 = sqrt.pow(2).div(new BigNumber(2).pow(192)); //token0
const tokenPrice1 = (2 ** 192 / parseInt(sqrtPriceX96) ** 2) * 10 ** decDiff; // WETH
// console.log(tokenPrice1);
// console.log(tokenPrice0.toString());
return tokenPrice1;
};
export const queryTokenPrice = async (
pool: ethers.Contract,
decDiff: number,
blockNumber: number = null
) => {
if (!blockNumber) {
blockNumber = await provider.getBlockNumber();
}
try {
const results = await pool.slot0({ blockTag: blockNumber });
const { sqrtPriceX96 } = results;
return getPriceOfSqrtPriceX96(sqrtPriceX96, decDiff);
} catch (err) {
return null;
}
};