embark-area-51/test_app/app/contracts/token.sol

67 lines
2.0 KiB
Solidity
Raw Normal View History

2017-02-10 02:05:45 +00:00
// https://github.com/nexusdev/erc20/blob/master/contracts/base.sol
2017-10-22 13:38:54 +00:00
pragma solidity ^0.4.17;
2017-02-10 02:05:45 +00:00
contract Token {
event Transfer(address indexed from, address indexed to, uint value);
event Approval( address indexed owner, address indexed spender, uint value);
mapping( address => uint ) _balances;
mapping( address => mapping( address => uint ) ) _approvals;
uint public _supply;
//uint public _supply2;
2017-10-21 19:01:09 +00:00
function Token( uint initial_balance ) public {
2017-02-10 02:05:45 +00:00
_balances[msg.sender] = initial_balance;
_supply = initial_balance;
}
2017-10-21 19:01:09 +00:00
function totalSupply() public constant returns (uint supply) {
2017-02-10 02:05:45 +00:00
return _supply;
}
2017-10-21 19:01:09 +00:00
function balanceOf( address who ) public constant returns (uint value) {
2017-02-10 02:05:45 +00:00
return _balances[who];
}
2017-10-21 19:01:09 +00:00
function transfer( address to, uint value) public returns (bool ok) {
2017-02-10 02:05:45 +00:00
if( _balances[msg.sender] < value ) {
2017-10-21 19:01:09 +00:00
revert();
2017-02-10 02:05:45 +00:00
}
if( !safeToAdd(_balances[to], value) ) {
2017-10-21 19:01:09 +00:00
revert();
2017-02-10 02:05:45 +00:00
}
_balances[msg.sender] -= value;
_balances[to] += value;
Transfer( msg.sender, to, value );
return true;
}
2017-10-21 19:01:09 +00:00
function transferFrom( address from, address to, uint value) public returns (bool ok) {
2017-02-10 02:05:45 +00:00
// if you don't have enough balance, throw
if( _balances[from] < value ) {
2017-10-21 19:01:09 +00:00
revert();
2017-02-10 02:05:45 +00:00
}
// if you don't have approval, throw
if( _approvals[from][msg.sender] < value ) {
2017-10-21 19:01:09 +00:00
revert();
2017-02-10 02:05:45 +00:00
}
if( !safeToAdd(_balances[to], value) ) {
2017-10-21 19:01:09 +00:00
revert();
2017-02-10 02:05:45 +00:00
}
// transfer and return true
_approvals[from][msg.sender] -= value;
_balances[from] -= value;
_balances[to] += value;
Transfer( from, to, value );
return true;
}
2017-10-21 19:01:09 +00:00
function approve(address spender, uint value) public returns (bool ok) {
2017-02-10 02:05:45 +00:00
// TODO: should increase instead
_approvals[msg.sender][spender] = value;
Approval( msg.sender, spender, value );
return true;
}
2017-10-21 19:01:09 +00:00
function allowance(address owner, address spender) public constant returns (uint _allowance) {
2017-02-10 02:05:45 +00:00
return _approvals[owner][spender];
}
2017-10-21 19:01:09 +00:00
function safeToAdd(uint a, uint b) internal pure returns (bool) {
2017-02-10 02:05:45 +00:00
return (a + b >= a);
}
}