-
-
Save grepruby/8379b302c8d13734132d7142e1866a14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.11; | |
/** | |
* @title SafeMath | |
* @dev Math operations with safety checks that throw on error | |
*/ | |
library SafeMath { | |
function mul(uint256 a, uint256 b) internal constant returns (uint256) { | |
uint256 c = a * b; | |
require(a == 0 || c / a == b); | |
return c; | |
} | |
function div(uint256 a, uint256 b) internal constant returns (uint256) { | |
// require(b > 0); // Solidity automatically throws when dividing by 0 | |
uint256 c = a / b; | |
// require(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
function sub(uint256 a, uint256 b) internal constant returns (uint256) { | |
require(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal constant returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a); | |
return c; | |
} | |
} | |
/** | |
* @title Ownable | |
* @dev The Ownable contract has an owner address, and provides basic authorization control | |
* functions, this simplifies the implementation of "user permissions". | |
*/ | |
contract Ownable { | |
address public owner; | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
/** | |
* @dev The Ownable constructor sets the original `owner` of the contract to the sender | |
* account. | |
*/ | |
function Ownable() public { | |
owner = msg.sender; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
/** | |
* @dev Allows the current owner to transfer control of the contract to a newOwner. | |
* @param newOwner The address to transfer ownership to. | |
*/ | |
function transferOwnership(address newOwner) public onlyOwner { | |
require(newOwner != address(0)); | |
OwnershipTransferred(owner, newOwner); | |
owner = newOwner; | |
} | |
} | |
interface UpgradeAgent { | |
function upgradeFrom(address _owner, uint256 _value) public; | |
} | |
contract OldToken is Ownable { | |
using SafeMath for uint; | |
address public newTokenAddr; | |
uint256 public totalSupply; | |
uint256 public totalUpgraded; | |
mapping(address => uint256) public balances; | |
event Upgrade(address indexed _from, address indexed newTokenAddr, uint256 _value); | |
function OldToken() public { | |
balances[msg.sender] = 10000; | |
totalSupply = 10000; | |
} | |
function setUpgradeAgent(address _newTokenAddr) public onlyOwner { | |
newTokenAddr = _newTokenAddr; | |
} | |
/** | |
* Allow the token holder to upgrade some of their tokens to a new contract. | |
*/ | |
function upgrade(uint256 value) public { | |
// Validate input value. | |
require(value != 0); | |
require(newTokenAddr != address(0)); | |
balances[msg.sender] = balances[msg.sender].sub(value); | |
// Take tokens out from circulation | |
totalSupply = totalSupply.sub(value); | |
totalUpgraded = totalUpgraded.add(value); | |
// Upgrade agent reissues the tokens | |
UpgradeAgent(newTokenAddr).upgradeFrom(msg.sender, value); | |
Upgrade(msg.sender, newTokenAddr, value); | |
} | |
} | |
contract NewToken { | |
using SafeMath for uint; | |
address public oldTokenAddr; | |
uint256 public totalSupply; | |
mapping(address => uint256) public balances; | |
function NewToken(address _oldTokenAddr) public { | |
oldTokenAddr = _oldTokenAddr; | |
} | |
/** | |
* Allow the new token contract to update the token balances. | |
*/ | |
function upgradeFrom(address _from, uint256 value) public { | |
// Make sure the call is from old token contract | |
require(msg.sender == oldTokenAddr); | |
// Validate input value. | |
balances[_from] = balances[_from].add(value); | |
// Take tokens out from circulation | |
totalSupply = totalSupply.add(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment