Skip to content

Instantly share code, notes, and snippets.

@jasonpaulos
Last active December 6, 2022 22:56
Show Gist options
  • Save jasonpaulos/99e4f8a75f2fc2ec9b8073c064530359 to your computer and use it in GitHub Desktop.
Save jasonpaulos/99e4f8a75f2fc2ec9b8073c064530359 to your computer and use it in GitHub Desktop.
import json
import os
from typing import Literal
from pyteal import *
pragma(compiler_version="0.20.1")
router = Router(
name="ABITestContract",
bare_calls=BareCallActions(no_op=OnCompleteAction.create_only(Approve())),
)
@router.method(
no_op=CallConfig.CREATE,
opt_in=CallConfig.CREATE,
delete_application=CallConfig.CREATE,
update_application=CallConfig.CREATE,
)
def create(i: abi.Uint64, *, output: abi.Uint64):
return output.set(i.get() * Int(2))
@router.method(update_application=CallConfig.CALL)
def update():
return Seq()
@router.method(opt_in=CallConfig.CALL)
def optIn(name: abi.String, *, output: abi.String):
return Seq(
App.localPut(Int(0), Bytes("name"), name.get()),
output.set(Concat(Bytes("hello "), App.localGet(Int(0), Bytes("name")))),
)
@router.method(close_out=CallConfig.CALL)
def closeOut(*, output: abi.String):
return output.set(Concat(Bytes("goodbye "), App.localGet(Int(0), Bytes("name"))))
@router.method(name="delete", delete_application=CallConfig.ALL)
def deleteApp():
return Assert(Txn.sender() == Global.creator_address())
@router.method
def add(a: abi.Uint64, b: abi.Uint64, *, output: abi.Uint64):
return output.set(a.get() + b.get())
@router.method
def empty():
return Log(Bytes("random inconsequential log"))
@router.method
def payment(txn: abi.PaymentTransaction, amount: abi.Uint64, *, output: abi.Bool):
return output.set(txn.get().amount() == amount.get())
@router.method(no_op=CallConfig.ALL, delete_application=CallConfig.CREATE)
def referenceTest(
account1: abi.Account,
application1: abi.Application,
account2: abi.Account,
asset1: abi.Asset,
account3: abi.Account,
asset2: abi.Asset,
asset3: abi.Asset,
application2: abi.Application,
application3: abi.Application,
*,
output: abi.StaticArray[abi.Uint8, Literal[9]]
):
account1_index = abi.Uint8()
account2_index = abi.Uint8()
account3_index = abi.Uint8()
application1_index = abi.Uint8()
application2_index = abi.Uint8()
application3_index = abi.Uint8()
asset1_index = abi.Uint8()
asset2_index = abi.Uint8()
asset3_index = abi.Uint8()
return Seq(
account1_index.set(account1.referenced_index()),
account2_index.set(account2.referenced_index()),
account3_index.set(account3.referenced_index()),
application1_index.set(application1.referenced_index()),
application2_index.set(application2.referenced_index()),
application3_index.set(application3.referenced_index()),
asset1_index.set(asset1.referenced_index()),
asset2_index.set(asset2.referenced_index()),
asset3_index.set(asset3.referenced_index()),
output.set(
[
account1_index,
account2_index,
account3_index,
application1_index,
application2_index,
application3_index,
asset1_index,
asset2_index,
asset3_index,
]
),
)
if __name__ == "__main__":
import os
path = os.path.dirname(__file__)
approval_program, clear_program, contract = router.compile_program(
version=5, optimize=OptimizeOptions(scratch_slots=True)
)
with open(os.path.join(path, "approval.teal"), "w") as f:
f.write(approval_program)
with open(os.path.join(path, "clear_state.teal"), "w") as f:
f.write(clear_program)
with open(os.path.join(path, "contract.json"), "w") as f:
f.write(json.dumps(contract.dictify(), indent=4))
#pragma version 5
txn NumAppArgs
int 0
==
bnz main_l20
txna ApplicationArgs 0
method "create(uint64)uint64"
==
bnz main_l19
txna ApplicationArgs 0
method "update()void"
==
bnz main_l18
txna ApplicationArgs 0
method "optIn(string)string"
==
bnz main_l17
txna ApplicationArgs 0
method "closeOut()string"
==
bnz main_l16
txna ApplicationArgs 0
method "delete()void"
==
bnz main_l15
txna ApplicationArgs 0
method "add(uint64,uint64)uint64"
==
bnz main_l14
txna ApplicationArgs 0
method "empty()void"
==
bnz main_l13
txna ApplicationArgs 0
method "payment(pay,uint64)bool"
==
bnz main_l12
txna ApplicationArgs 0
method "referenceTest(account,application,account,asset,account,asset,asset,application,application)uint8[9]"
==
bnz main_l11
err
main_l11:
txn OnCompletion
int NoOp
==
txn OnCompletion
int DeleteApplication
==
txn ApplicationID
int 0
==
&&
||
assert
txna ApplicationArgs 1
int 0
getbyte
store 13
txna ApplicationArgs 2
int 0
getbyte
store 14
txna ApplicationArgs 3
int 0
getbyte
store 15
txna ApplicationArgs 4
int 0
getbyte
store 16
txna ApplicationArgs 5
int 0
getbyte
store 17
txna ApplicationArgs 6
int 0
getbyte
store 18
txna ApplicationArgs 7
int 0
getbyte
store 19
txna ApplicationArgs 8
int 0
getbyte
store 20
txna ApplicationArgs 9
int 0
getbyte
store 21
load 13
load 14
load 15
load 16
load 17
load 18
load 19
load 20
load 21
callsub referenceTest_8
store 22
byte 0x151f7c75
load 22
concat
log
int 1
return
main_l12:
txn OnCompletion
int NoOp
==
txn ApplicationID
int 0
!=
&&
assert
txna ApplicationArgs 1
btoi
store 10
txn GroupIndex
int 1
-
store 9
load 9
gtxns TypeEnum
int pay
==
assert
load 9
load 10
callsub payment_7
store 11
byte 0x151f7c75
byte 0x00
int 0
load 11
setbit
concat
log
int 1
return
main_l13:
txn OnCompletion
int NoOp
==
txn ApplicationID
int 0
!=
&&
assert
callsub empty_6
int 1
return
main_l14:
txn OnCompletion
int NoOp
==
txn ApplicationID
int 0
!=
&&
assert
txna ApplicationArgs 1
btoi
store 6
txna ApplicationArgs 2
btoi
store 7
load 6
load 7
callsub add_5
store 8
byte 0x151f7c75
load 8
itob
concat
log
int 1
return
main_l15:
txn OnCompletion
int DeleteApplication
==
assert
callsub delete_4
int 1
return
main_l16:
txn OnCompletion
int CloseOut
==
txn ApplicationID
int 0
!=
&&
assert
callsub closeOut_3
store 4
byte 0x151f7c75
load 4
concat
log
int 1
return
main_l17:
txn OnCompletion
int OptIn
==
txn ApplicationID
int 0
!=
&&
assert
txna ApplicationArgs 1
callsub optIn_2
store 1
byte 0x151f7c75
load 1
concat
log
int 1
return
main_l18:
txn OnCompletion
int UpdateApplication
==
txn ApplicationID
int 0
!=
&&
assert
callsub update_1
int 1
return
main_l19:
txn OnCompletion
int NoOp
==
txn ApplicationID
int 0
==
&&
txn OnCompletion
int OptIn
==
txn ApplicationID
int 0
==
&&
||
txn OnCompletion
int UpdateApplication
==
txn ApplicationID
int 0
==
&&
||
txn OnCompletion
int DeleteApplication
==
txn ApplicationID
int 0
==
&&
||
assert
txna ApplicationArgs 1
btoi
callsub create_0
store 0
byte 0x151f7c75
load 0
itob
concat
log
int 1
return
main_l20:
txn OnCompletion
int NoOp
==
bnz main_l22
err
main_l22:
txn ApplicationID
int 0
==
assert
int 1
return
// create
create_0:
int 2
*
retsub
// update
update_1:
retsub
// optIn
optIn_2:
store 2
int 0
byte "name"
load 2
extract 2 0
app_local_put
byte "hello "
int 0
byte "name"
app_local_get
concat
store 3
load 3
len
itob
extract 6 0
load 3
concat
store 3
load 3
retsub
// closeOut
closeOut_3:
byte "goodbye "
int 0
byte "name"
app_local_get
concat
store 5
load 5
len
itob
extract 6 0
load 5
concat
store 5
load 5
retsub
// delete
delete_4:
txn Sender
global CreatorAddress
==
assert
retsub
// add
add_5:
+
retsub
// empty
empty_6:
byte "random inconsequential log"
log
retsub
// payment
payment_7:
store 12
gtxns Amount
load 12
==
!
!
retsub
// referenceTest
referenceTest_8:
store 30
store 29
store 28
store 27
store 26
store 25
store 24
store 23
store 31
load 31
int 256
<
assert
load 24
store 32
load 32
int 256
<
assert
load 26
store 33
load 33
int 256
<
assert
load 23
store 34
load 34
int 256
<
assert
load 29
store 35
load 35
int 256
<
assert
load 30
store 36
load 36
int 256
<
assert
load 25
store 37
load 37
int 256
<
assert
load 27
store 38
load 38
int 256
<
assert
load 28
store 39
load 39
int 256
<
assert
byte 0x00
int 0
load 31
setbyte
byte 0x00
int 0
load 32
setbyte
concat
byte 0x00
int 0
load 33
setbyte
concat
byte 0x00
int 0
load 34
setbyte
concat
byte 0x00
int 0
load 35
setbyte
concat
byte 0x00
int 0
load 36
setbyte
concat
byte 0x00
int 0
load 37
setbyte
concat
byte 0x00
int 0
load 38
setbyte
concat
byte 0x00
int 0
load 39
setbyte
concat
retsub
#pragma version 5
int 0
return
{
"name": "ABITestContract",
"methods": [
{
"name": "create",
"args": [
{
"type": "uint64",
"name": "i"
}
],
"returns": {
"type": "uint64"
}
},
{
"name": "update",
"args": [],
"returns": {
"type": "void"
}
},
{
"name": "optIn",
"args": [
{
"type": "string",
"name": "name"
}
],
"returns": {
"type": "string"
}
},
{
"name": "closeOut",
"args": [],
"returns": {
"type": "string"
}
},
{
"name": "delete",
"args": [],
"returns": {
"type": "void"
}
},
{
"name": "add",
"args": [
{
"type": "uint64",
"name": "a"
},
{
"type": "uint64",
"name": "b"
}
],
"returns": {
"type": "uint64"
}
},
{
"name": "empty",
"args": [],
"returns": {
"type": "void"
}
},
{
"name": "payment",
"args": [
{
"type": "pay",
"name": "txn"
},
{
"type": "uint64",
"name": "amount"
}
],
"returns": {
"type": "bool"
}
},
{
"name": "referenceTest",
"args": [
{
"type": "account",
"name": "account1"
},
{
"type": "application",
"name": "application1"
},
{
"type": "account",
"name": "account2"
},
{
"type": "asset",
"name": "asset1"
},
{
"type": "account",
"name": "account3"
},
{
"type": "asset",
"name": "asset2"
},
{
"type": "asset",
"name": "asset3"
},
{
"type": "application",
"name": "application2"
},
{
"type": "application",
"name": "application3"
}
],
"returns": {
"type": "uint8[9]"
}
}
],
"networks": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment