-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy path0xe7775a6e9bcf904eb39da2b68c5efb4f9360e08c-TAAS-TAAS.sol
557 lines (507 loc) · 19.7 KB
/
0xe7775a6e9bcf904eb39da2b68c5efb4f9360e08c-TAAS-TAAS.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
pragma solidity 0.4.8;
contract EToken2 {
function baseUnit(bytes32 _symbol) constant returns(uint8);
function name(bytes32 _symbol) constant returns(string);
function description(bytes32 _symbol) constant returns(string);
function owner(bytes32 _symbol) constant returns(address);
function isOwner(address _owner, bytes32 _symbol) constant returns(bool);
function totalSupply(bytes32 _symbol) constant returns(uint);
function balanceOf(address _holder, bytes32 _symbol) constant returns(uint);
function proxyTransferFromToICAPWithReference(address _from, bytes32 _icap, uint _value, string _reference, address _sender) returns(bool);
function proxyApprove(address _spender, uint _value, bytes32 _symbol, address _sender) returns(bool);
function allowance(address _from, address _spender, bytes32 _symbol) constant returns(uint);
function proxyTransferFromWithReference(address _from, address _to, uint _value, bytes32 _symbol, string _reference, address _sender) returns(bool);
}
contract Asset {
function _performTransferWithReference(address _to, uint _value, string _reference, address _sender) returns(bool);
function _performTransferToICAPWithReference(bytes32 _icap, uint _value, string _reference, address _sender) returns(bool);
function _performApprove(address _spender, uint _value, address _sender) returns(bool);
function _performTransferFromWithReference(address _from, address _to, uint _value, string _reference, address _sender) returns(bool);
function _performTransferFromToICAPWithReference(address _from, bytes32 _icap, uint _value, string _reference, address _sender) returns(bool);
function _performGeneric(bytes _data, address _sender) payable returns(bytes32) {
throw;
}
}
contract ERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed from, address indexed spender, uint256 value);
function totalSupply() constant returns(uint256 supply);
function balanceOf(address _owner) constant returns(uint256 balance);
function transfer(address _to, uint256 _value) returns(bool success);
function transferFrom(address _from, address _to, uint256 _value) returns(bool success);
function approve(address _spender, uint256 _value) returns(bool success);
function allowance(address _owner, address _spender) constant returns(uint256 remaining);
function decimals() constant returns(uint8);
}
contract AssetProxyInterface {
function _forwardApprove(address _spender, uint _value, address _sender) returns(bool);
function _forwardTransferFromWithReference(address _from, address _to, uint _value, string _reference, address _sender) returns(bool);
function _forwardTransferFromToICAPWithReference(address _from, bytes32 _icap, uint _value, string _reference, address _sender) returns(bool);
}
contract Bytes32 {
function _bytes32(string _input) internal constant returns(bytes32 result) {
assembly {
result := mload(add(_input, 32))
}
}
}
/**
* @title EToken2 Asset Proxy.
*
* Proxy implements ERC20 interface and acts as a gateway to a single EToken2 asset.
* Proxy adds etoken2Symbol and caller(sender) when forwarding requests to EToken2.
* Every request that is made by caller first sent to the specific asset implementation
* contract, which then calls back to be forwarded onto EToken2.
*
* Calls flow: Caller ->
* Proxy.func(...) ->
* Asset._performFunc(..., Caller.address) ->
* Proxy._forwardFunc(..., Caller.address) ->
* Platform.proxyFunc(..., symbol, Caller.address)
*
* Generic call flow: Caller ->
* Proxy.unknownFunc(...) ->
* Asset._performGeneric(..., Caller.address) ->
* Asset.unknownFunc(...)
*
* Asset implementation contract is mutable, but each user have an option to stick with
* old implementation, through explicit decision made in timely manner, if he doesn't agree
* with new rules.
* Each user have a possibility to upgrade to latest asset contract implementation, without the
* possibility to rollback.
*
* Note: all the non constant functions return false instead of throwing in case if state change
* didn't happen yet.
*/
contract TAAS is ERC20, AssetProxyInterface, Bytes32 {
// Assigned EToken2, immutable.
EToken2 public etoken2;
// Assigned symbol, immutable.
bytes32 public etoken2Symbol;
// Assigned name, immutable. For UI.
string public name;
string public symbol;
/**
* Sets EToken2 address, assigns symbol and name.
*
* Can be set only once.
*
* @param _etoken2 EToken2 contract address.
* @param _symbol assigned symbol.
* @param _name assigned name.
*
* @return success.
*/
function init(EToken2 _etoken2, string _symbol, string _name) returns(bool) {
if (address(etoken2) != 0x0) {
return false;
}
etoken2 = _etoken2;
etoken2Symbol = _bytes32(_symbol);
name = _name;
symbol = _symbol;
return true;
}
/**
* Only EToken2 is allowed to call.
*/
modifier onlyEToken2() {
if (msg.sender == address(etoken2)) {
_;
}
}
/**
* Only current asset owner is allowed to call.
*/
modifier onlyAssetOwner() {
if (etoken2.isOwner(msg.sender, etoken2Symbol)) {
_;
}
}
/**
* Returns asset implementation contract for current caller.
*
* @return asset implementation contract.
*/
function _getAsset() internal returns(Asset) {
return Asset(getVersionFor(msg.sender));
}
function recoverTokens(uint _value) onlyAssetOwner() returns(bool) {
return this.transferWithReference(msg.sender, _value, 'Tokens recovery');
}
/**
* Returns asset total supply.
*
* @return asset total supply.
*/
function totalSupply() constant returns(uint) {
return etoken2.totalSupply(etoken2Symbol);
}
/**
* Returns asset balance for a particular holder.
*
* @param _owner holder address.
*
* @return holder balance.
*/
function balanceOf(address _owner) constant returns(uint) {
return etoken2.balanceOf(_owner, etoken2Symbol);
}
/**
* Returns asset allowance from one holder to another.
*
* @param _from holder that allowed spending.
* @param _spender holder that is allowed to spend.
*
* @return holder to spender allowance.
*/
function allowance(address _from, address _spender) constant returns(uint) {
return etoken2.allowance(_from, _spender, etoken2Symbol);
}
/**
* Returns asset decimals.
*
* @return asset decimals.
*/
function decimals() constant returns(uint8) {
return etoken2.baseUnit(etoken2Symbol);
}
/**
* Transfers asset balance from the caller to specified receiver.
*
* @param _to holder address to give to.
* @param _value amount to transfer.
*
* @return success.
*/
function transfer(address _to, uint _value) returns(bool) {
return transferWithReference(_to, _value, '');
}
/**
* Transfers asset balance from the caller to specified receiver adding specified comment.
* Resolves asset implementation contract for the caller and forwards there arguments along with
* the caller address.
*
* @param _to holder address to give to.
* @param _value amount to transfer.
* @param _reference transfer comment to be included in a EToken2's Transfer event.
*
* @return success.
*/
function transferWithReference(address _to, uint _value, string _reference) returns(bool) {
return _getAsset()._performTransferWithReference(_to, _value, _reference, msg.sender);
}
/**
* Transfers asset balance from the caller to specified ICAP.
*
* @param _icap recipient ICAP to give to.
* @param _value amount to transfer.
*
* @return success.
*/
function transferToICAP(bytes32 _icap, uint _value) returns(bool) {
return transferToICAPWithReference(_icap, _value, '');
}
/**
* Transfers asset balance from the caller to specified ICAP adding specified comment.
* Resolves asset implementation contract for the caller and forwards there arguments along with
* the caller address.
*
* @param _icap recipient ICAP to give to.
* @param _value amount to transfer.
* @param _reference transfer comment to be included in a EToken2's Transfer event.
*
* @return success.
*/
function transferToICAPWithReference(bytes32 _icap, uint _value, string _reference) returns(bool) {
return _getAsset()._performTransferToICAPWithReference(_icap, _value, _reference, msg.sender);
}
/**
* Prforms allowance transfer of asset balance between holders.
*
* @param _from holder address to take from.
* @param _to holder address to give to.
* @param _value amount to transfer.
*
* @return success.
*/
function transferFrom(address _from, address _to, uint _value) returns(bool) {
return transferFromWithReference(_from, _to, _value, '');
}
/**
* Prforms allowance transfer of asset balance between holders adding specified comment.
* Resolves asset implementation contract for the caller and forwards there arguments along with
* the caller address.
*
* @param _from holder address to take from.
* @param _to holder address to give to.
* @param _value amount to transfer.
* @param _reference transfer comment to be included in a EToken2's Transfer event.
*
* @return success.
*/
function transferFromWithReference(address _from, address _to, uint _value, string _reference) returns(bool) {
return _getAsset()._performTransferFromWithReference(_from, _to, _value, _reference, msg.sender);
}
/**
* Performs transfer call on the EToken2 by the name of specified sender.
*
* Can only be called by asset implementation contract assigned to sender.
*
* @param _from holder address to take from.
* @param _to holder address to give to.
* @param _value amount to transfer.
* @param _reference transfer comment to be included in a EToken2's Transfer event.
* @param _sender initial caller.
*
* @return success.
*/
function _forwardTransferFromWithReference(address _from, address _to, uint _value, string _reference, address _sender) onlyImplementationFor(_sender) returns(bool) {
return etoken2.proxyTransferFromWithReference(_from, _to, _value, etoken2Symbol, _reference, _sender);
}
/**
* Prforms allowance transfer of asset balance between holders.
*
* @param _from holder address to take from.
* @param _icap recipient ICAP address to give to.
* @param _value amount to transfer.
*
* @return success.
*/
function transferFromToICAP(address _from, bytes32 _icap, uint _value) returns(bool) {
return transferFromToICAPWithReference(_from, _icap, _value, '');
}
/**
* Prforms allowance transfer of asset balance between holders adding specified comment.
* Resolves asset implementation contract for the caller and forwards there arguments along with
* the caller address.
*
* @param _from holder address to take from.
* @param _icap recipient ICAP address to give to.
* @param _value amount to transfer.
* @param _reference transfer comment to be included in a EToken2's Transfer event.
*
* @return success.
*/
function transferFromToICAPWithReference(address _from, bytes32 _icap, uint _value, string _reference) returns(bool) {
return _getAsset()._performTransferFromToICAPWithReference(_from, _icap, _value, _reference, msg.sender);
}
/**
* Performs allowance transfer to ICAP call on the EToken2 by the name of specified sender.
*
* Can only be called by asset implementation contract assigned to sender.
*
* @param _from holder address to take from.
* @param _icap recipient ICAP address to give to.
* @param _value amount to transfer.
* @param _reference transfer comment to be included in a EToken2's Transfer event.
* @param _sender initial caller.
*
* @return success.
*/
function _forwardTransferFromToICAPWithReference(address _from, bytes32 _icap, uint _value, string _reference, address _sender) onlyImplementationFor(_sender) returns(bool) {
return etoken2.proxyTransferFromToICAPWithReference(_from, _icap, _value, _reference, _sender);
}
/**
* Sets asset spending allowance for a specified spender.
* Resolves asset implementation contract for the caller and forwards there arguments along with
* the caller address.
*
* @param _spender holder address to set allowance to.
* @param _value amount to allow.
*
* @return success.
*/
function approve(address _spender, uint _value) returns(bool) {
return _getAsset()._performApprove(_spender, _value, msg.sender);
}
/**
* Performs allowance setting call on the EToken2 by the name of specified sender.
*
* Can only be called by asset implementation contract assigned to sender.
*
* @param _spender holder address to set allowance to.
* @param _value amount to allow.
* @param _sender initial caller.
*
* @return success.
*/
function _forwardApprove(address _spender, uint _value, address _sender) onlyImplementationFor(_sender) returns(bool) {
return etoken2.proxyApprove(_spender, _value, etoken2Symbol, _sender);
}
/**
* Emits ERC20 Transfer event on this contract.
*
* Can only be, and, called by assigned EToken2 when asset transfer happens.
*/
function emitTransfer(address _from, address _to, uint _value) onlyEToken2() {
Transfer(_from, _to, _value);
}
/**
* Emits ERC20 Approval event on this contract.
*
* Can only be, and, called by assigned EToken2 when asset allowance set happens.
*/
function emitApprove(address _from, address _spender, uint _value) onlyEToken2() {
Approval(_from, _spender, _value);
}
/**
* Resolves asset implementation contract for the caller and forwards there transaction data,
* along with the value. This allows for proxy interface growth.
*/
function () payable {
bytes32 result = _getAsset()._performGeneric.value(msg.value)(msg.data, msg.sender);
assembly {
mstore(0, result)
return(0, 32)
}
}
/**
* Indicates an upgrade freeze-time start, and the next asset implementation contract.
*/
event UpgradeProposal(address newVersion);
// Current asset implementation contract address.
address latestVersion;
// Proposed next asset implementation contract address.
address pendingVersion;
// Upgrade freeze-time start.
uint pendingVersionTimestamp;
// Timespan for users to review the new implementation and make decision.
uint constant UPGRADE_FREEZE_TIME = 3 days;
// Asset implementation contract address that user decided to stick with.
// 0x0 means that user uses latest version.
mapping(address => address) userOptOutVersion;
/**
* Only asset implementation contract assigned to sender is allowed to call.
*/
modifier onlyImplementationFor(address _sender) {
if (getVersionFor(_sender) == msg.sender) {
_;
}
}
/**
* Returns asset implementation contract address assigned to sender.
*
* @param _sender sender address.
*
* @return asset implementation contract address.
*/
function getVersionFor(address _sender) constant returns(address) {
return userOptOutVersion[_sender] == 0 ? latestVersion : userOptOutVersion[_sender];
}
/**
* Returns current asset implementation contract address.
*
* @return asset implementation contract address.
*/
function getLatestVersion() constant returns(address) {
return latestVersion;
}
/**
* Returns proposed next asset implementation contract address.
*
* @return asset implementation contract address.
*/
function getPendingVersion() constant returns(address) {
return pendingVersion;
}
/**
* Returns upgrade freeze-time start.
*
* @return freeze-time start.
*/
function getPendingVersionTimestamp() constant returns(uint) {
return pendingVersionTimestamp;
}
/**
* Propose next asset implementation contract address.
*
* Can only be called by current asset owner.
*
* Note: freeze-time should not be applied for the initial setup.
*
* @param _newVersion asset implementation contract address.
*
* @return success.
*/
function proposeUpgrade(address _newVersion) onlyAssetOwner() returns(bool) {
// Should not already be in the upgrading process.
if (pendingVersion != 0x0) {
return false;
}
// New version address should be other than 0x0.
if (_newVersion == 0x0) {
return false;
}
// Don't apply freeze-time for the initial setup.
if (latestVersion == 0x0) {
latestVersion = _newVersion;
return true;
}
pendingVersion = _newVersion;
pendingVersionTimestamp = now;
UpgradeProposal(_newVersion);
return true;
}
/**
* Cancel the pending upgrade process.
*
* Can only be called by current asset owner.
*
* @return success.
*/
function purgeUpgrade() onlyAssetOwner() returns(bool) {
if (pendingVersion == 0x0) {
return false;
}
delete pendingVersion;
delete pendingVersionTimestamp;
return true;
}
/**
* Finalize an upgrade process setting new asset implementation contract address.
*
* Can only be called after an upgrade freeze-time.
*
* @return success.
*/
function commitUpgrade() returns(bool) {
if (pendingVersion == 0x0) {
return false;
}
if (pendingVersionTimestamp + UPGRADE_FREEZE_TIME > now) {
return false;
}
latestVersion = pendingVersion;
delete pendingVersion;
delete pendingVersionTimestamp;
return true;
}
/**
* Disagree with proposed upgrade, and stick with current asset implementation
* until further explicit agreement to upgrade.
*
* @return success.
*/
function optOut() returns(bool) {
if (userOptOutVersion[msg.sender] != 0x0) {
return false;
}
userOptOutVersion[msg.sender] = latestVersion;
return true;
}
/**
* Implicitly agree to upgrade to current and future asset implementation upgrades,
* until further explicit disagreement.
*
* @return success.
*/
function optIn() returns(bool) {
delete userOptOutVersion[msg.sender];
return true;
}
// Backwards compatibility.
function multiAsset() constant returns(EToken2) {
return etoken2;
}
}