From b0b3be9d22bcec0dd425fb8a1dad236eb7731166 Mon Sep 17 00:00:00 2001 From: inaie ignacio Date: Mon, 13 May 2024 11:03:40 +0800 Subject: [PATCH] fix: add tests for transfer with rekey --- tests/test_transfer.py | 98 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/tests/test_transfer.py b/tests/test_transfer.py index 255f9985..3e0af497 100644 --- a/tests/test_transfer.py +++ b/tests/test_transfer.py @@ -17,8 +17,12 @@ transfer, transfer_asset, ) +from algokit_utils.application_client import ApplicationClient +from algokit_utils.application_specification import ApplicationSpecification from algokit_utils.dispenser_api import DispenserApiConfig from algokit_utils.network_clients import get_algod_client, get_algonode_config +from algosdk.atomic_transaction_composer import AccountTransactionSigner +from algosdk.transaction import PaymentTxn from algosdk.util import algos_to_microalgos from pytest_httpx import HTTPXMock @@ -28,16 +32,73 @@ if TYPE_CHECKING: from algosdk.kmd import KMDClient from algosdk.v2client.algod import AlgodClient + from algosdk.v2client.indexer import IndexerClient MINIMUM_BALANCE = 100_000 # see https://developer.algorand.org/docs/get-details/accounts/#minimum-balance +@pytest.fixture(scope="module") +def client_fixture( + algod_client: "AlgodClient", + indexer_client: "IndexerClient", + app_spec: ApplicationSpecification, + funded_account: Account, +) -> ApplicationClient: + return ApplicationClient(algod_client, app_spec, creator=funded_account, indexer_client=indexer_client) + + @pytest.fixture() def to_account(kmd_client: "KMDClient") -> Account: return create_kmd_wallet_account(kmd_client, get_unique_name()) +@pytest.fixture() +def rekeyed_account(algod_client: "AlgodClient", kmd_client: "KMDClient") -> Account: + account = create_kmd_wallet_account(kmd_client, get_unique_name()) + rekey_account = create_kmd_wallet_account(kmd_client, get_unique_name()) + + ensure_funded( + algod_client, + EnsureBalanceParameters( + account_to_fund=account, + min_spending_balance_micro_algos=300000, + min_funding_increment_micro_algos=1, + ), + ) + + rekey_txn = PaymentTxn( + sender=account.address, + receiver=account.address, + amt=0, + note="rekey account", + rekey_to=rekey_account.address, + sp=algod_client.suggested_params(), + ) # type: ignore[no-untyped-call] + signed_rekey_txn = rekey_txn.sign(account.private_key) # type: ignore[no-untyped-call] + algod_client.send_transaction(signed_rekey_txn) + + return Account(address=account.address, private_key=rekey_account.private_key) + + +@pytest.fixture() +def account_transaction_signer( + kmd_client: "KMDClient", + algod_client: "AlgodClient", +) -> AccountTransactionSigner: + account = create_kmd_wallet_account(kmd_client, get_unique_name()) + ensure_funded( + algod_client, + EnsureBalanceParameters( + account_to_fund=account, + min_spending_balance_micro_algos=300000, + min_funding_increment_micro_algos=1, + ), + ) + + return AccountTransactionSigner(private_key=account.private_key) + + @pytest.fixture() def clawback_account(kmd_client: "KMDClient") -> Account: return create_kmd_wallet_account(kmd_client, get_unique_name()) @@ -59,6 +120,43 @@ def test_transfer_algo(algod_client: "AlgodClient", to_account: Account, funded_ actual_amount = to_account_info.get("amount") assert actual_amount == requested_amount +def test_transfer_algo_rekey_account( + algod_client: "AlgodClient", to_account: Account, rekeyed_account: Account +) -> None: + requested_amount = 100_000 + transfer( + algod_client, + TransferParameters( + from_account=rekeyed_account, + to_address=to_account.address, + micro_algos=requested_amount, + ), + ) + + to_account_info = algod_client.account_info(to_account.address) + assert isinstance(to_account_info, dict) + actual_amount = to_account_info.get("amount") + assert actual_amount == requested_amount + + +def test_transfer_algo_transaction_signer_account( + algod_client: "AlgodClient", to_account: Account, account_transaction_signer: AccountTransactionSigner +) -> None: + requested_amount = 100_000 + transfer( + algod_client, + TransferParameters( + from_account=account_transaction_signer, + to_address=to_account.address, + micro_algos=requested_amount, + ), + ) + + to_account_info = algod_client.account_info(to_account.address) + assert isinstance(to_account_info, dict) + actual_amount = to_account_info.get("amount") + assert actual_amount == requested_amount + def test_transfer_algo_max_fee_fails(algod_client: "AlgodClient", to_account: Account, funded_account: Account) -> None: requested_amount = 100_000