Skip to content

Commit

Permalink
chore: simplified while loop example
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Kim (Hyunggun) committed Nov 22, 2024
1 parent b7de7f9 commit 264209a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ __puya_arc4_router__:
proto 0 1
txn NumAppArgs
bz __puya_arc4_router___bare_routing@5
method "clean_rooms()uint64"
method "loop()uint64"
txna ApplicationArgs 0
match __puya_arc4_router___clean_rooms_route@2
match __puya_arc4_router___loop_route@2
int 0
retsub

__puya_arc4_router___clean_rooms_route@2:
__puya_arc4_router___loop_route@2:
txn OnCompletion
!
assert // OnCompletion is NoOp
txn ApplicationID
assert // is not creating
callsub clean_rooms
callsub loop
itob
byte 0x151f7c75
swap
Expand All @@ -45,52 +45,47 @@ __puya_arc4_router___after_if_else@9:
retsub


// smart_contracts.control_flow.contract.WhileLoopExample.clean_rooms() -> uint64:
clean_rooms:
// smart_contracts.control_flow.contract.WhileLoopExample.loop() -> uint64:
loop:
proto 0 1
byte ""
int 100
byte "kitchen"

clean_rooms_while_top@1:
frame_dig 1
dup
frame_bury 0
bz clean_rooms_after_while@12
frame_dig 2
byte "closet"
==
bz clean_rooms_after_if_else@4
byte "kitchen"
frame_bury 2
b clean_rooms_while_top@1
int 10
int 0

clean_rooms_after_if_else@4:
loop_while_top@1:
frame_dig 2
byte "kitchen"
==
bz clean_rooms_else_body@6
byte "living room"
frame_bury 2
b clean_rooms_after_if_else@9

clean_rooms_else_body@6:
frame_bury 0
frame_dig 1
bz loop_after_while@7
frame_dig 1
int 5
>
bz loop_after_if_else@4
frame_dig 1
int 1
-
frame_bury 1
frame_dig 2
byte "living room"
==
bz clean_rooms_after_if_else@9
byte "closet"
int 1
+
frame_bury 2
b loop_while_top@1

clean_rooms_after_if_else@9:
loop_after_if_else@4:
frame_dig 1
int 50
int 2
-
dup
frame_bury 1
bnz clean_rooms_while_top@1
frame_dig 1
frame_dig 2
int 1
+
frame_bury 2
int 1
==
bz loop_while_top@1
frame_dig 2
frame_bury 0

clean_rooms_after_while@12:
loop_after_while@7:
retsub
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"hints": {
"clean_rooms()uint64": {
"loop()uint64": {
"call_config": {
"no_op": "CALL"
}
}
},
"source": {
"approval": "I3ByYWdtYSB2ZXJzaW9uIDEwCgpzbWFydF9jb250cmFjdHMuY29udHJvbF9mbG93LmNvbnRyYWN0LldoaWxlTG9vcEV4YW1wbGUuYXBwcm92YWxfcHJvZ3JhbToKICAgIGNhbGxzdWIgX19wdXlhX2FyYzRfcm91dGVyX18KICAgIHJldHVybgoKCi8vIHNtYXJ0X2NvbnRyYWN0cy5jb250cm9sX2Zsb3cuY29udHJhY3QuV2hpbGVMb29wRXhhbXBsZS5fX3B1eWFfYXJjNF9yb3V0ZXJfXygpIC0+IHVpbnQ2NDoKX19wdXlhX2FyYzRfcm91dGVyX186CiAgICBwcm90byAwIDEKICAgIHR4biBOdW1BcHBBcmdzCiAgICBieiBfX3B1eWFfYXJjNF9yb3V0ZXJfX19iYXJlX3JvdXRpbmdANQogICAgbWV0aG9kICJjbGVhbl9yb29tcygpdWludDY0IgogICAgdHhuYSBBcHBsaWNhdGlvbkFyZ3MgMAogICAgbWF0Y2ggX19wdXlhX2FyYzRfcm91dGVyX19fY2xlYW5fcm9vbXNfcm91dGVAMgogICAgaW50IDAKICAgIHJldHN1YgoKX19wdXlhX2FyYzRfcm91dGVyX19fY2xlYW5fcm9vbXNfcm91dGVAMjoKICAgIHR4biBPbkNvbXBsZXRpb24KICAgICEKICAgIGFzc2VydCAvLyBPbkNvbXBsZXRpb24gaXMgTm9PcAogICAgdHhuIEFwcGxpY2F0aW9uSUQKICAgIGFzc2VydCAvLyBpcyBub3QgY3JlYXRpbmcKICAgIGNhbGxzdWIgY2xlYW5fcm9vbXMKICAgIGl0b2IKICAgIGJ5dGUgMHgxNTFmN2M3NQogICAgc3dhcAogICAgY29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ArCiAgICBmcmFtZV9idXJ5IDIKICAgIGIgbG9vcF93aGlsZV90b3BAMQoKbG9vcF9hZnRlcl9pZl9lbHNlQDQ6CiAgICBmcmFtZV9kaWcgMQogICAgaW50IDIKICAgIC0KICAgIGR1cAogICAgZnJhbWVfYnVyeSAxCiAgICBmcmFtZV9kaWcgMgogICAgaW50IDEKICAgICsKICAgIGZyYW1lX2J1cnkgMgogICAgaW50IDEKICAgID09CiAgICBieiBsb29wX3doaWxlX3RvcEAxCiAgICBmcmFtZV9kaWcgMgogICAgZnJhbWVfYnVyeSAwCgpsb29wX2FmdGVyX3doaWxlQDc6CiAgICByZXRzdWIK",
"clear": "I3ByYWdtYSB2ZXJzaW9uIDEwCgpzbWFydF9jb250cmFjdHMuY29udHJvbF9mbG93LmNvbnRyYWN0LldoaWxlTG9vcEV4YW1wbGUuY2xlYXJfc3RhdGVfcHJvZ3JhbToKICAgIGludCAxCiAgICByZXR1cm4K"
},
"state": {
Expand All @@ -34,7 +34,7 @@
"name": "WhileLoopExample",
"methods": [
{
"name": "clean_rooms",
"name": "loop",
"args": [],
"readonly": false,
"returns": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

_APP_SPEC_JSON = r"""{
"hints": {
"clean_rooms()uint64": {
"loop()uint64": {
"call_config": {
"no_op": "CALL"
}
}
},
"source": {
"approval": "I3ByYWdtYSB2ZXJzaW9uIDEwCgpzbWFydF9jb250cmFjdHMuY29udHJvbF9mbG93LmNvbnRyYWN0LldoaWxlTG9vcEV4YW1wbGUuYXBwcm92YWxfcHJvZ3JhbToKICAgIGNhbGxzdWIgX19wdXlhX2FyYzRfcm91dGVyX18KICAgIHJldHVybgoKCi8vIHNtYXJ0X2NvbnRyYWN0cy5jb250cm9sX2Zsb3cuY29udHJhY3QuV2hpbGVMb29wRXhhbXBsZS5fX3B1eWFfYXJjNF9yb3V0ZXJfXygpIC0+IHVpbnQ2NDoKX19wdXlhX2FyYzRfcm91dGVyX186CiAgICB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X3B1eWFfYXJjNF9yb3V0ZXJfX19hZnRlcl9pZl9lbHNlQDk6CiAgICBpbnQgMAogICAgcmV0c3ViCgoKLy8gc21hcnRfY29udHJhY3RzLmNvbnRyb2xfZmxvdy5jb250cmFjdC5XaGlsZUxvb3BFeGFtcGxlLmxvb3AoKSAtPiB1aW50NjQ6Cmxvb3A6CiAgICBwcm90byAwIDEKICAgIGJ5dGUgIiIKICAgIGludCAxMAogICAgaW50IDAKCmxvb3Bfd2hpbGVfdG9wQDE6CiAgICBmcmFtZV9kaWcgMgogICAgZnJhbWVfYnVyeSAwCiAgICBmcmFtZV9kaWcgMQogICAgYnogbG9vcF9hZnRlcl93aGlsZUA3CiAgICBmcmFtZV9kaWcgMQogICAgaW50IDUKICAgID4KICAgIGJ6IGxvb3BfYWZ0ZXJfaWZfZWxzZUA0CiAgICBmcmFtZV9kaWcgMQogICAgaW50IDEKICAgIC0KICAgIGZyYW1lX2J1cnkgMQogICAgZnJhbWVfZGlnIDIKICAgIGludCAxCiAgICArCiAgICBmcmFtZV9idXJ5IDIKICAgIGIgbG9vcF93aGlsZV90b3BAMQoKbG9vcF9hZnRlcl9pZl9lbHNlQDQ6CiAgICBmcmFtZV9kaWcgMQogICAgaW50IDIKICAgIC0KICAgIGR1cAogICAgZnJhbWVfYnVyeSAxCiAgICBmcmFtZV9kaWcgMgogICAgaW50IDEKICAgICsKICAgIGZyYW1lX2J1cnkgMgogICAgaW50IDEKICAgID09CiAgICBieiBsb29wX3doaWxlX3RvcEAxCiAgICBmcmFtZV9kaWcgMgogICAgZnJhbWVfYnVyeSAwCgpsb29wX2FmdGVyX3doaWxlQDc6CiAgICByZXRzdWIK",
"clear": "I3ByYWdtYSB2ZXJzaW9uIDEwCgpzbWFydF9jb250cmFjdHMuY29udHJvbF9mbG93LmNvbnRyYWN0LldoaWxlTG9vcEV4YW1wbGUuY2xlYXJfc3RhdGVfcHJvZ3JhbToKICAgIGludCAxCiAgICByZXR1cm4K"
},
"state": {
Expand All @@ -57,7 +57,7 @@
"name": "WhileLoopExample",
"methods": [
{
"name": "clean_rooms",
"name": "loop",
"args": [],
"returns": {
"type": "uint64"
Expand Down Expand Up @@ -144,10 +144,10 @@ def _convert_deploy_args(


@dataclasses.dataclass(kw_only=True)
class CleanRoomsArgs(_ArgsBase[int]):
class LoopArgs(_ArgsBase[int]):
@staticmethod
def method() -> str:
return "clean_rooms()uint64"
return "loop()uint64"


@dataclasses.dataclass(kw_only=True)
Expand Down Expand Up @@ -181,17 +181,17 @@ def simulate(self, options: SimulateOptions | None = None) -> SimulateAtomicTran
def execute(self) -> AtomicTransactionResponse:
return self.app_client.execute_atc(self.atc)

def clean_rooms(
def loop(
self,
*,
transaction_parameters: algokit_utils.TransactionParameters | None = None,
) -> "Composer":
"""Adds a call to `clean_rooms()uint64` ABI method
"""Adds a call to `loop()uint64` ABI method
:param algokit_utils.TransactionParameters transaction_parameters: (optional) Additional transaction parameters
:returns Composer: This Composer instance"""

args = CleanRoomsArgs()
args = LoopArgs()
self.app_client.compose_call(
self.atc,
call_abi_method=args.method(),
Expand Down Expand Up @@ -359,17 +359,17 @@ def suggested_params(self) -> algosdk.transaction.SuggestedParams | None:
def suggested_params(self, value: algosdk.transaction.SuggestedParams | None) -> None:
self.app_client.suggested_params = value

def clean_rooms(
def loop(
self,
*,
transaction_parameters: algokit_utils.TransactionParameters | None = None,
) -> algokit_utils.ABITransactionResponse[int]:
"""Calls `clean_rooms()uint64` ABI method
"""Calls `loop()uint64` ABI method
:param algokit_utils.TransactionParameters transaction_parameters: (optional) Additional transaction parameters
:returns algokit_utils.ABITransactionResponse[int]: The result of the transaction"""

args = CleanRoomsArgs()
args = LoopArgs()
result = self.app_client.call(
call_abi_method=args.method(),
transaction_parameters=_convert_call_transaction_parameters(transaction_parameters),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,24 @@ def get_day(self, date: UInt64) -> String:
class WhileLoopExample(ARC4Contract):

@arc4.abimethod
def clean_rooms(self) -> UInt64:
def loop(self) -> UInt64:

# Clean all rooms except the closet
robot_energy = UInt64(100)
current_room = String("kitchen")
num = UInt64(10)
loop_num = UInt64(0)

while robot_energy > 0:
if current_room == "closet":
current_room = String("kitchen")
while num > 0:
if num > 5:
num -= 1
loop_num += 1
continue

if current_room == "kitchen":
current_room = String("living room")
elif current_room == "living room":
current_room = String("closet")
num -= 2
loop_num += 1

robot_energy -= 50

if robot_energy == 0:
if num == 1:
break

return robot_energy
return loop_num


# example: WHILE_LOOP
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def test_while_loop(
while_loop_app_client: while_loop_example_client.WhileLoopExampleClient,
) -> None:

txn_result = while_loop_app_client.clean_rooms()
txn_result = while_loop_app_client.loop()
print(f"loop result: {txn_result.return_value}")

assert txn_result.return_value == 0
assert txn_result.return_value == 7

0 comments on commit 264209a

Please sign in to comment.