Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for docs #694

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added _examples/__init__.py
Empty file.
65 changes: 65 additions & 0 deletions _examples/app_walkthrough.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import json
from pyteal import (
App,
Approve,
Assert,
BareCallActions,
Bytes,
Cond,
Global,
If,
Int,
Mode,
OnComplete,
OnCompleteAction,
Reject,
Return,
Router,
ScratchVar,
Seq,
TealType,
Txn,
compileTeal,
)


def step_1():
# example: APP_EMPTY_LOGIC
def approval_program():
program = Return(Int(1))
# Mode.Application specifies that this is a smart contract
return compileTeal(program, Mode.Application, version=5)

def clear_state_program():
program = Return(Int(1))
# Mode.Application specifies that this is a smart contract
return compileTeal(program, Mode.Application, version=5)

print(approval_program())
print(clear_state_program())
# example: APP_EMPTY_LOGIC


def app_manual_router():
# example: APP_MANUAL_ROUTER
def approval_program():

handle_creation = Approve()
handle_optin = Reject()
handle_closeout = Reject()
handle_update = Reject()
handle_delete = Reject()
handle_noop = Reject()

program = Cond(
[Txn.application_id() == Int(0), handle_creation],
[Txn.on_completion() == OnComplete.OptIn, handle_optin],
[Txn.on_completion() == OnComplete.CloseOut, handle_closeout],
[Txn.on_completion() == OnComplete.UpdateApplication, handle_update],
[Txn.on_completion() == OnComplete.DeleteApplication, handle_delete],
[Txn.on_completion() == OnComplete.NoOp, handle_noop],
)
return program

return compileTeal(approval_program(), Mode.Application, version=5)
# example: APP_MANUAL_ROUTER
59 changes: 59 additions & 0 deletions _examples/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from pyteal import (
App,
Bytes,
Cond,
Err,
Global,
Int,
Mode,
OnComplete,
Return,
Seq,
Txn,
compileTeal,
)


def opted_in():
# example: APPL_CHECK_OPTEDIN
program = App.optedIn(Int(0), Txn.application_id())
print(compileTeal(program, Mode.Application))
# example: APPL_CHECK_OPTEDIN


def global_ts():
# example: GLOBAL_LATEST_TIMESTAMP
program = Global.latest_timestamp() >= App.globalGet(Bytes("StartDate"))
print(compileTeal(program, Mode.Application))
# example: GLOBAL_LATEST_TIMESTAMP


def boilerplate():
# example: BOILERPLATE
# Handle each possible OnCompletion type. We don't have to worry about
# handling ClearState, because the ClearStateProgram will execute in that
# case, not the ApprovalProgram.
def approval_program():
handle_noop = Seq([Return(Int(1))])

handle_optin = Seq([Return(Int(1))])

handle_closeout = Seq([Return(Int(1))])

handle_updateapp = Err()

handle_deleteapp = Err()

program = Cond(
[Txn.on_completion() == OnComplete.NoOp, handle_noop],
[Txn.on_completion() == OnComplete.OptIn, handle_optin],
[Txn.on_completion() == OnComplete.CloseOut, handle_closeout],
[Txn.on_completion() == OnComplete.UpdateApplication, handle_updateapp],
[Txn.on_completion() == OnComplete.DeleteApplication, handle_deleteapp],
)
return program

with open("boilerplate_approval_pyteal.teal", "w") as f:
compiled = compileTeal(approval_program(), Mode.Application, version=5)
f.write(compiled)
# example: BOILERPLATE
18 changes: 18 additions & 0 deletions _examples/assets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pyteal import AssetHolding, AssetParam, If, Int, Mode, Return, Seq, compileTeal


def asset_balance():
# example: APPL_ASSET_BALANCE
asset_balance = AssetHolding.balance(Int(0), Int(2))
program = Seq(
asset_balance, If(asset_balance.hasValue(), Return(Int(1)), Return(Int(0)))
)
print(compileTeal(program, Mode.Application))
# example: APPL_ASSET_BALANCE


def asset_param():
# example: APPL_ASSET_PARAM
program = AssetParam.total(Int(0))
print(compileTeal(program, Mode.Application))
# example: APPL_ASSET_PARAM
71 changes: 71 additions & 0 deletions _examples/box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from pyteal import App, Assert, Bytes, Int, Itob, ScratchVar, Seq, TealType


def box_create():
return Seq(
# example: BOX_CREATE
# ...
# box created with box_create, size 100 bytes
App.box_create(Bytes("MyKey"), Int(100)),
# OR box created with box_put, size is implicitly the
# length of bytes written
App.box_put(Bytes("MyKey"), Bytes("My data values"))
# ...
# example: BOX_CREATE
)


def box_get():
return Seq(
App.box_put(Bytes("MyKey"), Itob(Int(123))),
# example: BOX_GET
boxval := App.box_get(Bytes("MyKey")),
Assert(boxval.hasValue()),
# do something with boxval.value()
# ...
# example: BOX_GET
)


def box_extract():
return Seq(
(scratchVar := ScratchVar(TealType.bytes)).store(Bytes("")),
# example: BOX_EXTRACT
# ...
App.box_put(
Bytes("BoxA"), Bytes("this is a test of a very very very very long string")
),
scratchVar.store(App.box_extract(Bytes("BoxA"), Int(5), Int(9))),
Assert(scratchVar.load() == Bytes("is a test"))
# ...
# example: BOX_EXTRACT
)


def box_len():
return Seq(
# example: BOX_LEN
App.box_put(
Bytes("BoxA"), Bytes("this is a test of a very very very very long string")
),
# box length is equal to the size of the box created
# not a measure of how many bytes have been _written_
# by the smart contract
bt := App.box_length(Bytes("BoxA")),
Assert(bt.hasValue()),
Assert(bt.value() == 51),
# example: BOX_LEN
)


def box_delete():
return Seq(
# example: BOX_DELETE
App.box_put(
Bytes("BoxA"), Bytes("this is a test of a very very very very long string")
),
# Box delete returns a 1/0 on the stack
# depending on if it was successful
Assert(App.box_delete(Bytes("BoxA"))),
# example: BOX_DELETE
)
Loading