Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: include Mock Contract Boilerplate for usage in for Mock ERC Tests #5491

Open
sambacha opened this issue Feb 6, 2025 · 0 comments
Open

Comments

@sambacha
Copy link

sambacha commented Feb 6, 2025

🧐 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant