def withdraw_liquidity():
    print("Building withdraw liquidity atomic transaction group...")

    encoded_app_args = [
        bytes("w", "utf-8"),
        bytes("5", "utf-8"),
        bytes("5", "utf-8"),
    ]

    # Transaction to Validator
    txn_1 = transaction.ApplicationCallTxn(
        sender=TEST_ACCOUNT_ADDRESS,
        sp=algod_client.suggested_params(),
        index=VALIDATOR_INDEX,
        on_complete=transaction.OnComplete.NoOpOC,
        accounts=[ESCROW_ADDRESS],
        app_args=encoded_app_args,
    )

    # Transaction to Manager
    txn_2 = transaction.ApplicationCallTxn(
        sender=TEST_ACCOUNT_ADDRESS,
        sp=algod_client.suggested_params(),
        index=MANAGER_INDEX,
        on_complete=transaction.OnComplete.NoOpOC,
        accounts=[ESCROW_ADDRESS],
        app_args=encoded_app_args,
    )

    # Transaction to Escrow
    txn_3 = transaction.AssetTransferTxn(
        sender=TEST_ACCOUNT_ADDRESS,
        sp=algod_client.suggested_params(),
        receiver=ESCROW_ADDRESS,
        amt=TOKEN_AMOUNT,
        index=LIQUIDITY_TOKEN_INDEX
    )

    # Get group ID and assign to transactions
    gid = transaction.calculate_group_id([txn_1, txn_2, txn_3])
    txn_1.group = gid
    txn_2.group = gid
    txn_3.group = gid

    # Sign transactions
    stxn_1 = txn_1.sign(TEST_ACCOUNT_PRIVATE_KEY)
    stxn_2 = txn_2.sign(TEST_ACCOUNT_PRIVATE_KEY)
    stxn_3 = txn_3.sign(TEST_ACCOUNT_PRIVATE_KEY)

    # Broadcast the transactions
    signed_txns = [stxn_1, stxn_2, stxn_3]
    tx_id = algod_client.send_transactions(signed_txns)

    # Wait for transaction
    wait_for_transaction(tx_id)

    print(f"Withdraw liquidity transaction sent from User to AlgoSwap successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}")

    print()
Пример #2
0
 def sample_txn(cls, sender, txn_type):
     """
     Helper function for creation Transaction for dryrun
     """
     sp = transaction.SuggestedParams(int(1000), int(1), int(100), "", flat_fee=True)
     if txn_type == payment_txn:
         txn = transaction.Transaction(sender, sp, None, None, payment_txn, None)
     elif txn_type == appcall_txn:
         txn = transaction.ApplicationCallTxn(sender, sp, 0, 0)
     else:
         raise ValueError("unsupported src object")
     return txn
Пример #3
0
def remove_liquidity_call(client, user, user_priv_key, suggested_params,
                          app_id, amount):
    txn = transaction.ApplicationCallTxn(
        user,
        suggested_params,
        app_id,
        transaction.OnComplete.NoOpOC.real,
        app_args=['REMOVE_LIQUIDITY'.encode('utf-8'),
                  int_to_bytes(amount)])

    signed_txn = txn.sign(user_priv_key)
    tx_id = client.send_transactions([signed_txn])
    return tx_id
Пример #4
0
def add_liquidity_call(
    client,
    user,
    user_priv_key,
    suggested_params,
    app_id,
    escrow_addr,
    asset_amount,
    algos_amount,
    asset_index,
):
    app_txn = transaction.ApplicationCallTxn(
        user,
        suggested_params,
        app_id,
        transaction.OnComplete.NoOpOC.real,
        app_args=['ADD_LIQUIDITY'.encode('utf-8')])

    asset_add_txn = transaction.AssetTransferTxn(
        user,
        suggested_params,
        escrow_addr,
        asset_amount,
        asset_index,
    )

    algos_add_txn = transaction.PaymentTxn(
        user,
        suggested_params,
        escrow_addr,
        algos_amount,
    )

    gid = transaction.calculate_group_id(
        [app_txn, asset_add_txn, algos_add_txn])
    app_txn.group = gid
    asset_add_txn.group = gid
    algos_add_txn.group = gid

    signed_app_txn = app_txn.sign(user_priv_key)
    signed_asset_add_txn = asset_add_txn.sign(user_priv_key)
    signed_algos_add_txn = algos_add_txn.sign(user_priv_key)

    tx_id = client.send_transactions([
        signed_app_txn,
        signed_asset_add_txn,
        signed_algos_add_txn,
    ])

    return tx_id
