I have a functioning contract that uses Ether as payment for functions and pays out ether based on actions. Is there any good documentation for converting it to use a given token?

For instance below, how would this work with tokens?

pragma solidity ^0.4.13;

contract Test{
  address public party1;
  address public party2;
  enum SState {zero,one,two,three}
  SState public currentState;

  modifier onlyState(SState expectedState) {require(expectedState == currentState);_;}
  function Test() {
      currentState = SState.zero;
  function pay1() payable onlyState(SState.zero) {
    require(msg.value == 1000000000000000000);
    party1 = msg.sender;
    currentState = SState.one;
   function pay2() payable onlyState(SState.one){
    require(msg.value == 1000000000000000000);
    party2 = msg.sender;
    currentState = SState.two;

  function finish(bool _winner) onlyState(SState.two){
    currentState = SState.three;
    if (_winner){
    else {


How would this work with a specific token:

First, throw this little blurb above your main contract. It tells the main contact how it can interact with an (ERC20) token.

contract ERC20 {
    function totalSupply() view returns (uint totalSupply);
    function balanceOf(address _owner) view returns (uint balance);
    function transfer(address _to, uint _value) returns (bool success);
    function transferFrom(address _from, address _to, uint _value) returns (bool success);
    function approve(address _spender, uint _value) returns (bool success);
    function allowance(address _owner, address _spender) pure returns (uint remaining);
    event Transfer(address indexed _from, address indexed _to, uint _value);
    event Approval(address indexed _owner, address indexed _spender, uint _value);

Secondly, you need to initialize your contract with something like this:

ERC20 myToken;

function Test(){
    myToken = ERC20(0xaabc...token address...112);

Now, you can call something like

bool successfulTransfer = myToken.transfer(player1Addr, value of transfer);
if (! successfulTransfer) revert();
  • 1
    Hey Alex, I figured it out a while ago, but thanks for the answer..enjoy the upvote and correct answer lol – thefett Nov 30 '17 at 2:34

A token is simply a representation of ether that has been sent to a smart contract from a particular address. The smart contract keeps track of the sent ether by issuing a token. The token is an entry in a map mapping addresses to token balances.

The transfer function of a token contract then simply removes the transferred tokens from the currently owning account to the new owner account.

The contract's creates new tokens by confirming that ether has been sent, and then increasing that account's balance by the amount of tokens that much ether represents. It does this by entering the balance at the sending address in the map.

Search for ERC20 token which is Ethereum's standardized interface for tokens. Most examples of an ERC20 smart contract, will probably have a map mapping accounts addresses to token balances.

  • Thanks for the response Thomas. I think I have that part. But I know that you have to link a contract to a specific token, and then use the approve/transfer methodology, so I was just wondering if best practices/full on contract examples were actually available for people who wanted to use tokens rather than just issue them. – thefett Aug 2 '17 at 1:38
  • In my understanding, the contract implements the token perhaps by deriving from it, or maybe storing the address of a separate token contract inside of itself and calling into it. Your example does neither of those. Check out the source of how any of the popular tokens work. Sorry if this doesn't help. Maybe I'm not understanding. – Thomas Jay Rush Aug 2 '17 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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