Skip to content

Commit

Permalink
Code changes to prep for new documentation (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos committed Aug 20, 2020
1 parent 169b34e commit 7865220
Show file tree
Hide file tree
Showing 39 changed files with 624 additions and 203 deletions.
48 changes: 29 additions & 19 deletions examples/atomic_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@

from pyteal import *

""" Atomic Swap
"""
"""Atomic Swap"""

alice = Addr("6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY")
bob = Addr("7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M")
secret = Bytes("base32", "23232323232323")
timeout = 3000

def htlc(tmpl_seller=alice,
tmpl_buyer=bob,
tmpl_fee=1000,
tmpl_secret=secret,
tmpl_hash_fn=Sha256,
tmpl_timeout=timeout):
fee_cond = Txn.fee() < Int(tmpl_fee)
type_cond = Txn.type_enum() == Int(1)
recv_cond = And(Txn.close_remainder_to() == Global.zero_address(),
Txn.receiver() == tmpl_seller,
tmpl_hash_fn(Arg(0)) == tmpl_secret)
esc_cond = And(Txn.close_remainder_to() == Global.zero_address(),
Txn.receiver() == tmpl_buyer,
Txn.first_valid() > Int(tmpl_timeout))
tmpl_buyer=bob,
tmpl_fee=1000,
tmpl_secret=secret,
tmpl_hash_fn=Sha256,
tmpl_timeout=timeout):

fee_cond = Txn.fee() < Int(tmpl_fee)
type_cond = Txn.type_enum() == TxnType.Payment

recv_cond = And(
Txn.close_remainder_to() == Global.zero_address(),
Txn.receiver() == tmpl_seller,
tmpl_hash_fn(Arg(0)) == tmpl_secret
)

esc_cond = And(
Txn.close_remainder_to() == Global.zero_address(),
Txn.receiver() == tmpl_buyer,
Txn.first_valid() > Int(tmpl_timeout)
)

return And(fee_cond,
type_cond,
Or(recv_cond, esc_cond))
return And(
fee_cond,
type_cond,
Or(recv_cond, esc_cond)
)

print(compileTeal(htlc(), Mode.Signature))
if __name__ == "__main__":
print(compileTeal(htlc(), Mode.Signature))
32 changes: 32 additions & 0 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This example is provided for informational purposes only and has not been audited for security.

from pyteal import *

"""Basic Bank"""

def bank_for_account(receiver):
"""Only allow receiver to withdraw funds from this contract account.
Args:
receiver (str): Base 32 Algorand address of the receiver.
"""

is_payment = Txn.type_enum() == TxnType.Payment
is_single_tx = Global.group_size() == Int(1)
is_correct_receiver = Txn.receiver() == Addr(receiver)
no_close_out_addr = Txn.close_remainder_to() == Global.zero_address()
no_rekey_addr = Txn.rekey_to() == Global.zero_address()
acceptable_fee = Txn.fee() <= Int(1000)

return And(
is_payment,
is_single_tx,
is_correct_receiver,
no_close_out_addr,
no_rekey_addr,
acceptable_fee
)

if __name__ == "__main__":
program = bank_for_account("ZZAF5ARA4MEC5PVDOP64JM5O5MQST63Q2KOY2FLYFLXXD3PFSNJJBYAFZM")
print(compileTeal(program, Mode.Signature))
24 changes: 24 additions & 0 deletions examples/basic.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma version 2
txn TypeEnum
int pay
==
global GroupSize
int 1
==
&&
txn Receiver
addr ZZAF5ARA4MEC5PVDOP64JM5O5MQST63Q2KOY2FLYFLXXD3PFSNJJBYAFZM
==
&&
txn CloseRemainderTo
global ZeroAddress
==
&&
txn RekeyTo
global ZeroAddress
==
&&
txn Fee
int 1000
<=
&&
53 changes: 35 additions & 18 deletions examples/periodic_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,48 @@

from pyteal import *

#template variables
"""Periodic Payment"""

tmpl_fee = Int(1000)
tmpl_period = Int(1000)
tmpl_dur = Int(1000)
tmpl_lease = Bytes("base64", "y9OJ5MRLCHQj8GqbikAUKMBI7hom+SOj8dlopNdNHXI=")
tmpl_amt = Int(200000)
tmpl_rcv = Addr("ZZAF5ARA4MEC5PVDOP64JM5O5MQST63Q2KOY2FLYFLXXD3PFSNJJBYAFZM")
tmpl_period = Int(50)
tmpl_dur = Int(5000)
tmpl_lease = Bytes("base64", "023sdDE2")
tmpl_amt = Int(2000)
tmpl_rcv = Addr("6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY")
tmpl_timeout = Int(30000)

def periodic_payment(tmpl_fee=tmpl_fee,
tmpl_period=tmpl_period,
tmpl_dur=tmpl_dur,
tmpl_lease=tmpl_lease,
tmpl_amt=tmpl_amt,
tmpl_rcv=tmpl_rcv):
tmpl_rcv=tmpl_rcv,
tmpl_timeout=tmpl_timeout):

periodic_pay_core = And(
Txn.type_enum() == TxnType.Payment,
Txn.fee() < tmpl_fee,
Txn.first_valid() % tmpl_period == Int(0),
Txn.last_valid() == tmpl_dur + Txn.first_valid(),
Txn.lease() == tmpl_lease
)

