Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions contracts/L1/src/MerkleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ contract MerkleManager {
* @dev Appends a new deposit commitment to the tree.
* Updates peaks, root, and mappings. Emits DepositHashAppended.
* @param commitmentHash The hash of the deposit commitment to append.
* @return newRoot The updated MMR root hash after the commitment is appended.
* @return newLeavesCount The total number of deposit leaves after this append operation.
* @return elementCount The total number of elements in the MMR after this append operation (including non-leaf nodes).
*/
function appendDepositHash(bytes32 commitmentHash) internal {
function appendDepositHash(bytes32 commitmentHash)
internal
returns (bytes32 newRoot, uint256 newLeavesCount, uint256 elementCount)
{
// Append element to the tree and retrieve updated peaks and root
(uint256 nextElementsCount, bytes32 nextRootHash, bytes32[] memory nextPeaks) =
StatelessMmr.appendWithPeaksRetrieval(commitmentHash, lastPeaks, lastElementsCount, lastRoot);
Expand All @@ -59,9 +65,9 @@ contract MerkleManager {
uint256 mmrLeafIndex = leafCountToMmrIndex(leavesCount);
commitmentHashToIndex[commitmentHash] = mmrLeafIndex + 1; // +1 to make it 1-based index
leavesCount += 1;

// Emit event for the appended deposit
emit DepositHashAppended(leavesCount, commitmentHash, lastRoot, lastElementsCount);
newRoot = lastRoot;
newLeavesCount = leavesCount;
elementCount = lastElementsCount;
}

/**
Expand Down
21 changes: 16 additions & 5 deletions contracts/L1/src/ZeroXBridgeL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ contract ZeroXBridgeL1 is Ownable, Starknet, MerkleManager {
event WhitelistEvent(address indexed token);
event DewhitelistEvent(address indexed token);
event DepositEvent(
address indexed token, AssetType assetType, uint256 amount, address indexed user, uint256 commitmentHash
uint256 depositId,
address indexed token,
AssetType assetType,
uint256 amount,
address indexed user,
uint256 nonce,
uint256 commitmentHash,
bytes32 newRoot,
uint256 elementCount
);
event TokenRegistered(bytes32 indexed assetKey, AssetType assetType, address tokenAddress);
event UserRegistered(address indexed user, uint256 starknetPubKey);
Expand Down Expand Up @@ -182,7 +190,7 @@ contract ZeroXBridgeL1 is Ownable, Starknet, MerkleManager {
* @param user The address that will receive the bridged tokens on L2
* @return commitmentHash Returns the generated commitment hash for verification on L2
*/
function depositAsset(AssetType assetType, address tokenAddress, uint256 amount, address user)
function depositAsset(uint256 depositId, AssetType assetType, address tokenAddress, uint256 amount, address user)
external
payable
returns (uint256)
Expand Down Expand Up @@ -228,13 +236,16 @@ contract ZeroXBridgeL1 is Ownable, Starknet, MerkleManager {
nextDepositNonce[user] = nonce + 1;

// Generate commitment hash
bytes32 commitmentHash = keccak256(abi.encodePacked(userRecord[user], usdVal, nonce, block.timestamp));
bytes32 commitmentHash =
keccak256(abi.encodePacked(depositId, userRecord[user], usdVal, nonce, block.timestamp));

// Append to Merkle tree
appendDepositHash(commitmentHash);
(bytes32 newRoot, uint256 count, uint256 elementCount) = appendDepositHash(commitmentHash);

// Emit deposit event
emit DepositEvent(tokenAddress, assetType, usdVal, user, uint256(commitmentHash));
emit DepositEvent(
depositId, tokenAddress, assetType, usdVal, user, nonce, uint256(commitmentHash), newRoot, elementCount
);

return uint256(commitmentHash);
}
Expand Down
Loading