-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
radar bear
committed
Aug 26, 2024
1 parent
bb5d04f
commit b1b01ed
Showing
4 changed files
with
125 additions
and
70 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
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,54 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.19; | ||
|
||
import "forge-std/Script.sol"; | ||
|
||
library Utils { | ||
/** | ||
* @dev Logs the inputs of a function call. | ||
* @param inputs The inputs to log. | ||
*/ | ||
function logInputs(string[] memory inputs) public view { | ||
string memory concatenatedInputs = ""; | ||
for (uint256 i = 0; i < inputs.length; i++) { | ||
concatenatedInputs = string(abi.encodePacked(concatenatedInputs, inputs[i], " ")); | ||
} | ||
console.log(concatenatedInputs); | ||
} | ||
|
||
/** | ||
* @dev Converts an address to its string representation. | ||
* @param addr The address to convert. | ||
* @return The string representation of the address. | ||
*/ | ||
function addressToString(address addr) internal pure returns (string memory) { | ||
bytes32 value = bytes32(uint256(uint160(addr))); | ||
bytes memory alphabet = "0123456789abcdef"; | ||
|
||
bytes memory str = new bytes(42); | ||
str[0] = '0'; | ||
str[1] = 'x'; | ||
for (uint256 i = 0; i < 20; i++) { | ||
str[2 + i * 2] = alphabet[uint8(value[i + 12] >> 4)]; | ||
str[3 + i * 2] = alphabet[uint8(value[i + 12] & 0x0f)]; | ||
} | ||
return string(str); | ||
} | ||
|
||
/** | ||
* @dev Converts bytes to a string without the '0x' prefix. | ||
* @param data The bytes to convert. | ||
* @return The string representation of the bytes. | ||
*/ | ||
function bytesToStringWithout0x(bytes memory data) internal pure returns (string memory) { | ||
bytes memory alphabet = "0123456789abcdef"; | ||
|
||
bytes memory str = new bytes(data.length * 2); | ||
for (uint256 i = 0; i < data.length; i++) { | ||
str[i * 2] = alphabet[uint8(data[i] >> 4)]; | ||
str[i * 2 + 1] = alphabet[uint8(data[i] & 0x0f)]; | ||
} | ||
return string(str); | ||
} | ||
} |