-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_pair_created_transactions.py
102 lines (79 loc) · 3.55 KB
/
get_pair_created_transactions.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
"""
For all pairs, get the transaction that created it
"""
from web3 import Web3
import time
from tools import robust_function_call, load_json, save_json
import numpy as np
from fire import Fire
from web3.middleware import geth_poa_middleware
def run(config):
config = load_json(config)
w3 = Web3(Web3.HTTPProvider(config["RPC"]))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
interval = config["interval"]
blockrange_swaps = config["blockrange_swaps"]
# get the n most recent pairs on the chain if specified
if not ("n_latest_pairs" in config):
n_latest = np.infty
elif config["n_latest_pairs"] is None:
n_latest = np.infty
else:
n_latest = config["n_latest_pairs"]
# get the most recent block if no until_block is specified
if not ("until_block" in config):
until_block = w3.eth.get_block('latest')["number"]
elif config["until_block"] == None:
until_block = w3.eth.get_block('latest')["number"]
else:
until_block = config["until_block"]
exchanges = config["exchanges"]
abi_router = load_json("abis/router.json")
exchange_to_transactions = dict()
for name in exchanges.keys():
print(f"exchange:{name}")
# get relevant contracts, and addresses for filtering the blockchain
address_router = Web3.toChecksumAddress(exchanges[name])
contract_router = w3.eth.contract(address=address_router, abi=abi_router)
address_factory = contract_router.functions.factory().call()
transactions_serialisable = []
i = 0
finished = False
while not finished:
now = time.time()
# define interval borders
left = until_block - (i+1)*interval +1 - blockrange_swaps
right = until_block - i*interval - blockrange_swaps
# when we arrive at block 0 we are done
if left <= 0:
finished = True
left =0
# create function and call it with robust_function_call to retry on errors
def get_entries():
w3 = Web3(Web3.HTTPProvider(config["RPC"]))
event_filter = w3.eth.filter(filter_params={"fromBlock": left, "toBlock": right, "address": address_factory})
return event_filter.get_all_entries()
transactions = robust_function_call(get_entries)[::-1]
later = time.time()
print(i*interval, " entries take ", later-now, " and yields #", len(transactions), " relevant transactions")
print(i*interval/until_block)
# increase i for the next step
i+=1
# get the data we are interested in
for tx in transactions:
attributes = dict()
attributes["tokenpair"] = "0x" + tx["data"][26:66]
attributes["tx_hash"] = tx["transactionHash"].hex()
attributes["blockHash"] = tx["blockHash"].hex()
attributes["blockNumber"] = tx["blockNumber"]
transactions_serialisable.append(attributes)
print(len(transactions_serialisable))
# go to the next exchange as soon as we have enough pairs
if len(transactions_serialisable) >= n_latest:
finished=True
# add all the data for the exchange in the dictionary
exchange_to_transactions[name] = transactions_serialisable[::-1]
# save dictionary
save_json(exchange_to_transactions, f"data/{config['name']}/pair_created_transactions.json")
if __name__ == "__main__":
Fire(run)