-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0_dune_grabber.py
173 lines (138 loc) · 5.29 KB
/
0_dune_grabber.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
# %%
import os
import json
import time
from dotenv import load_dotenv
from dune_client.types import QueryParameter
from dune_client.client import DuneClient
from dune_client.query import Query
def dune_query(id, **kwargs):
print(f'Execution started for {id}!')
dune = DuneClient(
api_key=API_KEY
)
dune.DEFAULT_TIMEOUT = 300
params = []
if len(kwargs)!=0:
for param in kwargs.keys():
params.append(
QueryParameter.text_type(
name=param, value=kwargs[param]
)
)
query = Query(
query_id=id,
params=params,
)
response = dune.execute(query)
target = 'ExecutionState.COMPLETED'
while str(dune.get_status(response.execution_id)) != target:
time.sleep(1)
print(f'Execution completed for {id}!')
dune.get_result(response.execution_id)
result = dune.get_result(response.execution_id).result.rows
return result
timestamp = int(time.time())
print(f"::set-output name=timestamp::{timestamp}")
load_dotenv () # load_dotenv('./Private/.env')
API_KEY = os.getenv('DUNE_API_KEY')
with open('./configs/configs.json', 'r') as file:
configs = json.load(file)
# Querying Active Addresses on the network
limit = configs['Address_Limit'] + 1
cooldown = configs['Dune_Cooldown']
dune_query_id = 3745025
addresses = dune_query(dune_query_id, limit=limit)
with open('./assets/Addresses.csv', 'w') as file:
content = '\n'.join(map(
lambda x: x.get('address'),
addresses
))
file.write(content)
time.sleep(cooldown)
# Querying New vs Returning Wallets
dune_query_id=3809972
users = dune_query(dune_query_id)
time.sleep(cooldown)
# Querying Users active days - Used Contracts Overlap
dune_query_id=3724566
overlap = dune_query(dune_query_id)
contracts = {}
for column in overlap:
for key, value in column.items():
if key != 'days_category':
if key in contracts:
contracts[key] += value
else:
contracts[key] = value
for row in overlap:
# Calculate the sum of contract values
total_sum = sum(value for key, value in row.items() if key != 'days_category')
# Add the "Sum" key to the dictionary
row['Sum'] = total_sum
# %%
# -----------------------------------------------------------------------------
# Save users data
# -----------------------------------------------------------------------------
users_new_schema = {
"error": None,
"data": {
"rows": users
},
"status": "ok"
}
users_json_output = json.dumps(users_new_schema, indent=4)
with open('./assets/Users.json', 'w') as json_file:
json_file.write(users_json_output)
markdown_table = "| Date | All TX Fee | Cumulative New Users | Returning Users | Total Active Users | Total New Users | TXs |\n"
markdown_table += "|------|------------|----------------------|-----------------|--------------------|-----------------|-----|\n"
for entry in users:
row = f"| {entry['date_time']} | {entry['all_tx_fee']} | {entry['cumulative_new_users']} | {entry['returning_users']} | {entry['total_active_users']} | {entry['total_new_users']} | {entry['txs']} |\n"
markdown_table += row
with open('./markdown/Users.md', 'w') as f:
f.write(markdown_table)
# -----------------------------------------------------------------------------
# Save overlap data
# -----------------------------------------------------------------------------
overlap_new_schema = {
"error": None,
"data": {
"rows": overlap
},
"status": "ok"
}
overlap_json_output = json.dumps(overlap_new_schema, indent=4)
with open('./assets/Overlap.json', 'w') as json_file:
json_file.write(overlap_json_output)
markdown_table = "| Days Category | 01 contract | 02 contracts | 03-05 contracts | 06-10 contracts | 11-20 contracts | 21-50 contracts | 51-100 contracts | Over 100 contracts | Sum |\n"
markdown_table += "|---------------|-------------|--------------|-----------------|-----------------|-----------------|-----------------|------------------|--------------------|-------|\n"
for entry in overlap:
row = f"| {entry['days_category']} | {entry['01 contract']} | {entry['02 contracts']} | {entry['03-05 contracts']} | {entry['06-10 contracts']} | {entry['11-20 contracts']} | {entry['21-50 contracts']} | {entry['51-100 contracts']} | {entry['Over 100 contracts']} | {entry['Sum']} |\n"
markdown_table += row
with open('./markdown/Overlap.md', 'w') as f:
f.write(markdown_table)
# -----------------------------------------------------------------------------
# Save contracts data
# -----------------------------------------------------------------------------
rows = [
{
"Used Contracts": key, "Addresses": value
} for key, value in contracts.items()
]
contracts_new_schema = {
"error": None,
"data": {
"rows": rows
},
"status": "ok"
}
contracts_json_output = json.dumps(contracts_new_schema, indent=4)
with open('./assets/Contracts.json', 'w') as json_file:
json_file.write(contracts_json_output)
markdown_table = "| Contracts | Count |\n"
markdown_table += "|--------------------|---------|\n"
for contract, count in contracts.items():
markdown_table += f"| {contract} | {count} |\n"
with open('./markdown/Contracts.md', 'w') as f:
f.write(markdown_table)
# %%