`smock` is a utility package that can generate mock Solidity contracts (for testing). `smock` hooks into a `ethereumjs-vm` instance so that mock contract functions can be written entirely in JavaScript. `smock` currently only supports [Hardhat](http://hardhat.org/), but will be extended to support other testing frameworks.
## NOTICE
Some nice benefits of hooking in at the VM level:
Smock v1 is being deprecated.
* Don't need to deploy any special contracts just for mocking!
Please migrate to [Smock v2](https://github.com/defi-wonderland/smock).
* All of the calls are synchronous.
You can find an archive of the Smock v1 codebase at [optimism@d337713c91](https://github.com/ethereum-optimism/optimism/tree/d337713c91c6634f546b8d6572392c0784ab8217/packages/smock).
* Perform arbitrary javascript logic within your return value (return a function).
* It sounds cool.
`smock` also contains `smoddit`, another utility that allows you to modify the internal storage of contracts. We've found this to be quite useful in cases where many interactions occur within a single contract (typically to save gas).
## Installation
You can easily install `smock` via `npm`:
```sh
npm install @eth-optimism/smock
```
Or via `yarn`:
```sh
yarn add @eth-optimism/smock
```
## Note on Using `smoddit`
`smoddit` requires access to the internal storage layout of your smart contracts. The Solidity compiler exposes this via the `storageLayout` flag, which you need to enable at your hardhat config.
Here's an example `hardhat.config.ts` that shows how to import the plugin:
```typescript
// hardhat.config.ts
import{HardhatUserConfig}from'hardhat/config'
constconfig:HardhatUserConfig={
...,
solidity:{
version:'0.7.0',
settings:{
outputSelection:{
"*":{
"*":["storageLayout"],
},
},
}
},
}
exportdefaultconfig
```
## API
### Functions
#### `smockit`
##### Import
```typescript
import{smockit}from'@eth-optimism/smock'
```
##### Signature
```typescript
constsmockit=async(
spec:ContractInterface|Contract|ContractFactory,
opts:{
provider?:any,
address?:string,
},
):Promise<MockContract>
```
#### `smoddit`
##### Import
```typescript
import{smoddit}from'@eth-optimism/smock'
```
##### Signature
```typescript
constsmoddit=async(
name:string,
signer?:any
):Promise<ModifiableContractFactory>
```
### Types
#### `smockit`
##### `MockContract`
```typescript
interfaceMockContractextendsethers.Contract{
smocked:{
[functionName:string]:MockContractFunction
}
// A wallet you can use to send transactions *from* a smocked contract
`Storage layout for ${name} not found. Did you forget to set the storage layout compiler option in your hardhat config? Read more: https://github.com/ethereum-optimism/smock#note-on-using-smoddit`