-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
157 lines (124 loc) · 4.83 KB
/
test_app.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
#!/usr/bin/python3
"""
Copyright 2022 Wormhole Project Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from typing import List, Tuple, Dict, Any, Optional, Union
from pyteal.ast import *
from pyteal.types import *
from pyteal.compiler import *
from pyteal.ir import *
from globals import *
from inlineasm import *
from algosdk.v2client.algod import AlgodClient
from TmplSig import TmplSig
from local_blob import LocalBlob
import sys
def fullyCompileContract(client: AlgodClient, contract: Expr) -> bytes:
teal = compileTeal(contract, mode=Mode.Application, version=6)
response = client.compile(teal)
return response
def clear_app():
return Int(1)
def approve_app():
me = Global.current_application_address()
def nop():
return Seq([Approve()])
def test1():
# Look! a proxy contract that sends message to the core
return Seq(
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.ApplicationCall,
TxnField.application_id: Btoi(Txn.application_args[2]),
TxnField.application_args: [Bytes("publishMessage"), Txn.application_args[1]],
TxnField.accounts: [Txn.accounts[1]],
TxnField.note: Bytes("publishMessage"),
TxnField.fee: Int(0),
}
),
InnerTxnBuilder.Submit(),
Approve()
)
def setup():
aid = ScratchVar()
return Seq([
# Create a test asset
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.sender: Global.current_application_address(),
TxnField.type_enum: TxnType.AssetConfig,
TxnField.config_asset_name: Bytes("TestAsset"),
TxnField.config_asset_unit_name: Bytes("testAsse"),
TxnField.config_asset_total: Int(int(1e17)),
TxnField.config_asset_decimals: Int(10),
TxnField.config_asset_manager: Global.current_application_address(),
TxnField.config_asset_reserve: Global.current_application_address(),
# We cannot freeze or clawback assets... per the spirit of
TxnField.config_asset_freeze: Global.zero_address(),
TxnField.config_asset_clawback: Global.zero_address(),
TxnField.fee: Int(0),
}
),
InnerTxnBuilder.Submit(),
aid.store(Itob(InnerTxn.created_asset_id())),
App.globalPut(Bytes("asset"), aid.load()),
Log(aid.load()),
Approve()
])
def mint():
return Seq([
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.sender: Global.current_application_address(),
TxnField.type_enum: TxnType.AssetTransfer,
TxnField.xfer_asset: Btoi(App.globalGet(Bytes("asset"))),
TxnField.asset_amount: Int(1000),
TxnField.asset_receiver: Txn.sender(),
TxnField.fee: Int(0),
}
),
InnerTxnBuilder.Submit(),
Approve()
])
METHOD = Txn.application_args[0]
router = Cond(
[METHOD == Bytes("nop"), nop()],
[METHOD == Bytes("test1"), test1()],
[METHOD == Bytes("setup"), setup()],
[METHOD == Bytes("mint"), mint()],
)
on_create = Seq( [
Return(Int(1))
])
on_update = Seq( [
Return(Int(1))
] )
on_delete = Seq( [
Return(Int(1))
] )
on_optin = Seq( [
Return(Int(1))
] )
return Cond(
[Txn.application_id() == Int(0), on_create],
[Txn.on_completion() == OnComplete.UpdateApplication, on_update],
[Txn.on_completion() == OnComplete.DeleteApplication, on_delete],
[Txn.on_completion() == OnComplete.OptIn, on_optin],
[Txn.on_completion() == OnComplete.NoOp, router]
)
def get_test_app(client: AlgodClient) -> Tuple[bytes, bytes]:
APPROVAL_PROGRAM = fullyCompileContract(client, approve_app())
CLEAR_STATE_PROGRAM = fullyCompileContract(client, clear_app())
return APPROVAL_PROGRAM, CLEAR_STATE_PROGRAM