1

Does anyone know how to create a function that requires sender to have at least one token to use the function? For example, to require sender's balance to have an ether, I'd use:

require(balances[msg.sender] >= 1 ether);

How would I instead create a requirement on msg.senders ERC20 balance of a particular token that was created through the contract they're interacting with? Thanks.

1 Answers1

2

You can do it like this:

token = IERC20(tokenContractAddress);
// tokens, like ethers, are usually 18 decimal places
// we use ether keyword here just to significant
// the number of decimals in the price
require(token.balanceOf(msg.sender) >= 1 ether)  

Grab a copy of IERC20 interface definition here.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • 1
    Thanks for the response. If I understand correctly, I'd need to run this "require(token.balanceOf(msg.sender)" in a function of a separate contract that imports my first ERC20 contract since it needs the "tokenContractAddress"? – Brian Lee Victory Jun 29 '21 at 04:00
  • 1
    Correct, you need refer to the token, of which balance you want to know, by its address and interface. – Mikko Ohtamaa Jun 29 '21 at 08:22