Beispiel #1
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(),
    )
Beispiel #2
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))])
Beispiel #3
0
def approval():
    # Checks that the app call sender is the creator of this app
    is_app_creator = Txn.sender() == Global.creator_address()

    verify = Seq(
                Pop( # In this example, we don't actually care about the result of the 
                # verification function, just pop it off the stack
                    Ed25519Verify( # Calls the ed25519 verify opcode on what is 
                    # normally some bytes that should be signed and the signature
                        Txn.application_args[0],  # Bytes signed
                        Txn.application_args[1],  # Signature
                        Txn.sender()              # Public key of signer
                    )
                ),
                # Return 1 so the transaction is approved
                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.application_args.length()>Int(0),                  Return(verify)],
        [Txn.application_args.length()==Int(0),                 Return(Int(1))]
    )
Beispiel #4
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(),
        ),
    )
Beispiel #5
0
def logicsig(tmpl_validator_application_id=validator_application_id,
             tmpl_manager_application_id=manager_application_id):
    program = And(
        Global.group_size() == Int(3),  # 3 transactions in this group
        # first one is an ApplicationCall
        Gtxn[0].type_enum() == TxnType.ApplicationCall,
        # the ApplicationCall must be approved by the validator application
        Gtxn[0].application_id() == tmpl_validator_application_id,
        # second one is an ApplicationCall
        Gtxn[1].type_enum() == TxnType.ApplicationCall,
        # Must be approved by the manager application
        Gtxn[1].application_id() == tmpl_manager_application_id,
        Txn.group_index() == Int(2),  # this AssetTransfer is the third one
    )
    return program
Beispiel #6
0
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)],
    )
Beispiel #7
0
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])),
             Int(1)]),

        # We're creating a contract account with the right behavior
        valid_contract(tc, Txn.application_args[2], contract_addr.load()),

        # Save it in creators local state
        caller_add_listing_addr(contract_addr.load()),
        price.load() <= max_price,
    )

    # TODO, check that contract doesnt already hold 1
    tag_listing = And(
        Global.group_size() == Int(3),
        valid_platform_asset(),  # first and only foreign arg
        set_foreign_asset(Gtxn[0], 0, tag_id),
        set_addr_as_tx(Gtxn[1], contract_addr),
        caller_is_listing_creator(contract_addr.load()),
        asa_optin_valid(Gtxn[1], tag_id.load(), contract_addr.load()),
        asa_xfer_valid(Gtxn[2], tag_id.load(), Int(1), platform_addr,
                       contract_addr.load()))

    untag_listing = And(
        Global.group_size() == Int(2), set_foreign_asset(Gtxn[0], 0, tag_id),
        valid_platform_asset(), set_addr_as_tx(Gtxn[1], contract_addr),
        caller_is_listing_creator(contract_addr.load()),
        asa_close_xfer_valid(Gtxn[1], tag_id.load(), contract_addr.load(),
                             platform_addr, platform_addr))

    # TODO, check that new value wont be > max price
    price_increase_listing = And(
        Global.group_size() == Int(2),
        set_addr_as_asset_rx(Gtxn[1], contract_addr),
        caller_is_listing_creator(contract_addr.load()),
        asa_xfer_valid(Gtxn[1], price_token, Btoi(Txn.application_args[1]),
                       platform_addr, contract_addr.load()),
    )

    # TODO, check that new price wont be 0
    price_decrease_listing = And(
        Global.group_size() == Int(2),
        set_addr_as_tx(Gtxn[1], contract_addr),
        caller_is_listing_creator(contract_addr.load()),
        asa_xfer_valid(Gtxn[1], price_token, Btoi(Txn.application_args[1]),
                       contract_addr.load(), platform_addr),
    )

    delete_listing = And(
        set_addr_as_tx(Gtxn[1], contract_addr),
        caller_is_listing_creator(contract_addr.load()),

        ## Add logic to check intermediate transactions
        valid_tag_closes(4, 8, platform_addr, contract_addr.load()),
        remove_listing_addr(Int(0), contract_addr.load()),
    )

    purchase_listing = And(
        # Make sure payment is going to creator
        Gtxn[1].receiver() == Gtxn[0].accounts[1],
        set_addr_as_tx(Gtxn[2], contract_addr),

        # Make sure payment amount is right
        check_balance_match(Gtxn[1], Int(2), price_token),

        ## Add logic to check intermediate transactions
        valid_tag_closes(5, 8, platform_addr, contract_addr.load()),

        # Remove the contract addr from the creators acct
        remove_listing_addr(Int(1), contract_addr.load()),
    )

    delist_listing = And(
        # Sent by admin
        Gtxn[0].sender() == Global.creator_address(),
        set_addr_as_tx(Gtxn[1], contract_addr),
        valid_tag_closes(4, 8, platform_addr, contract_addr.load()),
        remove_listing_addr(Int(1), contract_addr.load()),
    )

    return Cond(
        [Txn.application_id() == Int(0), on_creation],
        [Txn.on_completion() == OnComplete.DeleteApplication, on_delete],
        [
            Txn.on_completion() == OnComplete.UpdateApplication,
            Return(is_app_creator)
        ],
        [Txn.on_completion() == OnComplete.CloseOut, on_closeout],
        [Txn.on_completion() == OnComplete.OptIn, register],
        [Txn.application_args[0] == action_create,
         Return(create_listing)
         ],  # App approve price tokens && adds listing to local state
        [Txn.application_args[0] == action_tag,
         Return(tag_listing)],  # App approves manager of token requested
        [Txn.application_args[0] == action_untag,
         Return(untag_listing)
         ],  # App approves untag coming from listing creator
        [
            Txn.application_args[0] == action_dprice,
            Return(price_decrease_listing)
        ],  # App validates caller 
        [
            Txn.application_args[0] == action_iprice,
            Return(price_increase_listing)
        ],  # App validates caller 
        [Txn.application_args[0] == action_delete,
         Return(delete_listing)],  # App approves sender owns listing
        [Txn.application_args[0] == action_purchase,
         Return(purchase_listing)],  # App removes listing from local state
        [Txn.application_args[0] == action_safety,
         Return(delist_listing)]  # App removes listing from local state
    )
