|
| 1 | +"""TCK RPC handler for the approveAllowance endpoint.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from hiero_sdk_python.account.account_allowance_approve_transaction import ( |
| 6 | + AccountAllowanceApproveTransaction, |
| 7 | +) |
| 8 | +from hiero_sdk_python.account.account_id import AccountId |
| 9 | +from hiero_sdk_python.hbar import Hbar |
| 10 | +from hiero_sdk_python.response_code import ResponseCode |
| 11 | +from hiero_sdk_python.tokens.nft_id import NftId |
| 12 | +from hiero_sdk_python.tokens.token_id import TokenId |
| 13 | +from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt |
| 14 | +from tck.handlers.registry import rpc_method |
| 15 | +from tck.param.allowance import AllowanceEntry, ApproveAllowanceParams |
| 16 | +from tck.response.allowance import ApproveAllowanceResponse |
| 17 | +from tck.util.client_utils import get_client |
| 18 | +from tck.util.constants import DEFAULT_GRPC_TIMEOUT |
| 19 | + |
| 20 | + |
| 21 | +def _build_approve_allowance_transaction( |
| 22 | + params: ApproveAllowanceParams, |
| 23 | +) -> AccountAllowanceApproveTransaction: |
| 24 | + """Build an AccountAllowanceApproveTransaction from TCK params.""" |
| 25 | + transaction = AccountAllowanceApproveTransaction().set_grpc_deadline(DEFAULT_GRPC_TIMEOUT) |
| 26 | + |
| 27 | + if not params.allowances: |
| 28 | + raise ValueError("allowances list cannot be empty") |
| 29 | + |
| 30 | + for entry in params.allowances: |
| 31 | + _apply_allowance_entry(transaction, entry) |
| 32 | + |
| 33 | + return transaction |
| 34 | + |
| 35 | + |
| 36 | +def _parse_optional_account_id(value: str | None) -> AccountId | None: |
| 37 | + """Parse an optional account ID string, raising on empty string.""" |
| 38 | + if value is None: |
| 39 | + return None |
| 40 | + if not value.strip(): |
| 41 | + raise ValueError("Account ID cannot be an empty string") |
| 42 | + return AccountId.from_string(value) |
| 43 | + |
| 44 | + |
| 45 | +def _apply_allowance_entry( |
| 46 | + transaction: AccountAllowanceApproveTransaction, |
| 47 | + entry: AllowanceEntry, |
| 48 | +) -> None: |
| 49 | + """Apply a single allowance entry to the transaction.""" |
| 50 | + owner_account_id = _parse_optional_account_id(entry.ownerAccountId) |
| 51 | + spender_account_id = _parse_optional_account_id(entry.spenderAccountId) |
| 52 | + |
| 53 | + if entry.hbar is not None: |
| 54 | + if entry.hbar.amount is None: |
| 55 | + raise ValueError("hbar allowance requires an amount") |
| 56 | + amount = int(entry.hbar.amount) |
| 57 | + transaction.approve_hbar_allowance( |
| 58 | + owner_account_id, |
| 59 | + spender_account_id, |
| 60 | + Hbar.from_tinybars(amount), |
| 61 | + ) |
| 62 | + |
| 63 | + if entry.token is not None: |
| 64 | + if entry.token.tokenId is None or entry.token.amount is None: |
| 65 | + raise ValueError("token allowance requires tokenId and amount") |
| 66 | + token_id = TokenId.from_string(entry.token.tokenId) |
| 67 | + amount = int(entry.token.amount) |
| 68 | + transaction.approve_token_allowance( |
| 69 | + token_id, |
| 70 | + owner_account_id, |
| 71 | + spender_account_id, |
| 72 | + amount, |
| 73 | + ) |
| 74 | + |
| 75 | + if entry.nft is not None: |
| 76 | + token_id = TokenId.from_string(entry.nft.tokenId) |
| 77 | + |
| 78 | + if entry.nft.approvedForAll is True: |
| 79 | + transaction.approve_token_nft_allowance_all_serials( |
| 80 | + token_id, |
| 81 | + owner_account_id, |
| 82 | + spender_account_id, |
| 83 | + ) |
| 84 | + elif entry.nft.approvedForAll is False: |
| 85 | + transaction.delete_token_nft_allowance_all_serials( |
| 86 | + token_id, |
| 87 | + owner_account_id, |
| 88 | + spender_account_id, |
| 89 | + ) |
| 90 | + elif entry.nft.serialNumbers is not None: |
| 91 | + delegating_spender = _parse_optional_account_id(entry.nft.delegateSpenderAccountId) |
| 92 | + |
| 93 | + for serial in entry.nft.serialNumbers: |
| 94 | + nft_id = NftId(token_id=token_id, serial_number=int(serial)) |
| 95 | + if delegating_spender is not None: |
| 96 | + transaction.approve_token_nft_allowance_with_delegating_spender( |
| 97 | + nft_id, |
| 98 | + owner_account_id, |
| 99 | + spender_account_id, |
| 100 | + delegating_spender, |
| 101 | + ) |
| 102 | + else: |
| 103 | + transaction.approve_token_nft_allowance( |
| 104 | + nft_id, |
| 105 | + owner_account_id, |
| 106 | + spender_account_id, |
| 107 | + ) |
| 108 | + else: |
| 109 | + # nft object present with only tokenId — this is DeleteNftAllowanceAllSerials |
| 110 | + # with no approvedForAll field (defaults to delete) |
| 111 | + transaction.delete_token_nft_allowance_all_serials( |
| 112 | + token_id, |
| 113 | + owner_account_id, |
| 114 | + spender_account_id, |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +@rpc_method("approveAllowance") |
| 119 | +def approve_allowance(params: ApproveAllowanceParams) -> ApproveAllowanceResponse: |
| 120 | + """Approve allowances using TCK approveAllowance parameters.""" |
| 121 | + client = get_client(params.sessionId) |
| 122 | + |
| 123 | + transaction = _build_approve_allowance_transaction(params) |
| 124 | + |
| 125 | + if params.commonTransactionParams is not None: |
| 126 | + params.commonTransactionParams.apply_common_params(transaction, client) |
| 127 | + |
| 128 | + response = transaction.execute(client, wait_for_receipt=False) |
| 129 | + receipt: TransactionReceipt = response.get_receipt(client, validate_status=True) |
| 130 | + |
| 131 | + return ApproveAllowanceResponse(status=ResponseCode(receipt.status).name) |
0 commit comments