Check if a user has a certain erc20, using another contract

Hello OZers,
I’m stuck on a strange issue. I’m trying to check if a user has a certain erc20, using my contract. I’ve created an erc20 instance, I think correctly.

import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

// in the constructor
    ZRX_erc20_contrct = 0xabc;//a random erc20 on rinkeby that seems to work fine

function hasZRX(address user) public view returns(bool){
 ERC20 instance = ERC20(ZRX_erc20_contrct);
 bool result;
 if( instance.balanceOf(user) > 0){
   result = true;
   result = false;
 return result;

Hello @jschiarizzi! Welcome to the forum :slight_smile:

Have you been encountering an error with this? How’s your web3 configured?

1 Like

We’d love to help with this issue. The code looks fine on first inspection. Can you share more details of your setup? Ideally the full code to reproduce the problem ourselves.

1 Like

Thanks for the replies @frangio & @IvanTheGreatDev

full code here on ethereum-nft branch.

the captureSoul() function is failing every time, no matter how much eth I send. I thought the issue was most likely in hasZRX() since I’ve never really done that before.

Hey bud! Looked at the function.

Maybe you can try removing view from the hasZRX() function? That function emits an event and those change state so it probably errors when it reaches the event in that function.

Try that and let us know!


Hey @jschiarizzi, did you had any luck?

Yes, I resolved it. Something about solidity didnt like it when I was comparing public and private vars in require(). making all the global vars public fixes it, although I’m not sure why exactly.

The code for checking if someone has the specified erc20 in the original question is exactly what I used.

code in case it helps someone else.


Awesome, thanks for sharing!

1 Like