So I have two contracts

Contract A & Contract B

Contract A is an ERC721 that allows for the balanceOf function to be called after passing it an address. It will return response of how many NFTs the address you pass it has in the wallet.

Contract B is a ERC20 contract. I am trying to implement transfer requirements on Contract B that call for the balanceOf Contract A based on the msg.sender address you pass it & return the value to compare against >= 1 to make sure the transfer is even eligible.

Overall, trying to make Contract B check if the msg.sender has a balance of Contract A & then as long as it is >= 1, the Contract B transfer should work.

Anyone know how I would do this using require() in the transfer function on Contract B?

Below is the transfer function within Contract B that I am trying to mess with. I have tried a ton of methods & just cannot figure out the best way to implement this logic.

function _transfer(address sender, address recipient, uint256 amount) internal virtual {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        require(address(0xEBE7d6984917DEcceaCc089bb7E33471CCe566b8).balanceOf(msg.sender) >= 1, "You must be a member by obtaining your badge!");

        _beforeTokenTransfer(sender, recipient, amount);

        uint256 senderBalance = _balances[sender];
        require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
        _balances[sender] = senderBalance - amount;
        _balances[recipient] += amount;

        emit Transfer(sender, recipient, amount);
    } ```

1 Answer 1


You should first cast the contract to a certain type before accessing its methods. In this case, you need to query the balanceOf an ERC721, so we'll use the IERC721 interface:

require(IERC721(0x..).balanceOf(msg.sender) >= 1, "...");

P.S. To access IERC721 you can import the OZ implementation, which you're probably already doing.

  • Thanks, gonna implement this now, couldn't figure out why it wasn't working but swapping address for IERC721 makes sense. I will keep you updated
    – Stera Team
    Commented Jan 24, 2022 at 6:21
  • Just ran some tests, this method is working perfectly. Thanks a ton for the help
    – Stera Team
    Commented Jan 24, 2022 at 6:40

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.