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

feat: spike out what unit testing puyapy contracts could look like #117

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 30 additions & 30 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
repos:
- repo: local
hooks:
- id: black
name: black
description: "Black: The uncompromising Python code formatter"
entry: poetry run black .
pass_filenames: false
require_serial: true
language: system
minimum_pre_commit_version: 2.9.2
types_or: [ python, pyi ]
- id: ruff
name: ruff
description: "Run 'ruff' for extremely fast Python linting"
entry: poetry run ruff
language: system
'types': [python]
args: [--fix, --no-cache]
additional_dependencies: []
minimum_pre_commit_version: '0'
- id: mypy
name: mypy
description: '`mypy` will check Python types for correctness'
entry: poetry run mypy
language: system
types_or: [ python, pyi ]
require_serial: true
additional_dependencies: [ ]
minimum_pre_commit_version: '2.9.2'
exclude: '^src/puya/(_vendor|_typeshed)/'
- repo: local
hooks:
- id: black
name: black
description: "Black: The uncompromising Python code formatter"
entry: poetry run black .
pass_filenames: false
require_serial: true
language: system
minimum_pre_commit_version: 2.9.2
types_or: [python, pyi]
- id: ruff
name: ruff
description: "Run 'ruff' for extremely fast Python linting"
entry: poetry run ruff
language: system
"types": [python]
args: [--fix, --no-cache]
additional_dependencies: []
minimum_pre_commit_version: "0"
- id: mypy
name: mypy
description: "`mypy` will check Python types for correctness"
entry: poetry run mypy
language: system
types_or: [python, pyi]
require_serial: true
additional_dependencies: []
minimum_pre_commit_version: "2.9.2"
exclude: "^src/(puya/_vendor|puya/_typeshed|puyapy)/"
5 changes: 5 additions & 0 deletions examples/amm/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,14 @@ 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:
balance_before = op.AssetHoldingGet.asset_balance(receiver, asset.asset_id)

itxn.AssetTransfer(
xfer_asset=asset,
asset_amount=amount,
asset_receiver=receiver,
fee=0,
).submit()

balance_after = op.AssetHoldingGet.asset_balance(receiver, asset.asset_id)
assert balance_before[0] + amount == balance_after[0], "Balance not updated correctly"
44 changes: 32 additions & 12 deletions examples/amm/out/ConstantProductAMM.approval.mir

Large diffs are not rendered by default.

27 changes: 23 additions & 4 deletions examples/amm/out/ConstantProductAMM.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,13 @@ do_asset_transfer:
// @subroutine
// def do_asset_transfer(*, receiver: Account, asset: Asset, amount: UInt64) -> None:
proto 3 0
// amm/contract.py:361-366
// amm/contract.py:361
// balance_before = op.AssetHoldingGet.asset_balance(receiver, asset.asset_id)
frame_dig -3
frame_dig -2
asset_holding_get AssetBalance
pop
// amm/contract.py:363-368
// itxn.AssetTransfer(
// xfer_asset=asset,
// asset_amount=amount,
Expand All @@ -489,26 +495,39 @@ do_asset_transfer:
itxn_begin
frame_dig -2
itxn_field XferAsset
// amm/contract.py:361
// amm/contract.py:363
// itxn.AssetTransfer(
int axfer
itxn_field TypeEnum
// amm/contract.py:365
// amm/contract.py:367
// fee=0,
int 0
itxn_field Fee
frame_dig -3
itxn_field AssetReceiver
frame_dig -1
itxn_field AssetAmount
// amm/contract.py:361-366
// amm/contract.py:363-368
// itxn.AssetTransfer(
// xfer_asset=asset,
// asset_amount=amount,
// asset_receiver=receiver,
// fee=0,
// ).submit()
itxn_submit
// amm/contract.py:370
// balance_after = op.AssetHoldingGet.asset_balance(receiver, asset.asset_id)
frame_dig -3
frame_dig -2
asset_holding_get AssetBalance
pop
// amm/contract.py:371
// assert balance_before[0] + amount == balance_after[0], "Balance not updated correctly"
swap
frame_dig -1
+
==
assert // Balance not updated correctly
retsub


Expand Down
2 changes: 1 addition & 1 deletion examples/amm/out/ConstantProductAMM.arc32.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions examples/amm/out/ConstantProductAMM.destructured.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion examples/amm/out/ConstantProductAMM.ssa.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions examples/amm/out/ConstantProductAMM.ssa.opt_pass_1.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/amm/out/contract.awst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions examples/amm/out_O2/ConstantProductAMM.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ _do_opt_in:
// examples.amm.contract.do_asset_transfer(receiver: bytes, asset: uint64, amount: uint64) -> void:
do_asset_transfer:
proto 3 0
frame_dig -3
frame_dig -2
asset_holding_get AssetBalance
pop
itxn_begin
frame_dig -2
itxn_field XferAsset
Expand All @@ -301,6 +305,15 @@ do_asset_transfer:
frame_dig -1
itxn_field AssetAmount
itxn_submit
frame_dig -3
frame_dig -2
asset_holding_get AssetBalance
pop
swap
frame_dig -1
+
==
assert // Balance not updated correctly
retsub


Expand Down
5 changes: 5 additions & 0 deletions examples/amm/out_O2/ConstantProductAMM.destructured.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions examples/amm/out_unoptimized/ConstantProductAMM.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,13 @@ do_asset_transfer:
// @subroutine
// def do_asset_transfer(*, receiver: Account, asset: Asset, amount: UInt64) -> None:
proto 3 0
// amm/contract.py:361-366
// amm/contract.py:361
// balance_before = op.AssetHoldingGet.asset_balance(receiver, asset.asset_id)
frame_dig -3
frame_dig -2
asset_holding_get AssetBalance
pop
// amm/contract.py:363-368
// itxn.AssetTransfer(
// xfer_asset=asset,
// asset_amount=amount,
Expand All @@ -531,10 +537,10 @@ do_asset_transfer:
// def do_asset_transfer(*, receiver: Account, asset: Asset, amount: UInt64) -> None:
frame_dig -1
frame_dig -3
// amm/contract.py:365
// amm/contract.py:367
// fee=0,
int 0
// amm/contract.py:361
// amm/contract.py:363
// itxn.AssetTransfer(
int axfer
// amm/contract.py:360
Expand All @@ -545,14 +551,28 @@ do_asset_transfer:
itxn_field Fee
itxn_field AssetReceiver
itxn_field AssetAmount
// amm/contract.py:361-366
// amm/contract.py:363-368
// itxn.AssetTransfer(
// xfer_asset=asset,
// asset_amount=amount,
// asset_receiver=receiver,
// fee=0,
// ).submit()
itxn_submit
// amm/contract.py:370
// balance_after = op.AssetHoldingGet.asset_balance(receiver, asset.asset_id)
frame_dig -3
frame_dig -2
asset_holding_get AssetBalance
pop
// amm/contract.py:371
// assert balance_before[0] + amount == balance_after[0], "Balance not updated correctly"
swap
frame_dig -1
+
swap
==
assert // Balance not updated correctly
retsub


Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions examples/amm/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.