Skip to content

Comments

Feat: include depositId in depositAsset and emit single enriched DepositEvent#108

Merged
JoE11-y merged 1 commit intoExplore-Beyond-Innovations:mainfrom
truthixify:feat/single-deposit-event-with-id
Aug 3, 2025
Merged

Feat: include depositId in depositAsset and emit single enriched DepositEvent#108
JoE11-y merged 1 commit intoExplore-Beyond-Innovations:mainfrom
truthixify:feat/single-deposit-event-with-id

Conversation

@truthixify
Copy link
Contributor

@truthixify truthixify commented Jul 28, 2025

Summary

This PR refactors the deposit flow to emit a single DepositEvent with all necessary data, simplifying off-chain indexing.

Changes

  • Updated depositAsset to accept a depositId generated off-chain.
  • Included depositId in the commitmentHash.
  • Modified appendDepositHash to return (newRoot, leavesCount, elementCount).
  • Removed the DepositHashAppended event emission.
  • Emitted a single enriched DepositEvent containing deposit ID, token, asset type, USD value, user, nonce, commitment hash, Merkle root, and count.
  • Updated relevant unit and integration tests.

Verification

  • All tests pass
image

Closes

Closes #107

Summary by CodeRabbit

  • New Features
    • Deposit events now include deposit ID, nonce, new Merkle root, and element count for improved deposit tracking and transparency.
    • The deposit function requires a deposit ID and returns enhanced Merkle tree state information.
  • Bug Fixes
    • Tests updated to verify correct event emission and Merkle tree state after deposits.
  • Tests
    • Expanded test coverage to validate new deposit parameters and event fields, ensuring accurate Merkle root and element count tracking.

@coderabbitai
Copy link

coderabbitai bot commented Jul 28, 2025

Walkthrough

The changes update the deposit flow to emit a single enriched DepositEvent containing all relevant Merkle and deposit metadata, removing the previous two-event system. The depositAsset function now requires a depositId and includes it in the commitment hash. The Merkle append logic returns updated state, and tests are refactored to validate the new event structure and parameters.

Changes

