Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix problem with transfer from rekeyed account in wallet #86

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/html/searchindex.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions src/algokit_utils/_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def transfer(client: "AlgodClient", parameters: TransferParameters) -> PaymentTx
params = parameters
params.suggested_params = parameters.suggested_params or client.suggested_params()
from_account = params.from_account
sender = address_from_private_key(from_account.private_key) # type: ignore[no-untyped-call]
sender = _get_address(from_account)
transaction = PaymentTxn(
sender=sender,
receiver=params.to_address,
Expand All @@ -105,7 +105,7 @@ def transfer_asset(client: "AlgodClient", parameters: TransferAssetParameters) -

params = parameters
params.suggested_params = parameters.suggested_params or client.suggested_params()
sender = address_from_private_key(parameters.from_account.private_key) # type: ignore[no-untyped-call]
sender = _get_address(parameters.from_account)
suggested_params = parameters.suggested_params or client.suggested_params()
xfer_txn = AssetTransferTxn(
sp=suggested_params,
Expand Down Expand Up @@ -139,9 +139,14 @@ def _send_transaction(
client.send_transaction(signed_transaction)

txid = transaction.get_txid() # type: ignore[no-untyped-call]
logger.debug(
f"Sent transaction {txid} type={transaction.type} from "
f"{address_from_private_key(parameters.from_account.private_key)}" # type: ignore[no-untyped-call]
)
logger.debug(f"Sent transaction {txid} type={transaction.type} from {_get_address(parameters.from_account)}")

return transaction


def _get_address(account: Account | AccountTransactionSigner) -> str:
if type(account) is Account:
return account.address
else:
address = address_from_private_key(account.private_key) # type: ignore[no-untyped-call]
return str(address)
87 changes: 87 additions & 0 deletions tests/test_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
)
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

Expand All @@ -38,6 +40,53 @@ def to_account(kmd_client: "KMDClient") -> Account:
return create_kmd_wallet_account(kmd_client, get_unique_name())


@pytest.fixture()
def rekeyed_from_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 transaction_signer_from_account(
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())
Expand All @@ -60,6 +109,44 @@ def test_transfer_algo(algod_client: "AlgodClient", to_account: Account, funded_
assert actual_amount == requested_amount


def test_transfer_algo_rekey_account(
algod_client: "AlgodClient", to_account: Account, rekeyed_from_account: Account
) -> None:
requested_amount = 100_000
transfer(
algod_client,
TransferParameters(
from_account=rekeyed_from_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, transaction_signer_from_account: AccountTransactionSigner
) -> None:
requested_amount = 100_000
transfer(
algod_client,
TransferParameters(
from_account=transaction_signer_from_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_max_fee_fails(algod_client: "AlgodClient", to_account: Account, funded_account: Account) -> None:
requested_amount = 100_000
max_fee = 123
Expand Down
Loading