Skip to content

Commit

Permalink
feat: move ops into their own module
Browse files Browse the repository at this point in the history
Global and Transaction op values that are constant for a transaction are now class attributes
added `puyapy.log` that can log any primitive type

BREAKING CHANGE: many functions and classes under `puyapy` can now be found in `puyapy.op`. Values that are constant for a transaction in the `puyapy.op.Global` and `puyapy.op.Transaction` classes are now typed as final class vars
  • Loading branch information
daniel-makerx authored Feb 15, 2024
1 parent bdbdb11 commit 7678a7e
Show file tree
Hide file tree
Showing 386 changed files with 26,048 additions and 23,431 deletions.
4 changes: 4 additions & 0 deletions docs/api-puyapy.op.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```{autodoc2-object} puyapy.op
render_plugin = "myst"
```
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ maxdepth: 4
api-puyapy
api-puyapy.arc4
api-puyapy.op
```
82 changes: 39 additions & 43 deletions examples/amm/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
ARC4Contract,
Asset,
AssetTransferTransaction,
CreateInnerTransaction,
Global,
InnerTransaction,
PaymentTransaction,
Transaction,
TransactionType,
UInt64,
arc4,
sqrt,
op,
subroutine,
)

Expand All @@ -37,7 +33,7 @@ def __init__(self) -> None:
# The asset id of asset B
self.asset_b = Asset(0)
# The current governor of this contract, allowed to do admin type actions
self.governor = Transaction.sender()
self.governor = op.Transaction.sender
# The asset id of the Pool Token, used to track share of pool the holder may recover
self.pool_token = Asset(0)
# The ratio between assets (A*Scale/B)
Expand Down Expand Up @@ -68,8 +64,8 @@ def bootstrap(self, seed: PaymentTransaction, a_asset: Asset, b_asset: Asset) ->
"""
assert not self.pool_token, "application has already been bootstrapped"
self._check_is_governor()
assert Global.group_size() == 2, "group size not 2"
assert seed.receiver == Global.current_application_address(), "receiver not app address"
assert op.Global.group_size == 2, "group size not 2"
assert seed.receiver == op.Global.current_application_address, "receiver not app address"

assert seed.amount >= 300_000, "amount minimum not met" # 0.3 Algos
assert a_asset.asset_id < b_asset.asset_id, "asset a must be less than asset b"
Expand Down Expand Up @@ -117,19 +113,19 @@ def mint(
assert pool_asset == self.pool_token, "asset pool incorrect"
assert a_asset == self.asset_a, "asset a incorrect"
assert b_asset == self.asset_b, "asset b incorrect"
assert a_xfer.sender == Transaction.sender(), "sender invalid"
assert b_xfer.sender == Transaction.sender(), "sender invalid"
assert a_xfer.sender == op.Transaction.sender, "sender invalid"
assert b_xfer.sender == op.Transaction.sender, "sender invalid"

# valid asset a xfer
assert (
a_xfer.asset_receiver == Global.current_application_address()
a_xfer.asset_receiver == op.Global.current_application_address
), "receiver not app address"
assert a_xfer.xfer_asset == self.asset_a, "asset a incorrect"
assert a_xfer.asset_amount > 0, "amount minimum not met"

# valid asset b xfer
assert (
b_xfer.asset_receiver == Global.current_application_address()
b_xfer.asset_receiver == op.Global.current_application_address
), "receiver not app address"
assert b_xfer.xfer_asset == self.asset_b, "asset b incorrect"
assert b_xfer.asset_amount > 0, "amount minimum not met"
Expand All @@ -144,7 +140,7 @@ def mint(
assert to_mint > 0, "send amount too low"

# mint tokens
do_asset_transfer(receiver=Transaction.sender(), asset=self.pool_token, amount=to_mint)
do_asset_transfer(receiver=op.Transaction.sender, asset=self.pool_token, amount=to_mint)
self._update_ratio()

@arc4.abimethod(
Expand Down Expand Up @@ -177,11 +173,11 @@ def burn(
assert b_asset == self.asset_b, "asset b incorrect"

assert (
pool_xfer.asset_receiver == Global.current_application_address()
pool_xfer.asset_receiver == op.Global.current_application_address
), "receiver not app address"
assert pool_xfer.asset_amount > 0, "amount minimum not met"
assert pool_xfer.xfer_asset == self.pool_token, "asset pool incorrect"
assert pool_xfer.sender == Transaction.sender(), "sender invalid"
assert pool_xfer.sender == op.Transaction.sender, "sender invalid"

# Get the total number of tokens issued
# !important: this happens prior to receiving the current axfer of pool tokens
Expand All @@ -198,10 +194,10 @@ def burn(
)

# Send back commensurate amt of a
do_asset_transfer(receiver=Transaction.sender(), asset=self.asset_a, amount=a_amt)
do_asset_transfer(receiver=op.Transaction.sender, asset=self.asset_a, amount=a_amt)

# Send back commensurate amt of b
do_asset_transfer(receiver=Transaction.sender(), asset=self.asset_b, amount=b_amt)
do_asset_transfer(receiver=op.Transaction.sender, asset=self.asset_b, amount=b_amt)
self._update_ratio()

@arc4.abimethod(
Expand Down Expand Up @@ -229,7 +225,7 @@ def swap(
assert b_asset == self.asset_b, "asset b incorrect"

assert swap_xfer.asset_amount > 0, "amount minimum not met"
assert swap_xfer.sender == Transaction.sender(), "sender invalid"
assert swap_xfer.sender == op.Transaction.sender, "sender invalid"

match swap_xfer.xfer_asset:
case self.asset_a:
Expand All @@ -248,7 +244,7 @@ def swap(
)
assert to_swap > 0, "send amount too low"

do_asset_transfer(receiver=Transaction.sender(), asset=out_asset, amount=to_swap)
do_asset_transfer(receiver=op.Transaction.sender, asset=out_asset, amount=to_swap)
self._update_ratio()

@subroutine
Expand All @@ -265,45 +261,45 @@ def _update_ratio(self) -> None:
@subroutine
def _check_is_governor(self) -> None:
assert (
Transaction.sender() == self.governor
op.Transaction.sender == self.governor
), "Only the account set in global_state.governor may call this method"

@subroutine
def _create_pool_token(self) -> Asset:
CreateInnerTransaction.begin()
CreateInnerTransaction.set_type_enum(TransactionType.AssetConfig)
CreateInnerTransaction.set_config_asset_name(
op.CreateInnerTransaction.begin()
op.CreateInnerTransaction.set_type_enum(TransactionType.AssetConfig)
op.CreateInnerTransaction.set_config_asset_name(
b"DPT-" + self.asset_a.unit_name + b"-" + self.asset_b.unit_name
)
CreateInnerTransaction.set_config_asset_unit_name(b"dpt")
CreateInnerTransaction.set_config_asset_total(TOTAL_SUPPLY)
CreateInnerTransaction.set_config_asset_decimals(3)
CreateInnerTransaction.set_config_asset_manager(Global.current_application_address())
CreateInnerTransaction.set_config_asset_reserve(Global.current_application_address())
CreateInnerTransaction.set_fee(0)
CreateInnerTransaction.submit()
op.CreateInnerTransaction.set_config_asset_unit_name(b"dpt")
op.CreateInnerTransaction.set_config_asset_total(TOTAL_SUPPLY)
op.CreateInnerTransaction.set_config_asset_decimals(3)
op.CreateInnerTransaction.set_config_asset_manager(op.Global.current_application_address)
op.CreateInnerTransaction.set_config_asset_reserve(op.Global.current_application_address)
op.CreateInnerTransaction.set_fee(0)
op.CreateInnerTransaction.submit()

return Asset(InnerTransaction.created_asset_id())
return Asset(op.InnerTransaction.created_asset_id())

@subroutine
def _do_opt_in(self, asset: Asset) -> None:
do_asset_transfer(
receiver=Global.current_application_address(),
receiver=op.Global.current_application_address,
asset=asset,
amount=UInt64(0),
)

@subroutine
def _current_pool_balance(self) -> UInt64:
return self.pool_token.balance(Global.current_application_address())
return self.pool_token.balance(op.Global.current_application_address)

@subroutine
def _current_a_balance(self) -> UInt64:
return self.asset_a.balance(Global.current_application_address())
return self.asset_a.balance(op.Global.current_application_address)

@subroutine
def _current_b_balance(self) -> UInt64:
return self.asset_b.balance(Global.current_application_address())
return self.asset_b.balance(op.Global.current_application_address)


##############
Expand Down Expand Up @@ -335,7 +331,7 @@ def tokens_to_mint(
) -> UInt64:
is_initial_mint = a_balance == a_amount and b_balance == b_amount
if is_initial_mint:
return sqrt(a_amount * b_amount) - SCALE
return op.sqrt(a_amount * b_amount) - SCALE
issued = TOTAL_SUPPLY - pool_balance
a_ratio = SCALE * a_amount // (a_balance - a_amount)
b_ratio = SCALE * b_amount // (b_balance - b_amount)
Expand All @@ -360,10 +356,10 @@ def tokens_to_swap(*, in_amount: UInt64, in_supply: UInt64, out_supply: UInt64)

@subroutine
def do_asset_transfer(*, receiver: Account, asset: Asset, amount: UInt64) -> None:
CreateInnerTransaction.begin()
CreateInnerTransaction.set_type_enum(TransactionType.AssetTransfer)
CreateInnerTransaction.set_xfer_asset(asset.asset_id)
CreateInnerTransaction.set_asset_amount(amount)
CreateInnerTransaction.set_asset_receiver(receiver)
CreateInnerTransaction.set_fee(0)
CreateInnerTransaction.submit()
op.CreateInnerTransaction.begin()
op.CreateInnerTransaction.set_type_enum(TransactionType.AssetTransfer)
op.CreateInnerTransaction.set_xfer_asset(asset.asset_id)
op.CreateInnerTransaction.set_asset_amount(amount)
op.CreateInnerTransaction.set_asset_receiver(receiver)
op.CreateInnerTransaction.set_fee(0)
op.CreateInnerTransaction.submit()
4 changes: 2 additions & 2 deletions examples/amm/out/application.json

Large diffs are not rendered by default.

Loading

0 comments on commit 7678a7e

Please sign in to comment.