#pragma version 3
global int i
global int j
// main entry point
txn ApplicationCall
int 0
int i
int j
// update global variables with parameter values
i:= Txn.Ints[1]
j:= Txn.Ints[2]
// define the behavior of the child app using the parameter values
// ...
将代码上传到Algorand的智能合约编辑器中,并将其编译为字节码。复制字节码,以便在子应用程序创建过程中使用它。
创建带有参数的子应用程序。以下是一个示例PyTeal脚本,它将一个整数和一个字符串作为参数传递给子应用程序。
from pyteal import *
# compile the sub-application logic
sub_app = compileTeal(sub_app_logic, mode=Mode.Application, version=3)
# define the parent application
def parent_app(i: int, s: str) -> Contract:
return Contract(
# create the sub-application with the passed parameters
App.globalPut(Bytes("key1"), Int(i))
.And(App.globalPut(Bytes("key2"), Bytes(s)))
.And(App.localPut(Int(0), Bytes("app_id"), App.id(sub_app))),
# call the sub-application with the passed parameters
Seq([
App.localPut(Int(0), Bytes("i"), Int(i)),
App.localPut(Int(0), Bytes("s"), Bytes(s)),
App.localPut(Int(0), Bytes("call"), Int(1)),
App.localPut(Int(0), Bytes("app_id"), App.id(sub_app)),
App.optIn(sub_app),
App.call(App.id(sub_app), []),
App.globalPut(Bytes("key3"), App.globalGet(Bytes("result"))),
Return(Int(1)),
])
)
# compile the