-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmina_voting_query.py
217 lines (188 loc) · 6.76 KB
/
mina_voting_query.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
import os
import sys
import json
import pathlib
import requests
import mina_voting_constants as mvc
def pp(resp):
return json.dumps(resp, indent=4)
def get_next_staking_ledger_granola_github_auth(ep: int, ledger_hash: str, repo: str):
'''
### Requires github auth token
Request the next staking ledger via hash from Granola's `mina-ledger` GitHub repo
Write to local file {mvc.local_data_dir(ep) / ledger_hash}.json
'''
if not mvc.GITHUB_AUTH_TOKEN.exists():
print(f"You must copy a github auth token to file {mvc.GITHUB_AUTH_TOKEN}")
sys.exit(1)
with mvc.GITHUB_AUTH_TOKEN.open("r", encoding="utf-8") as f:
auth_token = f.read().strip()
f.close()
print(f"Fetching: Granola-Team/mina-ledger/main/mainnet/{ledger_hash}.json")
os.system(f'curl -H "Accept: application/vnd.github.v4.raw" \
-H "Authorization: bearer {auth_token}" \
"{mvc.LEDGER_SOURCES["granola"]}/{ledger_hash}.json" > {mvc.ledger_loc(ep, ledger_hash)}')
def get_all_sources(ep, ledger_hash):
for src in mvc.LEDGER_SOURCES:
get_next_staking_ledger(ep, ledger_hash, src, True)
def diff(fpath1: pathlib.Path, fpath2: pathlib.Path):
output = os.system(f'diff {fpath1} {fpath2}')
if output:
print(output)
sys.exit(1)
def get_next_staking_ledger(ep: int, ledger_hash: str, src: str, extra = False):
'''
Get the next staking ledger via `wget`
'''
assert src in mvc.LEDGER_SOURCES
target = mvc.ledger_loc(ep, ledger_hash)
if extra:
src_dir = mvc.LEDGERS_DIR / src
if not src_dir.exists():
if not mvc.LEDGERS_DIR.exists():
os.mkdir(mvc.LEDGERS_DIR)
os.mkdir(src_dir)
target = src_dir / f"{ledger_hash}.json"
if src == "granola":
os.system(f'wget "{mvc.LEDGER_SOURCES["granola"]}/{ledger_hash}.json" -O {target}')
if src == "zkvalidator":
os.system(f'wget "{mvc.LEDGER_SOURCES["zkvalidator"]}/{ledger_hash}.json" -O {target}')
def template_request_gql(query: str, variables: dict = {}, endpoint: str = mvc.MINA_EXPLORER) -> dict:
'''
Template GraphQL request
'''
query = " ".join(query.split())
payload = {"query": query}
if variables:
payload = {**payload, "variables": variables}
headers = {"Accept": "application/json"}
response = requests.post(endpoint, json=payload, headers=headers)
resp_json = response.json()
if response.status_code == 200 and "errors" not in resp_json:
return resp_json
else:
print(response.text)
raise Exception(f"Query failed -- returned code {response.status_code} with response {response.text}")
def get_next_ledger_hash_gql(epoch: int, endpoint: str = mvc.MINA_EXPLORER) -> dict:
'''
Returns ledger hash for supplied epoch
Variables: `epoch`
'''
query = """query($epoch: Int) {
blocks(query: {canonical: true, protocolState: {consensusState: {epoch: $epoch}}}, limit: 1) {
protocolState {
consensusState {
nextEpochData {
ledger {
hash
}
}
epoch
}
}
}
}"""
return template_request_gql(query, {"epoch": epoch}, endpoint)
def get_next_staking_ledger_gql(ledger_hash: str, endpoint: str = mvc.MINA_EXPLORER) -> dict:
'''
Return the staking ledger
Variables: `limit`, `ledgerHash`
'''
query = """query ($ledgerHash: String!, $limit: Int = 1000000) {
nextstakes(query: {ledgerHash: $ledgerHash}, limit: $limit) {
pk
balance
delegate
}
}"""
return template_request_gql(query, {"ledgerHash": ledger_hash}, endpoint)
def get_block_heights(variables, endpoint: str = mvc.MINA_EXPLORER):
'''
Returns the canonical block heights within specified parameters
Variables: `min_date_time`, `max_date_time`
'''
query = """query($min_date_time: DateTime!, $max_date_time: DateTime!) {
blocks(query: {canonical: true, dateTime_gte: $min_date_time, dateTime_lte: $max_date_time}, limit: 7140) {
blockHeight
}
}"""
return template_request_gql(query, variables, endpoint)
def get_payments_in_block_height(variables, endpoint: str = mvc.MINA_EXPLORER):
'''
Returns the PAYMENT transactions in the specified block
Variables: `block_height`, `limit`
'''
query = """query($block_height: Int!, $limit: Int = 1000) {
transactions(query: {blockHeight: $block_height, canonical: true, kind: "PAYMENT"}, sortBy: DATETIME_DESC, limit: $limit) {
blockHeight
memo
nonce
receiver {
publicKey
}
source {
publicKey
}
}
}"""
return template_request_gql(query, variables, endpoint)
def get_transactions(variables, endpoint: str = mvc.MINA_EXPLORER) -> dict:
'''
Returns transactions within specified parameters
Variables: `source`, `receiver`, `kind`, `min_block_height`, `max_block_height`, `min_date_time`, `max_date_time`, `limit`, `block_height`
'''
query = """query($source: String, $receiver: String, $kind: String, $block_height: Int, $min_block_height: Int, $max_block_height: Int, $min_date_time: DateTime, $max_date_time: DateTime, $limit: Int = 1000) {
transactions(query: {source: {publicKey: $source}, receiver: {publicKey: $receiver}, kind: $kind, memo_exists: $memo_exists, canonical: true, blockHeight_gte: $min_block_height, blockHeight_lte: $max_block_height, dateTime_gte: $min_date_time, dateTime_lte: $max_date_time}, sortBy: DATETIME_DESC, limit: $limit) {
memo
source {
publicKey
}
receiver {
publicKey
}
nonce
kind
dateTime
blockHeight
hash
amount
}
}"""
return template_request_gql(query, variables, endpoint)
def get_blocks(variables, endpoint: str = mvc.MINA_EXPLORER) -> dict:
'''
Returns blocks within specified parameters
Variables: `creator`, `epoch`, `min_block_height`, `max_block_height`, `min_date_time`, `max_date_time`, `limit`
'''
query = """query($creator: String, $epoch: Int, $min_block_height: Int, $max_block_height: Int, $min_date_time: DateTime, $max_date_time: DateTime, $limit: Int = 10) {
blocks(query: {creator: $creator, protocolState: {consensusState: {epoch: $epoch}}, canonical: true, blockHeight_gte: $min_block_height, blockHeight_lte: $max_block_height, dateTime_gte: $min_date_time, dateTime_lte: $max_date_time}, sortBy: DATETIME_DESC, limit: $limit) {
blockHeight
canonical
creator
dateTime
txFees
snarkFees
receivedTime
stateHash
stateHashField
protocolState {
consensusState {
blockHeight
epoch
slotSinceGenesis
}
}
transactions {
coinbase
coinbaseReceiverAccount {
publicKey
}
feeTransfer {
fee
recipient
type
}
}
}
}"""
return template_request_gql(query, variables, endpoint)