Cohort / File(s) Change Summary
MerkleManager appendDepositHash refactor
contracts/L1/src/MerkleManager.sol
Modified appendDepositHash to return (newRoot, newLeavesCount, elementCount), removed DepositHashAppended emission, and updated state assignment logic.
ZeroXBridgeL1 deposit flow and event enrichment
contracts/L1/src/ZeroXBridgeL1.sol
Updated depositAsset to accept depositId, include it in the commitment hash, call the new appendDepositHash signature, and emit a single enriched DepositEvent with Merkle state and deposit metadata.
Test updates for deposit interface and event
contracts/L1/test/ZeroXBridgeL1.t.sol
Updated all deposit tests to use depositId, expect the new event structure, and verify MMR state. Added a helper to predict expected MMR values after deposit. Adjusted error and signature tests to match new logic.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ZeroXBridgeL1
    participant MerkleManager
    participant StatelessMmr

    User->>ZeroXBridgeL1: depositAsset(depositId, assetType, token, amount, user)
    ZeroXBridgeL1->>ZeroXBridgeL1: Compute commitmentHash (incl. depositId)
    ZeroXBridgeL1->>MerkleManager: appendDepositHash(commitmentHash)
    MerkleManager->>StatelessMmr: appendWithPeaksRetrieval(commitmentHash, lastPeaks, lastElementsCount)
    StatelessMmr-->>MerkleManager: (newPeaks, newElementsCount, newRoot)
    MerkleManager-->>ZeroXBridgeL1: (newRoot, newLeavesCount, elementCount)
    ZeroXBridgeL1-->>User: emit DepositEvent(depositId, ..., newRoot, elementCount)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Extend depositAsset to accept depositId and include in commitment hash (#107)
Modify appendDepositHash to return Merkle metadata and remove DepositHashAppended emission (#107)
Emit a single enriched DepositEvent with all required fields (#107)
Update tests to match new interface and event structure (#107)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Possibly related issues

Possibly related PRs

Suggested reviewers

  • JoE11-y
  • Ugo-X

Poem

In the meadow of Merkle, a rabbit hops with glee,
One event to rule deposits, so simple now, you see!
With roots and IDs, each leaf shines bright,
The bridge is enriched, the hashes just right.
🐇✨
Onward we leap, with a single event in sight!

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 generate unit tests to generate unit tests for 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.

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

@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 comments (3)
contracts/L1/src/MerkleManager.sol (2)

39-39: Remove unused DepositHashAppended event definition

According to the PR objectives, the DepositHashAppended event should be completely removed as it's replaced by the enriched DepositEvent in ZeroXBridgeL1. This event definition is no longer used in appendDepositHash and creates inconsistency.

-    event DepositHashAppended(uint256 index, bytes32 commitmentHash, bytes32 rootHash, uint256 elementsCount);

78-106: Align multiAppendDepositHash with the new append pattern

multiAppendDepositHash still emits DepositHashAppended and doesn’t return anything, while appendDepositHash now returns (bytes32 newRoot, uint256 newLeavesCount, uint256 elementCount) and no longer emits events. To avoid this inconsistency, update or remove multiAppendDepositHash as follows:

• In contracts/L1/src/MerkleManager.sol:
– Change signature to
function multiAppendDepositHash(bytes32[] memory commitmentHashes) internal returns (bytes32 newRoot, uint256 newLeavesCount, uint256 elementCount)
– Remove the emit DepositHashAppended(…) call inside the loop
– Accumulate and return the final nextRoot, leavesCount, and nextElementsCount after the loop

• In contracts/L1/test/mocks/MerkleManagerMock.sol:
– Adjust multiAppendDepositHashPublic to capture and return the new values from the internal call

• In contracts/L1/test/MerkleManager.t.sol:
– Update tests to consume the returned (newRoot, newLeavesCount, elementCount) instead of relying on emitted events

This ensures batch‐append behaves identically to the single‐append API and defers event emission to ZeroXBridgeL1.

contracts/L1/src/ZeroXBridgeL1.sol (1)

193-251: Add user registration validation

The function uses userRecord[user] in the commitment hash calculation without verifying the user is registered. If an unregistered user deposits, their starknet public key will be 0, making funds unrecoverable on L2.

Add registration check at the beginning of the function:

     function depositAsset(uint256 depositId, AssetType assetType, address tokenAddress, uint256 amount, address user)
         external
         payable
         returns (uint256)
     {
         require(amount > 0, "ZeroXBridge: Amount must be greater than zero");
         require(user != address(0), "ZeroXBridge: Invalid user address");
+        require(userRecord[user] != 0, "ZeroXBridge: User not registered");

         TokenAssetData memory tokenData = getTokenData(assetType, tokenAddress);
🧹 Nitpick comments (2)
contracts/L1/src/ZeroXBridgeL1.sol (1)

243-243: Remove unused variable count

The count variable is assigned from appendDepositHash but never used. Consider removing it to improve code clarity.

-        (bytes32 newRoot, uint256 count, uint256 elementCount) = appendDepositHash(commitmentHash);
+        (bytes32 newRoot, , uint256 elementCount) = appendDepositHash(commitmentHash);
contracts/L1/test/ZeroXBridgeL1.t.sol (1)

703-725: Use unique depositIds in tests

The test uses depositId = 1 for both ETH deposit (line 703) and DAI deposit (line 713). If depositIds are meant to be unique identifiers, this test should use different values to better reflect production behavior.

         // 2. Deposit ETH
         vm.prank(user);
         uint256 depositId = 1;

         bridge.depositAsset{value: depositEth}(depositId, ZeroXBridgeL1.AssetType.ETH, address(0), depositEth, user);

         // Expect TokenReserveUpdated for DAI
         vm.expectEmit(true, false, false, true);
         emit TokenReserveUpdated(address(dai), depositDai);

         // 3. Deposit DAI
         vm.prank(user);
-        uint256 depositId1 = 1;
+        uint256 depositId1 = 2;

         bridge.depositAsset(depositId1, ZeroXBridgeL1.AssetType.ERC20, address(dai), depositDai, user);

         // Expect TokenReserveUpdated for USDC
         vm.expectEmit(true, false, false, true);
         emit TokenReserveUpdated(address(usdc), depositUsdc);

         // 4. Deposit USDC
         vm.prank(user);
-        uint256 depositId2 = 2;
+        uint256 depositId2 = 3;
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 67c335a and 9cc3671.

📒 Files selected for processing (3)
  • contracts/L1/src/MerkleManager.sol (2 hunks)
  • contracts/L1/src/ZeroXBridgeL1.sol (3 hunks)
  • contracts/L1/test/ZeroXBridgeL1.t.sol (16 hunks)

Copy link
Contributor

@JoE11-y JoE11-y left a comment

Choose a reason for hiding this comment

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

Great work

@JoE11-y JoE11-y merged commit 442e140 into Explore-Beyond-Innovations:main Aug 3, 2025
3 checks passed
@truthixify
Copy link
Contributor Author

Great work

Thanks, let’s do more of this.

@coderabbitai coderabbitai bot mentioned this pull request Aug 21, 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.

Consolidate deposit events into a single enriched DepositEvent

2 participants