def fn_mixed_annotations_0_with_ret(a: pt.ScratchVar, b: pt.Expr, c: pt.abi.Byte, *, output: pt.abi.Uint64) -> pt.Expr: return pt.Seq( a.store(c.get() * pt.Int(0x0FF1CE) * b), output.set(a.load()), )
def swap(x: pt.ScratchVar, y: pt.ScratchVar): z = pt.ScratchVar(pt.TealType.anytype) return pt.Seq( z.store(x.load()), x.store(y.load()), y.store(z.load()), )
def subr_string_mult(s: pt.ScratchVar, n): tmp = pt.ScratchVar(pt.TealType.bytes) return (pt.If(n == pt.Int(0)).Then(s.store(pt.Bytes(""))).Else( pt.Seq( tmp.store(s.load()), subr_string_mult(s, n - pt.Int(1)), s.store(pt.Concat(s.load(), tmp.load())), )))
def fn_mixed_annotation_1( a: pt.ScratchVar, b: pt.abi.StaticArray[pt.abi.Uint32, Literal[10]]) -> pt.Expr: return pt.Seq( (intermediate := pt.abi.Uint32()).set(b[a.load() % pt.Int(10)]), a.store(intermediate.get()), pt.Return(), )
def factorial_BAD(n: pt.ScratchVar): tmp = pt.ScratchVar(pt.TealType.uint64) return (pt.If(n.load() <= pt.Int(1)).Then(n.store(pt.Int(1))).Else( pt.Seq( tmp.store(n.load() - pt.Int(1)), factorial_BAD(tmp), n.store(n.load() * tmp.load()), )))
def plus_one(n: pt.ScratchVar): tmp = pt.ScratchVar(pt.TealType.uint64) return (pt.If(n.load() == pt.Int(0)).Then(n.store(pt.Int(1))).Else( pt.Seq( tmp.store(n.load() - pt.Int(1)), plus_one(tmp), n.store(tmp.load() + pt.Int(1)), )))
def platform_owner(): #Allow any grouped atomic txns that are approved by the application or the admin _app_id = ScratchVar(TealType.uint64) return And( Global.group_size() > Int(1), Seq([_app_id.store(app_id), Int(1)]), Or(valid_app_call(Gtxn[0], _app_id.load()), valid_admin_fee_pay(Gtxn[0])))
def make_return(e): if e.type_of() == TealType.uint64: return e if e.type_of() == TealType.bytes: return Len(e) if e.type_of() == TealType.anytype: x = ScratchVar(TealType.anytype) return Seq(x.store(e), Int(1337)) # TealType.none: return Seq(e, Int(1337))
def user_guide_snippet_ILLEGAL_recursion(): from pyteal import If, Int, ScratchVar, Seq, Subroutine, TealType @Subroutine(TealType.none) def ILLEGAL_recursion(i: ScratchVar): return (If(i.load() == Int(0)).Then(i.store( Int(1))).ElseIf(i.load() == Int(1)).Then(i.store(Int(0))).Else( Seq(i.store(i.load() - Int(2)), ILLEGAL_recursion(i)))) i = ScratchVar(TealType.uint64) return Seq(i.store(Int(15)), ILLEGAL_recursion(i), Int(1))
def string_mult(s: pt.ScratchVar, n): i = pt.ScratchVar(pt.TealType.uint64) tmp = pt.ScratchVar(pt.TealType.bytes) start = pt.Seq(i.store(pt.Int(1)), tmp.store(s.load()), s.store(pt.Bytes(""))) step = i.store(i.load() + pt.Int(1)) return pt.Seq( pt.For(start, i.load() <= n, step).Do(s.store(pt.Concat(s.load(), tmp.load()))), s.load(), )
def user_guide_snippet_dynamic_scratch_var() -> pt.Expr: """ The user guide docs use the test to illustrate `DynamicScratchVar` usage. If the test breaks, then the user guide docs must be updated along with the test. """ from pyteal import Assert, Int, DynamicScratchVar, ScratchVar, Seq, TealType s = ScratchVar(TealType.uint64) d = DynamicScratchVar(TealType.uint64) return Seq( d.set_index(s), s.store(Int(7)), d.store(d.load() + Int(3)), Assert(s.load() == Int(10)), Int(1), )
def approval(): if subdef.return_type == TealType.none: result = ScratchVar(TealType.uint64) part1 = [subr_caller(), result.store(Int(1337))] else: result = ScratchVar(subdef.return_type) part1 = [result.store(subr_caller())] part2 = [make_log(result.load()), make_return(result.load())] return Seq(*(part1 + part2))
def _arg_prep_n_call(self, i, p): subdef = self.subr.subroutine.subroutine arg_names = subdef.arguments() name = arg_names[i] arg_expr = Txn.application_args[i] if self.mode == Mode.Application else Arg(i) if p == TealType.uint64: arg_expr = Btoi(arg_expr) if name in subdef.by_ref_args: arg_var = ScratchVar(p) prep = arg_var.store(arg_expr) elif name in subdef.abi_args: arg_var = p.new_instance() prep = arg_var.decode(arg_expr) else: arg_var = arg_expr prep = None return prep, arg_var
def while_continue_accumulation(n): i = ScratchVar(TealType.uint64) return Seq( i.store(Int(0)), While(i.load() < n).Do( Seq( i.store(i.load() + Int(1)), Continue(), )), Return(i.load()), )
def abi_sum(to_sum: abi.DynamicArray[abi.Uint64], *, output: abi.Uint64) -> Expr: i = ScratchVar(TealType.uint64) value_at_index = abi.Uint64() return Seq( output.set(0), For(i.store(Int(0)), i.load() < to_sum.length(), i.store(i.load() + Int(1))).Do( Seq( to_sum[i.load()].store_into(value_at_index), output.set(output.get() + value_at_index.get()), )), )
def approval(): # Checks that the app call sender is the creator of this app is_app_creator = Txn.sender() == Global.creator_address() # Create a scratch var for iteration i = ScratchVar() # Set up the components of the for loop # start at 99 because thats how the song goes init = i.store(Int(99)) # this is the condition that will be evaluated in the for loop # the for loop will terminate when this returns false cond = i.load()>Int(97) # this is the decrement portion of this for loop, just subtracts 1 for every loop iteration iter = i.store(i.load() - Int(1)) log = Seq( # Call the for loop with the components we defined above For(init, cond, iter).Do( # sing is called with the current value of i, # it doesn't return anything so it is fine to include in # the `Do` portion of the loop sing(i.load()) ), # Return 1 so the transaction will pass Int(1) ) return Cond( [Txn.application_id() == Int(0), Return(Int(1))], [Txn.on_completion() == OnComplete.DeleteApplication, Return(is_app_creator)], [Txn.on_completion() == OnComplete.UpdateApplication, Return(is_app_creator)], [Txn.on_completion() == OnComplete.CloseOut, Return(Int(1))], [Txn.on_completion() == OnComplete.OptIn, Return(Int(1))], [Txn.on_completion() == OnComplete.NoOp, Return(log)], )
def listing(): creator_addr = ScratchVar(TealType.bytes) contract_addr = ScratchVar(TealType.bytes) buyer_addr = ScratchVar(TealType.bytes) asset_id = ScratchVar(TealType.uint64) _app_id = ScratchVar(TealType.uint64) _price_token = ScratchVar(TealType.uint64) setup = Seq([ creator_addr.store(Tmpl.Bytes("TMPL_CREATOR_ADDR")), asset_id.store(Btoi(Tmpl.Bytes("TMPL_ASSET_ID"))), _app_id.store(app_id), _price_token.store(price_token), Int(0) # return 0 so this cond case doesnt get executed ]) create = And( Global.group_size() == Int(7), valid_app_call( Gtxn[0], _app_id.load()), set_addr_as_rx( Gtxn[1], contract_addr), pay_txn_valid( Gtxn[1], seed_amt, creator_addr.load(), contract_addr.load()), asa_optin_valid(Gtxn[2], asset_id.load(), contract_addr.load()), asa_optin_valid(Gtxn[3], _price_token.load(), contract_addr.load()), asa_xfer_valid( Gtxn[4], asset_id.load(), Int(1), creator_addr.load(), contract_addr.load()), asa_xfer_valid( Gtxn[5], _price_token.load(), Btoi(Gtxn[0].application_args[1]), platform_addr, contract_addr.load()), asa_cfg_valid( Gtxn[6], asset_id.load(), contract_addr.load()), ) delete = And( Global.group_size() >= Int(5), valid_app_call(Gtxn[0], _app_id.load()), set_addr_as_tx( Gtxn[1], contract_addr), asa_close_xfer_valid(Gtxn[1], _price_token.load(), contract_addr.load(), platform_addr, platform_addr), asa_close_xfer_valid(Gtxn[2], asset_id.load(), contract_addr.load(), creator_addr.load(), creator_addr.load()), asa_cfg_valid( Gtxn[3], asset_id.load(), creator_addr.load()), # Possible Tag closes pay_close_txn_valid( Gtxn[Global.group_size() - Int(1)], contract_addr.load(), creator_addr.load(), creator_addr.load(), Int(0)), ) purchase = And( Global.group_size() >= Int(6), valid_app_call(Gtxn[0], _app_id.load()), set_addr_as_tx( Gtxn[1], buyer_addr), set_addr_as_tx( Gtxn[2], contract_addr), pay_txn_valid( Gtxn[1], Gtxn[1].amount(), buyer_addr.load(), creator_addr.load()), asa_close_xfer_valid(Gtxn[2], asset_id.load(), contract_addr.load(), buyer_addr.load(), buyer_addr.load()), asa_close_xfer_valid(Gtxn[3], _price_token.load(), contract_addr.load(), platform_addr, platform_addr), asa_cfg_valid( Gtxn[4], asset_id.load(), buyer_addr.load()), # Possible Tag closes pay_close_txn_valid( Gtxn[Global.group_size() - Int(1)], contract_addr.load(), platform_addr, creator_addr.load(), platform_fee), ) app_offload = Or( Gtxn[0].application_args[0] == action_tag, Gtxn[0].application_args[0] == action_untag, Gtxn[0].application_args[0] == action_dprice, Gtxn[0].application_args[0] == action_safety ) app_validate = valid_app_call(Gtxn[0], _app_id.load()) return Cond([setup, Int(0)], #NoOp [Gtxn[0].application_args[0] == action_create, create], [Gtxn[0].application_args[0] == action_delete, delete], [Gtxn[0].application_args[0] == action_purchase, purchase], [app_offload, app_validate])
def fn_mixed_annotation_1_with_ret(a: pt.ScratchVar, b: pt.abi.Uint64, *, output: pt.abi.Bool) -> pt.Expr: return output.set((a.load() + b.get()) % pt.Int(2))
def logcat_dynamic(first: pt.ScratchVar, an_int): return pt.Seq( first.store(pt.Concat(first.load(), pt.Itob(an_int))), pt.Log(first.load()), )
def c(x: pt.ScratchVar): return g(pt.Int(42)) - h(x.load())
def fn_mixed_annotations_0(a: pt.ScratchVar, b: pt.Expr, c: pt.abi.Byte) -> pt.Expr: return pt.Seq( a.store(c.get() * pt.Int(0x0FF1CE) * b), pt.Return(), )
def logicsig(a: int, p: int, q: int) -> Expr: """ Choices * (a, p, q) = (1, 5, 7) * compiling on program version 5 and * with assembleConstants = True results in Logic-Sig Contract Account Address: WO3TQD3WBSDKB6WEHUMSEBFH53GZVVXYGPWYDWKUZCKEXTVCDNDHJGG6II """ assert all( isinstance(x, int) and p < q and a > 0 and x >= 0 for x in (a, p, q)), f"require non-negative ints a, p, q with p < q but got {a, p, q}" b, c = a * (p + q), a * p * q msg = Bytes(f"Can you factor {a} * x^2 - {b} * x + {c} ?") A, B, C = Int(a), Int(b), Int(c) X1 = Btoi(Arg(0)) X2 = Btoi(Arg(1)) C1 = ScratchVar(TealType.uint64) C2 = ScratchVar(TealType.uint64) SUM = ScratchVar(TealType.uint64) PRIZE = ScratchVar(TealType.uint64) return Seq( Pop(msg), C1.store(root_closeness(A, B, C, X1)), C2.store(root_closeness(A, B, C, X2)), SUM.store(C1.load() + C2.load()), PRIZE.store(calculate_prize(SUM.load())), And( Txn.type_enum() == TxnType.Payment, Txn.close_remainder_to() == Global.zero_address(), X1 != X2, PRIZE.load(), Txn.amount() == PRIZE.load(), ), )
from pyteal import ScratchVar, TealType, Return, Btoi, Txn, OnComplete, Mode, Cond, compileTeal from validator import * from utils import * from config import * tc = TemplateContract(configuration) tc.write_tmpl_positions() creator_addr = ScratchVar(TealType.bytes) contract_addr = ScratchVar(TealType.bytes) asa_token_id = ScratchVar(TealType.uint64) price = ScratchVar(TealType.uint64) tag_id = ScratchVar(TealType.uint64) def approval(): is_app_creator = Global.creator_address() == Txn.sender() on_creation = Seq([Return(Int(1))]) on_delete = Seq([Return(is_app_creator)]) on_closeout = Seq([Return(is_app_creator)]) register = Seq([Return(Int(1))]) create_listing = And( set_addr_as_rx(Gtxn[1], contract_addr), set_addr_as_tx(Gtxn[1], creator_addr), Seq([price.store(Btoi(Txn.application_args[1])),
def euclid(x, y): a = ScratchVar(TealType.uint64) b = ScratchVar(TealType.uint64) tmp = ScratchVar(TealType.uint64) start = If(x < y, Seq(a.store(y), b.store(x)), Seq(a.store(x), b.store(y))) cond = b.load() > Int(0) step = Seq(tmp.store(b.load()), b.store(Mod(a.load(), b.load())), a.store(tmp.load())) return Seq(For(start, cond, step).Do(Seq()), a.load())
def tally(n, result: pt.ScratchVar): return (pt.If(n == pt.Int(0)).Then(result.store(pt.Bytes(""))).Else( pt.Seq( tally(n - pt.Int(1), result), result.store(pt.Concat(result.load(), pt.Bytes("a"))), )))
def square_byref(x: pt.ScratchVar): return x.store(x.load() * x.load())
def root_closeness(A, B, C, X): left = ScratchVar(TealType.uint64) right = ScratchVar(TealType.uint64) return Seq( left.store(A * X * X + C), right.store(B * X), If(left.load() < right.load()).Then(right.load() - left.load()).Else(left.load() - right.load()), )
def mixed_annotations(x: pt.Expr, y: pt.Expr, z: pt.ScratchVar) -> pt.Expr: return pt.Seq( z.store(x), pt.Log(pt.Concat(y, pt.Bytes("="), pt.Itob(x))), x, )
def get_validate_ops(self, contract_val): blank_contract = ScratchVar(TealType.bytes) pos = ScratchVar(TealType.uint64) concat_ops = [blank_contract.store(Bytes("")), pos.store(Int(0))] p = 0 for idx in range(len(self.template_vars)): tv = self.template_vars[idx] concat_ops.append( blank_contract.store( Concat( blank_contract.load(), Substring(contract_val, pos.load(), pos.load() + Int(tv.distance))))) p += tv.length + tv.distance concat_ops.append( pos.store(pos.load() + Int(tv.length) + Int(tv.distance))) concat_ops.append( blank_contract.store( Concat(blank_contract.load(), Substring(contract_val, pos.load(), Len(contract_val))))) concat_ops.append(Int(1)) return And( # prepare the blank contract Seq(concat_ops), # Make sure this is the contract being distributed to Sha256(blank_contract.load()) == Tmpl.Bytes("TMPL_BLANK_HASH"), )
def fn_2mixed_arg_1ret(a: pt.abi.Uint64, b: pt.ScratchVar, *, output: pt.abi.Uint64) -> pt.Expr: return pt.Seq(b.store(a.encode()), output.set(a))