Skip to content

docs: adr for gas estimation of protocol calls#43

Open
hpsing wants to merge 2 commits intomainfrom
sigh/adr-gas-estimation
Open

docs: adr for gas estimation of protocol calls#43
hpsing wants to merge 2 commits intomainfrom
sigh/adr-gas-estimation

Conversation

@hpsing
Copy link
Contributor

@hpsing hpsing commented Oct 24, 2024

  • ADR fpr protocol calls, chain specific to determine the gas estimation

Summary by CodeRabbit

  • New Features

    • Introduced gas estimation functionality for DeFi protocols, allowing users to predict transaction costs and avoid out-of-gas errors.
    • Added a new EstimateGas method in the Protocol Registry for enhanced transaction management.
  • Documentation

    • Updated documentation to include a detailed section on gas estimation processes and usage examples.

@hpsing hpsing requested review from adelowo and blewater October 24, 2024 06:06
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 24, 2024

Walkthrough

The changes introduce a new section in the documentation regarding gas estimation for a DeFi protocol registry. This includes the definition of a new method, EstimateGas, which generates calldata and estimates the gas required for transactions. The ProtocolRegistry interface is updated to incorporate this method, allowing protocols to manage their transaction costs more effectively.

Changes

File Change Summary
docs/03_gas_calculation.md Added a new section on gas estimation, introduced EstimateGas method in ProtocolRegistry, updated method signature.

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Contributor

@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: 4

🧹 Outside diff range and nitpick comments (2)
docs/03_gas_calculation.md (2)

1-21: Consider enhancing documentation with additional sections

While the current documentation provides a good foundation, consider adding sections about:

  • Error handling expectations and common error scenarios
  • Gas estimation limitations (e.g., how estimates might differ from actual gas used)
  • Chain-specific considerations (e.g., different gas calculation methods per chain)
🧰 Tools
🪛 LanguageTool

[uncategorized] ~17-~17: Loose punctuation mark.
Context: ...timate the gas. ### Parameters * ctx: A context object for managing timeouts ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~18-~18: Loose punctuation mark.
Context: ... timeouts and cancellations. * chainID: The blockchain ID where the transaction...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~19-~19: Loose punctuation mark.
Context: ...e.g., Ethereum Mainnet, BSC). * action: The specific operation, such as staking...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~20-~20: Loose punctuation mark.
Context: ... such as staking or swapping. * params: The transaction parameters, including a...

(UNLIKELY_OPENING_PUNCTUATION)


106-106: Consider adding testing strategies section

The document would benefit from a section describing:

  • Unit testing strategies for gas estimation
  • Integration testing approaches
  • Mock client setup for testing
  • Test cases for different chain scenarios

Would you like me to help draft this testing section?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 83a7d99 and 1dfd7ea.

📒 Files selected for processing (1)
  • docs/03_gas_calculation.md (1 hunks)
🧰 Additional context used
🪛 LanguageTool
docs/03_gas_calculation.md

[uncategorized] ~17-~17: Loose punctuation mark.
Context: ...timate the gas. ### Parameters * ctx: A context object for managing timeouts ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~18-~18: Loose punctuation mark.
Context: ... timeouts and cancellations. * chainID: The blockchain ID where the transaction...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~19-~19: Loose punctuation mark.
Context: ...e.g., Ethereum Mainnet, BSC). * action: The specific operation, such as staking...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~20-~20: Loose punctuation mark.
Context: ... such as staking or swapping. * params: The transaction parameters, including a...

(UNLIKELY_OPENING_PUNCTUATION)

Copy link
Contributor

@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: 0

🧹 Outside diff range and nitpick comments (3)
docs/03_gas_calculation.md (3)

11-20: Enhance method documentation with additional details.

The documentation would benefit from including:

  1. Return value descriptions (gasEstimate, calldata, error)
  2. Common error scenarios and their handling
  3. Chain-specific considerations for gas estimation

Add the following section after the Parameters section:

### Return Values

* `gasEstimate`: Estimated gas units required for the transaction (*big.Int)
* `calldata`: Generated transaction data as a hex string
* `error`: Error details, if any

### Chain-Specific Considerations

Gas estimation may vary across different chains due to:
* Different gas pricing models
* Chain-specific opcodes and their costs
* Network congestion patterns

### Error Scenarios