Пример #5
0
def withdraw_call(
    client,
    user,
    user_priv_key,
    suggested_params,
    app_id,
    escrow_addr,
    asset_index,
    algos_amount=0,
    asset_amount=0,
):
    app_txn = transaction.ApplicationCallTxn(
        user,
        suggested_params,
        app_id,
        transaction.OnComplete.NoOpOC.real,
        app_args=['WITHDRAW'.encode('utf-8')])

    asset_withdraw_txn = transaction.AssetTransferTxn(
        escrow_addr,
        suggested_params,
        user,
        asset_amount,
        asset_index,
    )
    algos_withdraw_txn = transaction.PaymentTxn(
        escrow_addr,
        suggested_params,
        user,
        algos_amount,
    )

    gid = transaction.calculate_group_id(
        [app_txn, asset_withdraw_txn, algos_withdraw_txn])
    app_txn.group = gid
    asset_withdraw_txn.group = gid
    algos_withdraw_txn.group = gid

    lsig = transaction.LogicSig(
        compile_program(client,
                        open('./contracts/escrow.teal', 'rb').read()))
    signed_asset_withdraw_txn = transaction.LogicSigTransaction(
        asset_withdraw_txn, lsig)
    signed_algos_withdraw_txn = transaction.LogicSigTransaction(
        algos_withdraw_txn, lsig)
    signed_app_txn = app_txn.sign(user_priv_key)
    tx_id = client.send_transactions(
        [signed_app_txn, signed_asset_withdraw_txn, signed_algos_withdraw_txn])
    return tx_id
Пример #6
0
 def appl(self, sender, index: int, on_complete=txn.OnComplete.NoOpOC,
          send=None, **kwargs):
     params = self.algod.suggested_params()
     local_schema = self.coerce_schema(kwargs.pop("local_schema", None))
     global_schema = self.coerce_schema(kwargs.pop("global_schema", None))
     tx = txn.ApplicationCallTxn(
         sender,
         params,
         index,
         on_complete,
         local_schema=local_schema,
         global_schema=global_schema,
         **kwargs,
     )
     return self.finish(tx, send)
Пример #7
0
def add_escrow(
    client,
    creator,
    creator_priv_key,
    suggested_params,
    app_id,
    escrow_addr,
):
    txn = transaction.ApplicationCallTxn(
        creator,
        suggested_params,
        app_id,
        transaction.OnComplete.NoOpOC.real,
        app_args=['UPDATE'.encode('utf-8')],
        accounts=[escrow_addr],
    )
    signed_txn = txn.sign(creator_priv_key)
    tx_id = client.send_transactions([signed_txn])
    return tx_id
Пример #8
0
def swap_call(client,
              user,
              user_priv_key,
              suggested_params,
              app_id,
              amount,
              escrow_addr,
              asset_index=None):
    app_txn = transaction.ApplicationCallTxn(
        user,
        suggested_params,
        app_id,
        transaction.OnComplete.NoOpOC.real,
        app_args=['SWAP'.encode('utf-8')])

    if asset_index:
        swap_txn = transaction.AssetTransferTxn(
            user,
            suggested_params,
            escrow_addr,
            amount,
            asset_index,
        )
    else:
        swap_txn = transaction.PaymentTxn(
            user,
            suggested_params,
            escrow_addr,
            amount,
        )

    gid = transaction.calculate_group_id([app_txn, swap_txn])
    app_txn.group = gid
    swap_txn.group = gid

    signed_app_txn = app_txn.sign(user_priv_key)
    signed_swap_txn = swap_txn.sign(user_priv_key)
    tx_id = client.send_transactions([signed_app_txn, signed_swap_txn])
    return tx_id
