How to call a method from a contract that was instantiated from another contract via truffle console?

Viewed 196

I'm trying to call a method from a contract that was instantiated from another contract. I use truffle console.

Here are the details: (I think you can gloss over the code, since this question is aimed at using truffle console, not fixing any bugs)

I have a contract called MyCoinSupply.sol:

pragma solidity ^0.8.0;

import "./MyCoin.sol";

contract MyCoinSupply is MyCoin("MyCoin", "MYC") // MyCoin is ERC20
{
    constructor() public // gives 1000 tokens to the owner
    {
        _mint(msg.sender, 1000);
    }

    function getCoinbase() public returns (address) // for debugging purposes
    {
        return block.coinbase;
    }

    function _mintMinerReward() internal // gives 20 tokens to the miner
    {
        // _mint(block.coinbase, 20); // for some reason, block.coinbase is address 0
    }

    function _transfer(address from, address to, uint256 value) override internal
    {
        _mintMinerReward();
        super._transfer(from, to, value);
    }
}

I instantiate MyCoinSupply.sol within MyCoinDEX.sol:

pragma solidity ^0.8.0;

import "./IERC20.sol";
import "./MyCoinSupply.sol";

contract MyCoinDEX
{
    IERC20 public token;

    event Bought(uint256 amount);
    event Sold(uint256 amount);

    constructor() public
    {
        token = new MyCoinSupply();
    }

    function showSender() public view returns (address) // for debugging purposes
    {
        return (msg.sender);
    }

    function buy(uint256 amountTobuy) payable public // send ether and get tokens in exchange; 1 token == 1 ether
    {
      uint256 dexBalance = token.balanceOf(address(this));
      require(amountTobuy > 0, "You need to send some ether");
      require(amountTobuy <= dexBalance, "Not enough tokens in the reserve");
      token.transfer(msg.sender, amountTobuy);
      emit Bought(amountTobuy);
    }

    function sell(uint256 amount) public // send tokens to get ether back
    {
      require(amount > 0, "You need to sell at least some tokens");
      uint256 allowance = token.allowance(msg.sender, address(this));
      require(allowance >= amount, "Check the token allowance");
      token.transferFrom(msg.sender, address(this), amount);
      payable(msg.sender).transfer(amount);
      emit Sold(amount);
    }

}

Here's my migration for deploying contracts:

const MyCoinSupply = artifacts.require("MyCoinSupply");
const MyCoinDEX = artifacts.require("MyCoinDEX");

module.exports = function(deployer) {
    deployer.deploy(MyCoinSupply);
    deployer.deploy(MyCoinDEX);
};

Here's my problem:

When I try to call functions that belong to MyCoinDEX, all works well:

truffle(development)> let instance = await MyCoinDEX.deployed()
undefined
truffle(development)> instance.buy(1)

All is good here.

However, when I try the following I get an error:

truffle(development)> instance.token.balanceOf("0xE1994C1054f9c4171B8ae9a7E7a68F404c2bF829")
evalmachine.<anonymous>:0
instance.token.balanceOf("0xE1994C1054f9c4171B8ae9a7E7a68F404c2bF829")
               ^

Uncaught TypeError: instance.token.balanceOf is not a function

I Googled this error (I read this solution and this solution), but I didn't really stumble upon a solution. Can someone tell me how can I call token's methods from MyCoinDEX?

Thank you in advance!

0 Answers
Related