-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
278 lines (249 loc) · 9.85 KB
/
main.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
import argparse
import logging
from alive_progress import alive_bar
from gql import Client
from gql.transport.requests import RequestsHTTPTransport
from gql.transport.exceptions import TransportQueryError
from gql.transport.requests import log as requests_logger
from pony import orm
from queries import query_pairs_init, query_pairs
from queries import query_transactions_init, query_transactions
from models import init_db
from models import Token, Pair, PairSnapshot
from models import Transaction, Mint, Burn, Swap
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s.%(msecs)03d %(levelname)s %(module)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
requests_logger.setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
url = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2"
transport = RequestsHTTPTransport(url=url)
client = Client(transport=transport, fetch_schema_from_transport=True)
@orm.db_session
def get_token(data):
token_id = str(data["id"])
token = Token.get(id=token_id)
if token is None:
token = Token(
id=token_id,
symbol=data["symbol"],
name=data["name"],
)
orm.commit()
return token
@orm.db_session
def get_pair(data):
pair_id = str(data["id"])
pair = Pair.get(id=pair_id)
if pair is None:
token0 = get_token(data["token0"])
token1 = get_token(data["token1"])
pair = Pair(
id=pair_id,
token0=token0,
token1=token1,
)
orm.commit()
return pair
def get_pair_snapshots(from_block, to_block):
# get the total number of snapshots
total = 0
skip_id = ""
while True:
query = query_pairs_init(skip_id, from_block, to_block)
try:
response = client.execute(query)
except TransportQueryError as e:
logger.error(e)
continue
data = response.get("pairs", [])
if len(data) == 0:
break
total += len(data)
skip_id = data[-1]["id"]
logger.info(f"total number of pair snapshots: {total}")
# crawl snapshot data
with alive_bar(manual=True) as bar:
count = 0
skip_id = ""
while True:
query = query_pairs(skip_id, from_block, to_block)
try:
response = client.execute(query)
except TransportQueryError as e:
logger.error(e)
continue
data = response.get("pairs", [])
if len(data) == 0:
break
logger.debug(f"fetching pair snapshots {data[0]['id']}")
for _data in data:
with orm.db_session:
snapshot_id = str(_data["id"]) + str(_data["createdAtBlockNumber"])
snapshot = PairSnapshot.get(id=snapshot_id)
if snapshot is None:
snapshot = PairSnapshot(
id=snapshot_id,
pair=get_pair(_data),
token0Price=_data["token0Price"],
token1Price=_data["token1Price"],
reserve0=_data["reserve0"],
reserve1=_data["reserve1"],
totalSupply=_data["totalSupply"],
reserveETH=_data["reserveETH"],
reserveUSD=_data["reserveUSD"],
trackedReserveETH=_data["trackedReserveETH"],
volumeToken0=_data["volumeToken0"],
volumeToken1=_data["volumeToken1"],
volumeUSD=_data["volumeUSD"],
untrackedVolumeUSD=_data["untrackedVolumeUSD"],
txCount=_data["txCount"],
createdAtTimestamp=_data["createdAtTimestamp"],
createdAtBlockNumber=_data["createdAtBlockNumber"],
liquidityProviderCount=_data["liquidityProviderCount"],
)
orm.commit()
count += len(data)
skip_id = data[-1]["id"]
bar(count / total)
def get_transactions(from_block, to_block):
# get the total number of transactions
total = 0
skip_id = ""
while True:
query = query_transactions_init(skip_id, from_block, to_block)
try:
response = client.execute(query)
except TransportQueryError as e:
logger.error(e)
continue
data = response.get("transactions", [])
if len(data) == 0:
break
total += len(data)
skip_id = data[-1]["id"]
logger.info(f"total number of transactions: {total}")
# crawl transaction data
with alive_bar(manual=True) as bar:
skip_id = ""
count = 0
while True:
query = query_transactions(skip_id, from_block, to_block)
try:
response = client.execute(query)
except TransportQueryError as e:
logger.error(e)
continue
data = response.get("transactions", [])
if len(data) == 0:
break
logger.debug(f"fetching transactions {data[0]['id']}")
for _data in data:
with orm.db_session:
tx_id = str(_data["id"])
tx = Transaction.get(id=tx_id)
if tx is None:
tx = Transaction(
id=tx_id,
blockNumber=_data["blockNumber"],
timestamp=_data["timestamp"],
)
mints = []
for _mint in _data["mints"]:
mint_id = str(_mint["id"])
mint = Mint.get(id=mint_id)
if mint is None:
mint = Mint(
id=mint_id,
transaction=tx,
timestamp=_mint["timestamp"],
pair=get_pair(_mint["pair"]),
sender=_mint["sender"],
to=_mint["to"],
feeTo=_mint["feeTo"],
liquidity=_mint["liquidity"],
feeLiquidity=_mint["feeLiquidity"],
amount0=_mint["amount0"],
amount1=_mint["amount1"],
amountUSD=_mint["amountUSD"],
logIndex=_mint["logIndex"],
)
mints.append(mint)
burns = []
for _burn in _data["burns"]:
burn_id = str(_burn["id"])
burn = Burn.get(id=burn_id)
if burn is None:
burn = Burn(
id=burn_id,
transaction=tx,
timestamp=_burn["timestamp"],
pair=get_pair(_burn["pair"]),
sender=_burn["sender"],
to=_burn["to"],
feeTo=_burn["feeTo"],
liquidity=_burn["liquidity"],
feeLiquidity=_burn["feeLiquidity"],
amount0=_burn["amount0"],
amount1=_burn["amount1"],
amountUSD=_burn["amountUSD"],
needsComplete=_burn["needsComplete"],
logIndex=_burn["logIndex"],
)
burns.append(burn)
swaps = []
for _swap in _data["swaps"]:
swap_id = str(_swap["id"])
swap = Swap.get(id=swap_id)
if swap is None:
swap = Swap(
id=swap_id,
transaction=tx,
timestamp=_swap["timestamp"],
pair=get_pair(_swap["pair"]),
sender=_swap["sender"],
to=_swap["to"],
amount0In=_swap["amount0In"],
amount1In=_swap["amount1In"],
amount0Out=_swap["amount0Out"],
amount1Out=_swap["amount1Out"],
amountUSD=_swap["amountUSD"],
logIndex=_swap["logIndex"],
)
swaps.append(swap)
orm.commit()
count += len(data)
skip_id = data[-1]["id"]
bar(count / total)
def main(args):
get_pair_snapshots(args.from_block, args.to_block)
get_transactions(args.from_block, args.to_block)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--database",
type=str,
default="./database.sqlite",
help="path for the SQLite database file",
)
parser.add_argument(
"--from_block",
type=int,
default=14960000,
help="blocknumber of the first block to crawl",
)
parser.add_argument(
"--to_block",
type=int,
default=14968000,
help="blocknumber of the last block to crawl",
)
args = parser.parse_args()
logger.info("initializing SQLite database")
init_db(args)
logger.info(
f"begin crawling Uniswap V2 data from block {args.from_block} to {args.to_block}"
)
main(args)