Skip to content

feat: add native gas pre-flight check and update unit tests#243

Open
DanFrolov wants to merge 1 commit intoOpenGradient:mainfrom
DanFrolov:feat/gas-preflight-check
Open

feat: add native gas pre-flight check and update unit tests#243
DanFrolov wants to merge 1 commit intoOpenGradient:mainfrom
DanFrolov:feat/gas-preflight-check

Conversation

@DanFrolov
Copy link
Copy Markdown

Description

This PR introduces a "fail-fast" gas pre-flight check within the ensure_opg_approval process. Currently, if a user has sufficient OPG tokens but lacks native ETH (Base Sepolia) for gas, the transaction fails late with a generic RPC error from the node.

This change ensures that we validate the user's native balance before building or signing the transaction, providing a much clearer error message and saving unnecessary computation.

Key Changes

  • Gas Estimation Logic: The process now estimates gas for the approve call and applies a 20% limit buffer to ensure the transaction doesn't run out of gas.
  • Pre-flight Balance Validation: Added a check to compare the wallet's native_balance against the total_cost (gas limit * gas price), including a 10% safety buffer to account for potential gas price volatility.
  • Enhanced Error Handling: If the balance is insufficient, it raises a RuntimeError that includes:
    • The specific Chain ID.
    • The required ETH vs. available ETH (formatted via Web3.from_wei).
    • A direct instruction to fund the wallet via the Base Sepolia faucet.
  • Unit Test Modernization: Updated tests/opg_token_test.py to:
    • Include mock ETH balances in existing tests so they remain compatible.
    • Use realistic transaction dictionaries in build_transaction mocks to satisfy eth-account signing requirements.

Testing Performed

  • Mock Verification: Verified that a RuntimeError is raised when the mock balance is 0.
  • Success Verification: Verified that the transaction proceeds to signing and submission when the balance is sufficient.
  • Regression Check: Ran the full suite of 11 tests to ensure no breakage in OPG balance capping or amount conversion logic.
  • Result: 11 passed, 3 warnings in 3.42s.

@kylexqian
Copy link
Copy Markdown
Collaborator

We'll be using OPG as the gas token soon -- so this may not be useful

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