-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy path0xe30e02f049957e2a5907589e06ba646fb2c321ba-DRPU-DRP_Utility.sol
926 lines (755 loc) · 24.6 KB
/
0xe30e02f049957e2a5907589e06ba646fb2c321ba-DRPU-DRP_Utility.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
pragma solidity ^0.4.15;
/**
* @title Input validation
*
* - Validates argument length
*
* #created 01/10/2017
* #author Frank Bonnet
*/
contract InputValidator {
/**
* ERC20 Short Address Attack fix
*/
modifier safe_arguments(uint _numArgs) {
assert(msg.data.length == _numArgs * 32 + 4);
_;
}
}
/**
* @title Multi-owned interface
*
* Interface that allows multiple owners
*
* #created 09/10/2017
* #author Frank Bonnet
*/
contract IMultiOwned {
/**
* Returns true if `_account` is an owner
*
* @param _account The address to test against
*/
function isOwner(address _account) constant returns (bool);
/**
* Returns the amount of owners
*
* @return The amount of owners
*/
function getOwnerCount() constant returns (uint);
/**
* Gets the owner at `_index`
*
* @param _index The index of the owner
* @return The address of the owner found at `_index`
*/
function getOwnerAt(uint _index) constant returns (address);
/**
* Adds `_account` as a new owner
*
* @param _account The account to add as an owner
*/
function addOwner(address _account);
/**
* Removes `_account` as an owner
*
* @param _account The account to remove as an owner
*/
function removeOwner(address _account);
}
/**
* @title Multi-owned
*
* Allows multiple owners
*
* #created 09/10/2017
* #author Frank Bonnet
*/
contract MultiOwned is IMultiOwned {
// Owners
mapping (address => uint) private owners;
address[] private ownersIndex;
/**
* Access is restricted to owners only
*/
modifier only_owner() {
require(isOwner(msg.sender));
_;
}
/**
* The publisher is the initial owner
*/
function MultiOwned() {
ownersIndex.push(msg.sender);
owners[msg.sender] = 0;
}
/**
* Returns true if `_account` is the current owner
*
* @param _account The address to test against
*/
function isOwner(address _account) public constant returns (bool) {
return owners[_account] < ownersIndex.length && _account == ownersIndex[owners[_account]];
}
/**
* Returns the amount of owners
*
* @return The amount of owners
*/
function getOwnerCount() public constant returns (uint) {
return ownersIndex.length;
}
/**
* Gets the owner at `_index`
*
* @param _index The index of the owner
* @return The address of the owner found at `_index`
*/
function getOwnerAt(uint _index) public constant returns (address) {
return ownersIndex[_index];
}
/**
* Adds `_account` as a new owner
*
* @param _account The account to add as an owner
*/
function addOwner(address _account) public only_owner {
if (!isOwner(_account)) {
owners[_account] = ownersIndex.push(_account) - 1;
}
}
/**
* Removes `_account` as an owner
*
* @param _account The account to remove as an owner
*/
function removeOwner(address _account) public only_owner {
if (isOwner(_account)) {
uint indexToDelete = owners[_account];
address keyToMove = ownersIndex[ownersIndex.length - 1];
ownersIndex[indexToDelete] = keyToMove;
owners[keyToMove] = indexToDelete;
ownersIndex.length--;
}
}
}
/**
* @title Token retrieve interface
*
* Allows tokens to be retrieved from a contract
*
* #created 29/09/2017
* #author Frank Bonnet
*/
contract ITokenRetriever {
/**
* Extracts tokens from the contract
*
* @param _tokenContract The address of ERC20 compatible token
*/
function retrieveTokens(address _tokenContract);
}
/**
* @title Token retrieve
*
* Allows tokens to be retrieved from a contract
*
* #created 18/10/2017
* #author Frank Bonnet
*/
contract TokenRetriever is ITokenRetriever {
/**
* Extracts tokens from the contract
*
* @param _tokenContract The address of ERC20 compatible token
*/
function retrieveTokens(address _tokenContract) public {
IToken tokenInstance = IToken(_tokenContract);
uint tokenBalance = tokenInstance.balanceOf(this);
if (tokenBalance > 0) {
tokenInstance.transfer(msg.sender, tokenBalance);
}
}
}
/**
* @title Observable interface
*
* Allows observers to register and unregister with the
* implementing smart-contract that is observable
*
* #created 09/10/2017
* #author Frank Bonnet
*/
contract IObservable {
/**
* Returns true if `_account` is a registered observer
*
* @param _account The account to test against
* @return Whether the account is a registered observer
*/
function isObserver(address _account) constant returns (bool);
/**
* Gets the amount of registered observers
*
* @return The amount of registered observers
*/
function getObserverCount() constant returns (uint);
/**
* Gets the observer at `_index`
*
* @param _index The index of the observer
* @return The observers address
*/
function getObserverAtIndex(uint _index) constant returns (address);
/**
* Register `_observer` as an observer
*
* @param _observer The account to add as an observer
*/
function registerObserver(address _observer);
/**
* Unregister `_observer` as an observer
*
* @param _observer The account to remove as an observer
*/
function unregisterObserver(address _observer);
}
/**
* @title Abstract Observable
*
* Allows observers to register and unregister with the the
* implementing smart-contract that is observable
*
* #created 09/10/2017
* #author Frank Bonnet
*/
contract Observable is IObservable {
// Observers
mapping(address => uint) private observers;
address[] private observerIndex;
/**
* Returns true if `_account` is a registered observer
*
* @param _account The account to test against
* @return Whether the account is a registered observer
*/
function isObserver(address _account) public constant returns (bool) {
return observers[_account] < observerIndex.length && _account == observerIndex[observers[_account]];
}
/**
* Gets the amount of registered observers
*
* @return The amount of registered observers
*/
function getObserverCount() public constant returns (uint) {
return observerIndex.length;
}
/**
* Gets the observer at `_index`
*
* @param _index The index of the observer
* @return The observers address
*/
function getObserverAtIndex(uint _index) public constant returns (address) {
return observerIndex[_index];
}
/**
* Register `_observer` as an observer
*
* @param _observer The account to add as an observer
*/
function registerObserver(address _observer) public {
require(canRegisterObserver(_observer));
if (!isObserver(_observer)) {
observers[_observer] = observerIndex.push(_observer) - 1;
}
}
/**
* Unregister `_observer` as an observer
*
* @param _observer The account to remove as an observer
*/
function unregisterObserver(address _observer) public {
require(canUnregisterObserver(_observer));
if (isObserver(_observer)) {
uint indexToDelete = observers[_observer];
address keyToMove = observerIndex[observerIndex.length - 1];
observerIndex[indexToDelete] = keyToMove;
observers[keyToMove] = indexToDelete;
observerIndex.length--;
}
}
/**
* Returns whether it is allowed to register `_observer` by calling
* canRegisterObserver() in the implementing smart-contract
*
* @param _observer The address to register as an observer
* @return Whether the sender is allowed or not
*/
function canRegisterObserver(address _observer) internal constant returns (bool);
/**
* Returns whether it is allowed to unregister `_observer` by calling
* canRegisterObserver() in the implementing smart-contract
*
* @param _observer The address to unregister as an observer
* @return Whether the sender is allowed or not
*/
function canUnregisterObserver(address _observer) internal constant returns (bool);
}
/**
* @title Token observer interface
*
* Allows a token smart-contract to notify observers
* when tokens are received
*
* #created 09/10/2017
* #author Frank Bonnet
*/
contract ITokenObserver {
/**
* Called by the observed token smart-contract in order
* to notify the token observer when tokens are received
*
* @param _from The address that the tokens where send from
* @param _value The amount of tokens that was received
*/
function notifyTokensReceived(address _from, uint _value);
}
/**
* @title ERC20 compatible token interface
*
* - Implements ERC 20 Token standard
* - Implements short address attack fix
*
* #created 29/09/2017
* #author Frank Bonnet
*/
contract IToken {
/**
* Get the total supply of tokens
*
* @return The total supply
*/
function totalSupply() constant returns (uint);
/**
* Get balance of `_owner`
*
* @param _owner The address from which the balance will be retrieved
* @return The balance
*/
function balanceOf(address _owner) constant returns (uint);
/**
* Send `_value` token to `_to` from `msg.sender`
*
* @param _to The address of the recipient
* @param _value The amount of token to be transferred
* @return Whether the transfer was successful or not
*/
function transfer(address _to, uint _value) returns (bool);
/**
* Send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value The amount of token to be transferred
* @return Whether the transfer was successful or not
*/
function transferFrom(address _from, address _to, uint _value) returns (bool);
/**
* `msg.sender` approves `_spender` to spend `_value` tokens
*
* @param _spender The address of the account able to transfer the tokens
* @param _value The amount of tokens to be approved for transfer
* @return Whether the approval was successful or not
*/
function approve(address _spender, uint _value) returns (bool);
/**
* Get the amount of remaining tokens that `_spender` is allowed to spend from `_owner`
*
* @param _owner The address of the account owning tokens
* @param _spender The address of the account able to transfer the tokens
* @return Amount of remaining tokens allowed to spent
*/
function allowance(address _owner, address _spender) constant returns (uint);
}
/**
* @title ERC20 compatible token
*
* - Implements ERC 20 Token standard
* - Implements short address attack fix
*
* #created 29/09/2017
* #author Frank Bonnet
*/
contract Token is IToken, InputValidator {
// Ethereum token standard
string public standard = "Token 0.3";
string public name;
string public symbol;
uint8 public decimals;
// Token state
uint internal totalTokenSupply;
// Token balances
mapping (address => uint) internal balances;
// Token allowances
mapping (address => mapping (address => uint)) internal allowed;
// Events
event Transfer(address indexed _from, address indexed _to, uint _value);
event Approval(address indexed _owner, address indexed _spender, uint _value);
/**
* Construct ERC20 token
*
* @param _name The full token name
* @param _symbol The token symbol (aberration)
* @param _decimals The token precision
*/
function Token(string _name, string _symbol, uint8 _decimals) {
name = _name;
symbol = _symbol;
decimals = _decimals;
balances[msg.sender] = 0;
totalTokenSupply = 0;
}
/**
* Get the total token supply
*
* @return The total supply
*/
function totalSupply() public constant returns (uint) {
return totalTokenSupply;
}
/**
* Get balance of `_owner`
*
* @param _owner The address from which the balance will be retrieved
* @return The balance
*/
function balanceOf(address _owner) public constant returns (uint) {
return balances[_owner];
}
/**
* Send `_value` token to `_to` from `msg.sender`
*
* @param _to The address of the recipient
* @param _value The amount of token to be transferred
* @return Whether the transfer was successful or not
*/
function transfer(address _to, uint _value) public safe_arguments(2) returns (bool) {
// Check if the sender has enough tokens
require(balances[msg.sender] >= _value);
// Check for overflows
require(balances[_to] + _value >= balances[_to]);
// Transfer tokens
balances[msg.sender] -= _value;
balances[_to] += _value;
// Notify listeners
Transfer(msg.sender, _to, _value);
return true;
}
/**
* Send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value The amount of token to be transferred
* @return Whether the transfer was successful or not
*/
function transferFrom(address _from, address _to, uint _value) public safe_arguments(3) returns (bool) {
// Check if the sender has enough
require(balances[_from] >= _value);
// Check for overflows
require(balances[_to] + _value >= balances[_to]);
// Check allowance
require(_value <= allowed[_from][msg.sender]);
// Transfer tokens
balances[_to] += _value;
balances[_from] -= _value;
// Update allowance
allowed[_from][msg.sender] -= _value;
// Notify listeners
Transfer(_from, _to, _value);
return true;
}
/**
* `msg.sender` approves `_spender` to spend `_value` tokens
*
* @param _spender The address of the account able to transfer the tokens
* @param _value The amount of tokens to be approved for transfer
* @return Whether the approval was successful or not
*/
function approve(address _spender, uint _value) public safe_arguments(2) returns (bool) {
// Update allowance
allowed[msg.sender][_spender] = _value;
// Notify listeners
Approval(msg.sender, _spender, _value);
return true;
}
/**
* Get the amount of remaining tokens that `_spender` is allowed to spend from `_owner`
*
* @param _owner The address of the account owning tokens
* @param _spender The address of the account able to transfer the tokens
* @return Amount of remaining tokens allowed to spent
*/
function allowance(address _owner, address _spender) public constant returns (uint) {
return allowed[_owner][_spender];
}
}
/**
* @title ManagedToken interface
*
* Adds the following functionality to the basic ERC20 token
* - Locking
* - Issuing
* - Burning
*
* #created 29/09/2017
* #author Frank Bonnet
*/
contract IManagedToken is IToken {
/**
* Returns true if the token is locked
*
* @return Whether the token is locked
*/
function isLocked() constant returns (bool);
/**
* Locks the token so that the transfering of value is disabled
*
* @return Whether the unlocking was successful or not
*/
function lock() returns (bool);
/**
* Unlocks the token so that the transfering of value is enabled
*
* @return Whether the unlocking was successful or not
*/
function unlock() returns (bool);
/**
* Issues `_value` new tokens to `_to`
*
* @param _to The address to which the tokens will be issued
* @param _value The amount of new tokens to issue
* @return Whether the tokens where sucessfully issued or not
*/
function issue(address _to, uint _value) returns (bool);
/**
* Burns `_value` tokens of `_from`
*
* @param _from The address that owns the tokens to be burned
* @param _value The amount of tokens to be burned
* @return Whether the tokens where sucessfully burned or not
*/
function burn(address _from, uint _value) returns (bool);
}
/**
* @title ManagedToken
*
* Adds the following functionality to the basic ERC20 token
* - Locking
* - Issuing
* - Burning
*
* #created 29/09/2017
* #author Frank Bonnet
*/
contract ManagedToken is IManagedToken, Token, MultiOwned {
// Token state
bool internal locked;
/**
* Allow access only when not locked
*/
modifier only_when_unlocked() {
require(!locked);
_;
}
/**
* Construct managed ERC20 token
*
* @param _name The full token name
* @param _symbol The token symbol (aberration)
* @param _decimals The token precision
* @param _locked Whether the token should be locked initially
*/
function ManagedToken(string _name, string _symbol, uint8 _decimals, bool _locked)
Token(_name, _symbol, _decimals) {
locked = _locked;
}
/**
* Send `_value` token to `_to` from `msg.sender`
*
* @param _to The address of the recipient
* @param _value The amount of token to be transferred
* @return Whether the transfer was successful or not
*/
function transfer(address _to, uint _value) public only_when_unlocked returns (bool) {
return super.transfer(_to, _value);
}
/**
* Send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value The amount of token to be transferred
* @return Whether the transfer was successful or not
*/
function transferFrom(address _from, address _to, uint _value) public only_when_unlocked returns (bool) {
return super.transferFrom(_from, _to, _value);
}
/**
* `msg.sender` approves `_spender` to spend `_value` tokens
*
* @param _spender The address of the account able to transfer the tokens
* @param _value The amount of tokens to be approved for transfer
* @return Whether the approval was successful or not
*/
function approve(address _spender, uint _value) public returns (bool) {
return super.approve(_spender, _value);
}
/**
* Returns true if the token is locked
*
* @return Whether the token is locked
*/
function isLocked() public constant returns (bool) {
return locked;
}
/**
* Locks the token so that the transfering of value is enabled
*
* @return Whether the locking was successful or not
*/
function lock() public only_owner returns (bool) {
locked = true;
return locked;
}
/**
* Unlocks the token so that the transfering of value is enabled
*
* @return Whether the unlocking was successful or not
*/
function unlock() public only_owner returns (bool) {
locked = false;
return !locked;
}
/**
* Issues `_value` new tokens to `_to`
*
* @param _to The address to which the tokens will be issued
* @param _value The amount of new tokens to issue
* @return Whether the approval was successful or not
*/
function issue(address _to, uint _value) public only_owner safe_arguments(2) returns (bool) {
// Check for overflows
require(balances[_to] + _value >= balances[_to]);
// Create tokens
balances[_to] += _value;
totalTokenSupply += _value;
// Notify listeners
Transfer(0, this, _value);
Transfer(this, _to, _value);
return true;
}
/**
* Burns `_value` tokens of `_recipient`
*
* @param _from The address that owns the tokens to be burned
* @param _value The amount of tokens to be burned
* @return Whether the tokens where sucessfully burned or not
*/
function burn(address _from, uint _value) public only_owner safe_arguments(2) returns (bool) {
// Check if the token owner has enough tokens
require(balances[_from] >= _value);
// Check for overflows
require(balances[_from] - _value <= balances[_from]);
// Burn tokens
balances[_from] -= _value;
totalTokenSupply -= _value;
// Notify listeners
Transfer(_from, 0, _value);
return true;
}
}
/**
* @title DRP Utility token (DRPU)
*
* DRPU as indicated by its ‘U’ designation is Dcorp’s utility token for those who are under strict
* compliance within their country of residence, and does not entitle holders to profit sharing.
*
* https://www.dcorp.it/drpu
*
* #created 01/10/2017
* #author Frank Bonnet
*/
contract DRPUToken is ManagedToken, Observable, TokenRetriever {
/**
* Construct the managed utility token
*/
function DRPUToken() ManagedToken("DRP Utility", "DRPU", 8, false) {}
/**
* Returns whether sender is allowed to register `_observer`
*
* @param _observer The address to register as an observer
* @return Whether the sender is allowed or not
*/
function canRegisterObserver(address _observer) internal constant returns (bool) {
return _observer != address(this) && isOwner(msg.sender);
}
/**
* Returns whether sender is allowed to unregister `_observer`
*
* @param _observer The address to unregister as an observer
* @return Whether the sender is allowed or not
*/
function canUnregisterObserver(address _observer) internal constant returns (bool) {
return msg.sender == _observer || isOwner(msg.sender);
}
/**
* Send `_value` token to `_to` from `msg.sender`
* - Notifies registered observers when the observer receives tokens
*
* @param _to The address of the recipient
* @param _value The amount of token to be transferred
* @return Whether the transfer was successful or not
*/
function transfer(address _to, uint _value) public returns (bool) {
bool result = super.transfer(_to, _value);
if (isObserver(_to)) {
ITokenObserver(_to).notifyTokensReceived(msg.sender, _value);
}
return result;
}
/**
* Send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
* - Notifies registered observers when the observer receives tokens
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value The amount of token to be transferred
* @return Whether the transfer was successful or not
*/
function transferFrom(address _from, address _to, uint _value) public returns (bool) {
bool result = super.transferFrom(_from, _to, _value);
if (isObserver(_to)) {
ITokenObserver(_to).notifyTokensReceived(_from, _value);
}
return result;
}
/**
* Failsafe mechanism
*
* Allows the owner to retrieve tokens from the contract that
* might have been send there by accident
*
* @param _tokenContract The address of ERC20 compatible token
*/
function retrieveTokens(address _tokenContract) public only_owner {
super.retrieveTokens(_tokenContract);
}
/**
* Prevents the accidental sending of ether
*/
function () payable {
revert();
}
}