def withdraw_protocol_fees():
    print("Building withdraw protocol fees atomic transaction group...")

    encoded_app_args = [bytes("p", "utf-8")]

    # Get unclaimed TOKEN1 and TOKEN2 protocol fees
    protocol_unused_token1 = protocol_unused_token2 = 0
    account_info = algod_client.account_info(ESCROW_ADDRESS)
    local_state = account_info['apps-local-state']
    for block in local_state:
        if block['id'] == MANAGER_INDEX:
            for kvs in block['key-value']:
                decoded_key = base64.b64decode(kvs['key'])
                prefix_bytes = decoded_key[:2]
                prefix_key = prefix_bytes.decode('utf-8')

                if (prefix_key == "P1"):
                    protocol_unused_token1 = kvs['value']['uint']
                elif (prefix_key == "P2"):
                    protocol_unused_token2 = kvs['value']['uint']

    print(
        f"Unused Protocol Fees is Token 1 = {protocol_unused_token1} and Token 2 = {protocol_unused_token2}"
    )

    if protocol_unused_token1 != 0 and protocol_unused_token2 != 0:
        # Transaction to Validator
        txn_1 = transaction.ApplicationCallTxn(
            sender=DEVELOPER_ACCOUNT_ADDRESS,
            sp=algod_client.suggested_params(),
            index=VALIDATOR_INDEX,
            on_complete=transaction.OnComplete.NoOpOC,
            accounts=[ESCROW_ADDRESS],
            app_args=encoded_app_args)

        # Transaction to Manager
        txn_2 = transaction.ApplicationCallTxn(
            sender=DEVELOPER_ACCOUNT_ADDRESS,
            sp=algod_client.suggested_params(),
            index=MANAGER_INDEX,
            on_complete=transaction.OnComplete.NoOpOC,
            accounts=[ESCROW_ADDRESS],
            app_args=encoded_app_args)

        # Transaction to send Token 1 from Escrow to Developer
        txn_3 = transaction.AssetTransferTxn(
            sender=ESCROW_ADDRESS,
            sp=algod_client.suggested_params(),
            receiver=DEVELOPER_ACCOUNT_ADDRESS,
            amt=protocol_unused_token1,
            index=TOKEN1_INDEX)

        # Transaction to send Token 2 from Escrow to Developer
        txn_4 = transaction.AssetTransferTxn(
            sender=ESCROW_ADDRESS,
            sp=algod_client.suggested_params(),
            receiver=DEVELOPER_ACCOUNT_ADDRESS,
            amt=protocol_unused_token2,
            index=TOKEN2_INDEX)

        # Make Logicsig
        program = base64.b64decode(ESCROW_LOGICSIG)
        lsig = transaction.LogicSig(program)

        # Get group ID and assign to transactions
        gid = transaction.calculate_group_id([txn_1, txn_2, txn_3, txn_4])
        txn_1.group = gid
        txn_2.group = gid
        txn_3.group = gid
        txn_4.group = gid

        # Sign transactions
        stxn_1 = txn_1.sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
        stxn_2 = txn_2.sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
        stxn_3 = transaction.LogicSigTransaction(txn_3, lsig)
        stxn_4 = transaction.LogicSigTransaction(txn_4, lsig)

        # Broadcast the transactions
        signed_txns = [stxn_1, stxn_2, stxn_3, stxn_4]
        tx_id = algod_client.send_transactions(signed_txns)

        # Wait for transaction
        wait_for_transaction(tx_id)

        print(
            f"Withdrew protocol fees for Token 1 and Token 2 from AlgoSwap to Developer successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
        )
    print()
Пример #10
0
def get_token1_refund():
    print("Attempting to get refund of Token 1 from Escrow...")

    encoded_app_args = [bytes("r", "utf-8")]

    # Calculate unused_token1
    unused_token1 = 0
    account_info = algod_client.account_info(TEST_ACCOUNT_ADDRESS)
    local_state = account_info['apps-local-state']
    for block in local_state:
        if block['id'] == MANAGER_INDEX:
            for kvs in block['key-value']:
                decoded_key = base64.b64decode(kvs['key'])
                prefix_bytes = decoded_key[:2]
                prefix_key = prefix_bytes.decode('utf-8')
                addr_bytes = decoded_key[2:]
                b32_encoded_addr = base64.b32encode(addr_bytes).decode('utf-8')
                escrow_addr = encoding.encode_address(
                    base64.b32decode(b32_encoded_addr))

                if (prefix_key == "U1" and ESCROW_ADDRESS == escrow_addr):
                    unused_token1 = int(kvs['value']['uint'])

    print(f"User unused Token 1 is {unused_token1}")

    if unused_token1 != 0:
        # Transaction to Validator
        txn_1 = transaction.ApplicationCallTxn(
            sender=TEST_ACCOUNT_ADDRESS,
            sp=algod_client.suggested_params(),
            index=VALIDATOR_INDEX,
            on_complete=transaction.OnComplete.NoOpOC,
            accounts=[ESCROW_ADDRESS],
            app_args=encoded_app_args)

        # Transaction to Manager
        txn_2 = transaction.ApplicationCallTxn(
            sender=TEST_ACCOUNT_ADDRESS,
            sp=algod_client.suggested_params(),
            index=MANAGER_INDEX,
            on_complete=transaction.OnComplete.NoOpOC,
            accounts=[ESCROW_ADDRESS],
            app_args=encoded_app_args)

        # Make LogicSig
        program = base64.b64decode(ESCROW_LOGICSIG)
        lsig = transaction.LogicSig(program)

        # Transaction to get refund of Token 1 from Escrow
        txn_3 = transaction.AssetTransferTxn(
            sender=ESCROW_ADDRESS,
            sp=algod_client.suggested_params(),
            receiver=TEST_ACCOUNT_ADDRESS,
            amt=unused_token1,
            index=TOKEN1_INDEX)

        # Get group ID and assign to transactions
        gid = transaction.calculate_group_id([txn_1, txn_2, txn_3])
        txn_1.group = gid
        txn_2.group = gid
        txn_3.group = gid

        # Sign transactions
        stxn_1 = txn_1.sign(TEST_ACCOUNT_PRIVATE_KEY)
        stxn_2 = txn_2.sign(TEST_ACCOUNT_PRIVATE_KEY)
        stxn_3 = transaction.LogicSigTransaction(txn_3, lsig)

        # Broadcast the transactions
        signed_txns = [stxn_1, stxn_2, stxn_3]
        tx_id = algod_client.send_transactions(signed_txns)

        print(
            f"Got refund of Token 1 from AlgoSwap to User successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
        )

    print()