defi-wonderland / smock

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

feature request: allow fakes to fallback to RPC calls for un-mocked methods #100

Open mds1 opened 2 years ago

mds1 commented 2 years ago

When testing against a forked mainnet, it's useful to use Fakes to mock responses at a given address. In some cases, you only want to mock the response to some calls to that mainnet address, and for others you want to fallback to the mainnet value.

This is similar to overwriting storage slots with Hardhat, where you overwrite a slot at an address—if you modified a slot locally, Hardhat returns that value, but for all other storage slots if returns the value of the RPC query. Similar functionality for fakes would be very helpful.

My current workaround for this is creating a fake, setting the desired mock response, then setting the mainnet response as the mocked response for other methods. For example:

// Create the fake
const fakeCrvPool = await smock.fake<ICurvePool>('ICurvePool', { address });

// Mock the response to get_virtual_price so it reverts
fakeCrvPool.get_virtual_price.reverts(); // set get_virtual_price() to revert

// "Mock" the response to balances so it returns the current mainnet values
balances.forEach((bal, i) => fakeCrvPool.balances.whenCalledWith(i).returns(bal));           

The downside to this approach is that it can become impractical to "mock" the true responses when the number of methods or the range of input values is large

For the syntax, you could add a flag to the smock.fake call which enables or disables this feature, e.g.:

const fakeCrvPool = await smock.fake<ICurvePool>('ICurvePool', { address, fallbackToRpc: true });
wei3erHase commented 2 years ago

hi @mds1 ! a task has been created to address this feature request, i hope there's progress anytime soon.