-
Notifications
You must be signed in to change notification settings - Fork 16
/
OfferLibrary.sol
98 lines (88 loc) · 3.53 KB
/
OfferLibrary.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
import {Errors} from "@src/libraries/Errors.sol";
import {Math} from "@src/libraries/Math.sol";
import {VariablePoolBorrowRateParams, YieldCurve, YieldCurveLibrary} from "@src/libraries/YieldCurveLibrary.sol";
struct LoanOffer {
// The maximum due date of the loan offer
// Since the yield curve is defined in relative terms, lenders can protect themselves by
// setting a maximum timestamp for a loan to be matched
uint256 maxDueDate;
// The yield curve in relative terms
YieldCurve curveRelativeTime;
}
struct BorrowOffer {
// The yield curve in relative terms
// Borrowers can protect themselves by setting an opening limit CR for a loan to be matched
YieldCurve curveRelativeTime;
}
/// @title OfferLibrary
/// @custom:security-contact security@size.credit
/// @author Size (https://size.credit/)
library OfferLibrary {
using YieldCurveLibrary for YieldCurve;
/// @notice Check if the loan offer is null
/// @param self The loan offer
/// @return True if the loan offer is null, false otherwise
function isNull(LoanOffer memory self) internal pure returns (bool) {
return self.maxDueDate == 0 && self.curveRelativeTime.isNull();
}
/// @notice Check if the borrow offer is null
/// @param self The borrow offer
/// @return True if the borrow offer is null, false otherwise
function isNull(BorrowOffer memory self) internal pure returns (bool) {
return self.curveRelativeTime.isNull();
}
/// @notice Get the APR by tenor of a loan offer
/// @param self The loan offer
/// @param params The variable pool borrow rate params
/// @param tenor The tenor
/// @return The APR
function getAPRByTenor(LoanOffer memory self, VariablePoolBorrowRateParams memory params, uint256 tenor)
internal
view
returns (uint256)
{
if (tenor == 0) revert Errors.NULL_TENOR();
return YieldCurveLibrary.getAPR(self.curveRelativeTime, params, tenor);
}
/// @notice Get the absolute rate per tenor of a loan offer
/// @param self The loan offer
/// @param params The variable pool borrow rate params
/// @param tenor The tenor
/// @return The absolute rate
function getRatePerTenor(LoanOffer memory self, VariablePoolBorrowRateParams memory params, uint256 tenor)
internal
view
returns (uint256)
{
uint256 apr = getAPRByTenor(self, params, tenor);
return Math.aprToRatePerTenor(apr, tenor);
}
/// @notice Get the APR by tenor of a borrow offer
/// @param self The borrow offer
/// @param params The variable pool borrow rate params
/// @param tenor The tenor
/// @return The APR
function getAPRByTenor(BorrowOffer memory self, VariablePoolBorrowRateParams memory params, uint256 tenor)
internal
view
returns (uint256)
{
if (tenor == 0) revert Errors.NULL_TENOR();
return YieldCurveLibrary.getAPR(self.curveRelativeTime, params, tenor);
}
/// @notice Get the absolute rate per tenor of a borrow offer
/// @param self The borrow offer
/// @param params The variable pool borrow rate params
/// @param tenor The tenor
/// @return The absolute rate
function getRatePerTenor(BorrowOffer memory self, VariablePoolBorrowRateParams memory params, uint256 tenor)
internal
view
returns (uint256)
{
uint256 apr = getAPRByTenor(self, params, tenor);
return Math.aprToRatePerTenor(apr, tenor);
}
}