Skip to content

Commit 442e140

Browse files
authored
Merge pull request #108 from truthixify/feat/single-deposit-event-with-id
Feat: include depositId in depositAsset and emit single enriched DepositEvent
2 parents 67c335a + 9cc3671 commit 442e140

3 files changed

Lines changed: 149 additions & 65 deletions

File tree

contracts/L1/src/MerkleManager.sol

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ contract MerkleManager {
4242
* @dev Appends a new deposit commitment to the tree.
4343
* Updates peaks, root, and mappings. Emits DepositHashAppended.
4444
* @param commitmentHash The hash of the deposit commitment to append.
45+
* @return newRoot The updated MMR root hash after the commitment is appended.
46+
* @return newLeavesCount The total number of deposit leaves after this append operation.
47+
* @return elementCount The total number of elements in the MMR after this append operation (including non-leaf nodes).
4548
*/
46-
function appendDepositHash(bytes32 commitmentHash) internal {
49+
function appendDepositHash(bytes32 commitmentHash)
50+
internal
51+
returns (bytes32 newRoot, uint256 newLeavesCount, uint256 elementCount)
52+
{
4753
// Append element to the tree and retrieve updated peaks and root
4854
(uint256 nextElementsCount, bytes32 nextRootHash, bytes32[] memory nextPeaks) =
4955
StatelessMmr.appendWithPeaksRetrieval(commitmentHash, lastPeaks, lastElementsCount, lastRoot);
@@ -59,9 +65,9 @@ contract MerkleManager {
5965
uint256 mmrLeafIndex = leafCountToMmrIndex(leavesCount);
6066
commitmentHashToIndex[commitmentHash] = mmrLeafIndex + 1; // +1 to make it 1-based index
6167
leavesCount += 1;
62-
63-
// Emit event for the appended deposit
64-
emit DepositHashAppended(leavesCount, commitmentHash, lastRoot, lastElementsCount);
68+
newRoot = lastRoot;
69+
newLeavesCount = leavesCount;
70+
elementCount = lastElementsCount;
6571
}
6672

6773
/**

contracts/L1/src/ZeroXBridgeL1.sol

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ contract ZeroXBridgeL1 is Ownable, Starknet, MerkleManager {
7272
event WhitelistEvent(address indexed token);
7373
event DewhitelistEvent(address indexed token);
7474
event DepositEvent(
75-
address indexed token, AssetType assetType, uint256 amount, address indexed user, uint256 commitmentHash
75+
uint256 depositId,
76+
address indexed token,
77+
AssetType assetType,
78+
uint256 amount,
79+
address indexed user,
80+
uint256 nonce,
81+
uint256 commitmentHash,
82+
bytes32 newRoot,
83+
uint256 elementCount
7684
);
7785
event TokenRegistered(bytes32 indexed assetKey, AssetType assetType, address tokenAddress);
7886
event UserRegistered(address indexed user, uint256 starknetPubKey);
@@ -182,7 +190,7 @@ contract ZeroXBridgeL1 is Ownable, Starknet, MerkleManager {
182190
* @param user The address that will receive the bridged tokens on L2
183191
* @return commitmentHash Returns the generated commitment hash for verification on L2
184192
*/
185-
function depositAsset(AssetType assetType, address tokenAddress, uint256 amount, address user)
193+
function depositAsset(uint256 depositId, AssetType assetType, address tokenAddress, uint256 amount, address user)
186194
external
187195
payable
188196
returns (uint256)
@@ -228,13 +236,16 @@ contract ZeroXBridgeL1 is Ownable, Starknet, MerkleManager {
228236
nextDepositNonce[user] = nonce + 1;
229237

230238
// Generate commitment hash
231-
bytes32 commitmentHash = keccak256(abi.encodePacked(userRecord[user], usdVal, nonce, block.timestamp));
239+
bytes32 commitmentHash =
240+
keccak256(abi.encodePacked(depositId, userRecord[user], usdVal, nonce, block.timestamp));
232241

233242
// Append to Merkle tree
234-
appendDepositHash(commitmentHash);
243+
(bytes32 newRoot, uint256 count, uint256 elementCount) = appendDepositHash(commitmentHash);
235244

236245
// Emit deposit event
237-
emit DepositEvent(tokenAddress, assetType, usdVal, user, uint256(commitmentHash));
246+
emit DepositEvent(
247+
depositId, tokenAddress, assetType, usdVal, user, nonce, uint256(commitmentHash), newRoot, elementCount
248+
);
238249

239250
return uint256(commitmentHash);
240251
}

0 commit comments

Comments
 (0)