Skip to content

Commit 1dd5533

Browse files
authored
refactor(tck): centralize transaction receipt validation in execute_validated helper (#2649)
Signed-off-by: Abhijeet Saharan <abhijeetsaharan2236@gmail.com>
1 parent bd665b2 commit 1dd5533

10 files changed

Lines changed: 128 additions & 89 deletions

File tree

tck/handlers/account.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from tck.util.client_utils import get_client
4242
from tck.util.constants import DEFAULT_GRPC_TIMEOUT
4343
from tck.util.key_utils import get_key_from_string, key_to_string
44+
from tck.util.transaction_utils import execute_validated
4445

4546

4647
def _build_create_account_transaction(params: CreateAccountParams) -> AccountCreateTransaction:
@@ -89,17 +90,15 @@ def create_account(params: CreateAccountParams) -> CreateAccountResponse:
8990
if params.commonTransactionParams is not None:
9091
params.commonTransactionParams.apply_common_params(transaction, client)
9192

92-
response = transaction.execute(client, wait_for_receipt=False)
93-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
93+
receipt = execute_validated(transaction, client)
9494

9595
account_id = ""
96-
if receipt.status == ResponseCode.SUCCESS:
97-
account_id = str(receipt.account_id)
96+
account_id = str(receipt.account_id)
9897

9998
return CreateAccountResponse(
10099
account_id,
101100
ResponseCode(receipt.status).name,
102-
str(response.transaction_id) if response.transaction_id is not None else None,
101+
str(receipt.transaction_id) if receipt.transaction_id is not None else None,
103102
)
104103

105104

@@ -150,8 +149,7 @@ def update_account(params: UpdateAccountParams) -> UpdateAccountResponse:
150149
if params.commonTransactionParams is not None:
151150
params.commonTransactionParams.apply_common_params(transaction, client)
152151

153-
response = transaction.execute(client, wait_for_receipt=False)
154-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
152+
receipt = execute_validated(transaction, client)
155153

156154
return UpdateAccountResponse(ResponseCode(receipt.status).name)
157155

@@ -258,8 +256,7 @@ def delete_account(params: DeleteAccountParams) -> DeleteAccountResponse:
258256
if params.commonTransactionParams is not None:
259257
params.commonTransactionParams.apply_common_params(transaction, client)
260258

261-
response = transaction.execute(client, wait_for_receipt=False)
262-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
259+
receipt = execute_validated(transaction, client)
263260

264261
return DeleteAccountResponse(status=ResponseCode(receipt.status).name)
265262

tck/handlers/allowance.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from hiero_sdk_python.response_code import ResponseCode
1414
from hiero_sdk_python.tokens.nft_id import NftId
1515
from hiero_sdk_python.tokens.token_id import TokenId
16-
from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt
1716
from tck.handlers.registry import rpc_method
1817
from tck.param.allowance import (
1918
AllowanceEntry,
@@ -24,6 +23,7 @@
2423
from tck.response.allowance import ApproveAllowanceResponse, DeleteAllowanceResponse
2524
from tck.util.client_utils import get_client
2625
from tck.util.constants import DEFAULT_GRPC_TIMEOUT
26+
from tck.util.transaction_utils import execute_validated
2727

2828

2929
def _build_approve_allowance_transaction(
@@ -130,8 +130,7 @@ def approve_allowance(params: ApproveAllowanceParams) -> ApproveAllowanceRespons
130130
if params.commonTransactionParams is not None:
131131
params.commonTransactionParams.apply_common_params(transaction, client)
132132

133-
response = transaction.execute(client, wait_for_receipt=False)
134-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
133+
receipt = execute_validated(transaction, client)
135134

136135
return ApproveAllowanceResponse(status=ResponseCode(receipt.status).name)
137136

@@ -178,7 +177,6 @@ def delete_allowance(params: DeleteAllowanceParams) -> DeleteAllowanceResponse:
178177
if params.commonTransactionParams is not None:
179178
params.commonTransactionParams.apply_common_params(transaction, client)
180179

181-
response = transaction.execute(client, wait_for_receipt=False)
182-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
180+
receipt = execute_validated(transaction, client)
183181

184182
return DeleteAllowanceResponse(status=ResponseCode(receipt.status).name)

tck/handlers/contract.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from hiero_sdk_python.Duration import Duration
66
from hiero_sdk_python.file.file_id import FileId
77
from hiero_sdk_python.response_code import ResponseCode
8-
from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt
98
from tck.errors import JsonRpcError
109
from tck.handlers.registry import rpc_method
1110
from tck.param.contract import CreateContractParams
@@ -14,6 +13,7 @@
1413
from tck.util.constants import DEFAULT_GRPC_TIMEOUT
1514
from tck.util.key_utils import get_key_from_string
1615
from tck.util.param_utils import decode_hex, to_int
16+
from tck.util.transaction_utils import execute_validated
1717

1818

1919
INT64_MIN = -(2**63)
@@ -99,11 +99,10 @@ def create_contract(params: CreateContractParams) -> CreateContractResponse:
9999
if params.commonTransactionParams is not None:
100100
params.commonTransactionParams.apply_common_params(transaction, client)
101101

102-
response = transaction.execute(client, wait_for_receipt=False)
103-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
102+
receipt = execute_validated(transaction, client)
104103

105104
contract_id = ""
106-
if receipt.status == ResponseCode.SUCCESS and receipt.contract_id is not None:
105+
if receipt.contract_id is not None:
107106
contract_id = str(receipt.contract_id)
108107

109108
return CreateContractResponse(contract_id, ResponseCode(receipt.status).name)

tck/handlers/file.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from hiero_sdk_python.hbar import Hbar
1010
from hiero_sdk_python.response_code import ResponseCode
1111
from hiero_sdk_python.timestamp import Timestamp
12-
from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt
1312
from tck.errors import JsonRpcError
1413
from tck.handlers.registry import rpc_method
1514
from tck.param.file import CreateFileParams, DeleteFileParams, GetFileContentsParams, GetFileInfoParams
@@ -18,6 +17,7 @@
1817
from tck.util.constants import DEFAULT_GRPC_TIMEOUT
1918
from tck.util.key_utils import get_key_from_string, key_to_string
2019
from tck.util.param_utils import to_int
20+
from tck.util.transaction_utils import execute_validated
2121

2222

2323
def _build_create_file_transaction(params: CreateFileParams) -> FileCreateTransaction:
@@ -51,11 +51,10 @@ def create_file(params: CreateFileParams) -> CreateFileResponse:
5151
if params.commonTransactionParams is not None:
5252
params.commonTransactionParams.apply_common_params(transaction, client)
5353

54-
response = transaction.execute(client, wait_for_receipt=False)
55-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
54+
receipt = execute_validated(transaction, client)
5655

5756
file_id = ""
58-
if receipt.status == ResponseCode.SUCCESS and receipt.file_id is not None:
57+
if receipt.file_id is not None:
5958
file_id = str(receipt.file_id)
6059

6160
return CreateFileResponse(file_id, ResponseCode(receipt.status).name)
@@ -122,7 +121,6 @@ def delete_file(params: DeleteFileParams) -> DeleteFileResponse:
122121
if params.commonTransactionParams is not None:
123122
params.commonTransactionParams.apply_common_params(transaction, client)
124123

125-
response = transaction.execute(client, wait_for_receipt=False)
126-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
124+
receipt = execute_validated(transaction, client)
127125

128126
return DeleteFileResponse(ResponseCode(receipt.status).name)

tck/handlers/schedule.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from hiero_sdk_python.schedule.schedule_sign_transaction import ScheduleSignTransaction
1212
from hiero_sdk_python.timestamp import Timestamp
1313
from hiero_sdk_python.transaction.transaction import Transaction
14-
from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt
1514
from tck.errors import JsonRpcError
1615
from tck.handlers.account import _build_create_account_transaction
1716
from tck.handlers.allowance import _build_approve_allowance_transaction
@@ -37,6 +36,7 @@
3736
from tck.util.constants import DEFAULT_GRPC_TIMEOUT
3837
from tck.util.key_utils import get_key_from_string
3938
from tck.util.param_utils import to_int
39+
from tck.util.transaction_utils import execute_validated
4040

4141

4242
# Maps a scheduled transaction method name to its params class and builder.
@@ -154,16 +154,14 @@ def create_schedule(params: CreateScheduleParams) -> CreateScheduleResponse:
154154
if params.commonTransactionParams is not None:
155155
params.commonTransactionParams.apply_common_params(transaction, client)
156156

157-
response = transaction.execute(client, wait_for_receipt=False)
158-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
157+
receipt = execute_validated(transaction, client)
159158

160159
schedule_id = ""
161160
scheduled_transaction_id = None
162-
if receipt.status == ResponseCode.SUCCESS:
163-
if receipt.schedule_id is not None:
164-
schedule_id = str(receipt.schedule_id)
165-
if receipt.scheduled_transaction_id is not None:
166-
scheduled_transaction_id = str(receipt.scheduled_transaction_id)
161+
if receipt.schedule_id is not None:
162+
schedule_id = str(receipt.schedule_id)
163+
if receipt.scheduled_transaction_id is not None:
164+
scheduled_transaction_id = str(receipt.scheduled_transaction_id)
167165

168166
return CreateScheduleResponse(schedule_id, scheduled_transaction_id, ResponseCode(receipt.status).name)
169167

@@ -188,8 +186,7 @@ def sign_schedule(params: SignScheduleParams) -> SignScheduleResponse:
188186
if common_params is not None:
189187
common_params.apply_common_params(transaction, client)
190188

191-
response = transaction.execute(client, wait_for_receipt=False)
192-
receipt: TransactionReceipt = response.get_receipt(client, validate_status=True)
189+
receipt = execute_validated(transaction, client)
193190

194191
return SignScheduleResponse(status=ResponseCode(receipt.status).name)
195192

@@ -204,7 +201,6 @@ def delete_schedule(params: DeleteScheduleParams) -> DeleteScheduleResponse:
204201
if params.commonTransactionParams is not None:
205202
params.commonTransactionParams.apply_common_params(transaction, client)
206203

207-
response = transaction.execute(client, wait_for_receipt=False)
208-
receipt = response.get_receipt(client, validate_status=True)
204+
receipt = execute_validated(transaction, client)
209205

210206
return DeleteScheduleResponse(status=ResponseCode(receipt.status).name)

0 commit comments

Comments
 (0)