Skip to content

Conversation

@sebsadface
Copy link
Member

Summary

This PR adds support for custom nonce management in the transaction utilities, allowing users to optionally specify a nonce value via the tx_options parameter. The implementation maintains full backward compatibility by automatically fetching the nonce from the blockchain when not provided, while adding proper validation to ensure nonce values are non-negative integers.

Changes

  • Modified build_and_send_transaction() to accept optional nonce in tx_options
  • Added validation to ensure nonce is a non-negative integer
  • Falls back to web3.eth.get_transaction_count() when nonce not provided
  • Added tests for transaction_utils

Use Cases

This change enables:

  • Transaction replacement: Reuse same nonce with higher gas price
  • Gap filling: Resubmit failed transactions with specific nonces
  • High-throughput apps: Manual nonce management for parallel transactions (poseidon)

Testing

All 11 unit tests pass covering:

  • Custom nonce usage
  • Automatic nonce fetching
  • Validation of invalid inputs (negative, string, float, None)
  • Edge cases (zero, large values)
  • Integration with other tx options

Example Usage

# Custom nonce
tx_options = {"nonce": 42}
build_and_send_transaction(web3, account, client_func, tx_options=tx_options)

# Automatic (existing behavior)
build_and_send_transaction(web3, account, client_func)

- Allow passing custom nonce via tx_options parameter
- Add validation to ensure nonce is a non-negative integer
- Fallback to web3.eth.get_transaction_count() when nonce not provided
- Add comprehensive unit tests for nonce handling

This enables advanced transaction management scenarios like:
- Transaction replacement with same nonce
- Manual nonce management for high-throughput applications
- Filling nonce gaps from failed transactions
Copy link
Collaborator

@bonnie57 bonnie57 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an integration test to ensure everything is working correctly?

@sebsadface
Copy link
Member Author

sebsadface commented Aug 11, 2025

Screenshot 2025-08-10 at 20 46 36 > Could you add an integration test to ensure everything is working correctly?

added in 8915cd6

@sebsadface sebsadface force-pushed the seb/allow-custom-nonce branch from a1052db to 8915cd6 Compare August 11, 2025 03:47
Copy link
Collaborator

@bonnie57 bonnie57 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Minor thing: The integration tests don't need the story_client object. Please remove the story_client argument and fixture.

@sebsadface
Copy link
Member Author

LGTM

Minor thing: The integration tests don't need the story_client object. Please remove the story_client argument and fixture.

Good catch, resolved in 28ac44a

@lucas2brh lucas2brh merged commit 106c28d into main Aug 11, 2025
12 checks passed
@lucas2brh lucas2brh deleted the seb/allow-custom-nonce branch August 11, 2025 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants