defi-wonderland / smock

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

Mock with an ABI #92

Closed impguard closed 2 years ago

impguard commented 3 years ago

Is it possible to mock with an ABI instead of a contract name? As far as I understand, the integration of smock into hardhat allows it to access all the mocks that hardhat ethers is aware of. Is there a way to generate a mock of an ABI though? The use case here is to mock out some third party library contracts that my contracts are interacting with.

I can use waffle mocks for this instead but it's not as nice to hop between two mocking frameworks.

0xGorilla commented 3 years ago

Creating mocks is done in order to be able to interact with the real contract logic and storage, that's why it's not possible to create from the ABI. What you could do is to create a fake from your ABI, with this you will be able to pre-program return values to all your external functions and to check call data. I highly recommend you to read the fake docs

impguard commented 3 years ago

Yea. There's a missing gap of knowledge for me for what the difference between creating a "real" contract from an ABI vs. creating a fake contract from an ABI. My guess is the real contract requires the actual "compiled" source code as bytecode while the ABI is just the interface.

The solution I have right now is copied from a separate repo and having test contracts that literally import the third party library classes so hardhat can compile them as my own. This allows me to use these contracts successfully with smock. Just feel like there's gotta be a workaround here that's cleaner than having fake contracts literally go "import from/third/party/library.sol" to use.

For context, the rationale here is because these third party libraries have provided useful mocks, and, in this case, I'm working with oracles, etc. that go outside of the blockchain, so testing flows where a contract method ultimately comes back around and executes a different method is a bit more hairy.

0xGorilla commented 3 years ago

Any chance you could share your repo or maybe a glimpse of the relevant code?

0xGorilla commented 2 years ago

Closing this issue because of inactivity, please feel free to reopen it if necessary.