|
| 1 | +// https://github.com/nexusdev/erc20/blob/master/contracts/base.sol |
| 2 | + |
| 3 | +pragma solidity ^0.4.2; |
| 4 | +contract Token { |
| 5 | + |
| 6 | + event Transfer(address indexed from, address indexed to, uint value); |
| 7 | + event Approval( address indexed owner, address indexed spender, uint value); |
| 8 | + |
| 9 | + mapping( address => uint ) _balances; |
| 10 | + mapping( address => mapping( address => uint ) ) _approvals; |
| 11 | + uint public _supply; |
| 12 | + //uint public _supply2; |
| 13 | + function Token( uint initial_balance ) { |
| 14 | + _balances[msg.sender] = initial_balance; |
| 15 | + _supply = initial_balance; |
| 16 | + } |
| 17 | + function totalSupply() constant returns (uint supply) { |
| 18 | + return _supply; |
| 19 | + } |
| 20 | + function balanceOf( address who ) constant returns (uint value) { |
| 21 | + return _balances[who]; |
| 22 | + } |
| 23 | + function transfer( address to, uint value) returns (bool ok) { |
| 24 | + if( _balances[msg.sender] < value ) { |
| 25 | + throw; |
| 26 | + } |
| 27 | + if( !safeToAdd(_balances[to], value) ) { |
| 28 | + throw; |
| 29 | + } |
| 30 | + _balances[msg.sender] -= value; |
| 31 | + _balances[to] += value; |
| 32 | + Transfer( msg.sender, to, value ); |
| 33 | + return true; |
| 34 | + } |
| 35 | + function transferFrom( address from, address to, uint value) returns (bool ok) { |
| 36 | + // if you don't have enough balance, throw |
| 37 | + if( _balances[from] < value ) { |
| 38 | + throw; |
| 39 | + } |
| 40 | + // if you don't have approval, throw |
| 41 | + if( _approvals[from][msg.sender] < value ) { |
| 42 | + throw; |
| 43 | + } |
| 44 | + if( !safeToAdd(_balances[to], value) ) { |
| 45 | + throw; |
| 46 | + } |
| 47 | + // transfer and return true |
| 48 | + _approvals[from][msg.sender] -= value; |
| 49 | + _balances[from] -= value; |
| 50 | + _balances[to] += value; |
| 51 | + Transfer( from, to, value ); |
| 52 | + return true; |
| 53 | + } |
| 54 | + function approve(address spender, uint value) returns (bool ok) { |
| 55 | + // TODO: should increase instead |
| 56 | + _approvals[msg.sender][spender] = value; |
| 57 | + Approval( msg.sender, spender, value ); |
| 58 | + return true; |
| 59 | + } |
| 60 | + function allowance(address owner, address spender) constant returns (uint _allowance) { |
| 61 | + return _approvals[owner][spender]; |
| 62 | + } |
| 63 | + function safeToAdd(uint a, uint b) internal returns (bool) { |
| 64 | + return (a + b >= a); |
| 65 | + } |
| 66 | +} |
0 commit comments