Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Escrow): cap the amount #88

Merged
merged 2 commits into from
Dec 18, 2024
Merged

feat(Escrow): cap the amount #88

merged 2 commits into from
Dec 18, 2024

Conversation

unknownunknown1
Copy link
Contributor

@unknownunknown1 unknownunknown1 commented Dec 12, 2024

Initially planned to create a separate contract for this feature, but since it's universal escrow contract it might be better to put it directly there as an option


PR-Codex overview

This PR introduces a new error handling mechanism and a cap on the amount of tokens that can be used in EscrowUniversal transactions. It adds functionality to manage these caps and ensures that transactions do not exceed specified limits.

Detailed summary

  • Added AmountExceedsCap() error in IEscrow.sol.
  • Introduced amountCaps mapping in EscrowUniversal to set caps for token amounts.
  • Created shouldNotExceedCap modifier to enforce the cap on token amounts during transactions.
  • Added changeAmountCap function for updating the cap on specific tokens.
  • Applied the shouldNotExceedCap modifier to payable and token transfer functions to enforce limits.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Summary by CodeRabbit

  • New Features

    • Introduced a new mapping to set maximum allowable amounts for ERC20 tokens in escrow transactions.
    • Added a modifier to validate transaction amounts against defined caps.
    • Implemented a function to allow governance to adjust amount caps for specific tokens.
    • Enhanced error handling with a new error declaration for exceeding transaction limits.
  • Bug Fixes

    • Improved transaction validation to prevent exceeding specified limits.

Copy link
Contributor

coderabbitai bot commented Dec 12, 2024

Walkthrough

The pull request introduces enhancements to the EscrowUniversal contract by adding functionality to manage transaction limits for ERC20 tokens. A new mapping, amountCaps, stores maximum allowable amounts, and a modifier, shouldNotExceedCap, validates transactions against these caps. Additionally, a function changeAmountCap allows the governor to set or update these limits. The IEscrow interface is updated with a new error declaration, AmountExceedsCap, to improve error handling when transaction amounts exceed defined limits.

Changes

File Change Summary
contracts/src/EscrowUniversal.sol - Added mapping: amountCaps for max amounts of ERC20 tokens.
- Added modifier: shouldNotExceedCap to validate transaction amounts.
- Added function: changeAmountCap for updating caps by the governor.
contracts/src/interfaces/IEscrow.sol - Added error: AmountExceedsCap for handling exceeded transaction limits.

Poem

In the garden where tokens play,
Caps are set to save the day.
With each transaction, checks are made,
To keep our escrow plans well-laid.
A hop, a skip, we dance with glee,
For limits help us all be free! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • 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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @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 or @coderabbitai title 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

netlify bot commented Dec 12, 2024

Deploy Preview for kleros-escrow-v2 ready!

Name Link
🔨 Latest commit ba18084
🔍 Latest deploy log https://app.netlify.com/sites/kleros-escrow-v2/deploys/675b36844cfaaf0008157db0
😎 Deploy Preview https://deploy-preview-88--kleros-escrow-v2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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: 1

🧹 Outside diff range and nitpick comments (1)
contracts/src/EscrowUniversal.sol (1)

40-40: Consider adding an event for cap changes.

While the mapping implementation is correct, emitting an event when caps are modified would improve transparency and make it easier for off-chain services to track changes.

 // Add this event declaration
+event AmountCapUpdated(IERC20 indexed token, uint256 oldCap, uint256 newCap);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d7aeca8 and ada038d.

📒 Files selected for processing (2)
  • contracts/src/EscrowUniversal.sol (5 hunks)
  • contracts/src/interfaces/IEscrow.sol (1 hunks)
🔇 Additional comments (3)
contracts/src/interfaces/IEscrow.sol (1)

183-183: LGTM! Error declaration follows interface conventions.

The new error AmountExceedsCap is appropriately named and placed in the errors section.

contracts/src/EscrowUniversal.sol (2)

139-139: LGTM! Consistent application of cap validation.

The ShouldNotExceedCap modifier is correctly applied to both transaction creation functions, ensuring consistent cap validation for both native and ERC20 transactions.

Also applies to: 166-166


51-54: Verify NATIVE constant implementation.

The modifier looks correct, but ensure that the NATIVE constant is properly defined and initialized, as it's used in createNativeTransaction but not visible in the provided code.

✅ Verification successful

Let me check the actual implementation of the NATIVE constant.


Let me verify how this NATIVE constant is used in the EscrowUniversal contract.


NATIVE constant is properly implemented and used correctly.

The NATIVE constant is correctly defined in Types.sol as IERC20 constant NATIVE = IERC20(address(0)) and is properly used throughout the EscrowUniversal contract for handling native currency transactions. The modifier ShouldNotExceedCap correctly handles both native and ERC20 token cases.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for NATIVE constant definition
rg -l "NATIVE\s*=|constant\s+NATIVE|immutable\s+NATIVE"

Length of output: 157


Script:

#!/bin/bash
# Check the content of Types.sol for NATIVE constant definition
cat contracts/src/interfaces/Types.sol

Length of output: 1390


Script:

#!/bin/bash
# Search for NATIVE usage in EscrowUniversal.sol
cat contracts/src/EscrowUniversal.sol

Length of output: 25166

contracts/src/EscrowUniversal.sol Show resolved Hide resolved
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 (1)
contracts/src/EscrowUniversal.sol (1)

40-40: Enhance mapping documentation for clarity.

Consider adding more detailed documentation to explain the cap behavior, especially for edge cases like zero values.

-    mapping(IERC20 => uint256) public amountCaps; // Caps the amount of the respective token for the Escrow transaction.
+    /// @notice Maps tokens to their maximum allowed transaction amounts
+    /// @dev A cap of 0 means no limit is enforced for that token
+    /// @return The maximum amount allowed for a given token in a single transaction
+    mapping(IERC20 => uint256) public amountCaps;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ada038d and ba18084.

📒 Files selected for processing (1)
  • contracts/src/EscrowUniversal.sol (5 hunks)
🔇 Additional comments (3)
contracts/src/EscrowUniversal.sol (3)

51-54: LGTM! Clean and efficient implementation.

The modifier correctly implements the capping logic with:

  • Proper handling of uncapped tokens (zero cap value)
  • Gas-efficient check
  • Appropriate error usage

139-139: LGTM! Consistent implementation of caps across transaction types.

The cap enforcement is properly integrated into both native and ERC20 transaction creation functions, maintaining consistency in the capping behavior.

Also applies to: 166-166


126-128: ⚠️ Potential issue

Add input validation and event emission for cap changes.

The governance function should include basic safety checks and emit events for transparency.

As mentioned in the previous review, please add:

  1. Input validation for the token address
  2. Event emission for cap changes
+    event AmountCapUpdated(IERC20 indexed token, uint256 oldCap, uint256 newCap);
+
     function changeAmountCap(IERC20 _token, uint256 _amountCap) external onlyByGovernor {
+        if (address(_token) == address(0)) revert InvalidToken();
+        uint256 oldCap = amountCaps[_token];
         amountCaps[_token] = _amountCap;
+        emit AmountCapUpdated(_token, oldCap, _amountCap);
     }

@jaybuidl jaybuidl merged commit a65d08f into master Dec 18, 2024
7 checks passed
@jaybuidl jaybuidl deleted the feat/value-cap branch December 18, 2024 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants