-
Notifications
You must be signed in to change notification settings - Fork 3
/
basketuniv3.py
441 lines (386 loc) · 16.1 KB
/
basketuniv3.py
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
from cProfile import label
import json
import asyncio
from unicodedata import decimal
from numpy import place
from concurrent.futures import ProcessPoolExecutor
import streamlit as st
from web3 import Web3
import plotly.express as px
from websockets import connect
from eth_abi import decode_single, decode_abi
import math
import requests
from websockets import connect
import math
from datetime import datetime
import pandas as pd
st.set_page_config(layout="wide")
# 0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45
api = 'https://node-api.flipsidecrypto.com/api/v2/queries/f7a3fd01-4651-4898-846e-f01109f7265c/data/latest'
labels = pd.read_json(api,
convert_dates=["TIMESTAMP_NTZ"],
)
labels = labels.set_index('ADDRESS')
# st.write(labels)
# session = requests.Session()
# w3 = Web3(Web3.WebsocketProvider("wss://mainnet.infura.io/ws/v3/43b2d6f15d164cb4bbe4d4789831f242"))
df_main_tri = pd.DataFrame(columns=['sold_id', 'tokens_sold','sold_name','sold_decimal','tokens_bought','bought_name','bought_decimal','bought_id','timestamp'])
false = False
placeholder1 = st.empty()
placeholder2 = st.empty()
placeholder3 = st.empty()
placeholder4 = st.empty()
placeholder5 = st.empty()
placeholder6 = st.empty()
placeholder000 = st.empty()
placeholder100 = st.empty()
placeholder200 = st.empty()
placeholder300 = st.empty()
placeholder400 = st.empty()
placeholder500 = st.empty()
placeholder600 = st.empty()
placeholder700 = st.empty()
placeholder800 = st.empty()
placeholder900 = st.empty()
placeholder1000 = st.empty()
placeholder1100 = st.empty()
placeholder1200 = st.empty()
df_main_uni = pd.DataFrame(columns=['price', 'timestamp','WETH','USDC', 'side'])
false = False
placeholder00 = st.empty()
placeholder01 = st.empty()
placeholder02 = st.empty()
placeholder03 = st.empty()
placeholder04 = st.empty()
placeholder05 = st.empty()
placeholder06 = st.empty()
placeholder07 = st.empty()
placeholder08 = st.empty()
placeholder09 = st.empty()
placeholder010 = st.empty()
placeholder011 = st.empty()
placeholder012 = st.empty()
df_arbi = pd.DataFrame(columns=['sold_id', 'tokens_sold','sold_name','sold_decimal','tokens_bought','bought_name','bought_decimal','bought_id','timestamp'])
laceholder1 = st.empty()
laceholder2 = st.empty()
laceholder3 = st.empty()
laceholder4 = st.empty()
laceholder5 = st.empty()
laceholder6 = st.empty()
laceholder7 = st.empty()
laceholder8 = st.empty()
laceholder9 = st.empty()
laceholder10 = st.empty()
laceholder11 = st.empty()
laceholder12 = st.empty()
laceholder13 = st.empty()
laceholder14 = st.empty()
laceholder15 = st.empty()
laceholder16 = st.empty()
laceholder17 = st.empty()
laceholder18 = st.empty()
laceholder19 = st.empty()
def get_event_op_uni(message):
lord_jesus = json.loads(message)
lord_jesus = json.dumps(lord_jesus)
lord_jesus = json.loads(lord_jesus)
# with placeholder1:
# st.write(lord_jesus, use_container_width=True)
now = datetime.now()
lord_jesus = lord_jesus["params"]["result"]
fromm = lord_jesus["topics"][1]
fromm = decode_single('address', bytes.fromhex(fromm[2:]))
fromm = str(fromm)
too = lord_jesus["topics"][2]
too = decode_single('address', bytes.fromhex(too[2:]))
too = str(too)
number = lord_jesus["data"][2:]
number = decode_single('(int256,int256,uint160,uint128,int24)',bytearray.fromhex(number))
# with placeholder2:
# st.write(number, use_container_width=True)
liq = number[3]
# liq = str(liq)
tick = number[4]
# tick = str(tick)
prinnt = (number[0]*math.pow(10,12)* -1)/number[1]
if number[0] > 0:
side = "BUY"
else:
side = "SELL"
usdc = abs(number[0]/math.pow(10,6))
usdc_net = (number[0]/math.pow(10,6))
weth = abs(number[1]/math.pow(10,18))
try:
labelz_fromm = labels.loc[fromm]["label"]
except:
labelz_fromm = fromm
try:
labelz_too = labels.loc[too]["label"]
except:
labelz_too = too
d = {'too': too, 'fromm':fromm, 'price': prinnt, 'timestamp': now, 'WETH': weth, 'USDC': usdc, 'side': side, 'USDC_net': usdc_net, 'labelz_fromm': labelz_fromm, 'labelz_too': labelz_too}
fixed_df = pd.DataFrame(d, index=[0])
df = df.append(fixed_df, ignore_index=True)
df['cumsum'] = df['USDC_net'].cumsum()
df['price_impact'] = df['price'].diff(periods=1)
df['price_impact_w_size'] = df['price_impact']/df['USDC']
df['taken_liquidity'] = df['Liq'].diff(periods=1)
df['current_price'] = 1/(((((1.0001) ** (df['Tick'])))/math.pow(10,12)))
df['price_deviation'] = (df['current_price'] - df['price'])
df['price_deviation_w_size'] = df['price_deviation']/df['USDC']
df['tick_change'] = df['Tick'].diff(periods=1)
return df
async def get_event_mainnet_uni():
global df_main_uni
global labels
async with connect("wss://mainnet.infura.io/ws/v3/43b2d6f15d164cb4bbe4d4789831f242") as ws:
global df_main_uni
await ws.send(json.dumps(
{"id": 1, "method": "eth_subscribe", "params":
["logs",
{
"address": "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",
"topics": [
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"
]
}
]
}
)
)
subscription_response = await ws.recv()
while True:
message = await asyncio.wait_for(ws.recv(), timeout=60000)
lord_jesus = json.loads(message)
lord_jesus = json.dumps(lord_jesus)
lord_jesus = json.loads(lord_jesus)
now = datetime.now()
lord_jesus = lord_jesus["params"]["result"]
fromm = lord_jesus["topics"][1]
fromm = decode_single('address', bytes.fromhex(fromm[2:]))
fromm = str(fromm)
too = lord_jesus["topics"][2]
too = decode_single('address', bytes.fromhex(too[2:]))
too = str(too)
number = lord_jesus["data"][2:]
number = decode_single('(int256,int256,uint160,uint128,int24)',bytearray.fromhex(number))
price_swap = number[0]/number[1]*math.pow(10,12)*-1
if number[0] > 0:
side = "BUY"
else:
side = "SELL"
usdc = abs(number[0]/math.pow(10,6))
usdc_net = (number[0]/math.pow(10,6))
weth = abs(number[1]/math.pow(10,18))
d = {'too': too, 'fromm':fromm, 'price': price_swap, 'timestamp': now, 'WETH': weth, 'USDC': usdc, 'side': side, 'USDC_net': usdc_net}
fixed_df = pd.DataFrame(d, index=[0])
df_main_uni = df_main_uni.append(fixed_df, ignore_index=True)
df_main_uni['cumsum'] = df_main_uni['USDC_net'].cumsum()
df_main_uni['price_impact'] = df_main_uni['price'].diff(periods=1)
df_main_uni['price_impact_w_size'] = df_main_uni['price_impact']/df_main_uni['USDC']
with placeholder02:
st.write(df_main_uni, use_container_width=True)
with placeholder04:
st.plotly_chart(px.scatter(df_main_uni, x="timestamp", y="price", size="USDC", color='side',color_discrete_sequence=["green", "red"],title='main'), use_container_width=True)
df1 = df_main_uni.copy()
return df1
# return df_main_uni
# return df_main_uni
# return df_main_uni
session = requests.Session()
w3 = Web3(Web3.WebsocketProvider("wss://mainnet.infura.io/ws/v3/43b2d6f15d164cb4bbe4d4789831f242"))
df_op_uni = pd.DataFrame(columns=['price', 'timestamp','WETH','USDC', 'side'])
false = False
async def get_event_op_uni():
global df_op_uni
global labels
async with connect("wss://ws-mainnet.optimism.io") as ws:
global df_op_uni
await ws.send(json.dumps(
{"id": 1, "method": "eth_subscribe", "params":
["logs",
{
"address": "0x85149247691df622eaF1a8Bd0CaFd40BC45154a9",
"topics": [
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"
]
}
]
}
)
)
subscription_response = await ws.recv()
# with placeholder000:
# st.write(subscription_response, use_container_width=True)
while True:
global labels
message = await asyncio.wait_for(ws.recv(), timeout=60000)
lord_jesus = json.loads(message)
lord_jesus = json.dumps(lord_jesus)
lord_jesus = json.loads(lord_jesus)
# with placeholder1:
# st.write(lord_jesus, use_container_width=True)
now = datetime.now()
lord_jesus = lord_jesus["params"]["result"]
fromm = lord_jesus["topics"][1]
fromm = decode_single('address', bytes.fromhex(fromm[2:]))
fromm = str(fromm)
too = lord_jesus["topics"][2]
too = decode_single('address', bytes.fromhex(too[2:]))
too = str(too)
number = lord_jesus["data"][2:]
number = decode_single('(int256,int256,uint160,uint128,int24)',bytearray.fromhex(number))
price_swap = (number[1]*math.pow(10,12)* -1)/number[0]
if number[0] > 0:
side = "sell"
else:
side = "buy"
usdc = abs(number[1]/math.pow(10,6))
usdc_net = (number[1]/math.pow(10,6))
weth = abs(number[0]/math.pow(10,18))
try:
labelz_fromm = labels.loc[fromm]["label"]
except:
labelz_fromm = 'n/a'
try:
labelz_too = labels.loc[too]["label"]
except:
labelz_too = 'n/a'
d = {'too': too, 'fromm':fromm, 'price': price_swap, 'timestamp': now, 'WETH': weth, 'USDC': usdc, 'side': side, 'USDC_net': usdc_net, 'labelz_fromm': labelz_fromm, 'labelz_too': labelz_too}
fixed_df = pd.DataFrame(d, index=[0])
df_op_uni = df_op_uni.append(fixed_df, ignore_index=True)
df_op_uni['cumsum'] = df_op_uni['USDC_net'].cumsum()
df_op_uni['price_impact'] = df_op_uni['price'].diff(periods=1)
df_op_uni['price_impact_w_size'] = df_op_uni['price_impact']/df_op_uni['USDC']
# df_op_uni['labelz_fromm'] = 'n/a'
# df_op_uni['labelz_fromm'] = labels.loc[fromm]["label"]
# '''make a new collum in df_op_uni called 'labelz_fromm' and 'labelz_too' and fill it with the fromm and too address we do this by matching the too and fromm address with the labels in the labels list and then'''
# '''if the label is null, write n/a'''
# def leblfrom():
# for i in range(len(df_op_uni)):
# try:
# df_op_uni['labelz_fromm'] = labels.loc[fromm]["label"]
# except:
# df_op_uni['labelz_fromm'] = 'n/a'
# # leblfrom()
# df_op_uni['labelz_too'] = df_op_uni['too'].apply(lambda x: labels[x] if x in labels else 'n/a')
# df_op_uni['labelz_fromm'] = df_op_uni['fromm'].map(labels)
# df_op_uni['labelz_too'] = df_op_uni['too'].map(labels)
# df_op_uni['labelz_fromm_too'] = df_op_uni['labelz_fromm'] + df_op_uni['labelz_too']
# df_op_uni['labelz_from'] = label.loc['address']["too"]
with placeholder400:
st.write(df_op_uni, use_container_width=True)
with placeholder600:
st.plotly_chart(px.scatter(df_op_uni, x="timestamp", y="price", size="USDC", color='side',color_discrete_sequence=["green", "red"],title='op'), use_container_width=True)
df2 = df_op_uni.copy()
return df2
# wss://polygon-mainnet.g.alchemy.com/v2/uKtRRMD3RVeY5JrQXLzUbyUF05VeHzcP
session = requests.Session()
w3 = Web3(Web3.WebsocketProvider("wss://polygon-mainnet.g.alchemy.com/v2/uKtRRMD3RVeY5JrQXLzUbyUF05VeHzcP"))
df = pd.DataFrame(columns=['price', 'timestamp','WETH','USDC', 'side'])
false = False
placeholder0 = st.empty()
placeholder1 = st.empty()
placeholder2 = st.empty()
placeholder3 = st.empty()
placeholder4 = st.empty()
placeholder5 = st.empty()
placeholder6 = st.empty()
placeholder7 = st.empty()
placeholder8 = st.empty()
placeholder9 = st.empty()
placeholder10 = st.empty()
placeholder11 = st.empty()
placeholder12 = st.empty()
async def polyuni():
global df
global labels
async with connect("wss://polygon-mainnet.g.alchemy.com/v2/uKtRRMD3RVeY5JrQXLzUbyUF05VeHzcP") as ws:
global df
await ws.send(json.dumps(
{"id": 1, "method": "eth_subscribe", "params":
["logs",
{
"address": "0x45dda9cb7c25131df268515131f647d726f50608",
"topics": [
"0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67"
]
}
]
}
)
)
subscription_response = await ws.recv()
# with placeholder0:
# st.json(subscription_response)
while True:
message = await asyncio.wait_for(ws.recv(), timeout=60000)
lord_jesus = json.loads(message)
lord_jesus = json.dumps(lord_jesus)
lord_jesus = json.loads(lord_jesus)
# with placeholder1:
# st.write(lord_jesus, use_container_width=True)
now = datetime.now()
lord_jesus = lord_jesus["params"]["result"]
fromm = lord_jesus["topics"][1]
fromm = decode_single('address', bytes.fromhex(fromm[2:]))
fromm = str(fromm)
too = lord_jesus["topics"][2]
too = decode_single('address', bytes.fromhex(too[2:]))
too = str(too)
number = lord_jesus["data"][2:]
number = decode_single('(int256,int256,uint160,uint128,int24)',bytearray.fromhex(number))
# with placeholder2:
# st.write(number, use_container_width=True)
Liq = number[3]
# liq = str(liq)
tick = number[4]
# tick = str(tick)
prinnt = (number[0]*math.pow(10,12)* -1)/number[1]
if number[0] > 0:
side = "BUY"
else:
side = "SELL"
usdc = abs(number[0]/math.pow(10,6))
usdc_net = (number[0]/math.pow(10,6))
weth = abs(number[1]/math.pow(10,18))
try:
labelz_fromm = labels.loc[fromm]["label"]
except:
labelz_fromm = 'n/a'
try:
labelz_too = labels.loc[too]["label"]
except:
labelz_too = 'n/a'
d = {'too': too, 'fromm':fromm, 'price': prinnt, 'timestamp': now, 'WETH': weth, 'USDC': usdc, 'side': side, 'USDC_net': usdc_net, 'labelz_fromm': labelz_fromm, 'labelz_too': labelz_too}
fixed_df = pd.DataFrame(d, index=[0])
df = df.append(fixed_df, ignore_index=True)
df['cumsum'] = df['USDC_net'].cumsum()
df['price_impact'] = df['price'].diff(periods=1)
df['price_impact_w_size'] = df['price_impact']/df['USDC']
# df['taken_liquidity'] = df['Liq'].diff(periods=1)
# df['current_price'] = 1/(((((1.0001) ** (df['Tick'])))/math.pow(10,12)))
# df['price_deviation'] = (df['current_price'] - df['price'])
# df['price_deviation_w_size'] = df['price_deviation']/df['USDC']
# df['tick_change'] = df['Tick'].diff(periods=1)
with placeholder3:
st.write(df, use_container_width=True)
# with placeholder3:
# st.plotly_chart(px.line(df, x="timestamp", y="price", color="side"), use_container_width=True)
with placeholder4:
st.plotly_chart(px.scatter(df, x="timestamp", y="price", size="USDC", color='side',color_discrete_sequence=["green", "red"],title='poly'), use_container_width=True)
# with placeholder5:
# st.plotly_chart(px.scatter(df, x="timestamp", y="current_price", color = 'price_deviation') , use_container_width=True)
df3 = df.copy()
return df3
# return df3
# return df3
async def main():
# Schedule three calls *concurrently*:
await asyncio.gather(
get_event_op_uni(),
get_event_mainnet_uni(),
polyuni(),
)
asyncio.run(main())