defi-wonderland / smock

The Solidity mocking library
MIT License
319 stars 40 forks source link

feat request: mocking Diamond proxy schemes #116

Closed hickscorp closed 2 years ago

hickscorp commented 2 years ago

This is not a bug report - rather it's probably the start of a discuthis ssion, which could either lead to discovering that smock could have new features around the diamond pattern, or a contribution from people who found ways to do things neatly.

Consider the following architecture:

At this point, I found myself stuck - because although FacetA and FacetB are split into different contacts, all their methods are in fact accessible through the Diamond contract. I didn't find a way to mock FacetB - and I found myself quite often testing things from the FacetA tests that are relevant to FacetB and reciproquelly.

I think since Diamonds often use libraries for functions that are reused across facets, there could be overlap with https://github.com/defi-wonderland/smock/issues/50 ... But it's not always the case. Very often in FacetA.sol there are calls such as FacetB(address(this)).someFunction() - and it's not something that smock can help with unfortunatelly.

Can we maybe brainstorm a bit - it might be that I am missing something obvious here - but if not, I think it would be of great help to think about how smock could be improved to help with the diamond pattern.

One of the solution I found which would potentially work, would be to have some facets duplicated just for tests... But that would require quite some boilerplate written in solidity all the time - for example having FacetBMock.sol have a fallback() function which would record all calls so that they could be inspected by the typescript test - return values wouldn't work though.

Another option would be to allow smock to "connect" to an existing address... For example the flow would look like:

In case it helps gathering ideas: this is a stack overflow post I've made regarding this particular topic.

wei3erHase commented 2 years ago

hi @hickscorp, could you provide a basic example within the smock repo of a diamond deployment and the expected behaviour? we're not very experienced with proxies and i'm afraid i do not totally understand how the feature would work. if you can include the required packages and some test contracts, i can help you to develop the tests and add it as a bounty to develop

hickscorp commented 2 years ago

I will!

hickscorp commented 2 years ago

@wei3erHase happy to team up together today if you have a bit of time. We could get clean requirements written together?

hickscorp commented 2 years ago

I made a repository which has a README explaining the specs: https://github.com/hickscorp/smock-diamond-showcase