hardhat
hardhat copied to clipboard
hardhat/console.sol `bytes` and `uint256[]` typed arguments support
Hardhat doesn't support logging bytes
object to console via hardhat/console.sol
though it might be useful in a huge amount of cases.
I'd suggest printing bytes
and uint256[]
object as an array of numbers.
From a user perspective I don't think that implementing this feature could be technically difficult, but I could be wrong.
There is a console.logBytes
, but I don't think we have a function for arrays.
With respect to adding an overload for console.log(bytes)
, I guess that's doable? But maybe there is a good reason why there isn't one now.
@fvictorio Overload for bytes would be great. Also logging arrays would be very helpful
seconded for overloading for bytes! 🙌
btw, current error message when I try to console.log bytes isn't super helpful:
TypeError: Member "log" not found or not visible after argument-dependent lookup in type(library console).
--> contracts/XXX.sol:16:9:
|
16 | console.log(encoded);
| ^^^^^^^^^^^
The console.log call you made isn’t supported. See https://hardhat.org/console-log for the list of supported methods.
ofc, console.logBytes
works for me.
Sadly, overloading for bytes it's not possible, at least for now. The reason is that if we include that overload, then this statement wouldn't compile:
console.log("abc")
because the compiler doesn't know if it should use console.log(bytes)
or console.log(string)
.
enum GameState {Opening, Waiting, Running}
struct Pool {
uint256 id;
GameState state;
uint256 tokenAmount;
address[] players;
}
id and tokenAmount can be logged, but state and players can't be logged
that is, console.log(pool.state) or console.log(pool.players) are not working
Is there any way to deal with them?
enum GameState {Opening, Waiting, Running} struct Pool { uint256 id; GameState state; uint256 tokenAmount; address[] players; } id and tokenAmount can be logged, but state and players can't be logged that is, console.log(pool.state) or console.log(pool.players) are not working Is there any way to deal with them?
There is no overload for address[]
especially for your custom GameState
enum.
Try iterating through address[]
array and log each one of them.
As for GameState
— try converting it to uint
.
It would also be great to support bytes32. Currently, bytes32 doesn't work for me in console.logBytes
, nor console.log
In the meantime, this worked:
function bytes32ToBytes(bytes32 b_) private pure returns (bytes memory){
return abi.encodePacked(b_);
}
console.logBytes(bytes32ToBytes(b)
@nanaknihal there is a console.logBytes32
overload.
This issue was marked as stale because it didn't have any activity in the last 30 days. If you think it's still relevant, please leave a comment indicating so. Otherwise, it will be closed in 7 days.
would have been useful to have this when testing today. Writing this function doesn't seem to work.
function keccakTest(address _address) public {
bytes32 dHash = keccak256(abi.encodePacked(_address));
console.log(bytes32(dHash));
bytes32 aHash = keccak256(abi.encodePacked(address(_address)));
console.log(bytes32(dHash));
}
Perhaps I am not testing the correct way, but having bytes support in console.log seems to make sense to me.
Ended up testing like this.
function hashTestA() public view returns (bytes32) {
bytes32 prevHash = blockhash(block.number -1);
uint256 nonce = diceGame.nonce();
return keccak256(abi.encodePacked(prevHash, diceGame, nonce));
}
function hashTestB() public view returns (bytes32) {
bytes32 prevHash = blockhash(block.number -1);
uint256 nonce = diceGame.nonce();
return keccak256(abi.encodePacked(prevHash, address(diceGame), nonce));
}
Side note: can anyone tell me why these hash function results match? I would expect them to be different.
@kmjones1979 see this comment.
@nanaknihal there is a
console.logBytes32
overload.
such a big help!
I'm going to close this issue now because I don't think there's anything we can do, at least with the current capabilities of solidity.