-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgonim_lib.py
executable file
·175 lines (155 loc) · 5.55 KB
/
algonim_lib.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
import os
import json
from algosdk import v2client, transaction
def create_algod_client(print_status=False, print_version=False):
"""HELP create_algod_client:
(bool, bool) - Returns an AlgodClient using local Node showing
network status and version. $ALGORAND_DATA must be set as environment
variable.
"""
ALGORAND_DATA = os.environ.get("ALGORAND_DATA")
if not ALGORAND_DATA:
raise Exception("Set the environment variable $ALGORAND_DATA from the "
+ "CLI entering: export ALGORAND_DATA=/path/to/data/")
if not ALGORAND_DATA[-1] == "/":
ALGORAND_DATA += "/"
with open(ALGORAND_DATA + "algod.token", "r") as file:
algod_token = file.read().splitlines()
algod_token = algod_token[0]
file.close()
with open(ALGORAND_DATA + "algod.net", "r") as file:
algod_address = file.read().splitlines()
file.close()
algod_address = 'http://' + algod_address[0]
algod_client = v2client.algod.AlgodClient(algod_token, algod_address)
try:
if print_status:
print("||-=-=-{ Algorand Network Status }-=-=-||")
print(json.dumps(algod_client.status(), indent=4))
print("")
if print_version:
print("||-=-=-{ Algorand Network Version }-=-=-||")
print(json.dumps(algod_client.versions(), indent=4))
print("")
except Exception as e:
print(e)
return algod_client
def wait_for_tx_confirmation(algod_client, txid):
"""HELP wait_for_tx_confirmation:
(AlgodClient, obj) - Wait for TX confirmation and displays confirmation
round.
"""
last_round = algod_client.status().get('last-round')
while True:
txinfo = algod_client.pending_transaction_info(txid)
if txinfo.get('confirmed-round') and txinfo.get('confirmed-round') > 0:
print("Transaction {} confirmed in round {}.".format(
txid, txinfo.get('confirmed-round')))
break
else:
print("Waiting for confirmation...")
last_round += 1
algod_client.status_after_block(last_round)
def unsigned_asset_send(algod_client, sender, receiver, asset_id, asa_amount,
validity_range=1000):
"""HELP unsigned_asset_send:
(AlgodClient, dict, str, int, int, int) - Returns an unsigned
AssetTransferTxn.
"""
assert type(asa_amount) == int and validity_range <= 1000
# Get network suggested params for transactions.
params = algod_client.suggested_params()
first_valid = params.first
last_valid = first_valid + validity_range
gh = params.gh
min_fee = params.min_fee
assert min_fee <= 1000
data = {
"sender": sender,
"fee": min_fee,
"first": first_valid,
"last": last_valid,
"gh": gh,
"receiver": receiver,
"amt": asa_amount,
"index": asset_id,
"flat_fee": True,
}
return transaction.AssetTransferTxn(**data)
def unsigned_send(algod_client, addr_sender, addr_receiver, microalgo_amount,
validity_range=1000):
"""HELP unsigned_send:
(AlgodClient, str, str, int, int) - Returns an unsigned PaymentTxn.
"""
assert type(microalgo_amount) == int and validity_range <= 1000
# Get network suggested params for transactions.
params = algod_client.suggested_params()
first_valid = params.first
last_valid = first_valid + validity_range
gh = params.gh
min_fee = params.min_fee
assert min_fee <= 1000
data = {
"sender": addr_sender,
"receiver": addr_receiver,
"fee": min_fee,
"first": first_valid,
"last": last_valid,
"gh": gh,
"amt": int(microalgo_amount),
"flat_fee": True,
}
return transaction.PaymentTxn(**data)
def send(algod_client, sender, addr_receiver, microalgo_amount,
validity_range=1000):
"""HELP send:
(AlgodClient, dict, str, int, int) - Executes a PaymentTxn.
"""
assert type(microalgo_amount) == int and validity_range <= 1000
# Get network suggested params for transactions.
params = algod_client.suggested_params()
first_valid = params.first
last_valid = first_valid + validity_range
gh = params.gh
min_fee = params.min_fee
assert min_fee <= 1000
data = {
"sender": sender['pk'],
"fee": min_fee,
"first": first_valid,
"last": last_valid,
"gh": gh,
"receiver": addr_receiver,
"amt": int(microalgo_amount),
"flat_fee": True
}
txn = transaction.PaymentTxn(**data)
stxn = txn.sign(sender['sk'])
txid = algod_client.send_transaction(stxn)
print("Transaction ID:", txid)
wait_for_tx_confirmation(algod_client, txid)
def unsigned_closeto(algod_client, sender, closeto, validity_range=1000):
"""HELP unsigned_closeto:
(AlgodClient, dict, str, int) - Returns an unsigned PaymentTxn with
close-to account.
"""
# Get network suggested params for transactions.
assert validity_range <= 1000
params = algod_client.suggested_params()
first_valid = params.first
last_valid = first_valid + validity_range
gh = params.gh
min_fee = params.min_fee
assert min_fee <= 1000
data = {
"sender": sender,
"receiver": closeto,
"fee": min_fee,
"first": first_valid,
"last": last_valid,
"gh": gh,
"amt": 0,
"flat_fee": True,
"close_remainder_to": closeto
}
return transaction.PaymentTxn(**data)