-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUtilityBrandedToken.sol
360 lines (312 loc) · 9.58 KB
/
UtilityBrandedToken.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
pragma solidity ^0.5.0;
// Copyright 2018 OpenST Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import "./UtilityTokenInterface.sol";
import "./Internal.sol";
import "./EIP20Token.sol";
import "./CoGatewayUtilityTokenInterface.sol";
/**
* @title UtilityBrandedToken contract.
*
* @notice UtilityBrandedToken is an EIP20 token which implements
* UtilityTokenInterface.
*
* @dev UtilityBrandedToken are designed to be used within a decentralised
* application and support increaseSupply and decreaseSupply of tokens.
*/
contract UtilityBrandedToken is EIP20Token, UtilityTokenInterface, Internal {
/* Events */
/** Emitted whenever a CoGateway address is set. */
event CoGatewaySet(address _coGateway);
/* Storage */
/** Address of BrandedToken in origin chain. */
EIP20Interface public brandedToken;
/**
* Address of CoGateway contract. It ensures that increaseSupply and
* decreaseSupply is done from coGateway.
*/
address public coGateway;
/* Modifiers */
/** Checks that msg.sender is coGateway address. */
modifier onlyCoGateway() {
require(
msg.sender == coGateway,
"Only CoGateway can call the function."
);
_;
}
/* Special Functions */
/**
* @notice Contract constructor.
*
* @dev Creates an EIP20Token contract with arguments passed in the
* contract constructor.
*
* @param _token Address of branded token on origin chain.
* It acts as an identifier.
* @param _symbol Symbol of the token.
* @param _name Name of the token.
* @param _decimals Decimal places of the token.
* @param _organization Address of the Organization contract.
*/
constructor(
EIP20Interface _token,
string memory _symbol,
string memory _name,
uint8 _decimals,
OrganizationInterface _organization
)
public
Internal(_organization)
EIP20Token(_symbol, _name, _decimals)
{
require(
address(_token) != address(0),
"Token address is null."
);
brandedToken = _token;
}
/* External functions */
/**
* @notice Increases the total token supply. Also, adds the number of
* tokens to the beneficiary balance.The parameters _account
* and _amount should not be zero. This check is added in function
* increaseSupplyInternal.
*
* @dev Function requires:
* - it should only be called by coGateway address
*
* @param _beneficiary Account address for which the balance will be increased.
* @param _amount Amount of tokens.
*
* @return True if increase supply is successful, false otherwise.
*/
function increaseSupply(
address _beneficiary,
uint256 _amount
)
external
onlyCoGateway
returns (bool success_)
{
require(
isInternalActor[_beneficiary],
"Beneficiary is not an internal actor."
);
success_ = increaseSupplyInternal(_beneficiary, _amount);
}
/**
* @notice Sets the CoGateway contract address.
*
* @dev Function requires:
* - Caller must be a whitelisted worker
* - coGateway is required to be address(0)
* - coGateway.utilityToken must be equal to this contract address
*
* @param _coGateway CoGateway contract address.
*/
function setCoGateway(address _coGateway)
external
onlyOrganization
returns (bool success_)
{
require(
coGateway == address(0),
"CoGateway address already set."
);
require(
_coGateway != address(0),
"CoGateway address should not be zero."
);
require(
CoGatewayUtilityTokenInterface(_coGateway).utilityToken() ==
address(this),
"CoGateway.utilityToken is required to be UBT address."
);
coGateway = _coGateway;
emit CoGatewaySet(coGateway);
success_ = true;
}
/**
* @notice Decreases the token supply.The parameters _amount should not be
* zero. This check is added in function decreaseSupplyInternal.
*
* @dev Function requires:
* - it should only be called by coGateway address
*
* @param _amount Amount of tokens.
*
* @return True if decrease supply is successful, false otherwise.
*/
function decreaseSupply(
uint256 _amount
)
external
onlyCoGateway
returns (bool success_)
{
success_ = decreaseSupplyInternal(_amount);
}
/* Public functions */
/**
* @notice Public function transfer.
*
* @dev Function requires:
* - _to address is an internal actor
*
* @param _to Address to which BT needs to transfer.
* @param _value Number of BTs that needs to transfer.
*
* @return Success/failure status of transfer.
*/
function transfer(
address _to,
uint256 _value
)
public
returns (bool)
{
require(
isInternalActor[_to],
"To address is not an internal actor."
);
return super.transfer(_to, _value);
}
/**
* @notice Public function transferFrom.
*
* @dev Function requires:
* - _to address is an internal actor
*
* @param _from Address from which BT needs to transfer.
* @param _to Address to which BT needs to transfer.
* @param _value Number of BTs that needs to transfer.
*
* @return Success/failure status of transferFrom.
*/
function transferFrom(
address _from,
address _to,
uint256 _value
)
public
returns (bool)
{
require(
isInternalActor[_to],
"To address is not an internal actor."
);
return super.transferFrom(_from, _to, _value);
}
/**
* @notice Public function approve.
*
* @dev It only allows approval to internal actors.
*
* @param _spender Address to which msg.sender is approving.
* @param _value Number of BTs to be approved.
*
* @return Success/failure status of approve.
*/
function approve(
address _spender,
uint256 _value
)
public
returns (bool)
{
require(
isInternalActor[_spender],
"Spender is not an internal actor."
);
return super.approve(_spender, _value);
}
/* Internal functions. */
/**
* @notice Internal function to increases the total token supply. Adds
* number of tokens to beneficiary balance and increases the total
* token supply.
*
* @dev Function requires:
* - _beneficiary address should not be zero
* - _amount should be greater than zero
*
* @param _beneficiary Account address for which the balance will be increased.
* @param _amount Amount of tokens.
*
* @return success_ `true` if increase supply is successful, false otherwise.
*/
function increaseSupplyInternal(
address _beneficiary,
uint256 _amount
)
internal
returns (bool success_)
{
require(
_beneficiary != address(0),
"Beneficiary address should not be zero."
);
require(
_amount > 0,
"Amount should be greater than zero."
);
// Increase the balance of the _account
balances[_beneficiary] = balances[_beneficiary].add(_amount);
totalTokenSupply = totalTokenSupply.add(_amount);
/*
* Creation of the new tokens should trigger a Transfer event with
* _from as 0x0.
*/
emit Transfer(address(0), _beneficiary, _amount);
success_ = true;
}
/**
* @notice Internal function to decreases the token supply. Decreases the
* token balance from the msg.sender address and decreases the
* total token supply count.
*
* @dev Function requires:
* - _amount should be greater than zero
*
* @param _amount Amount of tokens.
*
* @return success_ `true` if decrease supply is successful, false otherwise.
*/
function decreaseSupplyInternal(
uint256 _amount
)
internal
returns (bool success_)
{
require(
_amount > 0,
"Amount should be greater than zero."
);
address sender = msg.sender;
require(
balances[sender] >= _amount,
"Insufficient balance."
);
// Decrease the balance of the msg.sender account.
balances[sender] = balances[sender].sub(_amount);
totalTokenSupply = totalTokenSupply.sub(_amount);
/*
* Burning of the tokens should trigger a Transfer event with _to
* as 0x0.
*/
emit Transfer(sender, address(0), _amount);
success_ = true;
}
}