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

Implement interfaces in mock tokens #510

Merged
merged 3 commits into from
Feb 13, 2024
Merged

Implement interfaces in mock tokens #510

merged 3 commits into from
Feb 13, 2024

Conversation

arr00
Copy link
Contributor

@arr00 arr00 commented Feb 8, 2024

Currently MockERC20 and MockERC721 don't implement their respective interfaces. This PR fixes that.

@arr00 arr00 changed the title Inherit interfaces in mock tokens Implement interfaces in mock tokens Feb 8, 2024
@mds1
Copy link
Collaborator

mds1 commented Feb 9, 2024

Thanks for your PR :)

What is the benefit of having them inherit from the interfaces? This feels like a nontrivial diff that adds code for unclear benefit, since these ERC interfaces are finalized and static, and the mocks are already compliant. I'd like to keep everything in forge-std as simple as possible so it's easy to read/maintain and to reduce compilation times.

I'm also not a fan of making the variables private since that limits devs from inheriting the mock and adding e.g. new methods to set approvals

@arr00
Copy link
Contributor Author

arr00 commented Feb 9, 2024

It is unintuitive for there to be an interface as well as an implementation of that interface in the same repo but them not be tied together at all. I came across this when using the interface and the mock token in a situation shown below:

IERC20 fromToken;
fromToken = IERC20(address(new MockERC20()));

It is highly preferred for the below to work:

IERC20 fromToken;
fromToken = new MockERC20();

@mds1
Copy link
Collaborator

mds1 commented Feb 9, 2024

It is highly preferred for the below to work:

IERC20 fromToken;
fromToken = new MockERC20();

That would be really nice but I don't think that is supported since solidity doesn't have structural typing: https://forum.soliditylang.org/t/making-interfaces-structural/1790

import {MockERC20} from "../src/mocks/MockERC20.sol";
import {IERC20} from "../src/interfaces/IERC20.sol";
import {Test} from "../src/Test.sol";

contract ATest is Test {
    IERC20 public token;

    function setUp() public {
      IERC20 fromToken;
      fromToken = new MockERC20();
    }
}
$ forge build
[⠢] Compiling...
[⠊] Compiling 26 files with 0.8.23
[⠒] Solc 0.8.23 finished in 648.53ms
Error: 
Compiler run failed:
Warning (3420): Source file does not specify required compiler version! Consider adding "pragma solidity ^0.8.23;"
--> test/A.t.sol

Error (7407): Type contract MockERC20 is not implicitly convertible to expected type contract IERC20.
  --> test/A.t.sol:19:19:
   |
19 |       fromToken = new MockERC20();
   |                   ^^^^^^^^^^^^^^^

@arr00
Copy link
Contributor Author

arr00 commented Feb 9, 2024

@mds1 Consider running that test in the context of this PR. It works.

Copy link
Collaborator

@mds1 mds1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops you're right, I was on the wrong branch. Ok, agreed that is a much better UX!

Two small changes then we can merge

src/mocks/MockERC20.sol Outdated Show resolved Hide resolved
src/mocks/MockERC20.sol Outdated Show resolved Hide resolved
@arr00
Copy link
Contributor Author

arr00 commented Feb 13, 2024

@mds1 let me know if any other changes are needed

Copy link
Collaborator

@mds1 mds1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@mds1 mds1 merged commit 3725a22 into foundry-rs:master Feb 13, 2024
3 checks passed
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

Successfully merging this pull request may close these issues.

2 participants