-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelpers.py
More file actions
224 lines (176 loc) · 7.42 KB
/
Copy pathhelpers.py
File metadata and controls
224 lines (176 loc) · 7.42 KB
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
"""Module containing helper functions for accessing Algorand blockchain."""
import base64
import os
import pty
import subprocess
import time
from pathlib import Path
from algosdk import account, mnemonic
from algosdk.error import IndexerHTTPError
from algosdk.future.transaction import LogicSig, LogicSigTransaction, PaymentTxn
from algosdk.v2client import algod, indexer
INDEXER_TIMEOUT = 10 # 61 for devMode
## SANDBOX
def _cli_passphrase_for_account(address):
"""Return passphrase for provided address."""
process = call_sandbox_command("goal", "account", "export", "-a", address)
if process.stderr:
raise RuntimeError(process.stderr.decode("utf8"))
passphrase = ""
parts = process.stdout.decode("utf8").split('"')
if len(parts) > 1:
passphrase = parts[1]
if passphrase == "":
raise ValueError(
"Can't retrieve passphrase from the address: %s\nOutput: %s"
% (address, process.stdout.decode("utf8"))
)
return passphrase
def _sandbox_directory():
"""Return full path to Algorand's sandbox executable.
The location of sandbox directory is retrieved either from the SANDBOX_DIR
environment variable or if it's not set then the location of sandbox directory
is implied to be the sibling of this Django project in the directory tree.
"""
return os.environ.get("SANDBOX_DIR") or str(
Path(__file__).resolve().parent.parent / "sandbox"
)
def _sandbox_executable():
"""Return full path to Algorand's sandbox executable."""
return _sandbox_directory() + "/sandbox"
def call_sandbox_command(*args):
"""Call and return sandbox command composed from provided arguments."""
return subprocess.run(
[_sandbox_executable(), *args], stdin=pty.openpty()[1], capture_output=True
)
## CLIENTS
def _algod_client():
"""Instantiate and return Algod client object."""
algod_address = "http://localhost:4001"
algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
return algod.AlgodClient(algod_token, algod_address)
def _indexer_client():
"""Instantiate and return Indexer client object."""
indexer_address = "http://localhost:8980"
indexer_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
return indexer.IndexerClient(indexer_token, indexer_address)
## TRANSACTIONS
def _add_transaction(sender, receiver, passphrase, amount, note):
"""Create and sign transaction from provided arguments.
Returned non-empty tuple carries field where error was raised and description.
If the first item is None then the error is non-field/integration error.
Returned two-tuple of empty strings marks successful transaction.
"""
client = _algod_client()
params = client.suggested_params()
unsigned_txn = PaymentTxn(sender, params, receiver, amount, None, note.encode())
signed_txn = unsigned_txn.sign(mnemonic.to_private_key(passphrase))
transaction_id = client.send_transaction(signed_txn)
_wait_for_confirmation(client, transaction_id, 4)
return transaction_id
def _wait_for_confirmation(client, 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 = client.status()["last-round"] + 1
current_round = start_round
while current_round < start_round + timeout:
try:
pending_txn = client.pending_transaction_info(transaction_id)
except Exception:
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"]))
client.status_after_block(current_round)
current_round += 1
raise Exception(
"pending tx not found in timeout rounds, timeout value = : {}".format(timeout)
)
def create_payment_transaction(escrow_address, params, receiver, amount):
"""Create and return payment transaction from provided arguments."""
return PaymentTxn(escrow_address, params, receiver, amount)
def process_logic_sig_transaction(logic_sig, payment_transaction):
"""Create logic signature transaction and send it to the network."""
client = _algod_client()
logic_sig_transaction = LogicSigTransaction(payment_transaction, logic_sig)
transaction_id = client.send_transaction(logic_sig_transaction)
_wait_for_confirmation(client, transaction_id, 4)
return transaction_id
def process_transactions(transactions):
"""Send provided grouped `transactions` to network and wait for confirmation."""
client = _algod_client()
transaction_id = client.send_transactions(transactions)
_wait_for_confirmation(client, transaction_id, 4)
return transaction_id
def suggested_params():
"""Return the suggested params from the algod client."""
return _algod_client().suggested_params()
## CREATING
def add_standalone_account():
"""Create standalone account and return two-tuple of its private key and address."""
private_key, address = account.generate_account()
return private_key, address
def fund_account(address, initial_funds=1000000000):
"""Fund provided `address` with `initial_funds` amount of microAlgos."""
initial_funds_address = _initial_funds_address()
if initial_funds_address is None:
raise Exception("Initial funds weren't transferred!")
_add_transaction(
initial_funds_address,
address,
_cli_passphrase_for_account(initial_funds_address),
initial_funds,
"Initial funds",
)
## RETRIEVING
def _initial_funds_address():
"""Get the address of initially created account having enough funds.
Such an account is used to transfer initial funds for the accounts
created in this tutorial.
"""
return next(
(
account.get("address")
for account in _indexer_client().accounts().get("accounts", [{}, {}])
if account.get("created-at-round") == 0
and account.get("status") == "Offline" # "Online" for devMode
),
None,
)
def account_balance(address):
"""Return funds balance of the account having provided address."""
account_info = _algod_client().account_info(address)
return account_info.get("amount")
def transaction_info(transaction_id):
"""Return transaction with provided id."""
timeout = 0
while timeout < INDEXER_TIMEOUT:
try:
transaction = _indexer_client().transaction(transaction_id)
break
except IndexerHTTPError:
time.sleep(1)
timeout += 1
else:
raise TimeoutError(
"Timeout reached waiting for transaction to be available in indexer"
)
return transaction
## UTILITY
def _compile_source(source):
"""Compile and return teal binary code."""
compile_response = _algod_client().compile(source)
return base64.b64decode(compile_response["result"])
def logic_signature(teal_source):
"""Create and return logic signature for provided `teal_source`."""
compiled_binary = _compile_source(teal_source)
return LogicSig(compiled_binary)