Skip to content

Instantly share code, notes, and snippets.

@roynalnaruto
Last active March 9, 2018 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roynalnaruto/a797933084ff96a65adc8e1bac6e3f48 to your computer and use it in GitHub Desktop.
Save roynalnaruto/a797933084ff96a65adc8e1bac6e3f48 to your computer and use it in GitHub Desktop.
Token Swap (swap tokens 1:1 from `old_token_contract` to `new_token_contract`)

Workflow

old_token_contract is ERC20 compliant. new_token_contract is TokenSwap

Token swap contract

import "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";

contract TokenSwap is StandardToken {
  event Swap(address indexed to, uint256 amount);

  address public old_token_contract;
  
  function TokenSwap(address _old) public {
    old_token_contract = _old;
  }
  
  function swapTokens(uint256 amount) public {
    // require: balanceOf msg.sender in old_token_contract is at least "amount"
    // require: allowance of new_token_contract to spend "amount" tokens of msg.sender
    // require: burn old tokens (transferFrom new_token_contract to 0x0)
    // mint new_token_contract "amount" tokens to msg.sender
  }
  
  function mintTokens(address _to, uint256 _amount) internal {
    // add "amount" to totalSupply_ of this token
    // add "amount" to balances[_to]
  }
}
  1. new_token_contract inherits TokenSwap contract

  2. Users approve the new_token_contract to spend their old_token_contract tokens

    approve(new_token_contract_address, amount)

  3. Users call swapTokens in the TokenSwap.sol contract, with the amount they want to swap

    TokenSwap.swapTokens(amount)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment