-
Notifications
You must be signed in to change notification settings - Fork 3
/
transferableToken.sol
180 lines (149 loc) · 5.38 KB
/
transferableToken.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
An ERC20 compliant token that is linked to an external identifier. For exmaple, Meetup.com
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See MIT Licence for further details.
<https://opensource.org/licenses/MIT>.
*/
pragma solidity ^0.4.15;
contract ERC20Token
{
/* State */
// The Total supply of tokens
uint256 totSupply;
/// @return Token symbol
string sym;
string nam;
uint8 public decimals = 0;
// Token ownership mapping
mapping (address => uint256) balances;
// Allowances mapping
mapping (address => mapping (address => uint256)) allowed;
/* Events */
// Triggered when tokens are transferred.
event Transfer(
address indexed from,
address indexed to,
uint256 value);
// Triggered whenever approve(address _spender, uint256 _value) is called.
event Approval(
address indexed owner,
address indexed spender,
uint256 value);
/* Funtions Public */
function symbol() public constant returns (string)
{
return sym;
}
function name() public constant returns (string)
{
return nam;
}
// Using an explicit getter allows for function overloading
function totalSupply() public constant returns (uint256)
{
return totSupply;
}
// Using an explicit getter allows for function overloading
function balanceOf(address holderAddress) public constant returns (uint256 balance)
{
return balances[holderAddress];
}
// Using an explicit getter allows for function overloading
function allowance(address ownerAddress, address spenderAddress) public constant returns (uint256 remaining)
{
return allowed[ownerAddress][spenderAddress];
}
// Send amount amount of tokens to address _to
function transfer(address toAddress, uint256 amount) public returns (bool success)
{
return xfer(msg.sender, toAddress, amount);
}
// Send amount amount of tokens from address _from to address _to
function transferFrom(address fromAddress, address toAddress, uint256 amount) public returns (bool success)
{
require(amount <= allowed[fromAddress][msg.sender]);
allowed[fromAddress][msg.sender] -= amount;
xfer(fromAddress, toAddress, amount);
return true;
}
// Process a transfer internally.
function xfer(address fromAddress, address toAddress, uint amount) internal returns (bool success)
{
require(amount <= balances[fromAddress]);
balances[fromAddress] -= amount;
balances[toAddress] += amount;
Transfer(fromAddress, toAddress, amount);
return true;
}
// Approves a third-party spender
function approve(address spender, uint256 value) returns (bool) {
// To change the approve amount you first have to reduce the addresses`
// allowance to zero by calling `approve(_spender, 0)` if it is not
// already 0 to mitigate the race condition described here:
// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
require((value == 0) || (allowed[msg.sender][spender] == 0));
allowed[msg.sender][spender] = value;
Approval(msg.sender, spender, value);
return true;
}
/**
* approve should be called when allowed[_spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
*/
function increaseApproval (address spender, uint addedValue) returns (bool success)
{
allowed[msg.sender][spender] = allowed[msg.sender][spender] + addedValue;
Approval(msg.sender, spender, allowed[msg.sender][spender]);
return true;
}
function decreaseApproval (address spender, uint subtractedValue) returns (bool success)
{
uint oldValue = allowed[msg.sender][spender];
if (subtractedValue > oldValue) {
allowed[msg.sender][spender] = 0;
} else {
allowed[msg.sender][spender] = oldValue - subtractedValue;
}
Approval(msg.sender, spender, allowed[msg.sender][spender]);
return true;
}
}
contract TransferableMeetupToken is ERC20Token
{
address owner = msg.sender;
function TransferableMeetupToken(string tokenSymbol, string toeknName)
{
sym = tokenSymbol;
nam = toeknName;
}
event Issue(
address indexed toAddress,
uint256 amount,
string externalId,
string reason);
event Redeem(
address indexed fromAddress,
uint256 amount);
function issue(address toAddress, uint amount, string externalId, string reason) public returns (bool)
{
require(owner == msg.sender);
totSupply += amount;
balances[toAddress] += amount;
Issue(toAddress, amount, externalId, reason);
Transfer(0x0, toAddress, amount);
return true;
}
function redeem(uint amount) public returns (bool)
{
require(balances[msg.sender] >= amount);
totSupply -= amount;
balances[msg.sender] -= amount;
Redeem(msg.sender, amount);
Transfer(msg.sender, 0x0, amount);
return true;
}
}