From 7dc9fce0629b9367fe91ca70b5587120e11437d0 Mon Sep 17 00:00:00 2001 From: Sebastian Liu Date: Sun, 17 Aug 2025 15:22:08 -0700 Subject: [PATCH 1/4] feat: add wait_for_receipt and timeout options to tx_options - Added 'wait_for_receipt' option (bool, default True) to control whether to wait for transaction receipt - Added 'timeout' option (int/float) to specify custom timeout for waiting for receipt - Maintains backward compatibility by defaulting wait_for_receipt to True - Added comprehensive unit tests (10 new tests) and integration tests (5 new tests) - Fixed import issue in test_derivative_data.py (using pytest.raises instead of _pytest.raises) --- .../utils/transaction_utils.py | 28 +- .../test_integration_transaction_utils.py | 161 ++++++++++- tests/unit/utils/test_derivative_data.py | 2 +- tests/unit/utils/test_transaction_utils.py | 251 +++++++++++++++++- 4 files changed, 426 insertions(+), 16 deletions(-) diff --git a/src/story_protocol_python_sdk/utils/transaction_utils.py b/src/story_protocol_python_sdk/utils/transaction_utils.py index 0d80b62d..e203cffd 100644 --- a/src/story_protocol_python_sdk/utils/transaction_utils.py +++ b/src/story_protocol_python_sdk/utils/transaction_utils.py @@ -1,5 +1,3 @@ -# src/story_protcol_python_sdk/utils/transaction_utils.py - from web3 import Web3 TRANSACTION_TIMEOUT = 300 @@ -19,9 +17,16 @@ def build_and_send_transaction( :param account: The account to use for signing the transaction. :param client_function: The client function to build the transaction. :param client_args: Arguments to pass to the client function. - :param tx_options dict: Optional transaction options. Can include 'nonce' to use a custom nonce value. - If not provided, nonce will be fetched from web3.eth.get_transaction_count(). - :return dict: A dictionary with the transaction hash and receipt, or encoded data if encodedTxDataOnly is True. + :param tx_options dict: Optional transaction options. Can include: + - 'nonce': Custom nonce value (int). If not provided, nonce will be fetched from web3.eth.get_transaction_count(). + - 'wait_for_receipt': Whether to wait for transaction receipt (bool, default True). + - 'timeout': Custom timeout in seconds for waiting for receipt (int/float, default TRANSACTION_TIMEOUT). + - 'encodedTxDataOnly': If True, returns encoded transaction data without sending. + - 'value': Transaction value in wei. + - 'gasPrice': Gas price in gwei. + - 'maxFeePerGas': Max fee per gas in wei. + :return dict: A dictionary with the transaction hash and optionally receipt (if wait_for_receipt is True), + or encoded data if encodedTxDataOnly is True. :raises Exception: If there is an error during the transaction process. """ try: @@ -43,7 +48,6 @@ def build_and_send_transaction( account.address ) - # Add value if it exists in tx_options if "value" in tx_options: transaction_options["value"] = tx_options["value"] @@ -56,18 +60,20 @@ def build_and_send_transaction( transaction = client_function(*client_args, transaction_options) - # If encodedTxDataOnly is True, return the transaction data without sending if tx_options.get("encodedTxDataOnly"): return {"encodedTxData": transaction} signed_txn = account.sign_transaction(transaction) tx_hash = web3.eth.send_raw_transaction(signed_txn.raw_transaction) - tx_receipt = web3.eth.wait_for_transaction_receipt( - tx_hash, timeout=TRANSACTION_TIMEOUT - ) + wait_for_receipt = tx_options.get("wait_for_receipt", True) - return {"tx_hash": tx_hash.hex(), "tx_receipt": tx_receipt} + if wait_for_receipt: + timeout = tx_options.get("timeout", TRANSACTION_TIMEOUT) + tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash, timeout=timeout) + return {"tx_hash": tx_hash.hex(), "tx_receipt": tx_receipt} + else: + return {"tx_hash": tx_hash.hex()} except Exception as e: raise e diff --git a/tests/integration/test_integration_transaction_utils.py b/tests/integration/test_integration_transaction_utils.py index b88b828a..c4f835c8 100644 --- a/tests/integration/test_integration_transaction_utils.py +++ b/tests/integration/test_integration_transaction_utils.py @@ -1,4 +1,4 @@ -# tests/integration/test_integration_transaction_utils.py +import time import pytest @@ -122,3 +122,162 @@ def approve_func(tx_options): assert result["tx_receipt"]["status"] == 1 tx = web3.eth.get_transaction(result["tx_hash"]) assert tx["nonce"] == current_nonce + + def test_wait_for_receipt_false_returns_only_tx_hash(self): + """Test that wait_for_receipt=False returns immediately with only tx_hash.""" + + def create_transfer_tx(to_address, value): + def build_tx(tx_options): + return { + "to": to_address, + "value": value, + "data": "0x", + "gas": 21000, + "gasPrice": web3.eth.gas_price, + "chainId": 1315, + **tx_options, + } + + return build_tx + + tx_func = create_transfer_tx(account.address, 0) + start_time = time.time() + result = build_and_send_transaction( + web3, account, tx_func, tx_options={"wait_for_receipt": False} + ) + elapsed_time = time.time() - start_time + + assert elapsed_time < 2 + assert "tx_hash" in result + assert "tx_receipt" not in result + assert len(result["tx_hash"]) == 64 # 32 bytes hex without 0x prefix + + tx_receipt = web3.eth.wait_for_transaction_receipt(result["tx_hash"]) + assert tx_receipt["status"] == 1 + + def test_wait_for_receipt_true_returns_receipt(self): + """Test that wait_for_receipt=True (default) returns both tx_hash and receipt.""" + + def create_transfer_tx(to_address, value): + def build_tx(tx_options): + return { + "to": to_address, + "value": value, + "data": "0x", + "gas": 21000, + "gasPrice": web3.eth.gas_price, + "chainId": 1315, + **tx_options, + } + + return build_tx + + tx_func = create_transfer_tx(account.address, 0) + result = build_and_send_transaction( + web3, account, tx_func, tx_options={"wait_for_receipt": True} + ) + + assert "tx_hash" in result + assert "tx_receipt" in result + assert result["tx_receipt"]["status"] == 1 + assert "blockNumber" in result["tx_receipt"] + assert "gasUsed" in result["tx_receipt"] + + def test_custom_timeout_with_transaction(self): + """Test that custom timeout is used when specified.""" + + def create_transfer_tx(to_address, value): + def build_tx(tx_options): + return { + "to": to_address, + "value": value, + "data": "0x", + "gas": 21000, + "gasPrice": web3.eth.gas_price, + "chainId": 1315, + **tx_options, + } + + return build_tx + + tx_func = create_transfer_tx(account.address, 0) + result = build_and_send_transaction( + web3, account, tx_func, tx_options={"wait_for_receipt": True, "timeout": 30} + ) + + assert "tx_hash" in result + assert "tx_receipt" in result + assert result["tx_receipt"]["status"] == 1 + + def test_combined_options_nonce_wait_timeout(self): + """Test that all new options work correctly together.""" + current_nonce = web3.eth.get_transaction_count(account.address) + + def create_transfer_tx(to_address, value): + def build_tx(tx_options): + return { + "to": to_address, + "value": value, + "data": "0x", + "gas": 21000, + "gasPrice": web3.eth.gas_price, + "chainId": 1315, + **tx_options, + } + + return build_tx + + tx_func = create_transfer_tx(account.address, 0) + result = build_and_send_transaction( + web3, + account, + tx_func, + tx_options={ + "nonce": current_nonce, + "wait_for_receipt": True, + "timeout": 60, + }, + ) + + assert "tx_hash" in result + assert "tx_receipt" in result + assert result["tx_receipt"]["status"] == 1 + + tx = web3.eth.get_transaction(result["tx_hash"]) + assert tx["nonce"] == current_nonce + + def test_wait_for_receipt_false_with_contract_call(self): + """Test wait_for_receipt=False with actual contract interaction.""" + erc20_contract = web3.eth.contract( + address=MockERC20, + abi=[ + { + "inputs": [ + {"name": "spender", "type": "address"}, + {"name": "amount", "type": "uint256"}, + ], + "name": "approve", + "outputs": [{"name": "", "type": "bool"}], + "stateMutability": "nonpayable", + "type": "function", + } + ], + ) + + def approve_func(tx_options): + return erc20_contract.functions.approve( + account.address, 200 + ).build_transaction(tx_options) + + start_time = time.time() + result = build_and_send_transaction( + web3, account, approve_func, tx_options={"wait_for_receipt": False} + ) + elapsed_time = time.time() - start_time + + assert elapsed_time < 2 + assert "tx_hash" in result + assert "tx_receipt" not in result + + tx_receipt = web3.eth.wait_for_transaction_receipt(result["tx_hash"]) + assert tx_receipt["status"] == 1 diff --git a/tests/unit/utils/test_derivative_data.py b/tests/unit/utils/test_derivative_data.py index 7014ea61..1162df4f 100644 --- a/tests/unit/utils/test_derivative_data.py +++ b/tests/unit/utils/test_derivative_data.py @@ -1,7 +1,7 @@ from unittest.mock import MagicMock, patch import pytest -from _pytest.raises import raises +from pytest import raises from story_protocol_python_sdk.abi.IPAssetRegistry.IPAssetRegistry_client import ( IPAssetRegistryClient, diff --git a/tests/unit/utils/test_transaction_utils.py b/tests/unit/utils/test_transaction_utils.py index f14c86e5..33c0ec87 100644 --- a/tests/unit/utils/test_transaction_utils.py +++ b/tests/unit/utils/test_transaction_utils.py @@ -47,11 +47,9 @@ def test_custom_nonce_is_used(self, mock_web3, mock_account, mock_client_functio mock_client_function, tx_options=tx_options, ) - # Verify the client function was called with correct nonce mock_client_function.assert_called_once() call_args = mock_client_function.call_args[0][-1] assert call_args["nonce"] == custom_nonce - # When custom nonce is provided, get_transaction_count should not be called mock_web3.eth.get_transaction_count.assert_not_called() def test_automatic_nonce_fetch_when_not_provided( @@ -241,7 +239,6 @@ def test_encoded_tx_data_only_with_custom_nonce( mock_client_function.assert_called_once() call_args = mock_client_function.call_args[0][-1] assert call_args["nonce"] == custom_nonce - # Verify transaction was not sent mock_account.sign_transaction.assert_not_called() mock_web3.eth.send_raw_transaction.assert_not_called() @@ -270,3 +267,251 @@ def test_no_tx_options_uses_default_nonce( mock_client_function.assert_called_once() call_args = mock_client_function.call_args[0][-1] assert call_args["nonce"] == chain_nonce + + def test_wait_for_receipt_default_true( + self, mock_web3, mock_account, mock_client_function + ): + """Test that wait_for_receipt defaults to True for backward compatibility.""" + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhash123") + ) + mock_web3.eth.wait_for_transaction_receipt.return_value = {"status": 1} + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={}, + ) + + mock_web3.eth.wait_for_transaction_receipt.assert_called_once() + assert "tx_hash" in result + assert "tx_receipt" in result + assert result["tx_receipt"] == {"status": 1} + + def test_wait_for_receipt_false_returns_immediately( + self, mock_web3, mock_account, mock_client_function + ): + """Test that wait_for_receipt=False returns immediately without waiting.""" + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhash456") + ) + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={"wait_for_receipt": False}, + ) + + mock_web3.eth.wait_for_transaction_receipt.assert_not_called() + assert "tx_hash" in result + assert result["tx_hash"] == "0xhash456" + assert "tx_receipt" not in result + + def test_custom_timeout_is_used( + self, mock_web3, mock_account, mock_client_function + ): + """Test that custom timeout from tx_options is used when provided.""" + custom_timeout = 60 + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhash789") + ) + mock_web3.eth.wait_for_transaction_receipt.return_value = {"status": 1} + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={"timeout": custom_timeout}, + ) + + mock_web3.eth.wait_for_transaction_receipt.assert_called_once_with( + mock_web3.eth.send_raw_transaction.return_value, timeout=custom_timeout + ) + assert "tx_hash" in result + assert "tx_receipt" in result + + def test_timeout_ignored_when_not_waiting_for_receipt( + self, mock_web3, mock_account, mock_client_function + ): + """Test that timeout is ignored when wait_for_receipt is False.""" + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhashabc") + ) + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={"wait_for_receipt": False, "timeout": 60}, + ) + + mock_web3.eth.wait_for_transaction_receipt.assert_not_called() + assert "tx_hash" in result + assert "tx_receipt" not in result + + def test_wait_for_receipt_with_custom_nonce_and_timeout( + self, mock_web3, mock_account, mock_client_function + ): + """Test that wait_for_receipt works correctly with other options.""" + tx_options = { + "nonce": 42, + "wait_for_receipt": True, + "timeout": 120, + "value": 1000, + } + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhashdef") + ) + mock_web3.eth.wait_for_transaction_receipt.return_value = {"status": 1} + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options=tx_options, + ) + + mock_client_function.assert_called_once() + call_args = mock_client_function.call_args[0][-1] + assert call_args["nonce"] == 42 + assert call_args["value"] == 1000 + + mock_web3.eth.wait_for_transaction_receipt.assert_called_once_with( + mock_web3.eth.send_raw_transaction.return_value, timeout=120 + ) + assert "tx_hash" in result + assert "tx_receipt" in result + + def test_default_timeout_used_when_not_specified( + self, mock_web3, mock_account, mock_client_function + ): + """Test that default TRANSACTION_TIMEOUT is used when timeout not specified.""" + from story_protocol_python_sdk.utils.transaction_utils import ( + TRANSACTION_TIMEOUT, + ) + + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhash999") + ) + mock_web3.eth.wait_for_transaction_receipt.return_value = {"status": 1} + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={"wait_for_receipt": True}, # No timeout specified + ) + + mock_web3.eth.wait_for_transaction_receipt.assert_called_once_with( + mock_web3.eth.send_raw_transaction.return_value, timeout=TRANSACTION_TIMEOUT + ) + assert "tx_hash" in result + assert "tx_receipt" in result + + def test_wait_for_receipt_false_with_encoded_tx_data( + self, mock_web3, mock_account, mock_client_function + ): + """Test that encodedTxDataOnly takes precedence over wait_for_receipt.""" + tx_options = { + "wait_for_receipt": False, + "encodedTxDataOnly": True, + } + expected_tx = {"to": "0xabc", "data": "0x123"} + mock_client_function.return_value = expected_tx + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options=tx_options, + ) + + assert result == {"encodedTxData": expected_tx} + mock_account.sign_transaction.assert_not_called() + mock_web3.eth.send_raw_transaction.assert_not_called() + mock_web3.eth.wait_for_transaction_receipt.assert_not_called() + + def test_very_short_timeout_value( + self, mock_web3, mock_account, mock_client_function + ): + """Test that very short timeout values are accepted.""" + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhashxyz") + ) + mock_web3.eth.wait_for_transaction_receipt.return_value = {"status": 1} + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={"timeout": 1}, # 1 second timeout + ) + + mock_web3.eth.wait_for_transaction_receipt.assert_called_once_with( + mock_web3.eth.send_raw_transaction.return_value, timeout=1 + ) + assert "tx_hash" in result + assert "tx_receipt" in result + + def test_float_timeout_value(self, mock_web3, mock_account, mock_client_function): + """Test that float timeout values are accepted.""" + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhashfloat") + ) + mock_web3.eth.wait_for_transaction_receipt.return_value = {"status": 1} + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={"timeout": 45.5}, # Float timeout + ) + + mock_web3.eth.wait_for_transaction_receipt.assert_called_once_with( + mock_web3.eth.send_raw_transaction.return_value, timeout=45.5 + ) + assert "tx_hash" in result + assert "tx_receipt" in result + + def test_wait_for_receipt_explicitly_true( + self, mock_web3, mock_account, mock_client_function + ): + """Test explicitly setting wait_for_receipt to True.""" + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhashexplicit") + ) + mock_web3.eth.wait_for_transaction_receipt.return_value = { + "status": 1, + "blockNumber": 12345, + } + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + result = build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={"wait_for_receipt": True}, + ) + + mock_web3.eth.wait_for_transaction_receipt.assert_called_once() + assert result["tx_hash"] == "0xhashexplicit" + assert result["tx_receipt"]["status"] == 1 + assert result["tx_receipt"]["blockNumber"] == 12345 From 56931296fd2ecea8ab506ae7f9e63e1892626f37 Mon Sep 17 00:00:00 2001 From: Sebastian Liu Date: Sun, 17 Aug 2025 19:20:40 -0700 Subject: [PATCH 2/4] chore(test): remove elapsed time check --- tests/integration/test_integration_transaction_utils.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/integration/test_integration_transaction_utils.py b/tests/integration/test_integration_transaction_utils.py index c4f835c8..6e4b4669 100644 --- a/tests/integration/test_integration_transaction_utils.py +++ b/tests/integration/test_integration_transaction_utils.py @@ -141,13 +141,9 @@ def build_tx(tx_options): return build_tx tx_func = create_transfer_tx(account.address, 0) - start_time = time.time() result = build_and_send_transaction( web3, account, tx_func, tx_options={"wait_for_receipt": False} ) - elapsed_time = time.time() - start_time - - assert elapsed_time < 2 assert "tx_hash" in result assert "tx_receipt" not in result assert len(result["tx_hash"]) == 64 # 32 bytes hex without 0x prefix @@ -269,13 +265,11 @@ def approve_func(tx_options): account.address, 200 ).build_transaction(tx_options) - start_time = time.time() + time.time() result = build_and_send_transaction( web3, account, approve_func, tx_options={"wait_for_receipt": False} ) - elapsed_time = time.time() - start_time - assert elapsed_time < 2 assert "tx_hash" in result assert "tx_receipt" not in result From 0c43742a712a0e14bee6d32a5fe60b2151d70653 Mon Sep 17 00:00:00 2001 From: Sebastian Liu Date: Sun, 17 Aug 2025 19:42:06 -0700 Subject: [PATCH 3/4] test: additional tests --- .../test_integration_transaction_utils.py | 25 ++++++++++++++++++- tests/unit/utils/test_transaction_utils.py | 25 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_integration_transaction_utils.py b/tests/integration/test_integration_transaction_utils.py index 6e4b4669..155bacb7 100644 --- a/tests/integration/test_integration_transaction_utils.py +++ b/tests/integration/test_integration_transaction_utils.py @@ -1,6 +1,7 @@ import time import pytest +from web3.exceptions import TimeExhausted from story_protocol_python_sdk.utils.transaction_utils import build_and_send_transaction @@ -146,7 +147,7 @@ def build_tx(tx_options): ) assert "tx_hash" in result assert "tx_receipt" not in result - assert len(result["tx_hash"]) == 64 # 32 bytes hex without 0x prefix + assert len(result["tx_hash"]) == 64 tx_receipt = web3.eth.wait_for_transaction_receipt(result["tx_hash"]) assert tx_receipt["status"] == 1 @@ -275,3 +276,25 @@ def approve_func(tx_options): tx_receipt = web3.eth.wait_for_transaction_receipt(result["tx_hash"]) assert tx_receipt["status"] == 1 + + def test_timeout_too_short_raises_exception(self): + """Test that very short timeout raises TimeExhausted exception.""" + + def build_tx(tx_options): + return { + "to": account.address, + "value": 0, + "data": "0x", + "gas": 21000, + "gasPrice": web3.eth.gas_price, + "chainId": 1315, + **tx_options, + } + + with pytest.raises(TimeExhausted): + build_and_send_transaction( + web3, + account, + build_tx, + tx_options={"wait_for_receipt": True, "timeout": 0.001}, + ) diff --git a/tests/unit/utils/test_transaction_utils.py b/tests/unit/utils/test_transaction_utils.py index 33c0ec87..88392caa 100644 --- a/tests/unit/utils/test_transaction_utils.py +++ b/tests/unit/utils/test_transaction_utils.py @@ -2,6 +2,7 @@ import pytest from web3 import Web3 +from web3.exceptions import TimeExhausted from story_protocol_python_sdk.utils.transaction_utils import build_and_send_transaction @@ -515,3 +516,27 @@ def test_wait_for_receipt_explicitly_true( assert result["tx_hash"] == "0xhashexplicit" assert result["tx_receipt"]["status"] == 1 assert result["tx_receipt"]["blockNumber"] == 12345 + + def test_timeout_exception_is_propagated( + self, mock_web3, mock_account, mock_client_function + ): + """Test that TimeExhausted exception is properly propagated when timeout occurs.""" + mock_web3.eth.get_transaction_count.return_value = 10 + mock_web3.eth.send_raw_transaction.return_value = Mock( + hex=Mock(return_value="0xhashtimeout") + ) + mock_web3.eth.wait_for_transaction_receipt.side_effect = TimeExhausted( + "Transaction receipt not found after 0.5 seconds" + ) + mock_account.sign_transaction.return_value = Mock(raw_transaction=b"signed_tx") + + with pytest.raises(TimeExhausted): + build_and_send_transaction( + mock_web3, + mock_account, + mock_client_function, + tx_options={"wait_for_receipt": True, "timeout": 0.5}, + ) + mock_web3.eth.wait_for_transaction_receipt.assert_called_once_with( + mock_web3.eth.send_raw_transaction.return_value, timeout=0.5 + ) From 909b6c4bdad6ae188359f5b4555608457f67c065 Mon Sep 17 00:00:00 2001 From: Sebastian Liu Date: Sun, 17 Aug 2025 19:53:45 -0700 Subject: [PATCH 4/4] test: nit --- .../test_integration_transaction_utils.py | 7 ++++-- tests/unit/utils/test_transaction_utils.py | 23 ++++++------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/tests/integration/test_integration_transaction_utils.py b/tests/integration/test_integration_transaction_utils.py index 155bacb7..fd14c5d7 100644 --- a/tests/integration/test_integration_transaction_utils.py +++ b/tests/integration/test_integration_transaction_utils.py @@ -199,7 +199,10 @@ def build_tx(tx_options): tx_func = create_transfer_tx(account.address, 0) result = build_and_send_transaction( - web3, account, tx_func, tx_options={"wait_for_receipt": True, "timeout": 30} + web3, + account, + tx_func, + tx_options={"wait_for_receipt": True, "timeout": 120}, ) assert "tx_hash" in result @@ -232,7 +235,7 @@ def build_tx(tx_options): tx_options={ "nonce": current_nonce, "wait_for_receipt": True, - "timeout": 60, + "timeout": 120, }, ) diff --git a/tests/unit/utils/test_transaction_utils.py b/tests/unit/utils/test_transaction_utils.py index 88392caa..910dae63 100644 --- a/tests/unit/utils/test_transaction_utils.py +++ b/tests/unit/utils/test_transaction_utils.py @@ -1,7 +1,6 @@ from unittest.mock import Mock import pytest -from web3 import Web3 from web3.exceptions import TimeExhausted from story_protocol_python_sdk.utils.transaction_utils import build_and_send_transaction @@ -10,21 +9,13 @@ class TestBuildAndSendTransaction: """Test suite for build_and_send_transaction function.""" - @pytest.fixture - def mock_web3(self): - """Create a mock Web3 instance.""" - web3 = Mock(spec=Web3) - web3.eth = Mock() - web3.to_wei = Mock(side_effect=lambda value, unit: value * 1000000000) - return web3 - - @pytest.fixture - def mock_account(self): - """Create a mock account.""" - account = Mock() - account.address = "0x1234567890123456789012345678901234567890" - account.sign_transaction = Mock() - return account + @pytest.fixture(autouse=True) + def setup_mocks(self, mock_web3, mock_account): + """Configure and reset the mocks from conftest for our specific needs.""" + mock_web3.reset_mock() + mock_web3.eth.reset_mock() + mock_account.reset_mock() + mock_web3.to_wei = Mock(side_effect=lambda value, unit: value * 1000000000) @pytest.fixture def mock_client_function(self):