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
Changes from 1 commit
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
Next Next commit
starting to add itxn docs
  • Loading branch information
barnjamin committed Mar 6, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 175e274a74b0ec79f26ed765e507f2453d64e631
Empty file added _examples/__init__.py
Empty file.
Empty file added _examples/itxn.py
Empty file.
242 changes: 242 additions & 0 deletions _examples/overview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
from pyteal import *


@Subroutine(TealType.none)
def send_payment():

return Seq(
# example: ITXN_PAYMENT
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.Payment,
TxnField.amount: Int(5000),
TxnField.receiver: Txn.sender(),
}
),
InnerTxnBuilder.Submit(),
# ...
# The `Sender` for the above is implied to be Global.current_application_address().
# If a different sender is needed, it'd have to be an account that has been rekeyed to
# the application address.
# example: ITXN_PAYMENT
)


@Subroutine(TealType.none)
def send_asset():
return Seq(
# example: ITXN_ASSET_TRANSFER
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetTransfer,
TxnField.asset_amount: Int(5000),
TxnField.asset_receiver: Txn.sender(),
TxnField.xfer_asset: Txn.assets[0],
}
),
# ...
InnerTxnBuilder.Submit(),
# example: ITXN_ASSET_TRANSFER
)


@Subroutine(TealType.none)
def asset_freeze():
return Seq(
# example: ITXN_ASSET_FREEZE
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetFreeze,
TxnField.freeze_asset: Txn.assets[0],
TxnField.freeze_asset_account: Txn.accounts[1],
TxnField.freeze_asset_frozen: Int(1),
}
),
InnerTxnBuilder.Submit(),
# ...
# example: ITXN_ASSET_FREEZE
)


@Subroutine(TealType.none)
def asset_revoke():
return Seq(
# example: ITXN_ASSET_REVOKE
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetTransfer,
TxnField.asset_receiver: Global.current_application_address(),
# AssetSender is _only_ used in the case of clawback
# Sender is implied to be current_application_address
TxnField.asset_sender: Txn.accounts[1],
TxnField.asset_amount: Int(1000),
}
),
InnerTxnBuilder.Submit(),
# ...
# example: ITXN_ASSET_REVOKE
)


@Subroutine(TealType.none)
def asset_create():
return Seq(
# example: ITXN_ASSET_CREATE
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetConfig,
TxnField.config_asset_total: Int(1000000),
TxnField.config_asset_decimals: Int(3),
TxnField.config_asset_unit_name: Bytes("oz"),
TxnField.config_asset_name: Bytes("Gold"),
TxnField.config_asset_url: Bytes("https://gold.rush"),
TxnField.config_asset_manager: Global.current_application_address(),
TxnField.config_asset_reserve: Global.current_application_address(),
TxnField.config_asset_freeze: Global.current_application_address(),
TxnField.config_asset_clawback: Global.current_application_address(),
}
),
InnerTxnBuilder.Submit(),
# ...
# example: ITXN_ASSET_CREATE
)


@Subroutine(TealType.none)
def asset_config():
return Seq(
# example: ITXN_ASSET_CONFIG
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetConfig,
TxnField.config_asset: Txn.assets[0],
TxnField.config_asset_manager: Txn.sender(),
TxnField.config_asset_reserve: Txn.sender(),
TxnField.config_asset_freeze: Txn.sender(),
TxnField.config_asset_clawback: Txn.sender(),
}
),
InnerTxnBuilder.Submit(),
# ...
# example: ITXN_ASSET_CONFIG
)


@Subroutine(TealType.none)
def asset_destroy():
return Seq(
# example: ITXN_ASSET_DESTROY
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.AssetConfig,
TxnField.config_asset: Txn.assets[0],
}
),
InnerTxnBuilder.Submit(),
# ...
# example: ITXN_ASSET_DESTROY
)


@Subroutine(TealType.none)
def grouped_itxn():
return Seq(
# example: GROUPED_ITXN
# This returns a `MaybeValue`, see pyteal docs
addr := AppParam.address(Int(1234)),
Assert(addr.hasValue()),
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.Payment,
TxnField.receiver: addr.value(),
TxnField.amount: Int(1000000),
}
),
InnerTxnBuilder.Next(), # This indicates we're moving to constructing the next txn in the group
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.ApplicationCall,
TxnField.application_id: Int(1234),
TxnField.on_completion: OnComplete.NoOp,
# Note this is _not_ using the ABI to call the
# method in the other app
TxnField.application_args: [Bytes("buy")],
}
),
InnerTxnBuilder.Submit(),
# ...
# example: GROUPED_ITXN
)


@Subroutine(TealType.none)
def inner_c2c():
return Seq(
# example: ITXN_C2C
# ...
InnerTxnBuilder.Begin(),
InnerTxnBuilder.SetFields(
{
TxnField.type_enum: TxnType.ApplicationCall,
TxnField.application_id: Int(1234),
TxnField.on_completion: OnComplete.NoOp,
}
),
InnerTxnBuilder.Submit(),
# ...
# example: ITXN_C2C
)


# example: WRITE_GLOBAL_STATE
# example: WRITE_OWN_LOCAL_STATE
# example: WRITE_OTHER_LOCAL_STATE

# example: READ_GLOBAL_STATE
# example: READ_LOCAL_STATE
# example: READ_OWN_LOCAL_STATE_EX
# example: READ_LOCAL_STATE_EX
# example: READ_GLOBAL_STATE_EX

# example: BOX_CREATE
# example: BOX_GET
# example: BOX_EXTRACT
# example: BOX_LEN
# example: BOX_DELETE

# example: TXN_ONCOMPLETE
# example: TXN_APP_ARGS
# example: TXN_NUM_APP_ARGS
# example: TXN_APP_ARG_TO_INT
# example: TXN_AMOUNT
# example: TXN_GROUP_SIZE
# example: GTXN_TYPE_ENUM
# example: GTXN_APP_ARGS

# example: APPL_OPTIN
# example: APPL_CHECK_OPTEDIN
# example: APPL_CALL
# example: APPL_UPDATE
# example: APPL_UPDATE_REJECT
# example: APPL_ASSET_BALANCE
# example: APPL_ASSET_PARAM

# example: GLOBAL_LATEST_TIMESTAMP

# example: BOILERPLATE