Skip to content

getting values of emitted events #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stillmatic opened this issue Jan 22, 2022 · 8 comments · Fixed by #2161
Closed

getting values of emitted events #543

stillmatic opened this issue Jan 22, 2022 · 8 comments · Fixed by #2161
Assignees
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test
Milestone

Comments

@stillmatic
Copy link

I'm looking at updating https://github.com/optionality/clone-factory and testing it with forge.

One of the functions does the following, and emits an event with the clone address (vs returning it)

    function createThing(string memory _name, uint256 _value) public {
        address clone = createClone(libraryAddress);
        Thing(clone).init(_name, _value);
        emit ThingCreated(clone, libraryAddress);
    }

the tests then pick up this address by doing

 _factory.createThing(name, value)
          .then(tx => {
            return Thing.at(tx.logs[0].args.newThingAddress);
          })

Is there a similar way to get the logged / emitted events and their arguments in forge?

@onbjerg
Copy link
Collaborator

onbjerg commented Jan 22, 2022

That's not currently possible but it would definitely make sense to explore a cheatcode like that. Ergonomics wise it probably wouldn't be very good since you would need to do some manual abi.decode calls etc.

@onbjerg onbjerg added A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test labels Jan 22, 2022
@fubhy
Copy link
Contributor

fubhy commented Jan 23, 2022

I just ran into a case like that too. This is also slightly related to what's been discussed in #512 as extracting parameters from loosely matched events could also be a reasonable solution for that one.

@mds1
Copy link
Collaborator

mds1 commented Jan 23, 2022

I agree this we should have log functionality like this, though like @onbjerg mentioned UX will be probably be pretty gross unless we implement solidity pre-processing first

One note is that in this specific use case you can precaclulate the expected address, then assert there's code at the address after the createThing call. If you use create2 you can do this now, if you use create you'd need #554 to be implemented first

@gakonst
Copy link
Member

gakonst commented Jan 24, 2022

In this use case, I'd manually calculate the create2/create address via their corresponding algos using from/nonce/salt/bytecode hash.

More generally, similar to how we record accessed storage slots, should we add a captureLogs and getLogs cheatcode perhaps? Not sure what the ideal UX here would be, cc @brockelmore, and nice abi decoding automatically seems hard

@bentobox19
Copy link
Contributor

Is this in the roadmap somewhere? I was porting some Ethernaut, and while you can modify the contract Ethernaut.sol to return you the values instead of emitting an event, would be great to have a cheat code capturing that info for you.

I have 3 days of experience with forge 😅, @gakonst is talking above of some gotchas on implementing this, however I see that the logs are outputted at the traces.:

Screen Shot 2022-06-28 at 13 46 10

Anyways, I'm happy to brush up my rust skills and implement this cheat code if it is in the roadmap. Where should I go? Who should I talk to?

@onbjerg
Copy link
Collaborator

onbjerg commented Jun 28, 2022

You can use expectEmit for now, but yeah, we plan to add this at some point

@bentobox19
Copy link
Contributor

Started work on implementing vm.captureLogs

https://github.com/bentobox19/foundry/commits/feature/capture-logs

I'd like to set a draft PR in this repository

@bentobox19
Copy link
Contributor

bentobox19 commented Jun 29, 2022

Alright, just started a draft PR. Hope I can squeeze the hours to get it done 😅

#2161

@onbjerg onbjerg moved this from Todo to In Progress in Foundry Jun 29, 2022
@onbjerg onbjerg added this to the v1.0.0 milestone Jul 1, 2022
Repository owner moved this from In Progress to Done in Foundry Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants