Skip to content

Instantly share code, notes, and snippets.

@grepruby
Created March 27, 2018 10:54
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 grepruby/8379b302c8d13734132d7142e1866a14 to your computer and use it in GitHub Desktop.
Save grepruby/8379b302c8d13734132d7142e1866a14 to your computer and use it in GitHub Desktop.
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