hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

hardhat/console.sol `bytes` and `uint256[]` typed arguments support

Open MihanixA opened this issue 3 years ago • 12 comments

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.

MihanixA avatar Nov 07 '21 17:11 MihanixA

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 avatar Nov 08 '21 13:11 fvictorio

@fvictorio Overload for bytes would be great. Also logging arrays would be very helpful

MihanixA avatar Nov 08 '21 16:11 MihanixA

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.

alxiong avatar Nov 21 '21 09:11 alxiong

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).

fvictorio avatar Nov 25 '21 20:11 fvictorio

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?

sw0714 avatar Jan 27 '22 01:01 sw0714

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.

MihanixA avatar Jan 27 '22 18:01 MihanixA

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 avatar Feb 16 '22 19:02 nanaknihal

@nanaknihal there is a console.logBytes32 overload.

fvictorio avatar Feb 21 '22 22:02 fvictorio

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.

github-actions[bot] avatar Jul 11 '22 14:07 github-actions[bot]

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.

kmjones1979 avatar Aug 01 '22 17:08 kmjones1979

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 avatar Aug 01 '22 17:08 kmjones1979

@kmjones1979 see this comment.

fvictorio avatar Aug 03 '22 11:08 fvictorio

@nanaknihal there is a console.logBytes32 overload.

such a big help!

jakesung-harborx avatar Sep 27 '22 02:09 jakesung-harborx

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.

fvictorio avatar Dec 27 '22 19:12 fvictorio