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

added control flow code examples #5

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#pragma version 10

smart_contracts.control_flow.contract.ForLoopsExample.approval_program:
callsub __puya_arc4_router__
return


// smart_contracts.control_flow.contract.ForLoopsExample.__puya_arc4_router__() -> uint64:
__puya_arc4_router__:
proto 0 1
txn NumAppArgs
bz __puya_arc4_router___bare_routing@5
method "for_loop()uint8[4]"
txna ApplicationArgs 0
match __puya_arc4_router___for_loop_route@2
int 0
retsub

__puya_arc4_router___for_loop_route@2:
txn OnCompletion
!
assert // OnCompletion is NoOp
txn ApplicationID
assert // is not creating
callsub for_loop
byte 0x151f7c75
swap
concat
log
int 1
retsub

__puya_arc4_router___bare_routing@5:
txn OnCompletion
bnz __puya_arc4_router___after_if_else@9
txn ApplicationID
!
assert // is creating
int 1
retsub

__puya_arc4_router___after_if_else@9:
int 0
retsub


// smart_contracts.control_flow.contract.ForLoopsExample.for_loop() -> bytes:
for_loop:
proto 0 1
byte ""
byte 0x00000000
int 3
int 0

for_loop_for_body@2:
frame_dig 2
dup
itob
extract 7 1
frame_dig 3
dup
int 4
<
assert // Index access is out of bounds
frame_dig 1
swap
uncover 2
replace3
frame_bury 1
bz for_loop_after_for@5
frame_dig 2
int 1
-
frame_bury 2
frame_dig 3
int 1
+
frame_bury 3
b for_loop_for_body@2

for_loop_after_for@5:
int 0
frame_bury 3

for_loop_for_header@6:
frame_dig 3
int 4
<
dup
frame_bury 0
bz for_loop_after_for@9
frame_dig 3
dup
itob
extract 7 1
frame_dig 0
assert // Index access is out of bounds
frame_dig 1
dig 2
uncover 2
replace3
frame_bury 1
int 1
+
frame_bury 3
b for_loop_for_header@6

