arthurneuron commited on
Commit
3ee6dcb
1 Parent(s): ff5b5d1

Create download.py

Browse files
Files changed (1) hide show
  1. download.py +107 -0
download.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ import numpy as np
4
+ import pandas as pd
5
+ import os
6
+
7
+ import nest_asyncio
8
+ nest_asyncio.apply()
9
+
10
+ import asyncio
11
+ import aiohttp
12
+
13
+ query = """query {
14
+ pool(id: "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640") {
15
+ createdAtBlockNumber
16
+ token0 {
17
+ symbol
18
+ }
19
+ token1 {
20
+ symbol
21
+ }
22
+ }
23
+ }"""
24
+
25
+ url = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3'
26
+ r = requests.post(url, json={'query': query})
27
+
28
+ if r.status_code == 200:
29
+ json_data = json.loads(r.text)
30
+
31
+ created_at_block_number = json_data['data']['pool']['createdAtBlockNumber']
32
+ file_path = 'swaps.csv'
33
+ final_line = None
34
+ errors_count = 0
35
+ plus = 10000
36
+
37
+ if os.path.exists(file_path):
38
+ with open(file_path, "r", encoding="utf-8", errors="ignore") as scraped:
39
+ final_line = scraped.readlines()[-1]
40
+
41
+ if final_line is not None:
42
+ final_block = int(final_line.split(',')[0]) + 1
43
+ else:
44
+ final_block = created_at_block_number
45
+
46
+ arr = np.empty((0,5), int)
47
+
48
+ async def fetch(session, number):
49
+ global arr, errors_count
50
+
51
+ block_number = number + final_block
52
+
53
+ query = """query {
54
+ pool(id: "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", block: {number: %d}) {
55
+ volumeToken0
56
+ volumeToken1
57
+ txCount
58
+ token0Price
59
+ }
60
+ }""" % (block_number)
61
+
62
+ url = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3'
63
+ try:
64
+ async with session.post(url, json={'query': query}) as resp:
65
+ if resp.status == 200:
66
+ if block_number % 10000 == 0:
67
+ print(f"{block_number:,}", "/", f"{int((final_block + plus) / 10000) * 10000:,}")
68
+ j = await resp.json()
69
+ if 'data' in j:
70
+ arr = np.append(arr, np.array([[
71
+ block_number,
72
+ float(j['data']['pool']['volumeToken0']),
73
+ float(j['data']['pool']['volumeToken1']),
74
+ int(j['data']['pool']['txCount']),
75
+ float(j['data']['pool']['token0Price'])
76
+ ]]), axis=0)
77
+ return 1
78
+ else:
79
+ print(block_number, resp.status)
80
+ return 0
81
+ except asyncio.TimeoutError:
82
+ if errors_count % 10000 == 0:
83
+ print(f"{errors_count:,}", "errors")
84
+ errors_count += 1
85
+ return 0
86
+
87
+ async def fetch_concurrent(p):
88
+ loop = asyncio.get_event_loop()
89
+ async with aiohttp.ClientSession() as session:
90
+ tasks = []
91
+ for number in range(p):
92
+ tasks.append(loop.create_task(fetch(session, number)))
93
+ await asyncio.gather(*tasks)
94
+
95
+ asyncio.run(fetch_concurrent(plus))
96
+
97
+ pandas_data = pd.DataFrame(arr, columns=[
98
+ 'Block',
99
+ json_data['data']['pool']['token0']['symbol'],
100
+ json_data['data']['pool']['token1']['symbol'],
101
+ 'Transactions',
102
+ 'Price'
103
+ ]).sort_values(by=['Block'], ignore_index=True)
104
+
105
+ pandas_data = pandas_data.astype({'Block': 'int', 'Transactions': 'int'})
106
+
107
+ pandas_data.to_csv(file_path, mode='a', index=False, header=not os.path.exists(file_path))