-
Notifications
You must be signed in to change notification settings - Fork 7
/
Token.sol
99 lines (82 loc) · 2.5 KB
/
Token.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
pragma solidity ^0.4.18;
contract Token {
uint256 tokenSupply;
uint256 tokenDecimals;
string tokenSymbol;
string tokenName;
mapping (address => uint256) balance;
mapping (address =>
mapping (address => uint256)) m_allowance;
modifier onlyGoodData() {
require(msg.data.length % 32 == 4);
_;
}
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
function Token(string _name, string _symbol, uint256 _decimals, uint256 _supply) public {
tokenSupply = _supply;
tokenDecimals = _decimals;
tokenSymbol = _symbol;
tokenName = _name;
balance[msg.sender] = tokenSupply;
}
function balanceOf(address _account) view public returns (uint) {
return balance[_account];
}
function name() view public returns (string) {
return tokenName;
}
function symbol() view public returns (string) {
return tokenSymbol;
}
function decimals() view public returns (uint) {
return tokenDecimals;
}
function totalSupply() view public returns (uint) {
return tokenSupply;
}
function transfer(address _to, uint256 _value) onlyGoodData() public returns (bool success)
{
return doTransfer(msg.sender, _to, _value);
}
function transferFrom(address _from, address _to, uint256 _value) onlyGoodData() public returns (bool)
{
if (m_allowance[_from][msg.sender] >= _value) {
if (doTransfer(_from, _to, _value)) {
m_allowance[_from][msg.sender] -= _value;
}
return true;
} else {
revert();
}
}
function doTransfer(address _from, address _to, uint _value) internal returns (bool success)
{
if (balance[_from] >= _value && balance[_to] + _value >= balance[_to]) {
if (_value > 0) {
balance[_from] -= _value;
balance[_to] += _value;
Transfer(_from, _to, _value);
}
return true;
} else {
revert();
}
}
function approve(address _spender, uint256 _value) onlyGoodData() public returns (bool success)
{
if (_value > tokenSupply) {
revert();
}
// Avoid "front-running" attack
if (_value > 0 && m_allowance[msg.sender][_spender] > 0) {
revert();
}
m_allowance[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) view public returns (uint256) {
return m_allowance[_owner][_spender];
}
}