-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWeightedInterestRateModel.sol
100 lines (81 loc) · 3.25 KB
/
WeightedInterestRateModel.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "./InterestRateModel.sol";
import "../Tick.sol";
/**
* @title Weighted Interest Rate Model
* @author MetaStreet Labs
*/
contract WeightedInterestRateModel is InterestRateModel {
using SafeCast for uint256;
/**************************************************************************/
/* Constructor */
/**************************************************************************/
/**
* @notice WeightedInterestRateModel constructor
*/
constructor() {}
/**************************************************************************/
/* Implementation */
/**************************************************************************/
/**
* @inheritdoc InterestRateModel
*/
function INTEREST_RATE_MODEL_NAME() external pure override returns (string memory) {
return "WeightedInterestRateModel";
}
/**
* @inheritdoc InterestRateModel
*/
function INTEREST_RATE_MODEL_VERSION() external pure override returns (string memory) {
return "2.0";
}
/**
* @inheritdoc InterestRateModel
*/
function _price(
uint256 principal,
uint64 duration,
LiquidityLogic.NodeSource[] memory nodes,
uint16 count,
uint64[] memory rates,
uint32 adminFeeRate
) internal pure override returns (uint256, uint256) {
/* First pass to compute repayment and weights */
uint256[] memory weights = new uint256[](count);
uint256 repayment;
uint256 normalization;
for (uint256 i; i < count; i++) {
/* Compute tick repayment */
(, , uint256 rateIndex, ) = Tick.decode(nodes[i].tick, LiquidityLogic.BASIS_POINTS_SCALE);
uint256 pending = nodes[i].used +
Math.mulDiv(nodes[i].used, rates[rateIndex] * duration, LiquidityLogic.FIXED_POINT_SCALE);
/* Update cumulative repayment */
repayment += pending;
/* Compute tick weight */
weights[i] = Math.mulDiv(repayment, pending, principal);
/* Accumulate weight for normalization */
normalization += weights[i];
}
/* Compute interest and admin fee */
uint256 interest = repayment - principal;
uint256 adminFee = (interest * adminFeeRate) / LiquidityLogic.BASIS_POINTS_SCALE;
/* Deduct admin fee from interest */
interest -= adminFee;
/* Second pass to assign weighted interest to ticks */
uint256 interestRemaining = interest;
for (uint256 i; i < count; i++) {
/* Compute weighted interest to tick */
uint256 weightedInterest = Math.mulDiv(interest, weights[i], normalization);
/* Assign node pending amount */
nodes[i].pending = nodes[i].used + weightedInterest.toUint128();
/* Track remaining interest */
interestRemaining -= weightedInterest;
}
/* Drop off remaining interest dust at lowest node */
if (interestRemaining != 0) nodes[0].pending += interestRemaining.toUint128();
return (repayment, adminFee);
}
}