Beispiel #8
0
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))

    on_swap_deposit = Assert(
        And(
            # Group has 3 transactions
            Global.group_size() == Int(3),
            # This ApplicationCall is the 1st transaction
            Txn.group_index() == Int(0),
            # No additional actions are needed from this transaction
            Txn.on_completion() == OnComplete.NoOp,
            # Has one additional account attached
            Txn.accounts.length() == Int(1),
            # Has two application arguments
            Txn.application_args.length() == Int(2),
            # Second txn to manager
            # Is of type ApplicationCall
            Gtxn[1].type_enum() == TxnType.ApplicationCall,
            # No additional actions needed
            Gtxn[1].on_completion() == OnComplete.NoOp,
            # Has one additional account attached
            Gtxn[1].accounts.length() == Int(1),
            # Has two application arguments
            Gtxn[1].application_args.length() == Int(2),
            # Additional account is same in both calls
            Txn.accounts[1] == Gtxn[1].accounts[1],
            # Application argument is same in both calls
            Txn.application_args[0] == Gtxn[1].application_args[0],
            Txn.application_args[1] == Gtxn[1].application_args[1],
            # Third txn to escrow
            # Is of type AssetTransfer
            Gtxn[2].type_enum() == TxnType.AssetTransfer,
            Gtxn[2].sender() == Global.zero_address(),
            Gtxn[2].asset_receiver() == Txn.accounts[1],
        ))

    program = Cond(
        [Txn.application_id() == Int(0), on_create],
        [Txn.on_completion() == OnComplete.CloseOut, on_closeout],
        [Txn.on_completion() == OnComplete.OptIn, on_opt_in],
        [
            Txn.application_args[0]
            == TRANSACTION_TYPE_SWAP_DEPOSIT_TOKEN1_TO_TOKEN2, on_swap_deposit
        ],
        [
            Txn.application_args[0]
            == TRANSACTION_TYPE_SWAP_DEPOSIT_TOKEN2_TO_TOKEN1,
            on_swap_deposit_2
        ],
        [
            Txn.application_args[0] == TRANSACTION_TYPE_ADD_LIQUIDITY_DEPOSIT,
            on_add_liquidity_deposit
        ],
        [
            Txn.application_args[0] == TRANSACTION_TYPE_WITHDRAW_LIQUIDITY,
            on_withdraw_liquidity
        ],
        [Txn.application_args[0] == TRANSACTION_TYPE_REFUND, on_refund],
        [
            Txn.application_args[0] == TRANSACTION_TYPE_WITHDRAW_PROTOCOL_FEES,
            on_withdraw_protocol_fees
        ],
    )
    return