foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.32k stars 1.76k forks source link

feat(cheatcodes): add `vm.resolve` cheatcode to resolve labels to string inside of tests and scripts #6500

Open ckksec opened 11 months ago

ckksec commented 11 months ago

Component

Forge

Describe the feature you would like

Labels are a great way to see in the trace how contracts interact with each other in a "human readable" format. However, the Logs (when using console.log()) only show the addresses of e.g. tokens. It would be great if addresses in Logs would also be replaced by labels like it is done in stack traces.

Additional context

logExample
robbiesumner commented 11 months ago

I like the idea an would be happy to see it, but I'm not sure if everyone would expect this behavior.

Currently your console.log would be something like:

console.log("Transferring %s from %s to %s", address(token), address(from), address(to))

console.log just formats the inputs for now, and as it is only passing the addresses, the current behavior is the one I expect. I could imagine there being a second log function (logWithLabel?) that would also annotate the addresses

zerosnacks commented 4 months ago

I think it would make sense to add a cheatcode to vm.resolve() the label that can be called.

For future reference

contract LabelTest is Test {
    address public alice = address(1);
    address public bob = address(2);
    address public token = address(3);

    function setUp() public {
        vm.label(alice, "alice");
        vm.label(bob, "bob");
        vm.label(token, "token");
    }

    function test_logLabel() public {
        console.log(
            "Transferring %s from %s to %s",
            address(token),
            address(alice),
            address(bob)
        );
    }
}
[PASS] test_logLabel() (gas: 9937)
Logs:
  Transferring 0x0000000000000000000000000000000000000003 from 0x0000000000000000000000000000000000000001 to 0x0000000000000000000000000000000000000002

Traces:
  [9937] LabelTest::test_logLabel()
    ├─ [0] console::log("Transferring %s from %s to %s", token: [0x0000000000000000000000000000000000000003], alice: [0x0000000000000000000000000000000000000001], bob: [0x0000000000000000000000000000000000000002]) [staticcall]
    │   └─ ← [Stop] 
    └─ ← [Stop] 

Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 630.38µs (123.96µs CPU time)

Ran 1 test suite in 4.20ms (630.38µs CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)

Proposed alternative with vm.resolve()

        console.log(
            "Transferring %s from %s to %s",
            vm.resolve(address(token)),
            vm.resolve(address(alice)),
            vm.resolve(address(bob))
        );