Skip to content

Commit

Permalink
Improve array interface (#18)
Browse files Browse the repository at this point in the history
* Improve array interface for txn/gtxn/txna/gtxna
  • Loading branch information
jasonpaulos committed Aug 19, 2020
1 parent c8c52fb commit b46a663
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 524 deletions.
92 changes: 46 additions & 46 deletions examples/dutch_auction.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,54 @@
wrapup_time = Tmpl.Int("TMPL_WRAPUP_ROUND")

# the definition of i simplifies the original desgin by only constraining last_valid here
i_upper = (Gtxn.last_valid(0)- start_round)/ period
i_upper = (Gtxn[0].last_valid()- start_round)/ period

bid = And(Gtxn.last_valid(0) == Gtxn.last_valid(1),
Gtxn.last_valid(1) == Gtxn.last_valid(2),
Gtxn.last_valid(2) == Gtxn.last_valid(3),
Gtxn.last_valid(3) == Gtxn.last_valid(4),
Gtxn.last_valid(4) < (start_round + i_upper * period),
Gtxn.type_enum(0) == Int(4), # asset transfer
Gtxn.xfer_asset(0) == asset_d,
Gtxn.receiver(0) == receiver,
Gtxn.type_enum(1) == Int(4),
Gtxn.xfer_asset(1) == asset_b,
Gtxn.type_enum(2) == Int(4),
Gtxn.xfer_asset(2) == asset_c,
Gtxn.type_enum(3) == Int(4),
Gtxn.xfer_asset(3) == asset_c,
Gtxn.type_enum(4) == Int(1),
Gtxn.amount(4) == (Gtxn.fee(1) + Gtxn.fee(2) + Gtxn.fee(3)), #? why only 1, 2, 3
Gtxn.asset_amount(0) == Gtxn.asset_amount(1),
Gtxn.asset_amount(1) == Gtxn.asset_amount(2),
Gtxn.asset_amount(3) == i_upper * supply * price_decrement,
Gtxn.sender(0) == Gtxn.receiver(1),
Gtxn.receiver(2) == asset_c_sink,
Gtxn.sender(1) == Gtxn.sender(2),
Gtxn.sender(2) == Gtxn.sender(3),
Gtxn.sender(3) == Gtxn.receiver(3),
Gtxn.receiver(3) == Gtxn.receiver(4))
bid = And(Gtxn[0].last_valid() == Gtxn[1].last_valid(),
Gtxn[1].last_valid() == Gtxn[2].last_valid(),
Gtxn[2].last_valid() == Gtxn[3].last_valid(),
Gtxn[3].last_valid() == Gtxn[4].last_valid(),
Gtxn[4].last_valid() < (start_round + i_upper * period),
Gtxn[0].type_enum() == Int(4), # asset transfer
Gtxn[0].xfer_asset() == asset_d,
Gtxn[0].receiver() == receiver,
Gtxn[1].type_enum() == Int(4),
Gtxn[1].xfer_asset() == asset_b,
Gtxn[2].type_enum() == Int(4),
Gtxn[2].xfer_asset() == asset_c,
Gtxn[3].type_enum() == Int(4),
Gtxn[4].xfer_asset() == asset_c,
Gtxn[4].type_enum() == Int(1),
Gtxn[4].amount() == (Gtxn[1].fee() + Gtxn[2].fee() + Gtxn[3].fee()), #? why only 1, 2, 3
Gtxn[0].asset_amount() == Gtxn[1].asset_amount(),
Gtxn[1].asset_amount() == Gtxn[2].asset_amount(),
Gtxn[3].asset_amount() == i_upper * supply * price_decrement,
Gtxn[0].sender() == Gtxn[1].receiver(),
Gtxn[2].receiver() == asset_c_sink,
Gtxn[1].sender() == Gtxn[2].sender(),
Gtxn[2].sender() == Gtxn[3].sender(),
Gtxn[3].sender() == Gtxn[3].receiver(),
Gtxn[3].receiver() == Gtxn[4].receiver())

redeem = And(Gtxn.first_valid(0) == Gtxn.first_valid(1),
Gtxn.first_valid(1) == Gtxn.first_valid(2),
Gtxn.first_valid(2) == Gtxn.first_valid(3),
Gtxn.first_valid(3) >= redeem_round,
Gtxn.type_enum(0) == Int(4),
Gtxn.xfer_asset(0) == asset_b,
Gtxn.type_enum(1) == Int(4),
Gtxn.xfer_asset(1) == asset_a,
Gtxn.type_enum(2) == Int(4),
Gtxn.xfer_asset(2) == asset_c,
Gtxn.asset_amount(2) == redeem_round * supply * price_decrement,
Gtxn.type_enum(3) == Int(1),
Gtxn.amount(3) == Gtxn.fee(1) + Gtxn.fee(2),
Gtxn.asset_amount(1) ==
Gtxn.asset_amount(0) * (start_price - price_decrement * Btoi(Arg(0))),
Gtxn.sender(0) == Gtxn.receiver(1),
Gtxn.receiver(0) == Gtxn.sender(1),
Gtxn.sender(1) == Gtxn.sender(2),
Gtxn.sender(2) == Gtxn.receiver(2),
Gtxn.receiver(2) == Gtxn.receiver(3))
redeem = And(Gtxn[0].first_valid() == Gtxn[1].first_valid(),
Gtxn[1].first_valid() == Gtxn[2].first_valid(),
Gtxn[2].first_valid() == Gtxn[3].first_valid(),
Gtxn[3].first_valid() >= redeem_round,
Gtxn[0].type_enum() == Int(4),
Gtxn[0].xfer_asset() == asset_b,
Gtxn[1].type_enum() == Int(4),
Gtxn[1].xfer_asset() == asset_a,
Gtxn[2].type_enum() == Int(4),
Gtxn[2].xfer_asset() == asset_c,
Gtxn[2].asset_amount() == redeem_round * supply * price_decrement,
Gtxn[3].type_enum() == Int(1),
Gtxn[3].amount() == Gtxn[1].fee() + Gtxn[2].fee(),
Gtxn[1].asset_amount() ==
Gtxn[0].asset_amount() * (start_price - price_decrement * Btoi(Arg(0))),
Gtxn[0].sender() == Gtxn[1].receiver(),
Gtxn[0].receiver() == Gtxn[1].sender(),
Gtxn[1].sender() == Gtxn[2].sender(),
Gtxn[2].sender() == Gtxn[2].receiver(),
Gtxn[2].receiver() == Gtxn[3].receiver())

wrapup = And(Global.group_size() == Int(1),
Txn.first_valid() >= start_round + wrapup_time,
Expand Down
4 changes: 2 additions & 2 deletions pyteal/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

# properties
from .arg import Arg
from .txn import TxnType, TxnField, Txn, Txna
from .gtxn import Gtxn
from .txn import TxnType, TxnField, TxnExpr, TxnaExpr, Txn
from .gtxn import GtxnExpr, GtxnaExpr, Gtxn
from .global_ import Global, GlobalField
from .app import App, AppField, OnComplete
from .asset import AssetHolding, AssetParam
Expand Down
16 changes: 16 additions & 0 deletions pyteal/ast/array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from abc import ABC, abstractmethod

from .expr import Expr

class Array(ABC):
"""Represents a variable length array of objects."""

@abstractmethod
def length(self) -> Expr:
"""Get the length of the array."""
pass

@abstractmethod
def __getitem__(self, index: int):
"""Get the value at a given index in this array."""
pass

0 comments on commit b46a663

Please sign in to comment.