-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.js
239 lines (181 loc) · 6.61 KB
/
go.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const Web3 = require("web3");
const colors = require("colors");
// const decimals = require("./decimals.js");
const pairs = require("./pairs.js");
const tokenAddresses = require("./tokenAddresses.js");
const erc20Abi = require("./abis/erc20.js");
const uniswapPairAbi = require("./abis/uniswapPair.js");
const { ETH_ADDRESS, WEB3_INFURA_PROJECT_ID } = process.env;
const wsUrl = `wss://mainnet.infura.io/ws/v3/${WEB3_INFURA_PROJECT_ID}`;
const {
providers: { WebsocketProvider },
} = Web3;
const wsOptions = {
reconnect: {
auto: true,
delay: 5000,
maxAttempts: 5,
onTimeout: false,
},
};
const wsProvider = new WebsocketProvider(wsUrl, wsOptions);
const web3 = new Web3(wsProvider);
const {
eth: { Contract, getBlockNumber, getTransaction },
} = web3;
const { red, yellow, green, magenta, cyan } = colors;
const colorFuncs = [red, yellow, green, magenta, cyan];
const BLOCKS_TO_CHECK = 500_000;
let currentBlockNumber = BLOCKS_TO_CHECK;
let amount0 = 0;
let amount1 = 0;
let balance0 = 0;
let balance1 = 0;
let liquidityBalance = 0;
let mintAmount0 = 0;
let mintAmount1 = 0;
let totalSupply = 0;
const go = async () => {
for (const _pair of pairs) {
const { pair: pairAddress, tokenA, tokenB } = _pair;
const pairContract = new Contract(uniswapPairAbi, pairAddress);
const tokenAContract = new Contract(erc20Abi, tokenA);
const tokenBContract = new Contract(erc20Abi, tokenB);
const mintEvents = await pairContract.getPastEvents("Mint", {
fromBlock: currentBlockNumber - BLOCKS_TO_CHECK,
});
if (!mintEvents || !mintEvents.length) {
console.log("nothing here boss");
return;
}
let userHasPosition = false;
const lastEventIndex = mintEvents.length - 1;
let currentEvent = 0;
console.log(colors.white("=== SCANNING FOR MINTS ==="));
for (const item of mintEvents) {
const percentDone = (currentEvent / lastEventIndex) * 100;
if (currentEvent % 7 === 0) {
const colorIndex =
currentEvent === 0 ? 0 : (currentEvent / 7) % colorFuncs.length;
const colorFunc = colorFuncs[colorIndex];
const formattedPercentDone = `${percentDone.toFixed(2)}%`;
console.log(`${colorFunc(formattedPercentDone)}`);
}
const { returnValues, transactionHash } = item || {};
const transaction = await getTransaction(transactionHash);
const { from } = transaction || {};
if (from === ETH_ADDRESS) {
userHasPosition = true;
const { amount0: _amount0, amount1: _amount1 } = returnValues || {};
const mintAmount0Before = mintAmount0;
const mintAmount1Before = mintAmount1;
mintAmount0 += parseInt(_amount0, 10);
mintAmount1 += parseInt(_amount1, 10);
console.log({
mintAmount0Before,
mintAmount1Before,
mintAmount0After: mintAmount0,
mintAmount1After: mintAmount1,
});
}
currentEvent++;
}
if (!userHasPosition) {
console.log("no slp mint events found");
return;
}
const burnEvents =
(await pairContract.getPastEvents("Burn", {
fromBlock: currentBlockNumber - BLOCKS_TO_CHECK,
})) || [];
const lastBurnEventIndex = burnEvents.length - 1;
let currentBurnEvent = 0;
console.log(colors.white("=== SCANNING FOR BURNS ==="));
for (const item of burnEvents) {
const percentDone = (currentBurnEvent / lastBurnEventIndex) * 100;
if (currentBurnEvent % 7 === 0) {
const colorIndex =
currentBurnEvent === 0
? 0
: (currentBurnEvent / 7) % colorFuncs.length;
const colorFunc = colorFuncs[colorIndex];
const formattedPercentDone = `${percentDone.toFixed(2)}%`;
console.log(`${colorFunc(formattedPercentDone)}`);
}
const { returnValues, transactionHash } = item || {};
const transaction = await getTransaction(transactionHash);
const { from } = transaction || {};
if (from === ETH_ADDRESS) {
const { amount0: _amount0, amount1: _amount1 } = returnValues || {};
const burnAmount0 = parseInt(_amount0, 10);
const burnAmount1 = parseInt(_amount1, 10);
mintAmount0 -= burnAmount0;
mintAmount1 -= burnAmount1;
console.log({
burnAmount0,
burnAmount1,
mintAmount0After: mintAmount0,
mintAmount1After: mintAmount1,
});
}
currentBurnEvent++;
}
const _liquidityBalance = await pairContract.methods
.balanceOf(ETH_ADDRESS)
.call();
liquidityBalance = parseInt(_liquidityBalance, 10);
const _balance0 = await tokenAContract.methods
.balanceOf(pairAddress)
.call();
balance0 = parseInt(_balance0, 10);
const _balance1 = await tokenBContract.methods
.balanceOf(pairAddress)
.call();
balance1 = parseInt(_balance1, 10);
const _totalSupply = await pairContract.methods.totalSupply().call();
totalSupply = parseInt(_totalSupply, 10);
// const amount0Decimals = decimals[tokenA];
// const amount1Decimals = decimals[tokenB];
amount0 = totalSupply > 0 ? (liquidityBalance * balance0) / totalSupply : 0;
amount1 = totalSupply > 0 ? (liquidityBalance * balance1) / totalSupply : 0;
const tokenASymbol =
(tokenAddresses[tokenA] && tokenAddresses[tokenA].symbol) || tokenA;
const tokenBSymbol =
(tokenAddresses[tokenB] && tokenAddresses[tokenB].symbol) || tokenB;
const amount0Change = amount0 - mintAmount0;
const amount0PercentChange = (amount0Change / mintAmount0) * 100;
const formattedAmount0PercentChange = `${amount0PercentChange.toFixed(2)}%`;
const amount1Change = amount1 - mintAmount1;
const amount1PercentChange = (amount1Change / mintAmount1) * 100;
const formattedAmount1PercentChange = `${amount1PercentChange.toFixed(2)}%`;
const theDeal = {
[tokenASymbol]: {
initialAmount: mintAmount0,
currentAmount: amount0,
change: amount0Change,
percentChange: formattedAmount0PercentChange,
},
[tokenBSymbol]: {
initialAmount: mintAmount1,
currentAmount: amount1,
change: amount1Change,
percentChange: formattedAmount1PercentChange,
},
liquidityBalance,
};
console.log(theDeal);
}
};
// ===========================================
// ==== the party ============================
// ===========================================
console.log({ ETH_ADDRESS });
getBlockNumber().then(async (res) => {
if (typeof res === "number") {
currentBlockNumber = res;
await go();
process.exit(0);
} else {
console.log("woops couldn't make it happen");
}
});