Solidity – How to Reference One Contract Into Another Contract

blockchaincontract-deploymentsoliditytruffletruffle-console

I have 3 contracts that I am trying to deploy.2 are dependent on the third one, but for some reason, I am unable to call function from the other contract. Below is my code.

ERC20 Token

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0;

// Define your Smart Contract with the "Contract" keyword and an empty constructor
contract ERC20 {

    string public name = "Token";
    string public symbol = "Symbol";
    uint256 public totalSupply;

    // mapping of Balances
    mapping(address => uint256) public balanceOf;

    constructor(uint256 _initialSupply) {
        balanceOf[msg.sender] = _initialSupply;
        totalSupply = _initialSupply;
    }

}

NFT Market

contract NFTMarket is ReentrancyGuard {
  using Counters for Counters.Counter;
  Counters.Counter private _itemIds;
  Counters.Counter private _itemsSold;

  address payable owner;
  uint256 listingPrice = 0.025 ether;

  constructor() {
    owner = payable(msg.sender);
  }
}

NFT Contract

    // Import the ERC20 Token contract
    import "./ERC20.sol";
    
    
    contract NFT is ERC721URIStorage {
        using Counters for Counters.Counter;
        Counters.Counter private _tokenIds;
        address contractAddress;
    
        // Declaring the ERC20 token contract variable
        ERC20 public tokenContract;
    
        constructor(address marketplaceAddress, ERC20 _tokenContract) ERC721("My NFT", "MNFT") {
            contractAddress = marketplaceAddress;
    
            // Assign Token Contract Reference to the NFT Contract
            tokenContract = _tokenContract;

  }
}

Deployer

const ERC20 = artifacts.require("ERC20");
const NFT = artifacts.require("NFT");
const NFTMarket = artifacts.require("NFTMarket");

module.exports = function (deployer, network, accounts) {

    deployer.then(async () => {

        // Pass initial supply as argument of the deployer.
        await deployer.deploy(ERC20, 1000000); // 1000000 NZT tokens

        await deployer.deploy(NFTMarket);

        await deployer.deploy(NFT, NFTMarket.address, ERC20.address);

    })
};

Testing & Console

const balance = await tokenNFT.tokenContract().methods.balanceOf(await tokenNFT.address).call()
const balance2 = await tokenNFT.tokenContract.methods.balanceOf(await tokenNFT.address)
const balance3 = await tokenNFT.tokenContract.balanceOf(await tokenNFT.address)

Non of these returns anything. My NFT contract is not able to call the ERC20 token functions. What am I doing wrong here. Please help me solve this, as I have been stuck for multiple hours now.

Amicably,
Christian

Best Answer

In that case those cases dont work because your NFT contract does not inherits from ERC20 so the only places you can use those functions is where you declared instances of the ERC20 contract. For example:

This is your NFT contract:

 // Import the ERC20 Token contract
    import "./ERC20.sol";
    
    
    contract NFT is ERC721URIStorage {
        using Counters for Counters.Counter;
        Counters.Counter private _tokenIds;
        address contractAddress;
    
        // Declaring the ERC20 token contract variable
        ERC20 public tokenContract;
    
        constructor(address marketplaceAddress, ERC20 _tokenContract) ERC721("My NFT", "MNFT") {
            contractAddress = marketplaceAddress;
    
            // Assign Token Contract Reference to the NFT Contract
            tokenContract = _tokenContract;
            }
    function RandomFunction() public returns(uint256){
    //EXAMPLE OF USING ERC20 FUNCTION
        return tokenContract.balanceOf(msg.sender);
    }

}

As you can see inside a RandomFunction you can call any ERC20 function using the instance of the token you created on deploy using the contructor. But you cannot call those functions from the outside.

Related Topic