Example #1
0
def sing(count):
    return Seq(
        # Each call to Log takes the bytestring passed and logs the message 
        # itoa is a subroutine in the util.py file, it converts an integer to a bytestring
        # representing the ascii numberic characters
        Log(Concat(itoa(count), Bytes(" Bottles of beer on the wall"))),
        Log(Concat(itoa(count), Bytes(" Bottles of beer"))),
        Log(Bytes("Take one down, pass it around")),
        Log(Concat(itoa(count-Int(1)), Bytes(" Bottles of beer on the wall")))
    )
Example #2
0
def get_global_state_ex(foreign_id: int, key: str) -> MaybeValue:
    """
    Wrapper for global state getter.
    External state variables need to be evaluated before use.

    https://pyteal.readthedocs.io/en/stable/state.html#external-global
    """
    return App.globalGetEx(Int(foreign_id), Bytes(key))
Example #3
0
 def decompress():
     return EcdsaDecompress(
         EcdsaCurve.Secp256k1,
         Bytes(
             "base16",
             "03bd83d54f6a799d05b496653b64bc933e17a898cda4793fe662d50645ecc977d1",
         ),
     ).outputReducer(lambda x, y: And(
         x == Bytes(
             "base16",
             "bd83d54f6a799d05b496653b64bc933e17a898cda4793fe662d50645ecc977d1",
         ),
         y == Bytes(
             "base16",
             "d4f3063a1ffca4139ea921b5696a6597640289175afece3bc38217a29d6270f9",
         ),
     ))
Example #4
0
def approval():
    return And(
        Txn.fee() <= Int(10_000),
        Len(Arg(0)) == Int(73),
        Sha256(Arg(0)) == Bytes(
            "base64", "30AT2gOReDBdJmLBO/DgvjC6hIXgACecTpFDcP1bJHU="),
        Txn.close_remainder_to() == Txn.receiver(),
    )
Example #5
0
def escrow_account(note):
    precondition = And(
        Global.group_size() == Int(1),
        Txn.note() == Bytes(note),

        # Safety checks
        Txn.rekey_to() == Global.zero_address(),
        Txn.close_remainder_to() == Global.zero_address(),
        Txn.fee() <= Int(FEE),
    )

    return Seq([Assert(precondition), Return(Int(1))])
Example #6
0
 def recover():
     return EcdsaRecover(
         EcdsaCurve.Secp256k1,
         Sha512_256(Bytes("testdata")),
         Int(1),
         Bytes(
             "base16",
             "cabed943e1403fb93b388174c59a52c759b321855f2d7c4fcc23c99a8a6dce79",
         ),
         Bytes(
             "base16",
             "56192820dde344c32f81450db05e51c6a6f45a2a2db229f657d2c040baf31537",
         ),
     ).outputReducer(lambda x, y: And(
         x == Bytes(
             "base16",
             "71539e0c7a6902a3f5413d6e28a455b2a14316fcf0f6b21193343b3b9d455053",
         ),
         y == Bytes(
             "base16",
             "fa49ccd95795c7c9a447fdeee83a2193472507a4e41a47e0d50eeeb547b74c51",
         ),
     ))
Example #7
0
 def verify_fail():
     return EcdsaVerify(
         EcdsaCurve.Secp256k1,
         Sha512_256(Bytes("testdata")),
         Bytes(
             "base16",
             "13602297203d2753372cea7794ffe1756a278cbc4907b15a0dd132c9fb82555e",
         ),
         Bytes(
             "base16",
             "20f112126cf3e2eac6e8d4f97a403d21bab07b8dbb77154511bb7b07c0173195",
         ),
         (
             Bytes(
                 "base16",
                 "d6143a58c90c06b594e4414cb788659c2805e0056b1dfceea32c03f59efec517",
             ),
             Bytes(
                 "base16",
                 "00bd2400c479efe5ea556f37e1dc11ccb20f1e642dbfe00ca346fffeae508298",
             ),
         ),
     )
Example #8
0
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(),
        ),
    )
Example #9
0
 def get(self) -> App:
     return App.globalGet(Bytes(self._name))
Example #10
0
 def put(self, value) -> App:
     return App.globalPut(Bytes(self._name), value)
Example #11
0
 def get(self) -> App:
     return App.localGet(Int(0), Bytes(self._name))
Example #12
0
#!/usr/bin/env python3

from pyteal import compileTeal, Seq, App, Assert, Txn, Gtxn, TxnType, Btoi, Bytes, Int, Return, If, Cond, And, Or, Not, Global, Mode, OnComplete, Concat, AssetHolding, AssetParam

# Tmpl Constants
swap_fee = Int(45)
protocol_fee = Int(5)

# Keys
KEY_CREATOR = Bytes("C")

# Transaction Types
TRANSACTION_TYPE_SWAP_DEPOSIT_TOKEN1_TO_TOKEN2 = Bytes("s1")
TRANSACTION_TYPE_SWAP_DEPOSIT_TOKEN2_TO_TOKEN1 = Bytes("s2")
TRANSACTION_TYPE_ADD_LIQUIDITY_DEPOSIT = Bytes("a")
TRANSACTION_TYPE_WITHDRAW_LIQUIDITY = Bytes("w")
TRANSACTION_TYPE_REFUND = Bytes("r")
TRANSACTION_TYPE_WITHDRAW_PROTOCOL_FEES = Bytes("p")


def approval_program():

    # On application create, put the creator key in global storage
    on_create = Seq([App.globalPut(KEY_CREATOR, Txn.sender()), Int(1)])

    # Closeout on validator does nothing
    on_closeout = Return(Int(1))

    # Opt in on validator does nothing
    on_opt_in = Return(Int(1))
Example #13
0
def itoa(i: TealType.uint64):
    return If(
        i == Int(0), Bytes("0"),
        Concat(If(i / Int(10) > Int(0), itoa(i / Int(10)), Bytes("")),
               int_to_ascii(i % Int(10))))
Example #14
0
def int_to_ascii(arg: TealType.uint64):
    #return arg + ascii_offset Just returns a uint64, cant convert to bytes type
    return Substring(Bytes("0123456789"), arg, arg + Int(1))
Example #15
0
 def make_log(e):
     if e.type_of() == TealType.uint64:
         return Log(Itob(e))
     if e.type_of() == TealType.bytes:
         return Log(e)
     return Log(Bytes("nada"))
Example #16
0
 def put(self, value) -> App:
     return App.localPut(Int(0), Bytes(self._name), value)
Example #17
0
def get_config():
    config = None
    with open('../../../config.json', 'r') as f:
        config = json.load(f)

    for k, v in os.environ.items():
        if k[:5] == "TMPL_":
            config['application'][k[5:].lower()] = v

    return config


configuration = get_config()

listing_key = Bytes("listing")

tag_key = Bytes("tag:")

platform_fee = Tmpl.Int("TMPL_FEE_AMT")
platform_addr = Tmpl.Bytes("TMPL_OWNER_ADDR")
platform_admin = Tmpl.Bytes("TMPL_ADMIN_ADDR")

app_id = Tmpl.Int("TMPL_APP_ID")
price_token = Tmpl.Int("TMPL_PRICE_ID")

seed_amt = Int(int(configuration['application']['seed_amt']))
max_price = Int(int(configuration['application']['max_price']))

action_create = Bytes("create")
action_tag = Bytes("tag")