This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MarketRegistry.sol
1245 lines (1151 loc) · 41.5 KB
/
MarketRegistry.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Contracts
import "./EAS/TellerAS.sol";
import "./EAS/TellerASResolver.sol";
//must continue to use this so storage slots are not broken
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts/utils/Context.sol";
// Interfaces
import "./interfaces/IMarketRegistry.sol";
// Libraries
import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import { PaymentType } from "./libraries/V2Calculations.sol";
contract MarketRegistry is
IMarketRegistry,
Initializable,
Context,
TellerASResolver
{
using EnumerableSet for EnumerableSet.AddressSet;
/** Constant Variables **/
uint256 public constant CURRENT_CODE_VERSION = 8;
/* Storage Variables */
struct Marketplace {
address owner;
string metadataURI;
uint16 marketplaceFeePercent; // 10000 is 100%
bool lenderAttestationRequired;
EnumerableSet.AddressSet verifiedLendersForMarket;
mapping(address => bytes32) lenderAttestationIds;
uint32 paymentCycleDuration; // unix time (seconds)
uint32 paymentDefaultDuration; //unix time
uint32 bidExpirationTime; //unix time
bool borrowerAttestationRequired;
EnumerableSet.AddressSet verifiedBorrowersForMarket;
mapping(address => bytes32) borrowerAttestationIds;
address feeRecipient;
PaymentType paymentType;
PaymentCycleType paymentCycleType;
}
bytes32 public lenderAttestationSchemaId;
mapping(uint256 => Marketplace) internal markets;
mapping(bytes32 => uint256) internal __uriToId; //DEPRECATED
uint256 public marketCount;
bytes32 private _attestingSchemaId;
bytes32 public borrowerAttestationSchemaId;
uint256 public version;
mapping(uint256 => bool) private marketIsClosed;
TellerAS public tellerAS;
/* Modifiers */
modifier ownsMarket(uint256 _marketId) {
require(_getMarketOwner(_marketId) == _msgSender(), "Not the owner");
_;
}
modifier withAttestingSchema(bytes32 schemaId) {
_attestingSchemaId = schemaId;
_;
_attestingSchemaId = bytes32(0);
}
/* Events */
event MarketCreated(address indexed owner, uint256 marketId);
event SetMarketURI(uint256 marketId, string uri);
event SetPaymentCycleDuration(uint256 marketId, uint32 duration); // DEPRECATED - used for subgraph reference
event SetPaymentCycle(
uint256 marketId,
PaymentCycleType paymentCycleType,
uint32 value
);
event SetPaymentDefaultDuration(uint256 marketId, uint32 duration);
event SetBidExpirationTime(uint256 marketId, uint32 duration);
event SetMarketFee(uint256 marketId, uint16 feePct);
event LenderAttestation(uint256 marketId, address lender);
event BorrowerAttestation(uint256 marketId, address borrower);
event LenderRevocation(uint256 marketId, address lender);
event BorrowerRevocation(uint256 marketId, address borrower);
event MarketClosed(uint256 marketId);
event LenderExitMarket(uint256 marketId, address lender);
event BorrowerExitMarket(uint256 marketId, address borrower);
event SetMarketOwner(uint256 marketId, address newOwner);
event SetMarketFeeRecipient(uint256 marketId, address newRecipient);
event SetMarketLenderAttestation(uint256 marketId, bool required);
event SetMarketBorrowerAttestation(uint256 marketId, bool required);
event SetMarketPaymentType(uint256 marketId, PaymentType paymentType);
/* External Functions */
function initialize(TellerAS _tellerAS) external initializer {
tellerAS = _tellerAS;
lenderAttestationSchemaId = tellerAS.getASRegistry().register(
"(uint256 marketId, address lenderAddress)",
this
);
borrowerAttestationSchemaId = tellerAS.getASRegistry().register(
"(uint256 marketId, address borrowerAddress)",
this
);
}
/**
* @notice Creates a new market.
* @param _initialOwner Address who will initially own the market.
* @param _paymentCycleDuration Length of time in seconds before a bid's next payment is required to be made.
* @param _paymentDefaultDuration Length of time in seconds before a loan is considered in default for non-payment.
* @param _bidExpirationTime Length of time in seconds before pending bids expire.
* @param _requireLenderAttestation Boolean that indicates if lenders require attestation to join market.
* @param _requireBorrowerAttestation Boolean that indicates if borrowers require attestation to join market.
* @param _paymentType The payment type for loans in the market.
* @param _uri URI string to get metadata details about the market.
* @param _paymentCycleType The payment cycle type for loans in the market - Seconds or Monthly
* @return marketId_ The market ID of the newly created market.
*/
function createMarket(
address _initialOwner,
uint32 _paymentCycleDuration,
uint32 _paymentDefaultDuration,
uint32 _bidExpirationTime,
uint16 _feePercent,
bool _requireLenderAttestation,
bool _requireBorrowerAttestation,
PaymentType _paymentType,
PaymentCycleType _paymentCycleType,
string calldata _uri
) external returns (uint256 marketId_) {
marketId_ = _createMarket(
_initialOwner,
_paymentCycleDuration,
_paymentDefaultDuration,
_bidExpirationTime,
_feePercent,
_requireLenderAttestation,
_requireBorrowerAttestation,
_paymentType,
_paymentCycleType,
_uri
);
}
/**
* @notice Creates a new market.
* @dev Uses the default EMI payment type.
* @param _initialOwner Address who will initially own the market.
* @param _paymentCycleDuration Length of time in seconds before a bid's next payment is required to be made.
* @param _paymentDefaultDuration Length of time in seconds before a loan is considered in default for non-payment.
* @param _bidExpirationTime Length of time in seconds before pending bids expire.
* @param _requireLenderAttestation Boolean that indicates if lenders require attestation to join market.
* @param _requireBorrowerAttestation Boolean that indicates if borrowers require attestation to join market.
* @param _uri URI string to get metadata details about the market.
* @return marketId_ The market ID of the newly created market.
*/
function createMarket(
address _initialOwner,
uint32 _paymentCycleDuration,
uint32 _paymentDefaultDuration,
uint32 _bidExpirationTime,
uint16 _feePercent,
bool _requireLenderAttestation,
bool _requireBorrowerAttestation,
string calldata _uri
) external returns (uint256 marketId_) {
marketId_ = _createMarket(
_initialOwner,
_paymentCycleDuration,
_paymentDefaultDuration,
_bidExpirationTime,
_feePercent,
_requireLenderAttestation,
_requireBorrowerAttestation,
PaymentType.EMI,
PaymentCycleType.Seconds,
_uri
);
}
/**
* @notice Creates a new market.
* @param _initialOwner Address who will initially own the market.
* @param _paymentCycleDuration Length of time in seconds before a bid's next payment is required to be made.
* @param _paymentDefaultDuration Length of time in seconds before a loan is considered in default for non-payment.
* @param _bidExpirationTime Length of time in seconds before pending bids expire.
* @param _requireLenderAttestation Boolean that indicates if lenders require attestation to join market.
* @param _requireBorrowerAttestation Boolean that indicates if borrowers require attestation to join market.
* @param _paymentType The payment type for loans in the market.
* @param _uri URI string to get metadata details about the market.
* @param _paymentCycleType The payment cycle type for loans in the market - Seconds or Monthly
* @return marketId_ The market ID of the newly created market.
*/
function _createMarket(
address _initialOwner,
uint32 _paymentCycleDuration,
uint32 _paymentDefaultDuration,
uint32 _bidExpirationTime,
uint16 _feePercent,
bool _requireLenderAttestation,
bool _requireBorrowerAttestation,
PaymentType _paymentType,
PaymentCycleType _paymentCycleType,
string calldata _uri
) internal returns (uint256 marketId_) {
require(_initialOwner != address(0), "Invalid owner address");
// Increment market ID counter
marketId_ = ++marketCount;
// Set the market owner
markets[marketId_].owner = _initialOwner;
// Initialize market settings
_setMarketSettings(
marketId_,
_paymentCycleDuration,
_paymentType,
_paymentCycleType,
_paymentDefaultDuration,
_bidExpirationTime,
_feePercent,
_requireBorrowerAttestation,
_requireLenderAttestation,
_uri
);
emit MarketCreated(_initialOwner, marketId_);
}
/**
* @notice Closes a market so new bids cannot be added.
* @param _marketId The market ID for the market to close.
*/
function closeMarket(uint256 _marketId) public ownsMarket(_marketId) {
if (!marketIsClosed[_marketId]) {
marketIsClosed[_marketId] = true;
emit MarketClosed(_marketId);
}
}
/**
* @notice Returns the status of a market being open or closed for new bids.
* @param _marketId The market ID for the market to check.
*/
function isMarketClosed(uint256 _marketId)
public
view
override
returns (bool)
{
return marketIsClosed[_marketId];
}
/**
* @notice Adds a lender to a market.
* @dev See {_attestStakeholder}.
*/
function attestLender(
uint256 _marketId,
address _lenderAddress,
uint256 _expirationTime
) external {
_attestStakeholder(_marketId, _lenderAddress, _expirationTime, true);
}
/**
* @notice Adds a lender to a market via delegated attestation.
* @dev See {_attestStakeholderViaDelegation}.
*/
function attestLender(
uint256 _marketId,
address _lenderAddress,
uint256 _expirationTime,
uint8 _v,
bytes32 _r,
bytes32 _s
) external {
_attestStakeholderViaDelegation(
_marketId,
_lenderAddress,
_expirationTime,
true,
_v,
_r,
_s
);
}
/**
* @notice Removes a lender from an market.
* @dev See {_revokeStakeholder}.
*/
function revokeLender(uint256 _marketId, address _lenderAddress) external {
_revokeStakeholder(_marketId, _lenderAddress, true);
}
/**
* @notice Removes a borrower from a market via delegated revocation.
* @dev See {_revokeStakeholderViaDelegation}.
*/
function revokeLender(
uint256 _marketId,
address _lenderAddress,
uint8 _v,
bytes32 _r,
bytes32 _s
) external {
_revokeStakeholderViaDelegation(
_marketId,
_lenderAddress,
true,
_v,
_r,
_s
);
}
/**
* @notice Allows a lender to voluntarily leave a market.
* @param _marketId The market ID to leave.
*/
function lenderExitMarket(uint256 _marketId) external {
// Remove lender address from market set
bool response = markets[_marketId].verifiedLendersForMarket.remove(
_msgSender()
);
if (response) {
emit LenderExitMarket(_marketId, _msgSender());
}
}
/**
* @notice Adds a borrower to a market.
* @dev See {_attestStakeholder}.
*/
function attestBorrower(
uint256 _marketId,
address _borrowerAddress,
uint256 _expirationTime
) external {
_attestStakeholder(_marketId, _borrowerAddress, _expirationTime, false);
}
/**
* @notice Adds a borrower to a market via delegated attestation.
* @dev See {_attestStakeholderViaDelegation}.
*/
function attestBorrower(
uint256 _marketId,
address _borrowerAddress,
uint256 _expirationTime,
uint8 _v,
bytes32 _r,
bytes32 _s
) external {
_attestStakeholderViaDelegation(
_marketId,
_borrowerAddress,
_expirationTime,
false,
_v,
_r,
_s
);
}
/**
* @notice Removes a borrower from an market.
* @dev See {_revokeStakeholder}.
*/
function revokeBorrower(uint256 _marketId, address _borrowerAddress)
external
{
_revokeStakeholder(_marketId, _borrowerAddress, false);
}
/**
* @notice Removes a borrower from a market via delegated revocation.
* @dev See {_revokeStakeholderViaDelegation}.
*/
function revokeBorrower(
uint256 _marketId,
address _borrowerAddress,
uint8 _v,
bytes32 _r,
bytes32 _s
) external {
_revokeStakeholderViaDelegation(
_marketId,
_borrowerAddress,
false,
_v,
_r,
_s
);
}
/**
* @notice Allows a borrower to voluntarily leave a market.
* @param _marketId The market ID to leave.
*/
function borrowerExitMarket(uint256 _marketId) external {
// Remove borrower address from market set
bool response = markets[_marketId].verifiedBorrowersForMarket.remove(
_msgSender()
);
if (response) {
emit BorrowerExitMarket(_marketId, _msgSender());
}
}
/**
* @notice Verifies an attestation is valid.
* @dev This function must only be called by the `attestLender` function above.
* @param recipient Lender's address who is being attested.
* @param schema The schema used for the attestation.
* @param data Data the must include the market ID and lender's address
* @param
* @param attestor Market owner's address who signed the attestation.
* @return Boolean indicating the attestation was successful.
*/
function resolve(
address recipient,
bytes calldata schema,
bytes calldata data,
uint256 /* expirationTime */,
address attestor
) external payable override returns (bool) {
bytes32 attestationSchemaId = keccak256(
abi.encodePacked(schema, address(this))
);
(uint256 marketId, address lenderAddress) = abi.decode(
data,
(uint256, address)
);
return
(_attestingSchemaId == attestationSchemaId &&
recipient == lenderAddress &&
attestor == _getMarketOwner(marketId)) ||
attestor == address(this);
}
/**
* @notice Transfers ownership of a marketplace.
* @param _marketId The ID of a market.
* @param _newOwner Address of the new market owner.
*
* Requirements:
* - The caller must be the current owner.
*/
function transferMarketOwnership(uint256 _marketId, address _newOwner)
public
ownsMarket(_marketId)
{
markets[_marketId].owner = _newOwner;
emit SetMarketOwner(_marketId, _newOwner);
}
/**
* @notice Updates multiple market settings for a given market.
* @param _marketId The ID of a market.
* @param _paymentCycleDuration Delinquency duration for new loans
* @param _newPaymentType The payment type for the market.
* @param _paymentCycleType The payment cycle type for loans in the market - Seconds or Monthly
* @param _paymentDefaultDuration Default duration for new loans
* @param _bidExpirationTime Duration of time before a bid is considered out of date
* @param _metadataURI A URI that points to a market's metadata.
*
* Requirements:
* - The caller must be the current owner.
*/
function updateMarketSettings(
uint256 _marketId,
uint32 _paymentCycleDuration,
PaymentType _newPaymentType,
PaymentCycleType _paymentCycleType,
uint32 _paymentDefaultDuration,
uint32 _bidExpirationTime,
uint16 _feePercent,
bool _borrowerAttestationRequired,
bool _lenderAttestationRequired,
string calldata _metadataURI
) public ownsMarket(_marketId) {
_setMarketSettings(
_marketId,
_paymentCycleDuration,
_newPaymentType,
_paymentCycleType,
_paymentDefaultDuration,
_bidExpirationTime,
_feePercent,
_borrowerAttestationRequired,
_lenderAttestationRequired,
_metadataURI
);
}
/**
* @notice Sets the fee recipient address for a market.
* @param _marketId The ID of a market.
* @param _recipient Address of the new fee recipient.
*
* Requirements:
* - The caller must be the current owner.
*/
function setMarketFeeRecipient(uint256 _marketId, address _recipient)
public
ownsMarket(_marketId)
{
markets[_marketId].feeRecipient = _recipient;
emit SetMarketFeeRecipient(_marketId, _recipient);
}
/**
* @notice Sets the metadata URI for a market.
* @param _marketId The ID of a market.
* @param _uri A URI that points to a market's metadata.
*
* Requirements:
* - The caller must be the current owner.
*/
function setMarketURI(uint256 _marketId, string calldata _uri)
public
ownsMarket(_marketId)
{
//We do string comparison by checking the hashes of the strings against one another
if (
keccak256(abi.encodePacked(_uri)) !=
keccak256(abi.encodePacked(markets[_marketId].metadataURI))
) {
markets[_marketId].metadataURI = _uri;
emit SetMarketURI(_marketId, _uri);
}
}
/**
* @notice Sets the duration of new loans for this market before they turn delinquent.
* @notice Changing this value does not change the terms of existing loans for this market.
* @param _marketId The ID of a market.
* @param _paymentCycleType Cycle type (seconds or monthly)
* @param _duration Delinquency duration for new loans
*/
function setPaymentCycle(
uint256 _marketId,
PaymentCycleType _paymentCycleType,
uint32 _duration
) public ownsMarket(_marketId) {
require(
(_paymentCycleType == PaymentCycleType.Seconds) ||
(_paymentCycleType == PaymentCycleType.Monthly &&
_duration == 0),
"monthly payment cycle duration cannot be set"
);
Marketplace storage market = markets[_marketId];
uint32 duration = _paymentCycleType == PaymentCycleType.Seconds
? _duration
: 30 days;
if (
_paymentCycleType != market.paymentCycleType ||
duration != market.paymentCycleDuration
) {
markets[_marketId].paymentCycleType = _paymentCycleType;
markets[_marketId].paymentCycleDuration = duration;
emit SetPaymentCycle(_marketId, _paymentCycleType, duration);
}
}
/**
* @notice Sets the duration of new loans for this market before they turn defaulted.
* @notice Changing this value does not change the terms of existing loans for this market.
* @param _marketId The ID of a market.
* @param _duration Default duration for new loans
*/
function setPaymentDefaultDuration(uint256 _marketId, uint32 _duration)
public
ownsMarket(_marketId)
{
if (_duration != markets[_marketId].paymentDefaultDuration) {
markets[_marketId].paymentDefaultDuration = _duration;
emit SetPaymentDefaultDuration(_marketId, _duration);
}
}
function setBidExpirationTime(uint256 _marketId, uint32 _duration)
public
ownsMarket(_marketId)
{
if (_duration != markets[_marketId].bidExpirationTime) {
markets[_marketId].bidExpirationTime = _duration;
emit SetBidExpirationTime(_marketId, _duration);
}
}
/**
* @notice Sets the fee for the market.
* @param _marketId The ID of a market.
* @param _newPercent The percentage fee in basis points.
*
* Requirements:
* - The caller must be the current owner.
*/
function setMarketFeePercent(uint256 _marketId, uint16 _newPercent)
public
ownsMarket(_marketId)
{
require(_newPercent >= 0 && _newPercent <= 10000, "invalid percent");
if (_newPercent != markets[_marketId].marketplaceFeePercent) {
markets[_marketId].marketplaceFeePercent = _newPercent;
emit SetMarketFee(_marketId, _newPercent);
}
}
/**
* @notice Set the payment type for the market.
* @param _marketId The ID of the market.
* @param _newPaymentType The payment type for the market.
*/
function setMarketPaymentType(
uint256 _marketId,
PaymentType _newPaymentType
) public ownsMarket(_marketId) {
if (_newPaymentType != markets[_marketId].paymentType) {
markets[_marketId].paymentType = _newPaymentType;
emit SetMarketPaymentType(_marketId, _newPaymentType);
}
}
/**
* @notice Enable/disables market whitelist for lenders.
* @param _marketId The ID of a market.
* @param _required Boolean indicating if the market requires whitelist.
*
* Requirements:
* - The caller must be the current owner.
*/
function setLenderAttestationRequired(uint256 _marketId, bool _required)
public
ownsMarket(_marketId)
{
if (_required != markets[_marketId].lenderAttestationRequired) {
markets[_marketId].lenderAttestationRequired = _required;
emit SetMarketLenderAttestation(_marketId, _required);
}
}
/**
* @notice Enable/disables market whitelist for borrowers.
* @param _marketId The ID of a market.
* @param _required Boolean indicating if the market requires whitelist.
*
* Requirements:
* - The caller must be the current owner.
*/
function setBorrowerAttestationRequired(uint256 _marketId, bool _required)
public
ownsMarket(_marketId)
{
if (_required != markets[_marketId].borrowerAttestationRequired) {
markets[_marketId].borrowerAttestationRequired = _required;
emit SetMarketBorrowerAttestation(_marketId, _required);
}
}
/**
* @notice Gets the data associated with a market.
* @param _marketId The ID of a market.
*/
function getMarketData(uint256 _marketId)
public
view
returns (
address owner,
uint32 paymentCycleDuration,
uint32 paymentDefaultDuration,
uint32 loanExpirationTime,
string memory metadataURI,
uint16 marketplaceFeePercent,
bool lenderAttestationRequired
)
{
return (
markets[_marketId].owner,
markets[_marketId].paymentCycleDuration,
markets[_marketId].paymentDefaultDuration,
markets[_marketId].bidExpirationTime,
markets[_marketId].metadataURI,
markets[_marketId].marketplaceFeePercent,
markets[_marketId].lenderAttestationRequired
);
}
/**
* @notice Gets the attestation requirements for a given market.
* @param _marketId The ID of the market.
*/
function getMarketAttestationRequirements(uint256 _marketId)
public
view
returns (
bool lenderAttestationRequired,
bool borrowerAttestationRequired
)
{
return (
markets[_marketId].lenderAttestationRequired,
markets[_marketId].borrowerAttestationRequired
);
}
/**
* @notice Gets the address of a market's owner.
* @param _marketId The ID of a market.
* @return The address of a market's owner.
*/
function getMarketOwner(uint256 _marketId)
public
view
virtual
override
returns (address)
{
return _getMarketOwner(_marketId);
}
/**
* @notice Gets the address of a market's owner.
* @param _marketId The ID of a market.
* @return The address of a market's owner.
*/
function _getMarketOwner(uint256 _marketId)
internal
view
virtual
returns (address)
{
return markets[_marketId].owner;
}
/**
* @notice Gets the fee recipient of a market.
* @param _marketId The ID of a market.
* @return The address of a market's fee recipient.
*/
function getMarketFeeRecipient(uint256 _marketId)
public
view
override
returns (address)
{
address recipient = markets[_marketId].feeRecipient;
if (recipient == address(0)) {
return _getMarketOwner(_marketId);
}
return recipient;
}
/**
* @notice Gets the metadata URI of a market.
* @param _marketId The ID of a market.
* @return URI of a market's metadata.
*/
function getMarketURI(uint256 _marketId)
public
view
override
returns (string memory)
{
return markets[_marketId].metadataURI;
}
/**
* @notice Gets the loan delinquent duration of a market.
* @param _marketId The ID of a market.
* @return Duration of a loan until it is delinquent.
* @return The type of payment cycle for loans in the market.
*/
function getPaymentCycle(uint256 _marketId)
public
view
override
returns (uint32, PaymentCycleType)
{
return (
markets[_marketId].paymentCycleDuration,
markets[_marketId].paymentCycleType
);
}
/**
* @notice Gets the loan default duration of a market.
* @param _marketId The ID of a market.
* @return Duration of a loan repayment interval until it is default.
*/
function getPaymentDefaultDuration(uint256 _marketId)
public
view
override
returns (uint32)
{
return markets[_marketId].paymentDefaultDuration;
}
/**
* @notice Get the payment type of a market.
* @param _marketId the ID of the market.
* @return The type of payment for loans in the market.
*/
function getPaymentType(uint256 _marketId)
public
view
override
returns (PaymentType)
{
return markets[_marketId].paymentType;
}
function getBidExpirationTime(uint256 marketId)
public
view
override
returns (uint32)
{
return markets[marketId].bidExpirationTime;
}
/**
* @notice Gets the marketplace fee in basis points
* @param _marketId The ID of a market.
* @return fee in basis points
*/
function getMarketplaceFee(uint256 _marketId)
public
view
override
returns (uint16 fee)
{
return markets[_marketId].marketplaceFeePercent;
}
/**
* @notice Checks if a lender has been attested and added to a market.
* @param _marketId The ID of a market.
* @param _lenderAddress Address to check.
* @return isVerified_ Boolean indicating if a lender has been added to a market.
* @return uuid_ Bytes32 representing the UUID of the lender.
*/
function isVerifiedLender(uint256 _marketId, address _lenderAddress)
public
view
override
returns (bool isVerified_, bytes32 uuid_)
{
return
_isVerified(
_lenderAddress,
markets[_marketId].lenderAttestationRequired,
markets[_marketId].lenderAttestationIds,
markets[_marketId].verifiedLendersForMarket
);
}
/**
* @notice Checks if a borrower has been attested and added to a market.
* @param _marketId The ID of a market.
* @param _borrowerAddress Address of the borrower to check.
* @return isVerified_ Boolean indicating if a borrower has been added to a market.
* @return uuid_ Bytes32 representing the UUID of the borrower.
*/
function isVerifiedBorrower(uint256 _marketId, address _borrowerAddress)
public
view
override
returns (bool isVerified_, bytes32 uuid_)
{
return
_isVerified(
_borrowerAddress,
markets[_marketId].borrowerAttestationRequired,
markets[_marketId].borrowerAttestationIds,
markets[_marketId].verifiedBorrowersForMarket
);
}
/**
* @notice Gets addresses of all attested lenders.
* @param _marketId The ID of a market.
* @param _page Page index to start from.
* @param _perPage Number of items in a page to return.
* @return Array of addresses that have been added to a market.
*/
function getAllVerifiedLendersForMarket(
uint256 _marketId,
uint256 _page,
uint256 _perPage
) public view returns (address[] memory) {
EnumerableSet.AddressSet storage set = markets[_marketId]
.verifiedLendersForMarket;
return _getStakeholdersForMarket(set, _page, _perPage);
}
/**
* @notice Gets addresses of all attested borrowers.
* @param _marketId The ID of the market.
* @param _page Page index to start from.
* @param _perPage Number of items in a page to return.
* @return Array of addresses that have been added to a market.
*/
function getAllVerifiedBorrowersForMarket(
uint256 _marketId,
uint256 _page,
uint256 _perPage
) public view returns (address[] memory) {
EnumerableSet.AddressSet storage set = markets[_marketId]
.verifiedBorrowersForMarket;
return _getStakeholdersForMarket(set, _page, _perPage);
}
/**
* @notice Sets multiple market settings for a given market.
* @param _marketId The ID of a market.
* @param _paymentCycleDuration Delinquency duration for new loans
* @param _newPaymentType The payment type for the market.
* @param _paymentCycleType The payment cycle type for loans in the market - Seconds or Monthly
* @param _paymentDefaultDuration Default duration for new loans
* @param _bidExpirationTime Duration of time before a bid is considered out of date
* @param _metadataURI A URI that points to a market's metadata.
*/
function _setMarketSettings(
uint256 _marketId,
uint32 _paymentCycleDuration,
PaymentType _newPaymentType,
PaymentCycleType _paymentCycleType,
uint32 _paymentDefaultDuration,
uint32 _bidExpirationTime,
uint16 _feePercent,
bool _borrowerAttestationRequired,
bool _lenderAttestationRequired,
string calldata _metadataURI
) internal {
setMarketURI(_marketId, _metadataURI);
setPaymentDefaultDuration(_marketId, _paymentDefaultDuration);
setBidExpirationTime(_marketId, _bidExpirationTime);
setMarketFeePercent(_marketId, _feePercent);
setLenderAttestationRequired(_marketId, _lenderAttestationRequired);
setBorrowerAttestationRequired(_marketId, _borrowerAttestationRequired);
setMarketPaymentType(_marketId, _newPaymentType);
setPaymentCycle(_marketId, _paymentCycleType, _paymentCycleDuration);
}
/**
* @notice Gets addresses of all attested relevant stakeholders.
* @param _set The stored set of stakeholders to index from.
* @param _page Page index to start from.
* @param _perPage Number of items in a page to return.
* @return stakeholders_ Array of addresses that have been added to a market.
*/
function _getStakeholdersForMarket(
EnumerableSet.AddressSet storage _set,
uint256 _page,
uint256 _perPage
) internal view returns (address[] memory stakeholders_) {
uint256 len = _set.length();
uint256 start = _page * _perPage;
if (start <= len) {
uint256 end = start + _perPage;
// Ensure we do not go out of bounds
if (end > len) {
end = len;
}
stakeholders_ = new address[](end - start);
for (uint256 i = start; i < end; i++) {
stakeholders_[i] = _set.at(i);
}
}
}