forked from ccyanxyz/cycle-arb-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_lp_stats.py
95 lines (88 loc) · 4.94 KB
/
get_lp_stats.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
import json
from web3 import Web3
from datetime import datetime
from web3.providers.rpc import HTTPProvider
from web3.providers.ipc import IPCProvider
from web3 import WebsocketProvider
sync_topic = "0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1"
swap_topic = "0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822"
mint_topic = "0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f"
burn_topic = "0xdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496"
w3 = Web3()
pairABI = json.load(open('abi/IUniswapV2Pair.json'))['abi']
erc20abi = json.load(open('abi/erc20.abi'))
c = w3.eth.contract(abi=pairABI)
erc20 = w3.eth.contract(abi=erc20abi)
ts = json.load(open('data/ts.json'))
def to_dict(pairs):
d = {}
for i in range(len(pairs)):
d[pairs[i]['id']] = i
return d
def to_log_receipt(l):
l['blockHash'] = bytes.fromhex(l['blockHash'][2:])
l['blockNumber'] = int(l['blockNumber'], 16)
l['transactionIndex'] = int(l['transactionIndex'], 16)
l['transactionHash'] = bytes.fromhex(l['transactionHash'][2:])
l['logIndex'] = int(l['logIndex'], 16)
l['address'] = w3.toChecksumAddress(l['address'])
l['topics'] = [bytes.fromhex(t[2:]) for t in l['topics']]
l['data'] = bytes.fromhex(l['data'][2:])
return l
def to_tx_receipt(r):
r['blockHash'] = None if not r['blockHash'] else bytes.fromhex(r['blockHash'][2:])
r['blockNumber'] = None if not r['blockNumber'] else int(r['blockNumber'], 16)
r['transactionIndex'] = None if not r['transactionIndex'] else int(r['transactionIndex'], 16)
r['transactionHash'] = bytes.fromhex(r['transactionHash'][2:])
r['cumulativeGasUsed'] = int(r['cumulativeGasUsed'], 16)
r['status'] = int(r['status'], 16)
r['gasUsed'] = int(r['gasUsed'], 16)
r['contractAddress'] = None if not r['contractAddress'] else w3.toChecksumAddress(r['contractAddress'])
r['logs'] = [to_log_receipt(t) for t in r['logs']]
r['logsBloom'] = bytes.fromhex(r['logsBloom'][2:])
r['from'] = None if not r['from'] else w3.toChecksumAddress(r['from'])
r['to'] = w3.toChecksumAddress(r['to'])
return r
f = open('/data/receipts_export_new')
stats = {'0xe2aab7232a9545f29112f9e6441661fd6eeb0a5d' : {}, '0x97524f602706cdb64f9dfa71909ace06e98200b6' : {}, '0xaf996125e98b5804c00ffdb4f7ff386307c99a00' : {}, '0x1273ad5d8f3596a7a39efdb5a4b8f82e8f003fc3' : {}, '0x724d5c9c618a2152e99a45649a3b8cf198321f46' : {}, '0x6deb633e4441b8879aff48caa6e021e919ddbb0c' : {}}
k = 0
for line in f:
info = json.loads(line)
receipt = info['receipt']
bn = int(info['receipt']['blockNumber'], 16)
print(bn)
t = ts[str(bn)]
d = datetime.utcfromtimestamp(t).strftime('%Y-%m-%d')
for pair in stats.keys():
if d not in stats[pair].keys():
stats[pair][d] = 0
for i in range(len(receipt['logs'])):
if not len(receipt['logs'][i]['topics']):
continue
if receipt['logs'][i]['topics'][0] == mint_topic:
addr = str.lower(w3.toChecksumAddress(receipt['logs'][i]['address']))
if addr in stats.keys():
for j in reversed(range(i)):
if str.lower(w3.toChecksumAddress(receipt['logs'][i]['address'])) != addr:
continue
if len(receipt['logs'][j]['topics']) and receipt['logs'][j]['topics'][0] != "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef":
continue
l = to_log_receipt(receipt['logs'][j].copy())
event = c.events.Transfer().processLog(l)
if 'from' in event['args'].keys() and event['args']['from'] == '0x0000000000000000000000000000000000000000' and event['args']['to'] != '0x0000000000000000000000000000000000000000':
stats[addr][d] += event['args']['value']
break
if receipt['logs'][i]['topics'][0] == burn_topic:
addr = str.lower(w3.toChecksumAddress(receipt['logs'][i]['address']))
if addr in stats.keys():
for j in reversed(range(i)):
if str.lower(w3.toChecksumAddress(receipt['logs'][i]['address'])) != addr:
continue
if len(receipt['logs'][j]['topics']) and receipt['logs'][j]['topics'][0] != "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef":
continue
l = to_log_receipt(receipt['logs'][j].copy())
event = c.events.Transfer().processLog(l)
if 'from' in event['args'].keys() and event['args']['from'] != '0x0000000000000000000000000000000000000000' and event['args']['to'] == '0x0000000000000000000000000000000000000000':
stats[addr][d] = stats[addr][d] - event['args']['value']
break
json.dump(stats, open('data/lp_stats.json', 'w'))