embark/dapps/tests/app/contracts/token.sol

70 lines
2.1 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;
2018-09-14 18:20:41 +00:00
constructor( 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;
2018-09-14 18:20:41 +00:00
emit Transfer( msg.sender, to, value );
2017-02-10 02:05:45 +00:00
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;
2018-09-14 18:20:41 +00:00
emit Transfer( from, to, value );
2017-02-10 02:05:45 +00:00
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;
2018-09-14 18:20:41 +00:00
emit Approval( msg.sender, spender, value );
2017-02-10 02:05:45 +00:00
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);
}
2018-09-14 19:21:45 +00:00
function isAvailable() public pure returns (bool) {
2018-08-24 13:12:31 +00:00
return false;
2018-08-23 22:00:52 +00:00
}
2017-02-10 02:05:45 +00:00
}