for_loop_after_for@9:
frame_dig 1
frame_bury 0
retsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"hints": {
"for_loop()uint8[4]": {
"call_config": {
"no_op": "CALL"
}
}
},
"source": {
"approval": "I3ByYWdtYSB2ZXJzaW9uIDEwCgpzbWFydF9jb250cmFjdHMuY29udHJvbF9mbG93LmNvbnRyYWN0LkZvckxvb3BzRXhhbXBsZS5hcHByb3ZhbF9wcm9ncmFtOgogICAgY2FsbHN1YiBfX3B1eWFfYXJjNF9yb3V0ZXJfXwogICAgcmV0dXJuCgoKLy8gc21hcnRfY29udHJhY3RzLmNvbnRyb2xfZmxvdy5jb250cmFjdC5Gb3JMb29wc0V4YW1wbGUuX19wdXlhX2FyYzRfcm91dGVyX18oKSAtPiB1aW50NjQ6Cl9fcHV5YV9hcmM0X3JvdXRlcl9fOgogICAgcHJvdG8gMCAxCiAgICB0eG4gTnVtQXBwQXJncwogICAgYnogX19wdXlhX2FyYzRfcm91dGVyX19fYmFyZV9yb3V0aW5nQDUKICAgIG1ldGhvZCAiZm9yX2xvb3AoKXVpbnQ4WzRdIgogICAgdHhuYSBBcHBsaWNhdGlvbkFyZ3MgMAogICAgbWF0Y2ggX19wdXlhX2FyYzRfcm91dGVyX19fZm9yX2xvb3Bfcm91dGVAMgogICAgaW50IDAKICAgIHJldHN1YgoKX19wdXlhX2FyYzRfcm91dGVyX19fZm9yX2xvb3Bfcm91dGVAMjoKICAgIHR4biBPbkNvbXBsZXRpb24KICAgICEKICAgIGFzc2VydCAvLyBPbkNvbXBsZXRpb24gaXMgTm9PcAogICAgdHhuIEFwcGxpY2F0aW9uSUQKICAgIGFzc2VydCAvLyBpcyBub3QgY3JlYXRpbmcKICAgIGNhbGxzdWIgZm9yX2xvb3AKICAgIGJ5dGUgMHgxNTFmN2M3NQogICAgc3dhcAogICAgY29uY2F0CiAgICBs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yYW1lX2J1cnkgMwoKZm9yX2xvb3BfZm9yX2hlYWRlckA2OgogICAgZnJhbWVfZGlnIDMKICAgIGludCA0CiAgICA8CiAgICBkdXAKICAgIGZyYW1lX2J1cnkgMAogICAgYnogZm9yX2xvb3BfYWZ0ZXJfZm9yQDkKICAgIGZyYW1lX2RpZyAzCiAgICBkdXAKICAgIGl0b2IKICAgIGV4dHJhY3QgNyAxCiAgICBmcmFtZV9kaWcgMAogICAgYXNzZXJ0IC8vIEluZGV4IGFjY2VzcyBpcyBvdXQgb2YgYm91bmRzCiAgICBmcmFtZV9kaWcgMQogICAgZGlnIDIKICAgIHVuY292ZXIgMgogICAgcmVwbGFjZTMKICAgIGZyYW1lX2J1cnkgMQogICAgaW50IDEKICAgICsKICAgIGZyYW1lX2J1cnkgMwogICAgYiBmb3JfbG9vcF9mb3JfaGVhZGVyQDYKCmZvcl9sb29wX2FmdGVyX2ZvckA5OgogICAgZnJhbWVfZGlnIDEKICAgIGZyYW1lX2J1cnkgMAogICAgcmV0c3ViCg==",
"clear": "I3ByYWdtYSB2ZXJzaW9uIDEwCgpzbWFydF9jb250cmFjdHMuY29udHJvbF9mbG93LmNvbnRyYWN0LkZvckxvb3BzRXhhbXBsZS5jbGVhcl9zdGF0ZV9wcm9ncmFtOgogICAgaW50IDEKICAgIHJldHVybgo="
},
"state": {
"global": {
"num_byte_slices": 0,
"num_uints": 0
},
"local": {
"num_byte_slices": 0,
"num_uints": 0
}
},
"schema": {
"global": {
"declared": {},
"reserved": {}
},
"local": {
"declared": {},
"reserved": {}
}
},
"contract": {
"name": "ForLoopsExample",
"methods": [
{
"name": "for_loop",
"args": [],
"readonly": false,
"returns": {
"type": "uint8[4]"
}
}
],
"networks": {}
},
"bare_call_config": {
"no_op": "CREATE"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma version 10

smart_contracts.control_flow.contract.ForLoopsExample.clear_state_program:
int 1
return
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#pragma version 10

smart_contracts.control_flow.contract.IfElseExample.approval_program:
callsub __puya_arc4_router__
return


// smart_contracts.control_flow.contract.IfElseExample.__puya_arc4_router__() -> uint64:
__puya_arc4_router__:
proto 0 1
txn NumAppArgs
bz __puya_arc4_router___bare_routing@6
method "is_rich(uint64)string"
method "is_even(uint64)string"
txna ApplicationArgs 0
match __puya_arc4_router___is_rich_route@2 __puya_arc4_router___is_even_route@3
int 0
retsub

__puya_arc4_router___is_rich_route@2:
txn OnCompletion
!
assert // OnCompletion is NoOp
txn ApplicationID
assert // is not creating
txna ApplicationArgs 1
btoi
callsub is_rich
dup
len
itob
extract 6 2
swap
concat
byte 0x151f7c75
swap
concat
log
int 1
retsub

__puya_arc4_router___is_even_route@3:
txn OnCompletion
!
assert // OnCompletion is NoOp
txn ApplicationID
assert // is not creating
txna ApplicationArgs 1
btoi
callsub is_even
dup
len
itob
extract 6 2
swap
concat
byte 0x151f7c75
swap
concat
log
int 1
retsub

__puya_arc4_router___bare_routing@6:
txn OnCompletion
bnz __puya_arc4_router___after_if_else@10
txn ApplicationID
!
assert // is creating
int 1
retsub

__puya_arc4_router___after_if_else@10:
int 0
retsub


// smart_contracts.control_flow.contract.IfElseExample.is_rich(account_balance: uint64) -> bytes:
is_rich:
proto 1 1
frame_dig -1
int 1000
>
bz is_rich_else_body@2
byte "This account is rich!"
retsub

is_rich_else_body@2:
frame_dig -1
int 100
>
bz is_rich_else_body@4
byte "This account is doing well."
retsub

is_rich_else_body@4:
byte "This account is poor :("
retsub


// smart_contracts.control_flow.contract.IfElseExample.is_even(number: uint64) -> bytes:
is_even:
proto 1 1
frame_dig -1
int 2
%
bnz is_even_ternary_false@2
byte "Even"
b is_even_ternary_merge@3

is_even_ternary_false@2:
byte "Odd"

is_even_ternary_merge@3:
retsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"hints": {
"is_rich(uint64)string": {
"call_config": {
"no_op": "CALL"
}
},
"is_even(uint64)string": {
"call_config": {
"no_op":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lzIGFjY291bnQgaXMgcmljaCEiCiAgICByZXRzdWIKCmlzX3JpY2hfZWxzZV9ib2R5QDI6CiAgICBmcmFtZV9kaWcgLTEKICAgIGludCAxMDAKICAgID4KICAgIGJ6IGlzX3JpY2hfZWxzZV9ib2R5QDQKICAgIGJ5dGUgIlRoaXMgYWNjb3VudCBpcyBkb2luZyB3ZWxsLiIKICAgIHJldHN1YgoKaXNfcmljaF9lbHNlX2JvZHlANDoKICAgIGJ5dGUgIlRoaXMgYWNjb3VudCBpcyBwb29yIDooIgogICAgcmV0c3ViCgoKLy8gc21hcnRfY29udHJhY3RzLmNvbnRyb2xfZmxvdy5jb250cmFjdC5JZkVsc2VFeGFtcGxlLmlzX2V2ZW4obnVtYmVyOiB1aW50NjQpIC0+IGJ5dGVzOgppc19ldmVuOgogICAgcHJvdG8gMSAxCiAgICBmcmFtZV9kaWcgLTEKICAgIGludCAyCiAgICAlCiAgICBibnogaXNfZXZlbl90ZXJuYXJ5X2ZhbHNlQDIKICAgIGJ5dGUgIkV2ZW4iCiAgICBiIGlzX2V2ZW5fdGVybmFyeV9tZXJnZUAzCgppc19ldmVuX3Rlcm5hcnlfZmFsc2VAMjoKICAgIGJ5dGUgIk9kZCIKCmlzX2V2ZW5fdGVybmFyeV9tZXJnZUAzOgogICAgcmV0c3ViCg==",
"clear": "I3ByYWdtYSB2ZXJzaW9uIDEwCgpzbWFydF9jb250cmFjdHMuY29udHJvbF9mbG93LmNvbnRyYWN0LklmRWxzZUV4YW1wbGUuY2xlYXJfc3RhdGVfcHJvZ3JhbToKICAgIGludCAxCiAgICByZXR1cm4K"
},
"state": {
"global": {
"num_byte_slices": 0,
"num_uints": 0
},
"local": {
"num_byte_slices": 0,
"num_uints": 0
}
},
"schema": {
"global": {
"declared": {},
"reserved": {}
},
"local": {
"declared": {},
"reserved": {}
}
},
"contract": {
"name": "IfElseExample",
"methods": [
{
"name": "is_rich",
"args": [
{
"type": "uint64",
"name": "account_balance"
}
],
"readonly": false,
"returns": {
"type": "string"
}
},
{
"name": "is_even",
"args": [
{
"type": "uint64",
"name": "number"
}
],
"readonly": false,
"returns": {
"type": "string"
}
}
],
"networks": {}
},
"bare_call_config": {
"no_op": "CREATE"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma version 10

smart_contracts.control_flow.contract.IfElseExample.clear_state_program:
int 1
return
Loading