forked from MinaProtocol/coda-python-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCodaClient.py
728 lines (643 loc) · 20 KB
/
CodaClient.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/usr/bin/python3
import random
import requests
import time
import json
import asyncio
import websockets
import logging
from enum import Enum
class CurrencyFormat(Enum):
"""An Enum representing different formats of Currency in coda.
Constants:
WHOLE - represents whole coda (1 whole coda == 10^9 nanocodas)
NANO - represents the atomic unit of coda
"""
WHOLE = 1
NANO = 2
class CurrencyUnderflow(Exception):
pass
class Currency():
"""A convenience wrapper around interacting with coda currency values.
This class supports performing math on Currency values of differing formats.
Currency instances can be added or subtracted. Currency instances can also be
scaled through multiplication (either against another Currency instance or a
int scalar).
"""
@classmethod
def __nanocodas_from_int(_cls, n):
return n * 1000000000
@classmethod
def __nanocodas_from_string(_cls, s):
segments = s.split('.')
if len(segments) == 1:
return int(segments[0])
elif len(segments) == 2:
[l, r] = segments
if len(r) <= 9:
return int(l + r + ('0' * (9 - len(r))))
else:
raise Exception('invalid coda currency format: %s' % s)
@classmethod
def random(_cls, lower_bound, upper_bound):
"""Generates a random Currency instance between a provided lower_bound and upper_bound
Arguments:
lower_bound {Currency} -- A Currency instance representing the lower bound for the randomly generated value
upper_bound {Currency} -- A Currency instance representing the upper bound for the randomly generated value
Returns:
Currency - A randomly generated Currency instance between the lower_bound and upper_bound
"""
if not (isinstance(lower_bound, Currency) and isinstance(upper_bound, Currency)):
raise Exception('invalid call to Currency.random: lower and upper bound must be instances of Currency')
if not upper_bound.nanocodas() >= lower_bound.nanocodas():
raise Exception('invalid call to Currency.random: upper_bound is not greater than lower_bound')
if lower_bound == upper_bound:
return lower_bound
bound_range = upper_bound.nanocodas() - lower_bound.nanocodas()
delta = random.randint(0, bound_range)
return lower_bound + Currency(delta, format=CurrencyFormat.NANO)
def __init__(self, value, format=CurrencyFormat.WHOLE):
"""Constructs a new Currency instance. Values of different CurrencyFormats may be passed in to construct the instance.
Arguments:
value {int|float|string} - The value to construct the Currency instance from
format {CurrencyFormat} - The representation format of the value
Return:
Currency - The newly constructed Currency instance
In the case of format=CurrencyFormat.WHOLE, then it is interpreted as value * 10^9 nanocodas.
In the case of format=CurrencyFormat.NANO, value is only allowed to be an int, as there can be no decimal point for nanocodas.
"""
if format == CurrencyFormat.WHOLE:
if isinstance(value, int):
self.__nanocodas = Currency.__nanocodas_from_int(value)
elif isinstance(value, float):
self.__nanocodas = Currency.__nanocodas_from_string(str(value))
elif isinstance(value, str):
self.__nanocodas = Currency.__nanocodas_from_string(value)
else:
raise Exception('cannot construct whole Currency from %s' % type(value))
elif format == CurrencyFormat.NANO:
if isinstance(value, int):
self.__nanocodas = value
else:
raise Exception('cannot construct nano Currency from %s' % type(value))
else:
raise Exception('invalid Currency format %s' % format)
def decimal_format(self):
"""Computes the string decimal format representation of a Currency instance.
Return:
str - The decimal format representation of the Currency instance
"""
s = str(self.__nanocodas)
if len(s) > 9:
return s[:-9] + '.' + s[-9:]
else:
return '0.' + ('0' * (9 - len(s))) + s
def nanocodas(self):
"""Accesses the raw nanocodas representation of a Currency instance.
Return:
int - The nanocodas of the Currency instance represented as an integer
"""
return self.__nanocodas
def __str__(self):
return self.decimal_format()
def __repr__(self):
return 'Currency(%s)' % self.decimal_format()
def __add__(self, other):
if isinstance(other, Currency):
return Currency(self.nanocodas() + other.nanocodas(), format=CurrencyFormat.NANO)
else:
raise Exception('cannot add Currency and %s' % type(other))
def __sub__(self, other):
if isinstance(other, Currency):
new_value = self.nanocodas() - other.nanocodas()
if new_value >= 0:
return Currency(new_value, format=CurrencyFormat.NANO)
else:
raise CurrencyUnderflow()
else:
raise Exception('cannot subtract Currency and %s' % type(other))
def __mul__(self, other):
if isinstance(other, int):
return Currency(self.nanocodas() * other, format=CurrencyFormat.NANO)
elif isinstance(other, Currency):
return Currency(self.nanocodas() * other.nanocodas(), format=CurrencyFormat.NANO)
else:
raise Exception('cannot multiply Currency and %s' % type(other))
class Client():
# Implements a GraphQL Client for the Coda Daemon
def __init__(
self,
graphql_protocol: str = "http",
websocket_protocol: str = "ws",
graphql_host: str = "localhost",
graphql_path: str = "/graphql",
graphql_port: int = 3085,
):
self.endpoint = "{}://{}:{}{}".format(graphql_protocol, graphql_host, graphql_port, graphql_path)
self.websocket_endpoint = "{}://{}:{}{}".format(websocket_protocol, graphql_host, graphql_port, graphql_path)
self.logger = logging.getLogger(__name__)
def _send_query(self, query: str, variables: dict = {}) -> dict:
"""Sends a query to the Coda Daemon's GraphQL Endpoint
Arguments:
query {str} -- A GraphQL Query
Keyword Arguments:
variables {dict} -- Optional Variables for the query (default: {{}})
Returns:
dict -- A Response object from the GraphQL Server.
"""
return self._graphql_request(query, variables)
def _send_mutation(self, query: str, variables: dict = {}) -> dict:
"""Sends a mutation to the Coda Daemon's GraphQL Endpoint.
Arguments:
query {str} -- A GraphQL Mutation
Keyword Arguments:
variables {dict} -- Variables for the mutation (default: {{}})
Returns:
dict -- A Response object from the GraphQL Server.
"""
return self._graphql_request(query, variables)
def _graphql_request(self, 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"
}
self.logger.debug("Sending a Query: {}".format(payload))
response = requests.post(self.endpoint, json=payload, headers=headers)
resp_json = response.json()
if response.status_code == 200 and "errors" not in resp_json:
self.logger.debug("Got a Response: {}".format(response.json()))
return resp_json
else:
print(response.text)
raise Exception(
"Query failed -- returned code {}. {} -> {}".format(response.status_code, query, response.json()))
async def _graphql_subscription(self, query: str, variables: dict = {}, callback = None):
hello_message = {"type": "connection_init", "payload": {}}
# Strip all the whitespace and replace with spaces
query = " ".join(query.split())
payload = {'query': query}
if variables:
payload = { **payload, 'variables': variables }
query_message = {"id": "1", "type": "start", "payload": payload}
self.logger.info("Listening to GraphQL Subscription...")
uri = self.websocket_endpoint
self.logger.info(uri)
async with websockets.client.connect(uri, ping_timeout=None) as websocket:
# Set up Websocket Connection
self.logger.debug("WEBSOCKET -- Sending Hello Message: {}".format(hello_message))
await websocket.send(json.dumps(hello_message))
resp = await websocket.recv()
self.logger.debug("WEBSOCKET -- Recieved Response {}".format(resp))
self.logger.debug("WEBSOCKET -- Sending Subscribe Query: {}".format(query_message))
await websocket.send(json.dumps(query_message))
# Wait for and iterate over messages in the connection
async for message in websocket:
self.logger.debug("Recieved a message from a Subscription: {}".format(message))
if callback:
await callback(message)
else:
print(message)
def get_daemon_status(self) -> dict:
"""Gets the status of the currently configured Coda Daemon.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
query {
daemonStatus {
numAccounts
blockchainLength
highestBlockLengthReceived
uptimeSecs
ledgerMerkleRoot
stateHash
commitId
peers
userCommandsSent
snarkWorker
snarkWorkFee
syncStatus
blockProductionKeys
nextBlockProduction {
times {
endTime
epoch
globalSlot
slot
startTime
}
}
consensusTimeNow {
startTime
endTime
epoch
globalSlot
slot
}
consensusTimeBestTip
consensusMechanism
confDir
commitId
consensusConfiguration {
slotsPerEpoch
slotDuration
k
genesisStateTimestamp
epochDuration
delta
acceptableNetworkDelay
}
}
}
'''
res = self._send_query(query)
return res['data']
def get_daemon_version(self) -> dict:
"""Gets the version of the currently configured Coda Daemon.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
{
version
}
'''
res = self._send_query(query)
return res["data"]
def get_wallets(self) -> dict:
"""Gets the wallets that are currently installed in the Coda Daemon.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
{
ownedWallets {
publicKey
balance {
total
}
}
}
'''
res = self._send_query(query)
return res["data"]
def get_wallet(self, pk: str) -> dict:
"""Gets the wallet for the specified Public Key.
Arguments:
pk {str} -- A Public Key corresponding to a currently installed wallet.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
query($publicKey:PublicKey!){
wallet(publicKey:$publicKey) {
publicKey
balance {
total
unknown
}
nonce
receiptChainHash
delegate
votingFor
stakingActive
privateKeyPath
}
}
'''
variables = {
"publicKey": pk
}
res = self._send_query(query, variables)
return res["data"]
def create_wallet(self, password: str) -> dict:
"""Creates a new Wallet.
Arguments:
password {str} -- A password for the wallet to unlock.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
mutation ($password: String!) {
createAccount(input: {password: $password}) {
publicKey
}
}
'''
variables = {
"password": password
}
res = self._send_query(query, variables)
return res["data"]
def unlock_wallet(self, pk: str, password: str) -> dict:
"""Unlocks the wallet for the specified Public Key.
Arguments:
pk {str} -- A Public Key corresponding to a currently installed wallet.
password {str} -- A password for the wallet to unlock.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
mutation ($publicKey: PublicKey!, $password: String!) {
unlockWallet(input: {publicKey: $publicKey, password: $password}) {
account {
balance {
total
}
}
}
}
'''
variables = {
"publicKey": pk,
"password": password
}
res = self._send_query(query, variables)
return res["data"]
def get_blocks(self) -> dict:
"""Gets the blocks known to the Coda Daemon.
Mostly useful for Archive nodes.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
{
blocks{
nodes {
creator
stateHash
protocolState {
previousStateHash
blockchainState{
date
snarkedLedgerHash
stagedLedgerHash
}
}
transactions {
userCommands{
id
isDelegation
nonce
from
to
amount
fee
memo
}
feeTransfer {
recipient
fee
}
coinbase
}
snarkJobs {
prover
fee
workIds
}
}
pageInfo {
hasNextPage
hasPreviousPage
firstCursor
lastCursor
}
}
}
'''
res = self._send_query(query)
return res["data"]
def lock_wallet(self, pk: str) -> dict:
"""Locks the wallet for the specified Public Key.
Arguments:
pk {str} -- A Public Key corresponding to a currently installed wallet.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
mutation ($publicKey: PublicKey!) {
lockWallet(input: {publicKey: $publicKey}) {
account {
balance {
total
}
}
}
}
'''
variables = {
"publicKey": pk,
}
res = self._send_query(query, variables)
return res["data"]
def get_current_snark_worker(self) -> dict:
"""Gets the currently configured SNARK Worker from the Coda Daemon.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
{
currentSnarkWorker{
key
fee
}
}
'''
res = self._send_query(query)
return res["data"]
def get_sync_status(self) -> dict:
"""Gets the Sync Status of the Coda Daemon.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
{
syncStatus
}
'''
res = self._send_query(query)
return res["data"]
def set_current_snark_worker(self, worker_pk: str, fee: str) -> dict:
"""Set the current SNARK Worker preference.
Arguments:
worker_pk {str} -- The public key corresponding to the desired SNARK Worker
fee {str} -- The desired SNARK Work fee
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict
"""
query = '''
mutation($worker_pk:PublicKey!, $fee:UInt64!){
setSnarkWorker(input: {publicKey:$worker_pk}) {
lastSnarkWorker
}
setSnarkWorkFee(input: {fee:$fee})
}'''
variables = {
"worker_pk": worker_pk,
"fee": fee
}
res = self._send_mutation(query, variables)
return res["data"]
def send_payment(self, to_pk: str, from_pk: str, amount: int, fee: int, memo: str) -> dict:
"""Send a payment from the specified wallet to the specified target wallet.
Arguments:
to_pk {PublicKey} -- The target wallet where funds should be sent
from_pk {PublicKey} -- The installed wallet which will finance the payment
amount {UInt64} -- Tha amount of Coda to send
fee {UInt64} -- The transaction fee that will be attached to the payment
memo {str} -- A memo to attach to the payment
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict
"""
query = '''
mutation($from:PublicKey!, $to:PublicKey!, $amount:UInt64!, $fee:UInt64!, $memo:String){
sendPayment(input: {
from:$from,
to:$to,
amount:$amount,
fee:$fee,
memo:$memo
}) {
payment {
id,
isDelegation,
nonce,
from,
to,
amount,
fee,
memo
}
}
}
'''
variables = {
"from": from_pk,
"to": to_pk,
"amount": amount,
"fee": fee,
"memo": memo
}
res = self._send_mutation(query, variables)
return res["data"]
def get_pooled_payments(self, pk: str) -> dict:
"""Get the current transactions in the payments pool
Arguments:
pk {str} -- The public key corresponding to the installed wallet that will be queried
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict
"""
query = '''
query ($publicKey:String!){
pooledUserCommands(publicKey:$publicKey) {
id,
isDelegation,
nonce,
from,
to,
amount,
fee,
memo
}
}
'''
variables = {
"publicKey": pk
}
res = self._send_query(query, variables)
return res["data"]
def get_transaction_status(self, payment_id: str) -> dict:
"""Get the transaction status for the specified Payment Id.
Arguments:
payment_id {str} -- A Payment Id corresponding to a UserCommand.
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
query($paymentId:ID!){
transactionStatus(payment:$paymentId)
}
'''
variables = {
"paymentId": payment_id
}
res = self._send_query(query, variables)
return res["data"]
async def listen_sync_update(self, callback):
"""Creates a subscription for Network Sync Updates
"""
query = '''
subscription{
newSyncUpdate
}
'''
await self._graphql_subscription(query, {}, callback)
async def listen_block_confirmations(self, callback):
"""Creates a subscription for Block Confirmations
Calls callback when a new block is recieved.
"""
query = '''
subscription{
blockConfirmation {
stateHash
numConfirmations
}
}
'''
await self._graphql_subscription(query, {}, callback)
async def listen_new_blocks(self, callback):
"""Creates a subscription for new blocks, calls `callback` each time the subscription fires.
Arguments:
callback(block) {coroutine} -- This coroutine is executed with the new block as an argument each time the subscription fires
"""
query = '''
subscription(){
newBlock(){
creator
stateHash
protocolState {
previousStateHash
blockchainState {
date
snarkedLedgerHash
stagedLedgerHash
}
},
transactions {
userCommands {
id
isDelegation
nonce
from
to
amount
fee
memo
}
feeTransfer {
recipient
fee
}
coinbase
}
}
}
'''
variables = {
}
await self._graphql_subscription(query, variables, callback)