This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
hug_script_testnet.py
838 lines (716 loc) · 26.7 KB
/
hug_script_testnet.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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# Required for rest of hug scripts
from bitshares import BitShares
from bitshares.account import Account
from bitshares.amount import Amount
from bitshares.asset import Asset
from bitshares.blockchain import Blockchain
from bitshares.block import Block
from bitshares.dex import Dex
from bitshares.price import Price
from bitshares.market import Market
from bitshares.witness import Witness # Retrieving 1
from bitshares.witness import Witnesses # Listing many
from bitshares.proposal import Proposal # Retrieving 1
from bitshares.proposal import Proposals # Listing many
from bitshares.instance import shared_bitshares_instance # Used to reduce bitshares instance load
from bitshares.instance import set_shared_bitshares_instance # Used to reduce bitshares instance load
import bitshares
import hug
import requests
import pendulum
import math
"""
Configure Full/API node for querying network info
Only enable ONE of the following API nodes!
"""
full_node_url = 'wss://node.testnet.bitshares.eu'
bitshares_full_node = BitShares(full_node_url, nobroadcast=False)
set_shared_bitshares_instance(bitshares_full_node)
# End of node configuration
def check_api_token(api_key):
"""Check if the user's API key is valid."""
if (api_key == '123abc'):
return True
else:
return False
def request_json(input_data):
"""Request JSON data from full node, given request data input.
More info: http://docs.python-requests.org/en/master/"""
request_url = full_node_url.replace("wss://", "https://") # Your selected full node must have HTTPS configured properly!
requested_data = requests.get(request_url, data=input_data)
return requested_data
"""
request = request_json('')
if request.status_code is not 200:
# We want to catch any failed GET requests!
return {'request_status_code_error': True,
'valid_key': True,
'took': float(hug_timer)}
valid_request_json = request.json()
"""
def extract_object(input_object):
"""Chunk of code to extract the inner JSON from objects.
Required to reduce unneccessary lines in HUG script & improve maintainability."""
temp_dict = {}
for item in input_object:
temp_dict[str(item)] = input_object[item]
return temp_dict
@hug.get(examples='object_id=1.2.0&api_key=API_KEY')
def get_bts_object(object_id: hug.types.text, api_key: hug.types.text, hug_timer=15):
"""Enable retrieving and displaying any TEST object in JSON."""
if (check_api_token(api_key) == True): # Check the api key
try:
retrieved_object = bitshares_full_node.rpc.get_objects([object_id])[0]
except:
return {'valid_object_id': False,
'valid_key': True,
'took': float(hug_timer)}
if retrieved_object is not None:
return {'retrieved_object': retrieved_object,
'valid_object_id': True,
'valid_key': True,
'took': float(hug_timer)}
else:
return {'valid_object_id': False,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='committee_id=1.5.10&api_key=API_KEY')
def get_committee_member(committee_id: hug.types.text, api_key: hug.types.text, hug_timer=15):
"""Retrieve information about a single committee member (inc full account details)!"""
if (check_api_token(api_key) == True): # Check the api key
try:
target_committee_member = bitshares_full_node.rpc.get_objects([committee_id])[0]
except:
return {'valid_committee_id': False,
'valid_key': True,
'took': float(hug_timer)}
target_account = Account(target_committee_member['committee_member_account'], full=True) # Full info!
target_account_data = extract_object(target_account)
active_committee_members = Blockchain().config()['active_committee_members']
if committee_id in active_committee_members:
target_account_data['status'] = True
else:
target_account_data['status'] = False
target_committee_member['committee_member_details'] = target_account_data
return {'get_committee_member': target_committee_member,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def get_committee_members(api_key: hug.types.text, hug_timer=15):
"""Get a list of all committee members!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
num_committee_members_request = request_json('{"jsonrpc": "2.0", "method": "get_committee_count", "params": [], "id": 1}')
if num_committee_members_request.status_code is not 200:
# We want to catch any failed GET requests!
return {'request_status_code_error': True,
'valid_key': True,
'took': float(hug_timer)}
active_committee_members = Blockchain().config()['active_committee_members']
num_committee_members = num_committee_members_request.json()['result']
committee_member_list = []
for member in range(num_committee_members):
committee_id = "1.5." + str(member)
current_committee_member = bitshares_full_node.rpc.get_objects([committee_id])[0]
if committee_id in active_committee_members:
current_committee_member['status'] = True
else:
current_committee_member['status'] = False
committee_member_list.append(current_committee_member)
return {'committee_members': committee_member_list,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='worker_id=1.14.50&api_key=API_KEY')
def get_worker(worker_id: hug.types.text, api_key: hug.types.text, hug_timer=15):
"""Retrieve a specific worker proposal & the details of the worker's account."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_worker = bitshares_full_node.rpc.get_objects([worker_id])[0]
except:
return {'valid_worker': False,
'valid_key': True,
'took': float(hug_timer)}
target_account = Account(target_worker['worker_account'], full=True)
target_account_data = extract_object(target_account)
target_worker['worker_account_details'] = target_account_data
return {'worker': target_worker,
'valid_worker': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def get_worker_proposals(api_key: hug.types.text, hug_timer=15):
"""Get a list of all worker proposals!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
num_workers_request = request_json('{"jsonrpc": "2.0", "method": "get_worker_count", "params": [], "id": 1}')
if num_workers_request.status_code is not 200:
# We want to catch any failed GET requests!
return {'request_status_code_error': True,
'valid_key': True,
'took': float(hug_timer)}
num_workers = num_workers_request.json()['result']
worker_list = []
for worker in range(num_workers):
worker_id = "1.14." + str(worker)
current_worker = bitshares_full_node.rpc.get_objects([worker_id])[0]
target_account = Account(current_worker['worker_account'])
target_account_data = extract_object(target_account)
current_worker['worker_account_details'] = target_account_data
worker_list.append(current_worker)
return {'workers': worker_list,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='asset_name=USD&api_key=API_KEY')
def get_asset(asset_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Get info regarding a single input asset."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_asset = Asset(asset_name, full=True)
except:
return {'valid_asset': False,
'valid_key': True,
'took': float(hug_timer)}
try:
bitasset_data = target_asset['bitasset_data_id']
except:
bitasset_data = None
extracted_object = extract_object(target_asset)
return {'asset_data': extracted_object,
'valid_asset': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def chain_info(api_key: hug.types.text, hug_timer=5):
"""Bitshares current chain information!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
chain = Blockchain()
chain_info = chain.info()
extracted_object = extract_object(chain_info)
return {'chain_info': extracted_object,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def get_chain_properties(api_key: hug.types.text, hug_timer=5):
"""Bitshares current chain properties!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
chain = Blockchain()
chain_properties = chain.get_chain_properties()
return {'chain_properties': chain_properties,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def get_config(api_key: hug.types.text, hug_timer=5):
"""Bitshares current chain config!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
chain = Blockchain()
chain_config = chain.config()
return {'chain_config': chain_config,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def get_info(api_key: hug.types.text, hug_timer=5):
"""Bitshares current chain info!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
chain = Blockchain()
chain_info = chain.info()
return {'chain_info': chain_info,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def get_network(api_key: hug.types.text, hug_timer=5):
"""Bitshares current chain network!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
chain = Blockchain()
chain_get_network = chain.get_network()
return {'get_network': chain_get_network,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='block_number=50&api_key=API_KEY')
def get_block_details(block_number: hug.types.number, api_key: hug.types.text, hug_timer=5):
"""Retrieve a specific block's details (date & time) & output in JSON!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_block = Block(block_number)
except:
return {'valid_block_number': False,
'valid_key': True,
'took': float(hug_timer)}
chain = Blockchain()
block_date = chain.block_time(block_number)
return {'previous': target_block['previous'],
'timestamp': target_block['timestamp'],
'witness': target_block['witness'],
'transaction_merkle_root': target_block['transaction_merkle_root'],
'extensions': target_block['extensions'],
'witness_signature': target_block['witness_signature'],
'transactions': target_block['transactions'],
'id': target_block['id'],
'date': block_date,
'block_number': block_number,
'valid_block_number': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def get_latest_block(api_key: hug.types.text, hug_timer=5):
"""Retrieve the latest block's details (date & time) & output in JSON!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
chain = Blockchain()
current_block_number = chain.get_current_block_num()
block_date = chain.block_time(current_block_number)
target_block = Block(current_block_number)
return {'previous': target_block['previous'],
'timestamp': target_block['timestamp'],
'witness': target_block['witness'],
'transaction_merkle_root': target_block['transaction_merkle_root'],
'extensions': target_block['extensions'],
'witness_signature': target_block['witness_signature'],
'transactions': target_block['transactions'],
'id': target_block['id'],
'block_date': block_date,
'block_number': current_block_number,
'valid_block_number': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
##################
@hug.get(examples='api_key=API_KEY')
def get_all_accounts(api_key: hug.types.text, hug_timer=5):
"""Retrieve all Bitshares account names. Takes a while!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
chain = Blockchain()
chain_get_all_accounts = chain.get_all_accounts()
list_of_accounts = []
for account in chain_get_all_accounts:
list_of_accounts.append(account)
return {'accounts': list_of_accounts,
'num_accounts': len(list_of_accounts),
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='account_name=blahblahblah&api_key=API_KEY')
def account_info(account_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Retrieve information about an individual Bitshares account!"""
try:
target_account = Account(account_name)
except:
return {'valid_account': False,
'account': account_name,
'valid_key': True,
'took': float(hug_timer)}
extracted_object = extract_object(target_account)
return {'account_info': extracted_object,
'valid_key': True,
'took': float(hug_timer)}
@hug.get(examples='account_name=blahblahblah&api_key=API_KEY')
def full_account_info(account_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Retrieve verbose information about an individual Bitshares account!"""
try:
target_account = Account(account_name, full=True)
except:
return {'valid_account': False,
'account': account_name,
'valid_key': True,
'took': float(hug_timer)}
extracted_object = extract_object(target_account)
return {'full_account_info': extracted_object,
'valid_key': True,
'took': float(hug_timer)}
@hug.get(examples='account_name=blahblahblah&api_key=API_KEY')
def account_balances(account_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Bitshares account balances! Simply supply an account name & provide the API key!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_account = Account(account_name, full=True)
except:
print("Account doesn't exist.")
return {'valid_account': False,
'account': account_name,
'valid_key': True,
'took': float(hug_timer)}
target_account_balances = target_account.balances
if (len(target_account_balances) > 0):
balance_json_list = {}
for balance in target_account_balances:
current_balance_target = Amount(balance)
balance_json_list[current_balance_target.symbol] = current_balance_target.amount
return {'balances': balance_json_list,
'account_has_balances': True,
'account': account_name,
'valid_account': True,
'valid_key': True,
'took': float(hug_timer)}
else:
return {'account_has_balances': False,
'account': account_name,
'valid_account': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='account_name=blahblahblah&api_key=API_KEY')
def account_open_orders(account_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Bitshares account open orders! Simply supply an account name & provide the API key!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_account = Account(account_name)
except:
print("Account doesn't exist.")
return {'valid_account': False,
'account': account_name,
'valid_key': True,
'took': float(hug_timer)}
target_account_oo = target_account.openorders
if (len(target_account_oo) > 0):
open_order_list = []
for open_order in target_account_oo:
oo_str = str(open_order)
first_split_oo = oo_str.split(" @ ")[0]
second_split_oo = first_split_oo.split(" ")
buy_amount = second_split_oo[0].replace(",", "")
buy_asset = "Buy: " + second_split_oo[1]
sell_amount = second_split_oo[2].replace(",", "")
sell_asset = "Sell: " + second_split_oo[3]
rate_amount = float(sell_amount) / float(buy_amount)
rate_asset = second_split_oo[3] + "/" + second_split_oo[1]
open_order_list.append({sell_asset: sell_amount, buy_asset: buy_amount, rate_asset: rate_amount})
return {'open_orders': open_order_list,
'account_has_open_orders': True,
'account': account_name,
'valid_account': True,
'took': float(hug_timer)}
else:
return {'account_has_open_orders': False,
'account': account_name,
'valid_account': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='account_name=blahblahblah&api_key=API_KEY')
def account_callpositions(account_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Bitshares account call positions! Simply supply an account name & provide the API key!"""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_account = Account(account_name)
except:
print("Account doesn't exist.")
return {'valid_account': False,
'account': account_name,
'valid_key': True,
'took': float(hug_timer)}
target_account_callpos = target_account.callpositions
if (len(target_account_callpos) > 0):
return {'call_positions': target_account_callpos,
'account_has_call_positions': True,
'account': account_name,
'valid_account': True,
'valid_key': True,
'took': float(hug_timer)}
else:
return {'account_has_call_positions': False,
'account': account_name,
'valid_account': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='account_name=blahblahblah&api_key=API_KEY')
def account_history(account_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Given a valid account name, output the user's history in JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_account = Account(account_name)
except:
# Accoun is not valid!
return {'valid_account': False,
'account': account_name,
'valid_key': True,
'took': float(hug_timer)}
target_account_history = target_account.history(first=0, last=100, limit=100)
tx_container = []
for transaction in target_account_history:
tx_container.append(transaction)
if (len(tx_container) > 0):
return {'tx_history': tx_container,
'account_has_tx_history': True,
'account': account_name,
'valid_account': True,
'valid_key': True,
'took': float(hug_timer)}
else:
return {'account_has_tx_history': False,
'account': account_name,
'valid_account': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='account_name=blahblahblah&api_key=API_KEY')
def account_is_ltm(account_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Given a valid account name, check if they're LTM & output confirmation as JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_account = Account(account_name)
except:
# Accoun is not valid!
return {'valid_account': False,
'account': account_name,
'valid_key': True,
'took': float(hug_timer)}
target_account_ltm = target_account.is_ltm
return {'account_is_ltm': target_account_ltm,
'account': account_name,
'valid_account': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='market_pair=USD:TEST&api_key=API_KEY')
def market_ticker(market_pair: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Given a valid market pair, retrieve ticker data & output as JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_market = Market(market_pair)
except:
# Market is not valid
return {'valid_market': False,
'valid_key': True,
'took': float(hug_timer)}
target_market_ticker_data = target_market.ticker()
return {'market_ticker': target_market_ticker_data,
'market': market_pair,
'valid_market': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='market_pair=USD:TEST&api_key=API_KEY')
def market_orderbook(market_pair: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Given a valid market pair (e.g. USD:TEST) and your desired orderbook size limit, output the market pair's orderbook (buy/sell order) information in JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_market = Market(market_pair)
except:
# Market is not valid
return {'valid_market': False,
'valid_key': True,
'took': float(hug_timer)}
target_market_orderbook_data = target_market.orderbook(limit=50)
return {'market_orderbook': target_market_orderbook_data,
'market': market_pair,
'valid_market': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='market_pair=USD:TEST&api_key=API_KEY')
def market_24hr_vol(market_pair: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Given a valid market_pair (e.g. USD:TEST), output their 24hr market volume in JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_market = Market(market_pair)
except:
# Market is not valid
return {'valid_market': False,
'valid_key': True,
'took': float(hug_timer)}
return {'market_volume_24hr': target_market.volume24h(),
'market': market_pair,
'valid_market': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
# &start_time=2017-07-01T00:00:00Z&stop_time=2017-07-10T00:00:00Z
# start_time: hug.types.text, stop_time: hug.types.text,
@hug.get(examples='market_pair=USD:TEST&api_key=API_KEY')
def market_trade_history(market_pair: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Given a valid market_pair (e.g. USD:TEST) & a TX limit, output the market's trade history in JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_market = Market(market_pair)
except:
# Market is not valid
return {'valid_market': False,
'valid_key': True,
'took': float(hug_timer)}
temp_market_history = list(target_market.trades(limit=100))
#print(temp_market_history)
# (2017-12-24 15:37:21) 55.8699 USD 106.84792 TEST @ 1.912441583 TEST/USD
market_history_json_list = []
for market_trade in temp_market_history:
str_market_trade = str(market_trade).split(" @ ") # ["(2017-12-24 15:37:21) 55.8699 USD 106.84792 TEST", "1.912441583 TEST/USD"]
trade_rate = str_market_trade[1] # "1.912441583 TEST/USD"
trade_time = (str_market_trade[0].split(") ")[0]).replace("(", "")
trade_details = str_market_trade[0].split(") ")[1]
split_trade = trade_details.split(" ")
market_history_json_list.append({"datetime": trade_time.replace(" ", "T"), "bought": split_trade[0] + " " + split_trade[1], "sold": split_trade[2] + " " + split_trade[3], "rate ": trade_rate})
return {'market_trade_history': market_history_json_list,
'market': market_pair,
'valid_market': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='witness_name=blockchained&api_key=API_KEY')
def find_witness(witness_name: hug.types.text, api_key: hug.types.text, hug_timer=5):
"""Given a valid witness name, output witness data in JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
try:
target_witness = Witness(witness_name)
except:
# Market is not valid
return {'valid_witness': False,
'valid_key': True,
'took': float(hug_timer)}
target_account = Account(target_witness['witness_account'], full=True)
witness_account_data = extract_object(target_account)
witness_role_data = extract_object(target_witness)
active_witnesses = Blockchain().config()['active_witnesses']
if witness_role_data['id'] in active_witnesses:
witness_status = True
else:
witness_status = False
return {'witness_role_data': witness_role_data,
'witness_account_data': witness_account_data,
'active_witness': witness_status,
'valid_witness': True,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def list_of_witnesses(api_key: hug.types.text, hug_timer=5):
"""Output the list of active witnesses in JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
list_of_witnesses = Witnesses()
witness_data = []
for witness in list_of_witnesses:
target_account = Account(witness['witness_account'])
witness_account_data = extract_object(target_account)
witness_role_data = extract_object(witness)
active_witnesses = Blockchain().config()['active_witnesses']
if witness_role_data['id'] in active_witnesses:
witness_status = True
else:
witness_status = False
witness_data.append({'witness_role_data': witness_role_data,
'witness_account_data': witness_account_data,
'witness_status': witness_status})
return {'witnesses': witness_data,
'witness_count': len(list_of_witnesses),
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}
@hug.get(examples='api_key=API_KEY')
def list_fees(api_key: hug.types.text, hug_timer=5):
"""Output the current Bitshares network fees in JSON."""
if (check_api_token(api_key) == True): # Check the api key
# API KEY VALID
network_fees = Dex().returnFees()
extracted_fees = extract_object(network_fees)
return {'network_fees': extracted_fees,
'valid_key': True,
'took': float(hug_timer)}
else:
# API KEY INVALID!
return {'valid_key': False,
'took': float(hug_timer)}