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.