1

I have an ERC20 contract that calls BalanceOf in a ERC721 contract;

In my ERC20 contract;

import "./NFT.sol";

    contract ERC20 {
    
     NFT public nft;
    
     constructor (NFT _nftAddress) public {
      nft = _nftAddress;
    }
    function getNFTBalance(address _nftOwner) public view returns (uint256 result){
           nft.balanceOf(_nftOwner);
           return result;
       }

I'm using the ERC721PresetMinterPauserAutoId from OpenZeppelin

My NFT contract import ERC21.sol

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

In the ERC721.sol the BalanceOf function;

function balanceOf(address owner) public view override returns (uint256) {
        require(owner != address(0), "ERC721: balance query for the zero address");

        return _holderTokens[owner].length();
    }

This is how I pass the contract address on deployment in 2_deploy_contracts.js

module.exports = async function(deployer) {
  // deploying the ERC20 Token
  const accounts = await web3.eth.getAccounts();

  await deployer.deploy(Token);

  // deploying the NFT
  const NFT_NAME = "ID Exclusivity Token";
  const NFT_SYMBOL = "IDET";
  const NFT_API = "https://example.com/nft/api/token/"

  await deployer.deploy(NFT, NFT_NAME, NFT_SYMBOL, NFT_API)

  .then( async () => {
    nftContractAddress = NFT.address;
    console.log('NFT ADDRESS : ',nftContractAddress)
    const feeAccount = accounts[0];
    const feePercent = 10;
    //Deploying Exchange with the NFT address
    await deployer.deploy(Exchange, nftContractAddress, feeAccount, feePercent);
  })
};

I can confirm that the address is the right one with the console log.

Once deployed, in truffle console:

nft = await NFT.deployed()

await nft.mint("0x851ffbF315d9d32E8DBE75b10B1E0b8C030320cf")

exchange = await Exchange.deployed()

await exchange.getNFTBalance("0x851ffbF315d9d32E8DBE75b10B1E0b8C030320cf") // return 0

await nft.balanceOf("0x851ffbF315d9d32E8DBE75b10B1E0b8C030320cf") // return 1
3
  • Please explain If I query the same account using the NFT contract BalanceOf I get 1 (i.e., show your code). Oct 9, 2020 at 7:36
  • Also, the problem described in the title of your question is different from the problem described in the body of your question. Please fix this conflict. Oct 9, 2020 at 8:47
  • @goodvibration I added the complete deployment file and the command that return balance Oct 9, 2020 at 16:01

2 Answers 2

2

The problem is how you defined instance of nft contract in constructor. Your Code:

NFT public nft;

 constructor (NFT _nftAddress) public {
  nft = _nftAddress;
}

Try this instead:

constructor (NFT _nftAddress) public {
  nft = NFT(_nftAddress);
}
0

I was returning a result that i assumed was 0 by default.

Changed the function to;

 function getNFTBalance(address _owner) public view returns (uint256){
        
       return nft.balanceOf(_owner);
   }

Getting the right balance now

0

Your Answer

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

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