-
Notifications
You must be signed in to change notification settings - Fork 4
/
AssetProxy.sol
614 lines (564 loc) · 18.9 KB
/
AssetProxy.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
pragma solidity 0.5.8;
import './EToken2Interface.sol';
import './AssetInterface.sol';
import './ERC20Interface.sol';
import './AssetProxyInterface.sol';
import '../dependencies/contracts/Bytes32.sol';
import '../dependencies/contracts/ReturnData.sol';
/**
* @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 AssetProxy is ERC20Interface, AssetProxyInterface, Bytes32, ReturnData {
// Assigned EToken2, immutable.
EToken2Interface 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(EToken2Interface _etoken2, string memory _symbol, string memory _name)
public returns(bool)
{
if (address(etoken2) != address(0)) {
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 view returns(AssetInterface) {
return AssetInterface(getVersionFor(msg.sender));
}
/**
* Recovers tokens on proxy contract
*
* @param _asset type of tokens to recover.
* @param _value tokens that will be recovered.
* @param _receiver address where to send recovered tokens.
*
* @return success.
*/
function recoverTokens(ERC20Interface _asset, address _receiver, uint _value)
public onlyAssetOwner() returns(bool) {
return _asset.transfer(_receiver, _value);
}
/**
* Returns asset total supply.
*
* @return asset total supply.
*/
function totalSupply() public view returns(uint) {
return etoken2.totalSupply(etoken2Symbol);
}
/**
* Returns asset balance for a particular holder.
*
* @param _owner holder address.
*
* @return holder balance.
*/
function balanceOf(address _owner) public view 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) public view returns(uint) {
return etoken2.allowance(_from, _spender, etoken2Symbol);
}
/**
* Returns asset decimals.
*
* @return asset decimals.
*/
function decimals() public view 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) public 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 memory _reference)
public 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) public 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 memory _reference)
public 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) public 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 memory _reference)
public 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 memory _reference,
address _sender)
public 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)
public 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 memory _reference)
public 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 memory _reference,
address _sender)
public 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) public 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)
public 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) public onlyEToken2() {
emit 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) public onlyEToken2() {
emit 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 () external payable {
_getAsset()._performGeneric.value(msg.value)(msg.data, msg.sender);
_returnReturnData(true);
}
// Interface functions to allow specifying ICAP addresses as strings.
function transferToICAP(string memory _icap, uint _value) public returns(bool) {
return transferToICAPWithReference(_icap, _value, '');
}
function transferToICAPWithReference(string memory _icap, uint _value, string memory _reference)
public returns(bool) {
return transferToICAPWithReference(_bytes32(_icap), _value, _reference);
}
function transferFromToICAP(address _from, string memory _icap, uint _value)
public returns(bool)
{
return transferFromToICAPWithReference(_from, _icap, _value, '');
}
function transferFromToICAPWithReference(
address _from,
string memory _icap,
uint _value,
string memory _reference)
public returns(bool) {
return transferFromToICAPWithReference(_from, _bytes32(_icap), _value, _reference);
}
/**
* Indicates an upgrade freeze-time start, and the next asset implementation contract.
*/
event UpgradeProposed(address newVersion);
event UpgradePurged(address newVersion);
event UpgradeCommited(address newVersion);
event OptedOut(address sender, address version);
event OptedIn(address sender, address version);
// Current asset implementation contract address.
address internal latestVersion;
// Proposed next asset implementation contract address.
address internal pendingVersion;
// Upgrade freeze-time start.
uint internal pendingVersionTimestamp;
// Timespan for users to review the new implementation and make decision.
uint internal 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) internal 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) public view returns(address) {
return userOptOutVersion[_sender] == address(0) ?
latestVersion : userOptOutVersion[_sender];
}
/**
* Returns current asset implementation contract address.
*
* @return asset implementation contract address.
*/
function getLatestVersion() public view returns(address) {
return latestVersion;
}
/**
* Returns proposed next asset implementation contract address.
*
* @return asset implementation contract address.
*/
function getPendingVersion() public view returns(address) {
return pendingVersion;
}
/**
* Returns upgrade freeze-time start.
*
* @return freeze-time start.
*/
function getPendingVersionTimestamp() public view 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) public onlyAssetOwner() returns(bool) {
// Should not already be in the upgrading process.
if (pendingVersion != address(0)) {
return false;
}
// New version address should be other than 0x0.
if (_newVersion == address(0)) {
return false;
}
// Don't apply freeze-time for the initial setup.
if (latestVersion == address(0)) {
latestVersion = _newVersion;
return true;
}
pendingVersion = _newVersion;
// solhint-disable-next-line not-rely-on-time
pendingVersionTimestamp = now;
emit UpgradeProposed(_newVersion);
return true;
}
/**
* Cancel the pending upgrade process.
*
* Can only be called by current asset owner.
*
* @return success.
*/
function purgeUpgrade() public onlyAssetOwner() returns(bool) {
if (pendingVersion == address(0)) {
return false;
}
emit UpgradePurged(pendingVersion);
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() public returns(bool) {
if (pendingVersion == address(0)) {
return false;
}
// solhint-disable-next-line not-rely-on-time
if (pendingVersionTimestamp + UPGRADE_FREEZE_TIME > now) {
return false;
}
latestVersion = pendingVersion;
delete pendingVersion;
delete pendingVersionTimestamp;
emit UpgradeCommited(latestVersion);
return true;
}
/**
* Disagree with proposed upgrade, and stick with current asset implementation
* until further explicit agreement to upgrade.
*
* @return success.
*/
function optOut() public returns(bool) {
if (userOptOutVersion[msg.sender] != address(0)) {
return false;
}
userOptOutVersion[msg.sender] = latestVersion;
emit OptedOut(msg.sender, latestVersion);
return true;
}
/**
* Implicitly agree to upgrade to current and future asset implementation upgrades,
* until further explicit disagreement.
*
* @return success.
*/
function optIn() public returns(bool) {
delete userOptOutVersion[msg.sender];
emit OptedIn(msg.sender, latestVersion);
return true;
}
// Backwards compatibility.
function multiAsset() public view returns(EToken2Interface) {
return etoken2;
}
}