-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement log cheatcodes (#197)
- Loading branch information
Showing
2 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import {Test, console2 as console} from "../../lib/forge-std/src/Test.sol"; | ||
import {Constants} from "./Constants.sol"; | ||
|
||
struct Log { | ||
bytes32[] topics; | ||
bytes data; | ||
address emitter; | ||
} | ||
|
||
contract LogsTest is Test { | ||
event LogTopic1(uint256 indexed topic1, bytes data); | ||
|
||
function testRecordAndGetLogs() public { | ||
bytes memory testData1 = "test"; | ||
|
||
(bool success, ) = Constants.CHEATCODE_ADDRESS.call( | ||
abi.encodeWithSignature("recordLogs()") | ||
); | ||
require(success, "recordLogs failed"); | ||
|
||
emit LogTopic1(1, testData1); | ||
|
||
(bool success2, bytes memory rawData) = Constants | ||
.CHEATCODE_ADDRESS | ||
.call(abi.encodeWithSignature("getRecordedLogs()")); | ||
require(success2, "getRecordedLogs failed"); | ||
|
||
Log[] memory logs = abi.decode(rawData, (Log[])); | ||
console.log("logs length: %d", logs.length); | ||
require(logs.length == 8, "logs length should be 8"); | ||
} | ||
|
||
function trimReturnBytes( | ||
bytes memory rawData | ||
) internal pure returns (bytes memory) { | ||
uint256 lengthStartingPos = rawData.length - 32; | ||
bytes memory lengthSlice = new bytes(32); | ||
|
||
for (uint256 i = 0; i < 32; i++) { | ||
lengthSlice[i] = rawData[lengthStartingPos + i]; | ||
} | ||
|
||
uint256 length = abi.decode(lengthSlice, (uint256)); | ||
bytes memory data = new bytes(length); | ||
|
||
for (uint256 i = 0; i < length; i++) { | ||
data[i] = rawData[i]; | ||
} | ||
|
||
return data; | ||
} | ||
} |