* Invalid chain ID
* Contract not deployed on specified chain
* Insufficient balance for estimation
* RPC node timeout/errors
🧰 Tools
🪛 LanguageTool

[uncategorized] ~17-~17: Loose punctuation mark.
Context: ...timate the gas. ### Parameters * ctx: A context object for managing timeouts ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~18-~18: Loose punctuation mark.
Context: ... timeouts and cancellations. * chainID: The blockchain ID where the transaction...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~19-~19: Loose punctuation mark.
Context: ...e.g., Ethereum Mainnet, BSC). * action: The specific operation, such as staking...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~20-~20: Loose punctuation mark.
Context: ... such as staking or swapping. * params: The transaction parameters, including a...

(UNLIKELY_OPENING_PUNCTUATION)


89-111: Enhance usage example with practical next steps.

The example would be more complete by showing how to use the estimated gas and calldata in a transaction.

Add the following code to demonstrate transaction submission:

// Create transaction with estimated gas
tx := types.NewTransaction(
    nonce,                // get from client.PendingNonceAt()
    params.ToAddress,
    params.AmountIn,
    gasEstimate.Uint64(),
    gasPrice,            // get from client.SuggestGasPrice(ctx)
    common.FromHex(calldata),
)

// Sign and send transaction
signedTx, err := wallet.SignTx(tx, chainID)
if err != nil {
    return fmt.Errorf("failed to sign tx: %v", err)
}

err = client.SendTransaction(ctx, signedTx)
if err != nil {
    return fmt.Errorf("failed to send tx: %v", err)
}

fmt.Printf("Transaction sent: %s", signedTx.Hash().Hex())

1-113: Consider adding architectural recommendations for production deployment.

The ADR would benefit from addressing these architectural considerations:

  1. Caching strategy for gas estimates to reduce RPC calls
  2. Fallback RPC providers for reliability
  3. Monitoring and alerting for gas estimation failures
  4. Rate limiting to prevent DoS attacks

Consider adding a new section:

## Production Considerations

### Caching Strategy
- Cache gas estimates with TTL based on chain's block time
- Invalidate cache on significant price movements
- Consider using Redis or similar for distributed caching

### RPC Provider Management
- Implement round-robin or weighted load balancing across multiple RPC providers
- Add circuit breakers for failing providers
- Monitor RPC response times and error rates

### Monitoring
- Track gas estimation accuracy vs actual gas used
- Alert on estimation failures or significant deviations
- Monitor cache hit rates and RPC costs

### Rate Limiting
- Implement per-user and global rate limits
- Consider using token bucket algorithm
- Add retry mechanisms with exponential backoff
🧰 Tools
🪛 LanguageTool

[uncategorized] ~17-~17: Loose punctuation mark.
Context: ...timate the gas. ### Parameters * ctx: A context object for managing timeouts ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~18-~18: Loose punctuation mark.
Context: ... timeouts and cancellations. * chainID: The blockchain ID where the transaction...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~19-~19: Loose punctuation mark.
Context: ...e.g., Ethereum Mainnet, BSC). * action: The specific operation, such as staking...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~20-~20: Loose punctuation mark.
Context: ... such as staking or swapping. * params: The transaction parameters, including a...

(UNLIKELY_OPENING_PUNCTUATION)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1dfd7ea and 661e9ba.

📒 Files selected for processing (1)
  • docs/03_gas_calculation.md (1 hunks)
🧰 Additional context used
🪛 LanguageTool
docs/03_gas_calculation.md

[uncategorized] ~17-~17: Loose punctuation mark.
Context: ...timate the gas. ### Parameters * ctx: A context object for managing timeouts ...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~18-~18: Loose punctuation mark.
Context: ... timeouts and cancellations. * chainID: The blockchain ID where the transaction...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~19-~19: Loose punctuation mark.
Context: ...e.g., Ethereum Mainnet, BSC). * action: The specific operation, such as staking...

(UNLIKELY_OPENING_PUNCTUATION)


[uncategorized] ~20-~20: Loose punctuation mark.
Context: ... such as staking or swapping. * params: The transaction parameters, including a...

(UNLIKELY_OPENING_PUNCTUATION)

Copy link
Contributor

@blewater blewater left a comment

Choose a reason for hiding this comment

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

IMO, this proposal's focus on protocol-level essentially Eip4337 unichain callGasLimit estimation, offers little value to our users who need a complete gas estimate of their Intent and adds yet another requirement when adding a new protocol.

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