OpenZeppelin / OpenZeppelin/openzeppelin-contracts

Feature: add Mock Contract Boilerplate for usage in library

Open
#5,491 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Solidity
Stars
27.2k
Forks
12.4k
Avg merge
2d 19h
Merged PRs (30d)
33

Description

🧐 Motivation

Common testing pattern in Solidity is to mock ERC tokens such as:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

//Simple Mock ERC721
contract MockERC721 is ERC721 {
    constructor() ERC721("MockNFT", "MNFT") {}

    function mint(address to, uint256 tokenId) public {
        _mint(to, tokenId);
    }
}

This pattern is repeated over and over again, however the OZ Contracts do not include such trivial boilerplate contracts, in fact the contracts found in /mock are for internal testing and not typically used by end developers.

📝 Details

Include a *Mocking Harness (I use harness to distinguish between the existing usage of Mocks in the codebase and this new one meant to be used by end users

ERC20
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

// A simple Mock ERC20 for testing purposes.
contract MockERC20 is ERC20 {
    constructor(string memory name, string memory symbol, uint256 initialSupply) ERC20(name, symbol) {
        _mint(msg.sender, initialSupply);
    }

    function mint(address to, uint256 amount) public {
        _mint(to, amount);
    }
}
ERC721
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

// Simple Mock ERC721
contract MockERC721 is ERC721 {
    constructor() ERC721("MockNFT", "MNFT") {}

    function mint(address to, uint256 tokenId) public {
        _mint(to, tokenId);
    }
}
Naive Usage

[!NOTE]
The path is just an example, for this issue, change as you will!

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

import "@openzeppelin/contracts/testing/token/MockERC20.sol";
import "@openzeppelin/contracts/testing/token/MockERC721.sol";

// etc etc

Conclusion

No more unneeded boilerplate copy paste yes plz sers?

I can open a PR if you would like,

Cheers!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing contracts in /mock and the ERC20 and ERC721 entry points referenced in the issue. Compare their internal-testing role with the proposed end-user MockERC20 and MockERC721 harnesses, then determine the supported scope and package path; done means the agreed harness contracts are available through the documented import paths without duplicating internal mocks.

Written by the indexing model from the issue text.

Assessment

Tech stack
solidity
Domain
blockchain
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.