Last active
August 24, 2018 22:20
-
-
Save syuhei176/3ed7721c583ed48c566aacfe96788493 to your computer and use it in GitHub Desktop.
Exitable account state version
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
contract StandardToken is ERC20 { | |
using SafeMath for uint256; | |
uint256 private totalSupply_; | |
account[uint256] private balance; | |
account[address] private allowed; | |
/** | |
* @dev Total number of tokens exist in child chain and root chain | |
*/ | |
function totalSupply() public view returns (uint256) { | |
return totalSupply_; | |
} | |
/** | |
* @dev Gets the balance of the specified address. | |
*/ | |
function balanceOf(address _owner) public view returns (uint256) { | |
return _owner.state.balance | |
} | |
/** | |
* @dev Function to check the amount of tokens that an owner allowed to a spender. | |
*/ | |
function allowance( | |
address _owner, | |
address _spender | |
) | |
public | |
view | |
returns (uint256) | |
{ | |
return _spender.state.allowed[_owner] | |
} | |
/** | |
* @dev Transfer token for a specified address | |
*/ | |
function transfer(address _to, uint256 _value) public returns (bool) { | |
require(_value <= msg.sender.state.balances); | |
require(_to != address(0)); | |
msg.sender.state.balances = msg.sender.state.balances.sub(_value); | |
_to.state.balances = _to.state.balances.add(_value); | |
emit Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
/** | |
* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender. | |
*/ | |
function approve(address _spender, uint256 _value) public returns (bool) { | |
_spender.state.allowed[msg.sender] = _value; | |
emit Approval(msg.sender, _spender, _value); | |
return true; | |
} | |
/** | |
* @dev Transfer tokens from one address to another | |
* @param _from address The address which you want to send tokens from | |
* @param _to address The address which you want to transfer to | |
* @param _value uint256 the amount of tokens to be transferred | |
*/ | |
function transferFrom( | |
address _from, | |
address _to, | |
uint256 _value | |
) | |
public | |
returns (bool) | |
{ | |
require(_value <= _from.state.balance); | |
require(_value <= msg.sender.state.allowed[_from]); | |
require(_to != address(0)); | |
_from.state.balance = _from.state.balance.sub(_value); | |
_to.state.balance = _to.balance.add(_value); | |
msg.sender.state.allowed[_from] = msg.sender.state.allowed[_from].sub(_value); | |
emit Transfer(_from, _to, _value); | |
return true; | |
} | |
/** | |
* @dev mint token to address | |
*/ | |
function _mint(address _account, uint256 _amount) internal { | |
require(_account != 0); | |
totalSupply_ = totalSupply_.add(_amount); | |
_account.state.balance = _account.state.balances.add(_amount); | |
emit Transfer(address(0), _account, _amount); | |
} | |
/** | |
* @dev burn token | |
*/ | |
function _burn(address _account, uint256 _amount) internal { | |
require(_account != 0); | |
require(_amount <= _account.state.balance); | |
totalSupply_ = totalSupply_.sub(_amount); | |
_account.state.balance = _account.state.balance.sub(_amount); | |
emit Transfer(_account, address(0), _amount); | |
} | |
/** | |
* @dev burn from | |
*/ | |
function _burnFrom(address _account, uint256 _amount) internal { | |
require(_amount <= allowed[_account][msg.sender]); | |
// Should https://github.com/OpenZeppelin/zeppelin-solidity/issues/707 be accepted, | |
// this function needs to emit an event with the updated approval. | |
msg.sender.state.allowed[_account] = msg.sender.state.allowed[_account].sub(__amount) | |
_burn(_account, _amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment