Skip to content

Add delegation support to ContractFunc execute method and Wallet class#4

Open
devin-ai-integration[bot] wants to merge 5 commits intomainfrom
devin/1754430306-add-sponsored-execution-utility
Open

Add delegation support to ContractFunc execute method and Wallet class#4
devin-ai-integration[bot] wants to merge 5 commits intomainfrom
devin/1754430306-add-sponsored-execution-utility

Conversation

@devin-ai-integration
Copy link

Add delegation support to ContractFunc execute method and Wallet class

Summary

This PR adds utility methods to simplify EIP-7702 sponsored delegation transactions by abstracting the manual sponsor_delegation + sign + send flow into single method calls.

Key Changes:

  1. Enhanced execute() method in ContractFunc to accept optional delegation parameters (delegate_wallet, chain_id, gas)
  2. Added delegate_to_contract() method to BaseWallet for simple delegation without contract data
  3. Updated example to demonstrate both new utility methods

Usage Examples:

# Enhanced execute with delegation
tx_hash = await counter.increment().execute(
    wallet=sponsor_wallet,
    delegate_wallet=delegate_wallet
)

# Simple wallet delegation
tx_hash = await delegate_wallet.delegate_to_contract(
    sponsor_wallet=sponsor_wallet,
    contract_address=counter_address
)

Review & Testing Checklist for Human

  • End-to-end testing: Test both utility methods with real wallets on EIP-7702 compatible network
  • Backward compatibility: Verify existing execute() calls still work without delegation parameters
  • Type safety: Check that Optional parameter handling and overloads work correctly
  • Edge cases: Test delegation with various gas limits, chain IDs, and nonce scenarios
  • Gas estimation: Verify proper gas handling in delegation transactions

Recommended Test Plan:

  1. Run the updated example_eip7702.py with SPONSOR_PRIVATE_KEY set
  2. Test existing contract execution code to ensure no regressions
  3. Test delegation with different wallet types and network configurations
  4. Verify error handling for invalid delegation parameters

Diagram

%%{ init : { "theme" : "default" }}%%
graph TD
    FunctionPy["packages/eth_rpc/src/eth_rpc/<br/>contract/function.py"]:::major-edit
    WalletPy["packages/eth_rpc/src/eth_rpc/<br/>wallet.py"]:::major-edit
    ExamplePy["example_eip7702.py"]:::minor-edit
    DelegationPy["packages/eth_rpc/src/eth_rpc/<br/>delegation.py"]:::context
    
    FunctionPy -->|"uses"| DelegationPy
    WalletPy -->|"uses"| DelegationPy
    ExamplePy -->|"demonstrates"| FunctionPy
    ExamplePy -->|"demonstrates"| WalletPy
    
    subgraph Legend
        L1[Major Edit]:::major-edit
        L2[Minor Edit]:::minor-edit
        L3[Context/No Edit]:::context
    end
    
    classDef major-edit fill:#90EE90
    classDef minor-edit fill:#87CEEB  
    classDef context fill:#FFFFFF
Loading

Notes

  • Both utility methods internally use the existing sponsor_delegation function from delegation.py
  • The enhanced execute() method maintains full backward compatibility
  • Also includes a separate execute_sponsored() method that provides the same functionality with explicit naming
  • All changes follow existing async/sync patterns with proper type overloads
  • Risk: EIP-7702 is cutting-edge functionality - thorough testing recommended

Link to Devin run: https://app.devin.ai/sessions/6277ae0e44924c06b7205c3842b45a38
Requested by: @talos-agent

talos-agent and others added 3 commits August 5, 2025 14:31
* Add EIP-7702 delegation utilities

- Add AuthorizationItem model for delegation authorization lists
- Extend PreparedTransaction and BaseTransaction to support type 4 transactions
- Create delegation utilities for easy sponsorship of delegated transactions
- Add helper functions for creating authorization items and preparing delegation transactions

Implements EIP-7702 account delegation with utilities similar to web3.py patterns
but implemented natively in eth_rpc. Includes create_authorization_item,
prepare_delegation_transaction, and sponsor_delegation functions.

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Improve sponsor_delegation API to use delegate_wallet parameter

- Replace separate delegatee_address and delegatee_private_key parameters with single delegate_wallet parameter
- Makes API cleaner and more consistent with existing eth_rpc wallet patterns
- Updated test to verify the improved wallet-based API works correctly
- All EIP-7702 functionality continues to work as expected

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Create Counter contract example for EIP-7702 delegation workflow

- Rename test_eip7702.py to example_eip7702.py
- Add realistic Counter contract using ProtocolBase pattern with increment method
- Load delegate private key from DELEGATE_PRIVATE_KEY environment variable
- Demonstrate complete EIP-7702 delegation workflow:
  - Sponsor wallet pays gas for transaction
  - Delegate wallet authorizes delegation via EIP-7702
  - Counter contract increment method called on behalf of delegate
- Educational example showing practical usage of sponsor_delegation function
- Clear step-by-step workflow explanation for users learning EIP-7702

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Fix EIP-7702 delegation utilities implementation

- Fix create_authorization_item to authorize setting EOA code to contract
- Fix sponsor_delegation to use correct transaction structure (to=delegate_address)
- Update example workflow to match EIP-7702 specification
- Fix documentation to reflect correct delegation mechanics
- Resolve opcode 0xef error by implementing proper transaction structure
- Add fixed gas values to avoid RPC calls in example demonstration

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Fix sponsor/delegate wallet logic in EIP-7702 example

- Sponsor wallet now loaded from SPONSOR_PRIVATE_KEY environment variable (has funds to pay gas)
- Delegate wallet now randomly created (doesn't need funds, just authorizes code setting)
- Maintains correct EIP-7702 delegation mechanics and transaction structure
- Example demonstrates realistic sponsor/delegate relationship

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Implement network-aware nonce management for EIP-7702 delegation

- Add automatic network-aware nonce lookup using wallet[network].get_nonce()
- Handle sponsor == delegate case with correct nonce coordination
- Automatically detect chain_id from sponsor wallet's network context
- Simplify API by eliminating manual nonce management
- Update example script to demonstrate simplified workflow

Requested by: @talos-agent
Link to Devin run: https://app.devin.ai/sessions/db696e5c71a24f2486898e00991075ba

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Update example_eip7702.py

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…nsaction receipts (empyrealapp#106)

* Add event receipt utility for extracting and decoding events from transaction receipts

- Created EventReceiptUtility class with methods to extract events from receipts and transaction hashes
- Added support for single and multiple event extraction
- Included comprehensive test suite with mocked dependencies
- Added convenience functions for easier usage
- Integrated with existing Event system using Event.match() and Event.process_log()
- Handles edge cases like missing receipts and processing exceptions

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Add uv.lock file

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Fix Black formatting issues in test file

- Break long lines with HexAddress and HexStr constructors
- Improve code readability with proper indentation
- Address lint check failures

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Apply additional Black formatting fixes

- Break long function signatures into multiple lines
- Fix monkeypatch.setattr line length issues
- Ensure all lines comply with Black formatting standards

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Replace mocked tests with real transaction data

- Use real transaction receipts with Transfer and Approval events
- Import TransferEvent and ApprovalEvent from eth_typeshed
- Simplify test structure by removing complex mocking
- Maintain comprehensive test coverage for all utility functions
- Fix Black formatting issues

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Add comprehensive mocked RPC tests for end-to-end testing

- Mock Transaction.get_receipt_by_hash() calls to return constructed receipts
- Test get_events_from_tx_hash and get_single_event_from_tx_hash with mocks
- Add invalid hash handling test with mocked RPC
- Test convenience functions with mocked RPC calls
- Eliminate external API dependencies for complete test isolation

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Fix circular import issues in event_receipt utility

- Use TYPE_CHECKING imports to avoid runtime circular dependencies
- Add string type annotations for Event, EventData, TransactionReceipt
- Move Transaction import inside method to avoid initialization conflicts
- Resolves ImportError during test collection

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Replace eth_typeshed imports with local Event type definitions

- Define TransferEventType and ApprovalEventType locally in test file
- Create TransferEvent and ApprovalEvent instances from local types
- Follows same pattern as other test files to avoid import dependencies
- Resolves ModuleNotFoundError for eth_typeshed package

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Fix Black formatting in event_receipt.py

- Apply Black formatting to resolve lint check failures
- No functional changes, only formatting improvements

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Fix import sorting in test_event_receipt.py

- Apply isort formatting to resolve lint check failures
- Ensure imports are correctly sorted according to project standards

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Remove uv.lock file

- Remove uv.lock file as requested
- Clean up repository structure

Author: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
* Add wait_until_finalized method to TransactionReceipt class

- Adds async method that polls for transaction receipt until finalized
- Configurable sleep_time parameter (default 4.0 seconds)
- Optional timeout parameter to prevent infinite polling
- Raises exception for failed transactions (status == 0)
- Returns finalized receipt when status == 1
- Follows existing async patterns from codebase

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Remove test file, keep only the main implementation

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

* Remove uv.lock file that was accidentally committed

- uv.lock files should not be checked into version control
- Keeps only the intended changes for wait_until_finalized method

Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

- Enhanced execute() method in ContractFunc to accept optional delegate_wallet parameter
- When delegate_wallet is provided, automatically uses sponsored delegation flow
- Added delegate_to_contract() method to BaseWallet for simple delegation without contract data
- Updated example_eip7702.py to demonstrate both new utility methods
- Follows existing async/sync patterns with proper overloads
- Abstracts sponsor_delegation + sign + send flow into single method calls

Authored-by: Talos
Co-Authored-By: talosgma@gmail.com <talosgma@gmail.com>
@devin-ai-integration devin-ai-integration bot force-pushed the devin/1754430306-add-sponsored-execution-utility branch from cd098e4 to 7ea8221 Compare August 5, 2025 23:55
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