-
Notifications
You must be signed in to change notification settings - Fork 6
/
TokenIOLib.sol
1340 lines (1188 loc) · 63.4 KB
/
TokenIOLib.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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pragma solidity 0.4.24;
import "./SafeMath.sol";
import "./TokenIOStorage.sol";
/**
COPYRIGHT 2018 Token, Inc.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@title TokenIOLib
@author Ryan Tate <[email protected]>, Sean Pollock <[email protected]>
@notice This library proxies the TokenIOStorage contract for the interface contract,
allowing the library and the interfaces to remain stateless, and share a universally
available storage contract between interfaces.
*/
library TokenIOLib {
/// @dev all math operating are using SafeMath methods to check for overflow/underflows
using SafeMath for uint;
/// @dev the Data struct uses the Storage contract for stateful setters
struct Data {
TokenIOStorage Storage;
}
/// @notice Not using `Log` prefix for events to be consistent with ERC20 named events;
event Approval(address indexed owner, address indexed spender, uint amount);
event Deposit(string currency, address indexed account, uint amount, string issuerFirm);
event Withdraw(string currency, address indexed account, uint amount, string issuerFirm);
event Transfer(string currency, address indexed from, address indexed to, uint amount, bytes data);
event KYCApproval(address indexed account, bool status, string issuerFirm);
event AccountStatus(address indexed account, bool status, string issuerFirm);
event FxSwap(string tokenASymbol,string tokenBSymbol,uint tokenAValue,uint tokenBValue, uint expiration, bytes32 transactionHash);
event AccountForward(address indexed originalAccount, address indexed forwardedAccount);
event NewAuthority(address indexed authority, string issuerFirm);
/**
* @notice Set the token name for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param tokenName Name of the token contract
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setTokenName(Data storage self, string tokenName) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('token.name', address(this)));
require(
self.Storage.setString(id, tokenName),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set the token symbol for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param tokenSymbol Symbol of the token contract
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setTokenSymbol(Data storage self, string tokenSymbol) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('token.symbol', address(this)));
require(
self.Storage.setString(id, tokenSymbol),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set the token three letter abreviation (TLA) for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param tokenTLA TLA of the token contract
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setTokenTLA(Data storage self, string tokenTLA) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('token.tla', address(this)));
require(
self.Storage.setString(id, tokenTLA),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set the token version for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param tokenVersion Semantic (vMAJOR.MINOR.PATCH | e.g. v0.1.0) version of the token contract
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setTokenVersion(Data storage self, string tokenVersion) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('token.version', address(this)));
require(
self.Storage.setString(id, tokenVersion),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set the token decimals for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @dev This method is not set to the address of the contract, rather is maped to currency
* @dev To derive decimal value, divide amount by 10^decimal representation (e.g. 10132 / 10**2 == 101.32)
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param tokenDecimals Decimal representation of the token contract unit amount
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setTokenDecimals(Data storage self, string currency, uint tokenDecimals) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('token.decimals', currency));
require(
self.Storage.setUint(id, tokenDecimals),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set basis point fee for contract interface
* @dev Transaction fees can be set by the TokenIOFeeContract
* @dev Fees vary by contract interface specified `feeContract`
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param feeBPS Basis points fee for interface contract transactions
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setFeeBPS(Data storage self, uint feeBPS) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('fee.bps', address(this)));
require(
self.Storage.setUint(id, feeBPS),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set minimum fee for contract interface
* @dev Transaction fees can be set by the TokenIOFeeContract
* @dev Fees vary by contract interface specified `feeContract`
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param feeMin Minimum fee for interface contract transactions
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setFeeMin(Data storage self, uint feeMin) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('fee.min', address(this)));
require(
self.Storage.setUint(id, feeMin),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set maximum fee for contract interface
* @dev Transaction fees can be set by the TokenIOFeeContract
* @dev Fees vary by contract interface specified `feeContract`
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param feeMax Maximum fee for interface contract transactions
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setFeeMax(Data storage self, uint feeMax) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('fee.max', address(this)));
require(
self.Storage.setUint(id, feeMax),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set flat fee for contract interface
* @dev Transaction fees can be set by the TokenIOFeeContract
* @dev Fees vary by contract interface specified `feeContract`
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param feeFlat Flat fee for interface contract transactions
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setFeeFlat(Data storage self, uint feeFlat) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('fee.flat', address(this)));
require(
self.Storage.setUint(id, feeFlat),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set fee message for contract interface
* @dev Default fee messages can be set by the TokenIOFeeContract (e.g. "tx_fees")
* @dev Fee messages vary by contract interface specified `feeContract`
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param feeMsg Fee message included in a transaction with fees
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setFeeMsg(Data storage self, bytes feeMsg) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('fee.msg', address(this)));
require(
self.Storage.setBytes(id, feeMsg),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set fee contract for a contract interface
* @dev feeContract must be a TokenIOFeeContract storage approved contract
* @dev Fees vary by contract interface specified `feeContract`
* @dev | This method has an `internal` view
* @dev | This must be called directly from the interface contract
* @param self Internal storage proxying TokenIOStorage contract
* @param feeContract Set the fee contract for `this` contract address interface
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setFeeContract(Data storage self, address feeContract) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('fee.account', address(this)));
require(
self.Storage.setAddress(id, feeContract),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set contract interface associated with a given TokenIO currency symbol (e.g. USDx)
* @dev | This should only be called once from a token interface contract;
* @dev | This method has an `internal` view
* @dev | This method is experimental and may be deprecated/refactored
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setTokenNameSpace(Data storage self, string currency) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('token.namespace', currency));
require(
self.Storage.setAddress(id, address(this)),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set the KYC approval status (true/false) for a given account
* @dev | This method has an `internal` view
* @dev | Every account must be KYC'd to be able to use transfer() & transferFrom() methods
* @dev | To gain approval for an account, register at https://tsm.token.io/sign-up
* @param self Internal storage proxying TokenIOStorage contract
* @param account Ethereum address of account holder
* @param isApproved Boolean (true/false) KYC approval status for a given account
* @param issuerFirm Firm name for issuing KYC approval
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setKYCApproval(Data storage self, address account, bool isApproved, string issuerFirm) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('account.kyc', getForwardedAccount(self, account)));
require(
self.Storage.setBool(id, isApproved),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
/// @dev NOTE: Issuer is logged for setting account KYC status
emit KYCApproval(account, isApproved, issuerFirm);
return true;
}
/**
* @notice Set the global approval status (true/false) for a given account
* @dev | This method has an `internal` view
* @dev | Every account must be permitted to be able to use transfer() & transferFrom() methods
* @dev | To gain approval for an account, register at https://tsm.token.io/sign-up
* @param self Internal storage proxying TokenIOStorage contract
* @param account Ethereum address of account holder
* @param isAllowed Boolean (true/false) global status for a given account
* @param issuerFirm Firm name for issuing approval
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setAccountStatus(Data storage self, address account, bool isAllowed, string issuerFirm) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('account.allowed', getForwardedAccount(self, account)));
require(
self.Storage.setBool(id, isAllowed),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
/// @dev NOTE: Issuer is logged for setting account status
emit AccountStatus(account, isAllowed, issuerFirm);
return true;
}
/**
* @notice Set a forwarded address for an account.
* @dev | This method has an `internal` view
* @dev | Forwarded accounts must be set by an authority in case of account recovery;
* @dev | Additionally, the original owner can set a forwarded account (e.g. add a new device, spouse, dependent, etc)
* @dev | All transactions will be logged under the same KYC information as the original account holder;
* @param self Internal storage proxying TokenIOStorage contract
* @param originalAccount Original registered Ethereum address of the account holder
* @param forwardedAccount Forwarded Ethereum address of the account holder
* @return {"success" : "Returns true when successfully called from another contract"}
*/
function setForwardedAccount(Data storage self, address originalAccount, address forwardedAccount) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('master.account', forwardedAccount));
require(
self.Storage.setAddress(id, originalAccount),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Get the original address for a forwarded account
* @dev | This method has an `internal` view
* @dev | Will return the registered account for the given forwarded account
* @param self Internal storage proxying TokenIOStorage contract
* @param account Ethereum address of account holder
* @return { "registeredAccount" : "Will return the original account of a forwarded account or the account itself if no account found"}
*/
function getForwardedAccount(Data storage self, address account) internal view returns (address registeredAccount) {
bytes32 id = keccak256(abi.encodePacked('master.account', account));
address originalAccount = self.Storage.getAddress(id);
if (originalAccount != 0x0) {
return originalAccount;
} else {
return account;
}
}
/**
* @notice Get KYC approval status for the account holder
* @dev | This method has an `internal` view
* @dev | All forwarded accounts will use the original account's status
* @param self Internal storage proxying TokenIOStorage contract
* @param account Ethereum address of account holder
* @return { "status" : "Returns the KYC approval status for an account holder" }
*/
function getKYCApproval(Data storage self, address account) internal view returns (bool status) {
bytes32 id = keccak256(abi.encodePacked('account.kyc', getForwardedAccount(self, account)));
return self.Storage.getBool(id);
}
/**
* @notice Get global approval status for the account holder
* @dev | This method has an `internal` view
* @dev | All forwarded accounts will use the original account's status
* @param self Internal storage proxying TokenIOStorage contract
* @param account Ethereum address of account holder
* @return { "status" : "Returns the global approval status for an account holder" }
*/
function getAccountStatus(Data storage self, address account) internal view returns (bool status) {
bytes32 id = keccak256(abi.encodePacked('account.allowed', getForwardedAccount(self, account)));
return self.Storage.getBool(id);
}
/**
* @notice Get the contract interface address associated with token symbol
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @return { "contractAddress" : "Returns the contract interface address for a symbol" }
*/
function getTokenNameSpace(Data storage self, string currency) internal view returns (address contractAddress) {
bytes32 id = keccak256(abi.encodePacked('token.namespace', currency));
return self.Storage.getAddress(id);
}
/**
* @notice Get the token name for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return {"tokenName" : "Name of the token contract"}
*/
function getTokenName(Data storage self, address contractAddress) internal view returns (string tokenName) {
bytes32 id = keccak256(abi.encodePacked('token.name', contractAddress));
return self.Storage.getString(id);
}
/**
* @notice Get the token symbol for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return {"tokenSymbol" : "Symbol of the token contract"}
*/
function getTokenSymbol(Data storage self, address contractAddress) internal view returns (string tokenSymbol) {
bytes32 id = keccak256(abi.encodePacked('token.symbol', contractAddress));
return self.Storage.getString(id);
}
/**
* @notice Get the token Three letter abbreviation (TLA) for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return {"tokenTLA" : "TLA of the token contract"}
*/
function getTokenTLA(Data storage self, address contractAddress) internal view returns (string tokenTLA) {
bytes32 id = keccak256(abi.encodePacked('token.tla', contractAddress));
return self.Storage.getString(id);
}
/**
* @notice Get the token version for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return {"tokenVersion" : "Semantic version of the token contract"}
*/
function getTokenVersion(Data storage self, address contractAddress) internal view returns (string) {
bytes32 id = keccak256(abi.encodePacked('token.version', contractAddress));
return self.Storage.getString(id);
}
/**
* @notice Get the token decimals for Token interfaces
* @dev This method must be set by the token interface's setParams() method
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @return {"tokenDecimals" : "Decimals of the token contract"}
*/
function getTokenDecimals(Data storage self, string currency) internal view returns (uint tokenDecimals) {
bytes32 id = keccak256(abi.encodePacked('token.decimals', currency));
return self.Storage.getUint(id);
}
/**
* @notice Get the basis points fee of the contract address; typically TokenIOFeeContract
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return { "feeBps" : "Returns the basis points fees associated with the contract address"}
*/
function getFeeBPS(Data storage self, address contractAddress) internal view returns (uint feeBps) {
bytes32 id = keccak256(abi.encodePacked('fee.bps', contractAddress));
return self.Storage.getUint(id);
}
/**
* @notice Get the minimum fee of the contract address; typically TokenIOFeeContract
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return { "feeMin" : "Returns the minimum fees associated with the contract address"}
*/
function getFeeMin(Data storage self, address contractAddress) internal view returns (uint feeMin) {
bytes32 id = keccak256(abi.encodePacked('fee.min', contractAddress));
return self.Storage.getUint(id);
}
/**
* @notice Get the maximum fee of the contract address; typically TokenIOFeeContract
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return { "feeMax" : "Returns the maximum fees associated with the contract address"}
*/
function getFeeMax(Data storage self, address contractAddress) internal view returns (uint feeMax) {
bytes32 id = keccak256(abi.encodePacked('fee.max', contractAddress));
return self.Storage.getUint(id);
}
/**
* @notice Get the flat fee of the contract address; typically TokenIOFeeContract
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return { "feeFlat" : "Returns the flat fees associated with the contract address"}
*/
function getFeeFlat(Data storage self, address contractAddress) internal view returns (uint feeFlat) {
bytes32 id = keccak256(abi.encodePacked('fee.flat', contractAddress));
return self.Storage.getUint(id);
}
/**
* @notice Get the flat message of the contract address; typically TokenIOFeeContract
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return { "feeMsg" : "Returns the fee message (bytes) associated with the contract address"}
*/
function getFeeMsg(Data storage self, address contractAddress) internal view returns (bytes feeMsg) {
bytes32 id = keccak256(abi.encodePacked('fee.msg', contractAddress));
return self.Storage.getBytes(id);
}
/**
* @notice Set the master fee contract used as the default fee contract when none is provided
* @dev | This method has an `internal` view
* @dev | This value is set in the TokenIOAuthority contract
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return { "success" : "Returns true when successfully called from another contract"}
*/
function setMasterFeeContract(Data storage self, address contractAddress) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('fee.contract.master'));
require(
self.Storage.setAddress(id, contractAddress),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Get the master fee contract set via the TokenIOAuthority contract
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @return { "masterFeeContract" : "Returns the master fee contract set for TSM."}
*/
function getMasterFeeContract(Data storage self) internal view returns (address masterFeeContract) {
bytes32 id = keccak256(abi.encodePacked('fee.contract.master'));
return self.Storage.getAddress(id);
}
/**
* @notice Get the fee contract set for a contract interface
* @dev | This method has an `internal` view
* @dev | Custom fee pricing can be set by assigning a fee contract to transactional contract interfaces
* @dev | If a fee contract has not been set by an interface contract, then the master fee contract will be returned
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the queryable interface
* @return { "feeContract" : "Returns the fee contract associated with a contract interface"}
*/
function getFeeContract(Data storage self, address contractAddress) internal view returns (address feeContract) {
bytes32 id = keccak256(abi.encodePacked('fee.account', contractAddress));
address feeAccount = self.Storage.getAddress(id);
if (feeAccount == 0x0) {
return getMasterFeeContract(self);
} else {
return feeAccount;
}
}
/**
* @notice Get the token supply for a given TokenIO TSM currency symbol (e.g. USDx)
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @return { "supply" : "Returns the token supply of the given currency"}
*/
function getTokenSupply(Data storage self, string currency) internal view returns (uint supply) {
bytes32 id = keccak256(abi.encodePacked('token.supply', currency));
return self.Storage.getUint(id);
}
/**
* @notice Get the token spender allowance for a given account
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param account Ethereum address of account holder
* @param spender Ethereum address of spender
* @return { "allowance" : "Returns the allowance of a given spender for a given account"}
*/
function getTokenAllowance(Data storage self, string currency, address account, address spender) internal view returns (uint allowance) {
bytes32 id = keccak256(abi.encodePacked('token.allowance', currency, getForwardedAccount(self, account), getForwardedAccount(self, spender)));
return self.Storage.getUint(id);
}
/**
* @notice Get the token balance for a given account
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param account Ethereum address of account holder
* @return { "balance" : "Return the balance of a given account for a specified currency"}
*/
function getTokenBalance(Data storage self, string currency, address account) internal view returns (uint balance) {
bytes32 id = keccak256(abi.encodePacked('token.balance', currency, getForwardedAccount(self, account)));
return self.Storage.getUint(id);
}
/**
* @notice Get the frozen token balance for a given account
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param account Ethereum address of account holder
* @return { "frozenBalance" : "Return the frozen balance of a given account for a specified currency"}
*/
function getTokenFrozenBalance(Data storage self, string currency, address account) internal view returns (uint frozenBalance) {
bytes32 id = keccak256(abi.encodePacked('token.frozen', currency, getForwardedAccount(self, account)));
return self.Storage.getUint(id);
}
/**
* @notice Set the frozen token balance for a given account
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param account Ethereum address of account holder
* @param amount Amount of tokens to freeze for account
* @return { "success" : "Return true if successfully called from another contract"}
*/
function setTokenFrozenBalance(Data storage self, string currency, address account, uint amount) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('token.frozen', currency, getForwardedAccount(self, account)));
require(
self.Storage.setUint(id, amount),
"Error: Unable to set storage value. Please ensure contract interface is allowed by the storage contract."
);
return true;
}
/**
* @notice Set the frozen token balance for a given account
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param contractAddress Contract address of the fee contract
* @param amount Transaction value
* @return { "calculatedFees" : "Return the calculated transaction fees for a given amount and fee contract" }
*/
function calculateFees(Data storage self, address contractAddress, uint amount) internal view returns (uint calculatedFees) {
uint maxFee = self.Storage.getUint(keccak256(abi.encodePacked('fee.max', contractAddress)));
uint minFee = self.Storage.getUint(keccak256(abi.encodePacked('fee.min', contractAddress)));
uint bpsFee = self.Storage.getUint(keccak256(abi.encodePacked('fee.bps', contractAddress)));
uint flatFee = self.Storage.getUint(keccak256(abi.encodePacked('fee.flat', contractAddress)));
uint fees = ((amount.mul(bpsFee)).div(10000)).add(flatFee);
if (fees > maxFee) {
return maxFee;
} else if (fees < minFee) {
return minFee;
} else {
return fees;
}
}
/**
* @notice Verified KYC and global status for two accounts and return true or throw if either account is not verified
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param accountA Ethereum address of first account holder to verify
* @param accountB Ethereum address of second account holder to verify
* @return { "verified" : "Returns true if both accounts are successfully verified" }
*/
function verifyAccounts(Data storage self, address accountA, address accountB) internal view returns (bool verified) {
require(
verifyAccount(self, accountA),
"Error: Account is not verified for operation. Please ensure account has been KYC approved."
);
require(
verifyAccount(self, accountB),
"Error: Account is not verified for operation. Please ensure account has been KYC approved."
);
return true;
}
/**
* @notice Verified KYC and global status for a single account and return true or throw if account is not verified
* @dev | This method has an `internal` view
* @param self Internal storage proxying TokenIOStorage contract
* @param account Ethereum address of account holder to verify
* @return { "verified" : "Returns true if account is successfully verified" }
*/
function verifyAccount(Data storage self, address account) internal view returns (bool verified) {
require(
getKYCApproval(self, account),
"Error: Account does not have KYC approval."
);
require(
getAccountStatus(self, account),
"Error: Account status is `false`. Account status must be `true`."
);
return true;
}
/**
* @notice Transfer an amount of currency token from msg.sender account to another specified account
* @dev This function is called by an interface that is accessible directly to the account holder
* @dev | This method has an `internal` view
* @dev | This method uses `forceTransfer()` low-level api
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param to Ethereum address of account to send currency amount to
* @param amount Value of currency to transfer
* @param data Arbitrary bytes data to include with the transaction
* @return { "success" : "Return true if successfully called from another contract" }
*/
function transfer(Data storage self, string currency, address to, uint amount, bytes data) internal returns (bool success) {
require(address(to) != 0x0, "Error: `to` address cannot be null." );
require(amount > 0, "Error: `amount` must be greater than zero");
address feeContract = getFeeContract(self, address(this));
uint fees = calculateFees(self, feeContract, amount);
require(
setAccountSpendingAmount(self, msg.sender, getFxUSDAmount(self, currency, amount)),
"Error: Unable to set spending amount for account.");
require(
forceTransfer(self, currency, msg.sender, to, amount, data),
"Error: Unable to transfer funds to account.");
// @dev transfer fees to fee contract
require(
forceTransfer(self, currency, msg.sender, feeContract, fees, getFeeMsg(self, feeContract)),
"Error: Unable to transfer fees to fee contract.");
return true;
}
/**
* @notice Transfer an amount of currency token from account to another specified account via an approved spender account
* @dev This function is called by an interface that is accessible directly to the account spender
* @dev | This method has an `internal` view
* @dev | Transactions will fail if the spending amount exceeds the daily limit
* @dev | This method uses `forceTransfer()` low-level api
* @dev | This method implements ERC20 transferFrom() method with approved spender behavior
* @dev | msg.sender == spender; `updateAllowance()` reduces approved limit for account spender
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param from Ethereum address of account to send currency amount from
* @param to Ethereum address of account to send currency amount to
* @param amount Value of currency to transfer
* @param data Arbitrary bytes data to include with the transaction
* @return { "success" : "Return true if successfully called from another contract" }
*/
function transferFrom(Data storage self, string currency, address from, address to, uint amount, bytes data) internal returns (bool success) {
require(
address(to) != 0x0,
"Error: `to` address must not be null."
);
address feeContract = getFeeContract(self, address(this));
uint fees = calculateFees(self, feeContract, amount);
/// @dev NOTE: This transaction will fail if the spending amount exceeds the daily limit
require(
setAccountSpendingAmount(self, from, getFxUSDAmount(self, currency, amount)),
"Error: Unable to set account spending amount."
);
/// @dev Attempt to transfer the amount
require(
forceTransfer(self, currency, from, to, amount, data),
"Error: Unable to transfer funds to account."
);
// @dev transfer fees to fee contract
require(
forceTransfer(self, currency, from, feeContract, fees, getFeeMsg(self, feeContract)),
"Error: Unable to transfer fees to fee contract."
);
/// @dev Attempt to update the spender allowance
require(
updateAllowance(self, currency, from, amount),
"Error: Unable to update allowance for spender."
);
return true;
}
/**
* @notice Low-level transfer method
* @dev | This method has an `internal` view
* @dev | This method does not include fees or approved allowances.
* @dev | This method is only for authorized interfaces to use (e.g. TokenIOFX)
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param from Ethereum address of account to send currency amount from
* @param to Ethereum address of account to send currency amount to
* @param amount Value of currency to transfer
* @param data Arbitrary bytes data to include with the transaction
* @return { "success" : "Return true if successfully called from another contract" }
*/
function forceTransfer(Data storage self, string currency, address from, address to, uint amount, bytes data) internal returns (bool success) {
require(
address(to) != 0x0,
"Error: `to` address must not be null."
);
bytes32 id_a = keccak256(abi.encodePacked('token.balance', currency, getForwardedAccount(self, from)));
bytes32 id_b = keccak256(abi.encodePacked('token.balance', currency, getForwardedAccount(self, to)));
require(
self.Storage.setUint(id_a, self.Storage.getUint(id_a).sub(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract."
);
require(
self.Storage.setUint(id_b, self.Storage.getUint(id_b).add(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract."
);
emit Transfer(currency, from, to, amount, data);
return true;
}
/**
* @notice Low-level method to update spender allowance for account
* @dev | This method is called inside the `transferFrom()` method
* @dev | msg.sender == spender address
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param account Ethereum address of account holder
* @param amount Value to reduce allowance by (i.e. the amount spent)
* @return { "success" : "Return true if successfully called from another contract" }
*/
function updateAllowance(Data storage self, string currency, address account, uint amount) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('token.allowance', currency, getForwardedAccount(self, account), getForwardedAccount(self, msg.sender)));
require(
self.Storage.setUint(id, self.Storage.getUint(id).sub(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract."
);
return true;
}
/**
* @notice Low-level method to set the allowance for a spender
* @dev | This method is called inside the `approve()` ERC20 method
* @dev | msg.sender == account holder
* @param self Internal storage proxying TokenIOStorage contract
* @param spender Ethereum address of account spender
* @param amount Value to set for spender allowance
* @return { "success" : "Return true if successfully called from another contract" }
*/
function approveAllowance(Data storage self, address spender, uint amount) internal returns (bool success) {
require(spender != 0x0,
"Error: `spender` address cannot be null.");
string memory currency = getTokenSymbol(self, address(this));
require(
getTokenFrozenBalance(self, currency, getForwardedAccount(self, spender)) == 0,
"Error: Spender must not have a frozen balance directly");
bytes32 id_a = keccak256(abi.encodePacked('token.allowance', currency, getForwardedAccount(self, msg.sender), getForwardedAccount(self, spender)));
bytes32 id_b = keccak256(abi.encodePacked('token.balance', currency, getForwardedAccount(self, msg.sender)));
require(
self.Storage.getUint(id_a) == 0 || amount == 0,
"Error: Allowance must be zero (0) before setting an updated allowance for spender.");
require(
self.Storage.getUint(id_b) >= amount,
"Error: Allowance cannot exceed msg.sender token balance.");
require(
self.Storage.setUint(id_a, amount),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
emit Approval(msg.sender, spender, amount);
return true;
}
/**
* @notice Deposit an amount of currency into the Ethereum account holder
* @dev | The total supply of the token increases only when new funds are deposited 1:1
* @dev | This method should only be called by authorized issuer firms
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param account Ethereum address of account holder to deposit funds for
* @param amount Value of currency to deposit for account
* @param issuerFirm Name of the issuing firm authorizing the deposit
* @return { "success" : "Return true if successfully called from another contract" }
*/
function deposit(Data storage self, string currency, address account, uint amount, string issuerFirm) internal returns (bool success) {
bytes32 id_a = keccak256(abi.encodePacked('token.balance', currency, getForwardedAccount(self, account)));
bytes32 id_b = keccak256(abi.encodePacked('token.issued', currency, issuerFirm));
bytes32 id_c = keccak256(abi.encodePacked('token.supply', currency));
require(self.Storage.setUint(id_a, self.Storage.getUint(id_a).add(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
require(self.Storage.setUint(id_b, self.Storage.getUint(id_b).add(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
require(self.Storage.setUint(id_c, self.Storage.getUint(id_c).add(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
emit Deposit(currency, account, amount, issuerFirm);
return true;
}
/**
* @notice Withdraw an amount of currency from the Ethereum account holder
* @dev | The total supply of the token decreases only when new funds are withdrawn 1:1
* @dev | This method should only be called by authorized issuer firms
* @param self Internal storage proxying TokenIOStorage contract
* @param currency Currency symbol of the token (e.g. USDx, JYPx, GBPx)
* @param account Ethereum address of account holder to deposit funds for
* @param amount Value of currency to withdraw for account
* @param issuerFirm Name of the issuing firm authorizing the withdraw
* @return { "success" : "Return true if successfully called from another contract" }
*/
function withdraw(Data storage self, string currency, address account, uint amount, string issuerFirm) internal returns (bool success) {
bytes32 id_a = keccak256(abi.encodePacked('token.balance', currency, getForwardedAccount(self, account)));
bytes32 id_b = keccak256(abi.encodePacked('token.issued', currency, issuerFirm)); // possible for issuer to go negative
bytes32 id_c = keccak256(abi.encodePacked('token.supply', currency));
require(
self.Storage.setUint(id_a, self.Storage.getUint(id_a).sub(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
require(
self.Storage.setUint(id_b, self.Storage.getUint(id_b).sub(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
require(
self.Storage.setUint(id_c, self.Storage.getUint(id_c).sub(amount)),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
emit Withdraw(currency, account, amount, issuerFirm);
return true;
}
/**
* @notice Method for setting a registered issuer firm
* @dev | Only Token, Inc. and other authorized institutions may set a registered firm
* @dev | The TokenIOAuthority.sol interface wraps this method
* @dev | If the registered firm is unapproved; all authorized addresses of that firm will also be unapproved
* @param self Internal storage proxying TokenIOStorage contract
* @param issuerFirm Name of the firm to be registered
* @param approved Approval status to set for the firm (true/false)
* @return { "success" : "Return true if successfully called from another contract" }
*/
function setRegisteredFirm(Data storage self, string issuerFirm, bool approved) internal returns (bool success) {
bytes32 id = keccak256(abi.encodePacked('registered.firm', issuerFirm));
require(
self.Storage.setBool(id, approved),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract."
);
return true;
}
/**
* @notice Method for setting a registered issuer firm authority
* @dev | Only Token, Inc. and other approved institutions may set a registered firm
* @dev | The TokenIOAuthority.sol interface wraps this method
* @dev | Authority can only be set for a registered issuer firm
* @param self Internal storage proxying TokenIOStorage contract
* @param issuerFirm Name of the firm to be registered to authority
* @param authorityAddress Ethereum address of the firm authority to be approved
* @param approved Approval status to set for the firm authority (true/false)
* @return { "success" : "Return true if successfully called from another contract" }
*/
function setRegisteredAuthority(Data storage self, string issuerFirm, address authorityAddress, bool approved) internal returns (bool success) {
require(
isRegisteredFirm(self, issuerFirm),
"Error: `issuerFirm` must be registered.");
bytes32 id_a = keccak256(abi.encodePacked('registered.authority', issuerFirm, authorityAddress));
bytes32 id_b = keccak256(abi.encodePacked('registered.authority.firm', authorityAddress));
require(
self.Storage.setBool(id_a, approved),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
require(
self.Storage.setString(id_b, issuerFirm),
"Error: Unable to set storage value. Please ensure contract has allowed permissions with storage contract.");
return true;
}
/**
* @notice Get the issuer firm registered to the authority Ethereum address
* @dev | Only one firm can be registered per authority
* @param self Internal storage proxying TokenIOStorage contract
* @param authorityAddress Ethereum address of the firm authority to query
* @return { "issuerFirm" : "Name of the firm registered to authority" }
*/
function getFirmFromAuthority(Data storage self, address authorityAddress) internal view returns (string issuerFirm) {
bytes32 id = keccak256(abi.encodePacked('registered.authority.firm', getForwardedAccount(self, authorityAddress)));
return self.Storage.getString(id);
}
/**