Skip to content

Add safe buffer to Rootstock collective staking gas estimation#697

Merged
kvhnuke merged 1 commit intoenkryptcom:devop/release-v-2-7-0from
ahsan-javaiid:fix/collective-staking-fee
May 29, 2025
Merged

Add safe buffer to Rootstock collective staking gas estimation#697
kvhnuke merged 1 commit intoenkryptcom:devop/release-v-2-7-0from
ahsan-javaiid:fix/collective-staking-fee

Conversation

@ahsan-javaiid
Copy link
Contributor

@ahsan-javaiid ahsan-javaiid commented May 22, 2025

Description

Enkrypt uses web3 estimateGas function to estimate gas for the transactions. estimateGas sometimes/ intermittently returns less gas estimations in case of staking contract calls. Due to this reason contract call goes out of gas. Here is example of such failed transaction which consumed 100% of gas and went out of gas during execution.

Reasons

Due to the nature of staking contract / upgradeable contract / OpenZeppelin Upgradeable Proxy patterns / storage / networks conditions makes estimateGas to reutrn very optimistic estimate which sometimes not enough for the contract execution.
We have also seen some custom gas estimation logic in enkrypt for other networks which involves swap operation and enkrypt is adding safe buffer to estimateGas to prevent the contract call going out of the gas here.

Testing

Using the successful stake tx from history to figure out the safe buffer for stake tx on top of estimateGas call. We tested staking with this estimation approach and would like it to be part of enkrypt team.

Motivation

Users are extensively using enkrypt with collective for staking and facing out of gas issue on stake contract call. This PR adds an safe buffer to gas estimated by web3 to prevent the stake contract call going out of gas.

Summary by CodeRabbit

  • New Features

    • Improved gas estimation for staking transactions on Rootstock (RSK) mainnet and testnet, providing more accurate gas limits based on recent network activity.
  • Bug Fixes

    • Enhanced reliability of staking transaction processing on RSK networks by dynamically adjusting gas limits for legacy transactions.

@coderabbitai
Copy link

coderabbitai bot commented May 22, 2025

"""

Walkthrough

The changes introduce specialized gas estimation logic for staking transactions on Rootstock (RSK) mainnet and testnet. A new configuration and function are added to fetch recent staking transactions and adjust gas limits accordingly. The transaction finalization process is updated to use this logic for legacy staking transactions on RSK networks.

Changes

File(s) Change Summary
packages/extension/src/providers/ethereum/libs/transaction/gas-utils.ts Added collectiveGasConfig for RSK staking, and implemented safeGasForStaking to fetch historical staking transactions and adjust gas estimates. Exported both.
packages/extension/src/providers/ethereum/libs/transaction/index.ts Updated finalizeTransaction to use safeGasForStaking for legacy staking transactions on RSK and RSK Testnet.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Transaction
    participant GasUtils

    User->>Transaction: finalizeTransaction(tx, ...)
    Transaction->>GasUtils: safeGasForStaking(chainID, estimatedGas)
    GasUtils->>ExternalAPI: Fetch recent staking transactions
    GasUtils->>ExternalAPI: Fetch transaction details (for each staking tx)
    GasUtils-->>Transaction: Return adjusted gas limit
    Transaction-->>User: Finalized transaction with adjusted gas
Loading

Suggested reviewers

  • kvhnuke
    """

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2ad1a33 and a3d14cf.

📒 Files selected for processing (2)
  • packages/extension/src/providers/ethereum/libs/transaction/gas-utils.ts (2 hunks)
  • packages/extension/src/providers/ethereum/libs/transaction/index.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/extension/src/providers/ethereum/libs/transaction/index.ts
  • packages/extension/src/providers/ethereum/libs/transaction/gas-utils.ts
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/extension/src/providers/ethereum/libs/transaction/gas-utils.ts (1)

198-242: Smart gas limit adjustment logic with proper fallbacks

The implementation intelligently analyzes recent successful staking transactions to determine appropriate gas limits. I particularly like:

  • Fallback to original estimate if API requests fail
  • Additional 30% buffer when gas usage is high (>80%)
  • Using Math.max() to ensure we never go below the estimated gas

Consider adding error logging in the catch block to help with debugging:

-  } catch {
+  } catch (error) {
+    console.error('Error fetching safe gas for staking:', error);
     return estimatedGas;
   }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f7b86b4 and 2ad1a33.

📒 Files selected for processing (2)
  • packages/extension/src/providers/ethereum/libs/transaction/gas-utils.ts (2 hunks)
  • packages/extension/src/providers/ethereum/libs/transaction/index.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/extension/src/providers/ethereum/libs/transaction/index.ts (2)
packages/utils/src/index.ts (1)
  • numberToHex (46-46)
packages/extension/src/providers/ethereum/libs/transaction/gas-utils.ts (2)
  • collectiveGasConfig (247-247)
  • safeGasForStaking (246-246)
🔇 Additional comments (3)
packages/extension/src/providers/ethereum/libs/transaction/gas-utils.ts (1)

182-195: Well-structured configuration for RSK staking transactions

The configuration is clean and properly organized with all necessary parameters for both RSK mainnet and testnet. The use of dynamic keys with [rsk.chainID] is a good practice.

packages/extension/src/providers/ethereum/libs/transaction/index.ts (2)

17-22: Required imports for RSK staking functionality

The imports are correctly added to support the new gas estimation feature for RSK staking transactions.


112-118: Verify API service reliability and performance impact

Making API calls during transaction finalization could affect wallet responsiveness if the external APIs are slow or temporarily unavailable.

While the implementation has proper fallbacks when APIs fail, it's important to verify:

  1. The reliability and response time of the Blockscout APIs being used
  2. The impact on transaction preparation time for users

Consider implementing caching of recent staking transaction gas data to minimize API calls during high usage periods.

@ahsan-javaiid ahsan-javaiid force-pushed the fix/collective-staking-fee branch from 2ad1a33 to a3d14cf Compare May 27, 2025 05:40
@ahsan-javaiid
Copy link
Contributor Author

Hi @kvhnuke
Would be great whenever you get a chance to review this PR for next release. Thanks!

@ahsan-javaiid ahsan-javaiid changed the base branch from main to develop May 27, 2025 17:07
@kvhnuke kvhnuke changed the base branch from develop to devop/release-v-2-7-0 May 29, 2025 18:21
@kvhnuke kvhnuke merged commit 13807d4 into enkryptcom:devop/release-v-2-7-0 May 29, 2025
2 of 3 checks passed
@coderabbitai coderabbitai bot mentioned this pull request May 29, 2025
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