Skip to content

Refactor EVM transaction handling for clarity and maintainability#703

Closed
seunlanlege wants to merge 1 commit intomainfrom
claude/refactor-evm-tx-Ywkme
Closed

Refactor EVM transaction handling for clarity and maintainability#703
seunlanlege wants to merge 1 commit intomainfrom
claude/refactor-evm-tx-Ywkme

Conversation

@seunlanlege
Copy link
Member

Summary

This PR significantly refactors the EVM transaction handling code to improve readability, reduce duplication, and better organize the codebase. The changes extract common patterns into reusable helper functions and reorganize code into logical sections with clear separation of concerns.

Key Changes

  • Extracted helper functions to eliminate duplication:

    • build_tx_request(): Constructs TransactionRequest with optional gas price
    • gas_with_buffer(): Applies 5% buffer to gas estimates with fallback logic
    • decode_mmr_proof_with_indices(): Decodes MMR proofs and computes k_index/leaf_index pairs
    • extract_state_machine_id(): Extracts numeric state machine ID from Polkadot/Kusama variants
    • build_solidity_proof(): Constructs solidity Proof structs from MMR proofs
    • extract_event_commitments(): Extracts handled-event commitments from receipts
  • Refactored transaction lifecycle management:

    • Split wait_for_success() into focused functions: handle_missing_receipt(), retry_consensus_message(), and cancel_stuck_transaction()
    • Improved error handling and logging clarity
    • Better separation between receipt polling, consensus retries, and stuck transaction cancellation
  • Simplified contract call generation:

    • Extracted resolve_gas_price() to handle gas price resolution with buffer application
    • Created build_post_request_tx() and build_post_response_tx() to reduce duplication in message-to-transaction conversion
    • Removed verbose trace logging in favor of cleaner code structure
  • Reorganized code structure with clear section markers:

    • Helpers section for utility functions
    • Public API section for main entry points
    • Contract call generation section
    • Message submission pipeline section
    • Tests section
  • Improved handle_message_submission():

    • Extracted collect_tx_receipts() to match handled commitments against original messages
    • Simplified logic using functional patterns (filter_map)
  • Enhanced code clarity:

    • Simplified string formatting in log messages
    • Removed redundant comments and trace logging
    • Improved variable naming and control flow
    • Cleaner test code with reduced verbosity

Implementation Details

The refactoring maintains all existing functionality while improving code organization. Helper functions are designed to be composable and reusable across the transaction submission pipeline. The separation of concerns makes the code easier to test, maintain, and extend in the future.

https://claude.ai/code/session_01PdTZdbbn5tYByuC44jkSY5

…ucing duplication

- Extract `build_tx_request` to eliminate 3x duplicated TransactionRequest construction
- Extract `gas_with_buffer` for repeated gas estimation + 5% buffer pattern
- Extract `decode_mmr_proof_with_indices` for duplicated MMR proof decoding
- Extract `build_solidity_proof` and `extract_state_machine_id` for proof construction
- Extract `extract_event_commitments` from inline receipt log parsing
- Break `wait_for_success` into `handle_missing_receipt`, `retry_consensus_message`,
  and `cancel_stuck_transaction`
- Break `generate_contract_calls` message handling into `build_post_request_tx` and
  `build_post_response_tx`
- Extract `resolve_gas_price` from inline gas price logic
- Replace imperative loops in `handle_message_submission` with `collect_tx_receipts`
  using iterators
- Remove excessive trace logging that cluttered the code

https://claude.ai/code/session_01PdTZdbbn5tYByuC44jkSY5
@seunlanlege seunlanlege marked this pull request as draft March 16, 2026 13:17
@seunlanlege seunlanlege deleted the claude/refactor-evm-tx-Ywkme branch March 16, 2026 13:19
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.

2 participants