-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (96 loc) · 3.48 KB
/
index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { ethers, BigNumber } from "ethers";
import axios from "axios";
import { readFileSync } from "fs";
const CUSTODIAN = readFileSync("./custodian/artifacts/contracts/Custodian.sol/Custodian.json", "utf8");
const uniswap_graph =
"https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3";
export const main = async (poolAddress, owner, lossThreshold) => {
owner = owner.toLowerCase();
poolAddress = poolAddress.toLowerCase();
let data;
try {
data = await axios.post(uniswap_graph, {
query: `
{
positions(where: {pool_contains: "${poolAddress}", liquidity_gt: "0" owner: "${owner}"}){
owner
liquidity
depositedToken0
depositedToken1
pool{
token0{
id
symbol
decimals
}
token1{
id
symbol
decimals
}
tick
token0Price
token1Price
}
tickLower{
price0
price1
}
tickUpper{
price0
price1
}
}
}
`,
});
} catch (err) {
console.log(err);
return err;
}
console.log("Result: ", data.data.data);
data.data.data.positions.forEach(function (item, index){
viewPosition(item, lossThreshold, owner, poolAddress);
})
};
function viewPosition(position, lossThreshold, owner, poolAddress){
priceLower = position.tickLower.price0
priceUpper = position.tickUpper.price0
liquidity = position.liquidity
price = 1300; // need to know how to fetch
currentPrice = position.pool.token0Price
// amount of token0
x = position.depositedToken0
current_x = 0;
// amount of token1
y = position.depositedToken1
current_y = 0;
if(currentPrice < priceLower){
current_x = liquidity/sqrt(priceLower) - liquidity/sqrt(priceUpper);
}
else if(currentPrice > priceUpper){
current_y = liquidity * (priceUpper - priceLower);
}
else{
current_x = liquidity/sqrt(currentPrice) - liquidity/sqrt(priceUpper);
current_y = liquidity * (currentPrice - priceLower);
}
calculateImpermanentLoss(x, y, current_x, current_y, lossThreshold, owner, poolAddress);
}
function calculateImpermanentLoss(x, y, current_x, current_y, lossThreshold, owner, pool){
price0, price1, currentPrice0, currentPrice1, custodianAddress;
loss =
((currentPrice0 * current_x +
currentPrice1 * current_y -
price0 * x -
price1 * y) *
100) /
(price0 * x + price1 * y);
if(loss > lossThreshold){
const provider = new ethers.providers.JsonRpcProvider(process.env.RpcUrl)
const wallet = new ethers.Wallet(process.env.PrivateKey, provider)
const custodian = new ethers.Contract(custodianAddress, CUSTODIAN, wallet);
custodian.decreaseLiquidity(0);
}
}
// main("0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640","0xf9404708c16ec50dd563ca0bef2f07eb32d3d7c2");