forked from 0LNetworkCommunity/scorpions-claw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphClient.py
108 lines (98 loc) · 3.95 KB
/
GraphClient.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
# GraphClient.py
from QueryParams import params
from neo4j import GraphDatabase
import json
class Neo4jClient:
def __init__(self, uri, username, password):
self.uri = uri
self.username = username
self.password = password
self.driver = None
def connect(self):
self.driver = GraphDatabase.driver(self.uri, auth=(self.username, self.password))
def close(self):
if self.driver:
self.driver.close()
def execute_query(self, cypher_query, parameters=None):
with self.driver.session() as session:
result = session.run(cypher_query, parameters)
result_data = [dict(record["wallet"]) for record in result if record["wallet"]]
return result_data
def get_sanity(self):
with open('queries/sanity.cql', 'r') as file:
cypher_query = file.read()
with self.driver.session() as session:
result = session.run(cypher_query)
for record in result:
print(record["addr"])
return result
def get_balances(self):
with open('queries/balances.cql', 'r') as file:
cypher_query = file.read()
with self.driver.session() as session:
result = session.run(cypher_query)
balances = []
for record in result:
balance_data = {
"address": record["address"],
"balance": record["balance"],
"locked": record["locked"],
"cw": record["community_wallet"],
}
balances.append(balance_data)
with open('balances.json', 'w') as json_file:
json.dump(balances, json_file, indent=2)
return result
def get_root_sprayers(self):
with open('queries/root_sprayers.cql', 'r') as file:
cypher_query = file.read()
with self.driver.session() as session:
result = session.run(cypher_query)
balances = []
for record in result:
balance_data = {
"address": record["address"],
"destinations": record["destinations"],
"total_out": record["total_out"], #don't scale, the query already does it
}
balances.append(balance_data)
with open('root_sprayers.json', 'w') as json_file:
json.dump(balances, json_file, indent=2)
return result
def get_spray_tree(self):
with open('queries/spray_tree.cql', 'r') as file:
cypher_query = file.read()
with self.driver.session() as session:
params = { "root_sprayer": "0xbd6323842b5dc76e178ae7eaeacce7f" }
result = session.run(cypher_query, params)
balances = []
for record in result:
balance_data = {
"root" : params["root_sprayer"],
"address": record["address"]
}
balances.append(balance_data)
with open('spray_tree.json', 'w') as json_file:
json.dump(balances, json_file, indent=2)
return result
def get_spray_tree_with_balances(self, root_sprayer_literal):
with open('queries/spray_tree_with_balances.cql', 'r') as file:
cypher_query = file.read()
with self.driver.session() as session:
params = { "root_sprayer": root_sprayer_literal}
result = session.run(cypher_query, params)
balances = []
total_sum = 0
for record in result:
balance_data = {
"root" : params["root_sprayer"],
"address": record["address"],
"balance": record["balance"],
"locked": record["locked"]
}
total_sum = total_sum + record["balance"]
balances.append(balance_data)
print("total balance in tree {:,}".format(total_sum))
with open('spray_tree_with_balances.json', 'w') as json_file:
json.dump(balances, json_file, indent=2)
return result