This repository was archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimulate.py
More file actions
135 lines (116 loc) · 3.74 KB
/
simulate.py
File metadata and controls
135 lines (116 loc) · 3.74 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
from algosdk.v2client import algod
from algosdk.dryrun_results import DryrunResponse, StackPrinterConfig
from algosdk.atomic_transaction_composer import (
AtomicTransactionComposer,
TransactionWithSigner,
)
from algosdk import transaction
from util import (
create_app,
create_asa,
get_approval_program,
get_clear_program,
get_contract,
)
from beaker import sandbox
import os
import json
def main():
# setup
algod_client = sandbox.clients.get_algod_client()
acct = sandbox.kmd.get_accounts().pop()
_, approval_bin, _ = get_approval_program(algod_client)
_, clear_bin, _ = get_clear_program(algod_client)
contract = get_contract()
app_id, app_addr = create_app(
algod_client,
acct.address,
acct.private_key,
approval_bin,
clear_bin,
transaction.StateSchema(1, 0),
transaction.StateSchema(0, 0),
)
asa_id = create_asa(
algod_client, acct.address, acct.private_key, "tmp_asset", "tmp", 10000, 0
)
sp = algod_client.suggested_params()
# TODO: Need to cover fees for the inner transaction (uncomment these lines)
# sp.flat_fee = True # Tell the SDK we know exactly what our fee should be
# sp.fee = 2000 # Cover 2 transaction (outer + inner)
# Create transaction to bootstrap application
atc = AtomicTransactionComposer()
atc.add_method_call(
app_id,
contract.get_method_by_name("bootstrap"),
acct.address,
sp,
signer=acct.signer,
# TODO: the asset id should be passed
method_args=[0],
# method_args=[asa_id],
)
try:
atc.execute(algod_client, 4)
except Exception as e:
perform_simulate(atc, algod_client)
return
# Create group transaction to send asset and call method
atc = AtomicTransactionComposer()
atc.add_method_call(
app_id,
contract.get_method_by_name("transfer"),
acct.address,
sp,
signer=acct.signer,
method_args=[
TransactionWithSigner(
# TODO: make this not fail
txn=transaction.AssetTransferTxn(acct.address, sp, app_addr, 9, asa_id),
# txn=transaction.AssetTransferTxn(acct.address, sp, app_addr, 10, asa_id),
signer=acct.signer,
),
asa_id,
],
)
try:
atc.execute(algod_client, 4)
except Exception as e:
perform_simulate(atc, algod_client)
return
# Create group transaction to send asset and call method
# See TODO in contracts/application.py
atc = AtomicTransactionComposer()
atc.add_method_call(
app_id,
contract.get_method_by_name("withdraw"),
acct.address,
sp,
signer=acct.signer,
method_args=[asa_id],
)
try:
atc.execute(algod_client, 4)
except Exception as e:
perform_simulate(atc, algod_client)
return
def perform_simulate(atc: AtomicTransactionComposer, client: algod.AlgodClient):
r = atc.simulate(client)
print(f"Failure Message: {r.failure_message}")
print(f"Simulation Transaction Results:")
for g in r.simulate_response["txn-groups"]:
for i, t in enumerate(g["txn-results"]):
print(f" Transaction {i}:")
for k, v in t["txn-result"].items():
print(f" {k}: {v}")
failed_at_msg = f"Failed at: gtxn index {r.failed_at[0]}"
if len(r.failed_at) > 1:
failed_at_msg += (
f", inner tnx index {r.failed_at[-1]} at depth level {len(r.failed_at) - 1}"
)
print(failed_at_msg)
print(f"ABI Return Values:")
for res in r.abi_results:
print(f" {res.tx_id}: {res.return_value}")
if __name__ == "__main__":
main()