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;
}
0 Likes

#2

Hello @jschiarizzi! Welcome to the forum :slight_smile:

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

1 Like

#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.

1 Like

#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.

0 Likes

#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!

2 Likes

#6

Hey @jschiarizzi, did you had any luck?

0 Likes

#7

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. https://github.com/polezo/soulCap/blob/emit_tests/contracts/Soulcapper.sol

2 Likes

#8

Awesome, thanks for sharing!

1 Like