test_contract.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/python3
  2. """
  3. Copyright 2022 Wormhole Project Contributors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. """
  14. from typing import List, Tuple, Dict, Any, Optional, Union
  15. from pyteal.ast import *
  16. from pyteal.types import *
  17. from pyteal.compiler import *
  18. from pyteal.ir import *
  19. from globals import *
  20. from inlineasm import *
  21. from algosdk.v2client.algod import AlgodClient
  22. from TmplSig import TmplSig
  23. from local_blob import LocalBlob
  24. import sys
  25. portal_transfer_selector = MethodSignature("portal_transfer(byte[])byte[]")
  26. def fullyCompileContract(client: AlgodClient, contract: Expr) -> bytes:
  27. teal = compileTeal(contract, mode=Mode.Application, version=6)
  28. response = client.compile(teal)
  29. return response
  30. def clear_app():
  31. return Int(1)
  32. def approve_app():
  33. me = Global.current_application_address()
  34. def nop():
  35. return Seq([Approve()])
  36. def test1():
  37. # Look! a proxy contract that sends message to the core
  38. return Seq(
  39. InnerTxnBuilder.Begin(),
  40. InnerTxnBuilder.SetFields(
  41. {
  42. TxnField.type_enum: TxnType.ApplicationCall,
  43. TxnField.application_id: Btoi(Txn.application_args[2]),
  44. TxnField.application_args: [Bytes("publishMessage"), Txn.application_args[1]],
  45. TxnField.accounts: [Txn.accounts[1]],
  46. TxnField.note: Bytes("publishMessage"),
  47. TxnField.fee: Int(0),
  48. }
  49. ),
  50. InnerTxnBuilder.Submit(),
  51. Approve()
  52. )
  53. def setup():
  54. aid = ScratchVar()
  55. return Seq([
  56. # Create a test asset
  57. InnerTxnBuilder.Begin(),
  58. InnerTxnBuilder.SetFields(
  59. {
  60. TxnField.sender: Global.current_application_address(),
  61. TxnField.type_enum: TxnType.AssetConfig,
  62. TxnField.config_asset_name: Bytes("TestAsset"),
  63. TxnField.config_asset_unit_name: Bytes("testAsse"),
  64. TxnField.config_asset_total: Int(int(1e17)),
  65. TxnField.config_asset_decimals: Int(10),
  66. TxnField.config_asset_manager: Global.current_application_address(),
  67. TxnField.config_asset_reserve: Global.current_application_address(),
  68. # We cannot freeze or clawback assets... per the spirit of
  69. TxnField.config_asset_freeze: Global.zero_address(),
  70. TxnField.config_asset_clawback: Global.zero_address(),
  71. TxnField.fee: Int(0),
  72. }
  73. ),
  74. InnerTxnBuilder.Submit(),
  75. aid.store(Itob(InnerTxn.created_asset_id())),
  76. App.globalPut(Bytes("asset"), aid.load()),
  77. Log(aid.load()),
  78. Approve()
  79. ])
  80. def completeTransfer():
  81. off = ScratchVar()
  82. return Seq([
  83. off.store(Btoi(Extract(Txn.application_args[1], Int(7), Int(1))) * Int(66) + Int(192)),
  84. Log(Extract(Txn.application_args[1], off.load(), Len(Txn.application_args[1]) - off.load())),
  85. Approve()
  86. ])
  87. def mint():
  88. return Seq([
  89. InnerTxnBuilder.Begin(),
  90. InnerTxnBuilder.SetFields(
  91. {
  92. TxnField.sender: Global.current_application_address(),
  93. TxnField.type_enum: TxnType.AssetTransfer,
  94. TxnField.xfer_asset: Btoi(App.globalGet(Bytes("asset"))),
  95. TxnField.asset_amount: Int(100000),
  96. TxnField.asset_receiver: Txn.sender(),
  97. TxnField.fee: Int(0),
  98. }
  99. ),
  100. InnerTxnBuilder.Submit(),
  101. Approve()
  102. ])
  103. METHOD = Txn.application_args[0]
  104. router = Cond(
  105. [METHOD == Bytes("nop"), nop()],
  106. [METHOD == Bytes("test1"), test1()],
  107. [METHOD == Bytes("setup"), setup()],
  108. [METHOD == Bytes("mint"), mint()],
  109. [METHOD == portal_transfer_selector, completeTransfer()],
  110. )
  111. on_create = Seq( [
  112. Return(Int(1))
  113. ])
  114. on_update = Seq( [
  115. Return(Int(1))
  116. ] )
  117. on_delete = Seq( [
  118. Return(Int(1))
  119. ] )
  120. on_optin = Seq( [
  121. Return(Int(1))
  122. ] )
  123. return Cond(
  124. [Txn.application_id() == Int(0), on_create],
  125. [Txn.on_completion() == OnComplete.UpdateApplication, on_update],
  126. [Txn.on_completion() == OnComplete.DeleteApplication, on_delete],
  127. [Txn.on_completion() == OnComplete.OptIn, on_optin],
  128. [Txn.on_completion() == OnComplete.NoOp, router]
  129. )
  130. def get_test_app(client: AlgodClient) -> Tuple[bytes, bytes]:
  131. APPROVAL_PROGRAM = fullyCompileContract(client, approve_app())
  132. CLEAR_STATE_PROGRAM = fullyCompileContract(client, clear_app())
  133. return APPROVAL_PROGRAM, CLEAR_STATE_PROGRAM