forked from garethtdavies/mina-payout-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphQL.py
127 lines (111 loc) · 3.19 KB
/
GraphQL.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
import requests
def _graphql_request(query: str, variables: dict = {}):
"""GraphQL queries all look alike, this is a generic function to facilitate a GraphQL Request.
Arguments:
query {str} -- A GraphQL Query
Keyword Arguments:
variables {dict} -- Optional Variables for the GraphQL Query (default: {{}})
Raises:
Exception: Raises an exception if the response is anything other than 200.
Returns:
dict -- Returns the JSON Response as a Dict.
"""
# Strip all the whitespace and replace with spaces
query = " ".join(query.split())
payload = {'query': query}
if variables:
payload = {**payload, 'variables': variables}
headers = {"Accept": "application/json"}
response = requests.post("https://graphql.minaexplorer.com",
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("Query failed -- returned code {}. {}".format(
response.status_code, query))
def getStakingLedger(variables):
"""Return the staking ledger."""
query = '''query($delegate: String!, $ledgerHash: String!){
stakes(query: {delegate: $delegate, ledgerHash: $ledgerHash}, limit: 10000) {
public_key
balance
chainId
timing {
cliff_amount
cliff_time
initial_minimum_balance
timed_epoch_end
timed_in_epoch
timed_weighting
untimed_slot
vesting_increment
vesting_period
}
}
}
'''
return _graphql_request(query, variables)
def getBlocks(variables):
"""Returns all blocks the pool won."""
query = """query($creator: String!, $epoch: Int, $blockHeightMin: Int, $blockHeightMax: Int, $dateTimeMin: DateTime, $dateTimeMax: DateTime){
blocks(query: {creator: $creator, protocolState: {consensusState: {epoch: $epoch}}, canonical: true, blockHeight_gte: $blockHeightMin, blockHeight_lte: $blockHeightMax, dateTime_gte:$dateTimeMin, dateTime_lte:$dateTimeMax}, sortBy: DATETIME_DESC, limit: 1000) {
blockHeight
canonical
creator
dateTime
txFees
snarkFees
receivedTime
stateHash
stateHashField
protocolState {
consensusState {
blockHeight
epoch
slotSinceGenesis
}
}
transactions {
coinbase
coinbaseReceiverAccount {
publicKey
}
feeTransfer {
fee
recipient
type
}
}
}
}
"""
return _graphql_request(query, variables)
def getLedgerHash(epoch: int) -> dict:
query = """query ($epoch: Int) {
blocks(query: {canonical: true, protocolState: {consensusState: {epoch: $epoch}}}, limit: 1) {
protocolState {
consensusState {
stakingEpochData {
ledger {
hash
}
}
epoch
}
}
}
}"""
variables = {
"epoch": epoch
}
return _graphql_request(query, variables)
def getLatestHeight():
query = """{
blocks(query: {canonical: true}, sortBy: DATETIME_DESC, limit: 1) {
blockHeight
}
}"""
return _graphql_request(query)