0

I just want to double check if my below approach is correct.

I've made two ERC 20 compliant tokens (Token A and Token B) as based on OZ standard:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.13;

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

contract AToken is ERC20 {
    constructor() ERC20("A", "ATOK") {
        _mint(msg.sender, 100000000000000000000000000);
    }
}

Would the below code be correct in instantiating them into a smart contract assuming I deployed both tokens and had their addresses?

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.13;

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

contract Tokens { 

    IERC20 public immutable tokenA;
    IERC20 public immutable tokenB;

    constructor(address _tokenA, address _tokenB) {
        tokenA = IERC20(_tokenA);
        tokenB = IERC20(_tokenB);
    }

1 Answer 1

1

Looks like you didn't import IERC20.sol into the second contract, but yes that's the correct way to initialize other contracts within a contract

2
  • Thank you! I was hoping I could ask two more questions if that's ok? 1. INTERFACE_NAME(_address) will always work right so long as both the name of the interface and the address is valid? 2.Are there any scenarios in the original code block where we might consider using the actual ERC20 instead of IERC20?
    – Mike Chan
    Aug 22, 2022 at 2:52
  • Keep in mind that the purpose of interfaces are to keep the size of your codebase down (so you're not importing an entire contract every time you want to interface with another contract) & you can make an interface for any contract. Take a look at IERC20.sol and ERC20.sol — they have the same functions, except in the interface the functions aren't implemented (has nothing in the code block). This makes it more lightweight
    – Ryan Sea
    Aug 22, 2022 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.