-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
60 lines (50 loc) · 1.9 KB
/
util.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
import base64
from algosdk.v2client import algod
from time import sleep
# Helper function to compile program source to base64 encoding
def compile_program(client, source_code):
compile_response = client.compile(source_code)
return base64.b64decode(compile_response["result"])
# Helper function to compile program source
def compile_program_b64(client, source_code):
compile_response = client.compile(source_code)
return compile_response["result"]
# helper function that formats global state for printing
def format_state(state):
formatted = {}
for item in state:
key = item["key"]
value = item["value"]
formatted_key = base64.b64decode(key).decode("utf-8")
if value["type"] == 1:
# byte string
formatted_value = base64.b64decode(value["bytes"])
formatted[formatted_key] = formatted_value
else:
# integer
formatted[formatted_key] = value["uint"]
return formatted
# helper function to read app global state
def read_global_state(client, app_id):
app = client.application_info(app_id)
global_state = (
app["params"]["global-state"] if "global-state" in app["params"] else []
)
return format_state(global_state)
# helper function to read app local state for account
def read_local_state(client, address, app_id):
app = client.account_application_info(address, app_id)
local_state = (
app["app-local-state"]["key-value"] if "key-value" in app["app-local-state"] else []
)
return format_state(local_state)
# Function waits until a block with specific round has been accepted
def waitUntilRound(
client: algod.AlgodClient,
round: int,
):
print("Waiting for round {} ...".format(round))
currentRound = client.status().get('last-round')
while not currentRound >= round:
currentRound = client.status().get('last-round')
sleep(1)