-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgod.py
101 lines (88 loc) · 3.18 KB
/
algod.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
from algosdk import account, mnemonic
from algosdk.constants import microalgos_to_algos_ratio
from algosdk.transaction import PaymentTxn
from algosdk.v2client import algod
import settings
def algod_client():
""" Setup AlgoClient to handle requests to Algorand network
:return: AlgodClient
"""
return algod.AlgodClient(
algod_token=settings.algod_api_key,
algod_address=settings.algod_api,
headers={
"X-API-Key": settings.algod_api_key,
}
)
def create_account():
""" Create user wallet account
:return: mnemonic
"""
private_key, address = account.generate_account()
return mnemonic.from_private_key(private_key)
def get_balance(address):
""" Get wallet's Algo balance. Divide by microalgo value because its the default unit for account balance.
:param address - Wallet address
:return: balance - Algo balance
"""
account_info = algod_client().account_info(address)
balance = account_info.get('amount') / microalgos_to_algos_ratio
return balance
def send_txn(sender, quantity, receiver, note, sk):
"""Create and sign a transaction. Return transaction status and transaction id
:param sender: address of sender
quantity: amount
receiver: address of receiver
note: transaction data
sk: signing key
:return transaction status
transaction id
"""
quantity = int(quantity * microalgos_to_algos_ratio)
params = algod_client().suggested_params()
note = note.encode()
try:
unsigned_txn = PaymentTxn(sender, params, receiver, quantity, None, note)
except Exception as err:
print(err)
return False, ""
signed_txn = unsigned_txn.sign(sk)
try:
txid = algod_client().send_transaction(signed_txn)
except Exception as err:
print(err)
return False, ""
try:
wait_for_confirmation(txid, 4)
return True, txid
except Exception as err:
print(err)
return False, ""
def wait_for_confirmation(transaction_id, timeout):
"""
Wait until the transaction is confirmed or rejected, or until 'timeout'
number of rounds have passed.
Args:
transaction_id (str): the transaction to wait for
timeout (int): maximum number of rounds to wait
Returns:
dict: pending transaction information, or throws an error if the transaction
is not confirmed or rejected in the next timeout rounds
"""
start_round = algod_client().status()["last-round"] + 1
current_round = start_round
while current_round < start_round + timeout:
try:
pending_txn = algod_client().pending_transaction_info(transaction_id)
except Exception as err:
print(err)
return
if pending_txn.get("confirmed-round", 0) > 0:
return pending_txn
elif pending_txn["pool-error"]:
raise Exception(
'pool error: {}'.format(pending_txn["pool-error"]))
algod_client().status_after_block(current_round)
current_round += 1
raise Exception(
'pending tx not found in timeout rounds, timeout value = : {}'.format(timeout))