0

i created an erc20 token and i'm trying to create a smart contract that stores some informations in a blockchain just if the wallet contain my tokens.

How can i do this? Have i to work on the token creation contract or on the other smart contract that stores information?

1 Answer 1

0

The ERC20 standard exposes a balanceOf function which will return the token balance of a given address.

In your smart contract (contract 2) you can check the balance of that address within the context of the token contract (contract 1) you created.

pragma solidity ^0.4.17;

contract ERC20 {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);
    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}

contract MyContract {
  address token_address = // Address here;

  function hasTokens() public returns (bool) {
    let balance = ERC20(token_address).balanceOf(msg.sender);
    return (balance > 0);
  }

}

From there, you can call the hasTokens() function whenever you want to check within your smart contract that a user has some token balance in token_address contract.

2
  • Thank you, so i haven't to put this function in the ERC20 contract that is used to create the token but in the other contracts that i want to deploy, true? Commented Jan 21, 2019 at 14:44
  • Yeah, your ERC20 token will already expose balanceOf so you will have everything you need. Commented Jan 21, 2019 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.