| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #!/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 completeTransfer():
- return Seq([
- 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(100000),
- 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()],
- [METHOD == Bytes("completeTransfer"), completeTransfer()],
- )
- 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
|