How to properly test events with indexed parameters in Foundry? #14281
-
|
Hey everyone, I'm trying to test that my contract emits the correct event with indexed parameters, but I'm confused about how Here's my contract: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Staking {
event Staked(address indexed user, uint256 indexed poolId, uint256 amount, uint256 timestamp);
mapping(address => mapping(uint256 => uint256)) public stakes;
function stake(uint256 poolId) external payable {
require(msg.value > 0, "Must stake > 0");
stakes[msg.sender][poolId] += msg.value;
emit Staked(msg.sender, poolId, msg.value, block.timestamp);
}
}And my test: function testStakeEmitsEvent() public {
vm.deal(alice, 1 ether);
vm.prank(alice);
vm.expectEmit(true, true, false, true);
emit Staked(alice, 1, 1 ether, block.timestamp);
staking.stake{value: 1 ether}(1);
}My questions:
Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
The 4 booleans in In Solidity events, So for your event: The mapping is:
When you set a boolean to Why set one to
|
Beta Was this translation helpful? Give feedback.
The 4 booleans in
vm.expectEmitmap directly to the event's topics and data:In Solidity events,
indexedparameters become topics (topic1, topic2, topic3) and non-indexed parameters go into the data field. Topic0 is always the event signature hash and is checked automatically.So for your event:
The mapping is: