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


#1

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;
 }else{
   result = false;
 }
 return result;
}

#2

Hello @jschiarizzi! Welcome to the forum :slight_smile:

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


#3

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.


#4

Thanks for the replies @frangio & @IvanTheGreatDev

full code here https://github.com/polezo/soulCap/blob/ethereum-nft/contracts/Soulcapper.sol 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.


#5

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!