periodic_pay_transfer = And(
Txn.close_remainder_to() == Global.zero_address(),
Txn.receiver() == tmpl_rcv,
Txn.amount() == tmpl_amt
)

periodic_pay_close = And(
Txn.close_remainder_to() == tmpl_rcv,
Txn.receiver() == Global.zero_address(),
Txn.first_valid() == tmpl_timeout,
Txn.amount() == Int(0)
)

periodic_pay_core = And(Txn.type_enum() == Int(1),
Txn.fee() <= tmpl_fee)

periodic_pay_transfer = And(Txn.close_remainder_to() == Global.zero_address(),
Txn.receiver() == tmpl_rcv,
Txn.amount() == tmpl_amt,
Txn.first_valid() % tmpl_period == Int(0),
Txn.last_valid() == tmpl_dur + Txn.first_valid(),
Txn.lease() == tmpl_lease)
periodic_pay_escrow = periodic_pay_core.And(periodic_pay_transfer.Or(periodic_pay_close))

return And(periodic_pay_core, periodic_pay_transfer)
return periodic_pay_escrow

# print(compileTeal(periodic_payment(), Mode.Signature))
if __name__ == "__main__":
print(compileTeal(periodic_payment(), Mode.Signature))
2 changes: 1 addition & 1 deletion examples/periodic_payment_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#--------- compile & send transaction using Goal and Python SDK ----------

teal_source = compileTeal(periodic_payment())
teal_source = compileTeal(periodic_payment(), Mode.Signature)

# compile teal
teal_file = str(uuid.uuid4()) + ".teal"
Expand Down
63 changes: 40 additions & 23 deletions examples/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

from pyteal import *

"""Split
"""
"""Split Payment"""

# template variables
tmpl_fee = Int(1000)
tmpl_rcv1 = Addr("6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY")
tmpl_rcv2 = Addr("7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M")
Expand All @@ -15,23 +13,42 @@
tmpl_min_pay = Int(1000)
tmpl_timeout = Int(3000)


split_core = (Txn.type_enum() == Int(1)).And(Txn.fee() < tmpl_fee)

split_transfer = And(Gtxn.sender(0) == Gtxn.sender(1),
Txn.close_remainder_to() == Global.zero_address(),
Gtxn.receiver(0) == tmpl_rcv1,
Gtxn.receiver(1) == tmpl_rcv2,
Gtxn.amount(0) == ((Gtxn.amount(0) + Gtxn.amount(1)) * tmpl_ratn) / tmpl_ratd,
Gtxn.amount(0) == tmpl_min_pay)

split_close = And(Txn.close_remainder_to() == tmpl_own,
Txn.receiver() == Global.zero_address(),
Txn.first_valid() == tmpl_timeout)

split = And(split_core,
If(Global.group_size() == Int(2),
split_transfer,
split_close))

print(compileTeal(split, Mode.Signature))
def split(tmpl_fee=tmpl_fee,
tmpl_rcv1=tmpl_rcv1,
tmpl_rcv2=tmpl_rcv2,
tmpl_own=tmpl_own,
tmpl_ratn=tmpl_ratn,
tmpl_ratd=tmpl_ratd,
tmpl_min_pay=tmpl_min_pay,
tmpl_timeout=tmpl_timeout):

split_core = (Txn.type_enum() == TxnType.Payment).And(Txn.fee() < tmpl_fee)

split_transfer = And(
Gtxn[0].sender() == Gtxn[1].sender(),
Txn.close_remainder_to() == Global.zero_address(),
Gtxn[0].receiver() == tmpl_rcv1,
Gtxn[1].receiver() == tmpl_rcv2,
Gtxn[0].amount() == ((Gtxn[0].amount() + Gtxn[1].amount()) * tmpl_ratn) / tmpl_ratd,
Gtxn[0].amount() == tmpl_min_pay
)

split_close = And(
Txn.close_remainder_to() == tmpl_own,
Txn.receiver() == Global.zero_address(),
Txn.amount() == Int(0),
Txn.first_valid() > tmpl_timeout
)

split = And(
split_core,
If(Global.group_size() == Int(2),
split_transfer,
split_close
)
)

return split

if __name__ == "__main__":
print(compileTeal(split, Mode.Signature))
5 changes: 3 additions & 2 deletions pyteal/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

# properties
from .arg import Arg
from .txn import TxnType, TxnField, TxnExpr, TxnaExpr, Txn
from .gtxn import GtxnExpr, GtxnaExpr, Gtxn
from .txn import TxnType, TxnField, TxnExpr, TxnaExpr, TxnArray, TxnObject, Txn
from .gtxn import GtxnExpr, GtxnaExpr, TxnGroup, Gtxn
from .global_ import Global, GlobalField
from .app import App, AppField, OnComplete
from .asset import AssetHolding, AssetParam

# meta
from .array import Array
from .tmpl import Tmpl
from .nonce import Nonce

Expand Down
2 changes: 2 additions & 0 deletions pyteal/ast/addr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ def __str__(self):

def type_of(self):
return TealType.bytes

Addr.__module__ = "pyteal"

0 comments on commit 7865220

Please sign in to comment.