-
Notifications
You must be signed in to change notification settings - Fork 7
/
RewardLogic.sol
181 lines (157 loc) · 7.07 KB
/
RewardLogic.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.23;
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../libraries/MathUtils.sol";
import "../LoanManagerState.sol";
library RewardLogic {
using MathUtils for uint256;
event RewardIndexesUpdated(
uint8 poolId,
uint256 collateralRewardIndex,
uint256 borrowRewardIndex,
uint256 lastUpdateTimestamp
);
function updateUserCollateralReward(
mapping(bytes32 accountId => mapping(uint8 poolId => LoanManagerState.UserPoolRewards)) storage userPoolRewards,
LoanManagerState.UserLoan storage loan,
LoanManagerState.LoanPool storage loanPool,
uint8 poolId
) internal {
LoanManagerState.UserPoolRewards storage userLoanPoolRewards = userPoolRewards[loan.accountId][poolId];
LoanManagerState.UserLoanCollateral storage userLoanCollateral = loan.collaterals[poolId];
uint256 collateralRewardIndex = loanPool.reward.collateralRewardIndex;
userLoanPoolRewards.collateral += MathUtils.calcAccruedRewards(
userLoanCollateral.balance,
collateralRewardIndex,
userLoanCollateral.rewardIndex
);
userLoanCollateral.rewardIndex = collateralRewardIndex;
}
function updateUserBorrowReward(
mapping(bytes32 accountId => mapping(uint8 poolId => LoanManagerState.UserPoolRewards)) storage userPoolRewards,
LoanManagerState.UserLoan storage loan,
LoanManagerState.LoanPool storage loanPool,
uint8 poolId
) internal {
LoanManagerState.UserPoolRewards storage userLoanPoolRewards = userPoolRewards[loan.accountId][poolId];
LoanManagerState.UserLoanBorrow storage userLoanBorrow = loan.borrows[poolId];
uint256 borrowRewardIndex = loanPool.reward.borrowRewardIndex;
userLoanPoolRewards.borrow += MathUtils.calcAccruedRewards(
userLoanBorrow.amount,
borrowRewardIndex,
userLoanBorrow.rewardIndex
);
userLoanBorrow.rewardIndex = borrowRewardIndex;
}
function updateUserBorrowRewardWithRepayment(
mapping(bytes32 accountId => mapping(uint8 poolId => LoanManagerState.UserPoolRewards)) storage userPoolRewards,
LoanManagerState.UserLoan storage loan,
LoanManagerState.LoanPool storage loanPool,
uint8 poolId,
uint256 principalPaid,
uint256 interestPaid
) internal {
LoanManagerState.UserPoolRewards storage userLoanPoolRewards = userPoolRewards[loan.accountId][poolId];
LoanManagerState.UserLoanBorrow storage userLoanBorrow = loan.borrows[poolId];
uint256 borrowRewardIndex = loanPool.reward.borrowRewardIndex;
userLoanPoolRewards.interestPaid += interestPaid;
if (principalPaid > 0) {
userLoanPoolRewards.borrow += MathUtils.calcAccruedRewards(
userLoanBorrow.amount + principalPaid,
borrowRewardIndex,
userLoanBorrow.rewardIndex
);
userLoanBorrow.rewardIndex = borrowRewardIndex;
}
}
function updateRewardIndexes(LoanManagerState.LoanPool storage loanPool, uint8 poolId) internal {
LoanManagerState.LoanPoolReward storage loanPoolReward = loanPool.reward;
uint256 oldLastUpdateTimestamp = loanPoolReward.lastUpdateTimestamp;
if (block.timestamp > oldLastUpdateTimestamp) {
uint256 collateralRewardIndex = loanPoolReward.collateralRewardIndex;
uint256 borrowRewardIndex = loanPoolReward.borrowRewardIndex;
uint256 lastUpdateTimestamp = block.timestamp;
uint256 minimumAmount = loanPoolReward.minimumAmount;
uint256 collateralUsed = loanPool.collateralUsed;
uint256 borrowUsed = loanPool.borrowUsed;
if (collateralUsed > minimumAmount) {
collateralRewardIndex += MathUtils.calcRewardIndexIncrement(
oldLastUpdateTimestamp,
loanPoolReward.collateralSpeed,
collateralUsed
);
loanPoolReward.collateralRewardIndex = collateralRewardIndex;
}
if (borrowUsed > minimumAmount) {
borrowRewardIndex += MathUtils.calcRewardIndexIncrement(
oldLastUpdateTimestamp,
loanPoolReward.borrowSpeed,
borrowUsed
);
loanPoolReward.borrowRewardIndex = borrowRewardIndex;
}
loanPoolReward.lastUpdateTimestamp = SafeCast.toUint64(lastUpdateTimestamp);
emit RewardIndexesUpdated(poolId, collateralRewardIndex, borrowRewardIndex, lastUpdateTimestamp);
}
}
function updateLoanPoolsRewardIndexes(
mapping(uint16 loanTypeId => LoanManagerState.LoanType) storage loanTypes,
uint16[] calldata loanTypeIds,
uint8[][] calldata poolIdsForLoanType
) external {
uint16 loanTypeId;
uint8[] memory poolIds;
for (uint256 i = 0; i < loanTypeIds.length; ) {
loanTypeId = loanTypeIds[i];
poolIds = poolIdsForLoanType[i];
for (uint256 j = 0; j < poolIds.length; ) {
updateRewardIndexes(loanTypes[loanTypeId].pools[poolIds[j]], poolIds[j]);
unchecked {
++j;
}
}
unchecked {
++i;
}
}
}
function updateUserLoansPoolsRewards(
mapping(uint16 loanTypeId => LoanManagerState.LoanType) storage loanTypes,
mapping(bytes32 => LoanManagerState.UserLoan) storage userLoans,
mapping(bytes32 accountId => mapping(uint8 poolId => LoanManagerState.UserPoolRewards)) storage userPoolRewards,
bytes32[] calldata loanIds
) external {
uint16 loanTypeId;
uint8[] memory pools;
uint8 poolId;
for (uint256 i = 0; i < loanIds.length; ) {
LoanManagerState.UserLoan storage userLoan = userLoans[loanIds[i]];
loanTypeId = userLoan.loanTypeId;
pools = userLoan.colPools;
for (uint256 j = 0; j < pools.length; ) {
poolId = pools[j];
LoanManagerState.LoanPool storage loanPool = loanTypes[loanTypeId].pools[poolId];
updateRewardIndexes(loanPool, poolId);
updateUserCollateralReward(userPoolRewards, userLoan, loanPool, poolId);
unchecked {
++j;
}
}
pools = userLoan.borPools;
for (uint256 j = 0; j < pools.length; ) {
poolId = pools[j];
LoanManagerState.LoanPool storage loanPool = loanTypes[loanTypeId].pools[poolId];
updateRewardIndexes(loanPool, poolId);
updateUserBorrowReward(userPoolRewards, userLoan, loanPool, poolId);
unchecked {
++j;
}
}
unchecked {
++i;
}
}
}
}