-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathAssetLogic.sol
More file actions
244 lines (215 loc) · 8.57 KB
/
AssetLogic.sol
File metadata and controls
244 lines (215 loc) · 8.57 KB
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
// SPDX-License-Identifier: UNLICENSED
// Copyright (c) 2025 Aave Labs
pragma solidity ^0.8.20;
import {SafeCast} from 'src/dependencies/openzeppelin/SafeCast.sol';
import {MathUtils} from 'src/libraries/math/MathUtils.sol';
import {PercentageMath} from 'src/libraries/math/PercentageMath.sol';
import {WadRayMath} from 'src/libraries/math/WadRayMath.sol';
import {SharesMath} from 'src/hub/libraries/SharesMath.sol';
import {Premium} from 'src/hub/libraries/Premium.sol';
import {IBasicInterestRateStrategy} from 'src/hub/interfaces/IBasicInterestRateStrategy.sol';
import {IHub} from 'src/hub/interfaces/IHub.sol';
/// @title AssetLogic library
/// @author Aave Labs
/// @notice Implements the base logic and share price conversions for asset data.
library AssetLogic {
using AssetLogic for IHub.Asset;
using SafeCast for uint256;
using MathUtils for uint256;
using PercentageMath for uint256;
using WadRayMath for *;
using SharesMath for uint256;
/// @notice Converts an amount of shares to the equivalent amount of drawn assets, rounding up.
function toDrawnAssetsUp(
IHub.Asset storage asset,
uint256 shares
) internal view returns (uint256) {
return shares.rayMulUp(asset.getDrawnIndex());
}
/// @notice Converts an amount of shares to the equivalent amount of drawn assets, rounding down.
function toDrawnAssetsDown(
IHub.Asset storage asset,
uint256 shares
) internal view returns (uint256) {
return shares.rayMulDown(asset.getDrawnIndex());
}
/// @notice Converts an amount of drawn assets to the equivalent amount of shares, rounding up.
function toDrawnSharesUp(
IHub.Asset storage asset,
uint256 assets
) internal view returns (uint256) {
return assets.rayDivUp(asset.getDrawnIndex());
}
/// @notice Converts an amount of drawn assets to the equivalent amount of shares, rounding down.
function toDrawnSharesDown(
IHub.Asset storage asset,
uint256 assets
) internal view returns (uint256) {
return assets.rayDivDown(asset.getDrawnIndex());
}
/// @notice Returns the total drawn assets amount for the specified asset.
function drawn(IHub.Asset storage asset, uint256 drawnIndex) internal view returns (uint256) {
return asset.drawnShares.rayMulUp(drawnIndex);
}
/// @notice Returns the total premium amount for the specified asset.
function premium(IHub.Asset storage asset, uint256 drawnIndex) internal view returns (uint256) {
return
Premium
.calculatePremiumRay({
premiumShares: asset.premiumShares,
drawnIndex: drawnIndex,
premiumOffsetRay: asset.premiumOffsetRay
})
.fromRayUp();
}
/// @notice Returns the total amount owed for the specified asset, including drawn and premium.
function totalOwed(IHub.Asset storage asset, uint256 drawnIndex) internal view returns (uint256) {
return asset.drawn(drawnIndex) + asset.premium(drawnIndex);
}
/// @notice Returns the total added assets for the specified asset.
function totalAddedAssets(IHub.Asset storage asset) internal view returns (uint256) {
uint256 drawnIndex = asset.getDrawnIndex();
uint256 aggregatedOwedRay = _calculateAggregatedOwedRay({
drawnShares: asset.drawnShares,
premiumShares: asset.premiumShares,
premiumOffsetRay: asset.premiumOffsetRay,
deficitRay: asset.deficitRay,
drawnIndex: drawnIndex
});
return
asset.liquidity +
asset.swept +
aggregatedOwedRay.fromRayUp() -
asset.realizedFees -
asset.getUnrealizedFees(drawnIndex);
}
/// @notice Converts an amount of shares to the equivalent amount of added assets, rounding up.
function toAddedAssetsUp(
IHub.Asset storage asset,
uint256 shares
) internal view returns (uint256) {
return shares.toAssetsUp(asset.totalAddedAssets(), asset.addedShares);
}
/// @notice Converts an amount of shares to the equivalent amount of added assets, rounding down.
function toAddedAssetsDown(
IHub.Asset storage asset,
uint256 shares
) internal view returns (uint256) {
return shares.toAssetsDown(asset.totalAddedAssets(), asset.addedShares);
}
/// @notice Converts an amount of added assets to the equivalent amount of shares, rounding up.
function toAddedSharesUp(
IHub.Asset storage asset,
uint256 assets
) internal view returns (uint256) {
return assets.toSharesUp(asset.totalAddedAssets(), asset.addedShares);
}
/// @notice Converts an amount of added assets to the equivalent amount of shares, rounding down.
function toAddedSharesDown(
IHub.Asset storage asset,
uint256 assets
) internal view returns (uint256) {
return assets.toSharesDown(asset.totalAddedAssets(), asset.addedShares);
}
/// @notice Updates the drawn rate of a specified asset.
/// @dev Uses last stored index; asset accrual should have already occurred.
function updateDrawnRate(IHub.Asset storage asset, uint256 assetId) internal {
uint256 drawnIndex = asset.drawnIndex;
uint256 newDrawnRate = asset.getDrawnRate(assetId, drawnIndex);
asset.drawnRate = newDrawnRate.toUint96();
emit IHub.UpdateAsset(assetId, drawnIndex, newDrawnRate, asset.realizedFees);
}
/// @notice Accrues interest and fees for the specified asset.
function accrue(IHub.Asset storage asset) internal {
if (asset.lastUpdateTimestamp == block.timestamp) {
return;
}
uint256 drawnIndex = asset.getDrawnIndex();
asset.realizedFees += asset.getUnrealizedFees(drawnIndex).toUint120();
asset.drawnIndex = drawnIndex.toUint120();
asset.lastUpdateTimestamp = block.timestamp.toUint40();
}
/// @notice Calculates the drawn index of a specified asset based on the existing drawn rate and index.
function getDrawnIndex(IHub.Asset storage asset) internal view returns (uint256) {
uint256 previousIndex = asset.drawnIndex;
uint40 lastUpdateTimestamp = asset.lastUpdateTimestamp;
if (
lastUpdateTimestamp == block.timestamp || (asset.drawnShares == 0 && asset.premiumShares == 0)
) {
return previousIndex;
}
return
previousIndex.rayMulUp(
MathUtils.calculateLinearInterest(asset.drawnRate, lastUpdateTimestamp)
);
}
/// @notice Calculates the latest drawn rate of a specified asset using the specified drawnIndex.
/// @dev Premium debt is not used in the interest rate calculation.
/// @dev Imprecision from downscaling `deficitRay` does not accumulate.
function getDrawnRate(
IHub.Asset storage asset,
uint256 assetId,
uint256 drawnIndex
) internal view returns (uint256) {
return
IBasicInterestRateStrategy(asset.irStrategy).calculateInterestRate({
assetId: assetId,
liquidity: asset.liquidity,
drawn: asset.drawn(drawnIndex),
deficit: asset.deficitRay.fromRayUp(),
swept: asset.swept
});
}
/// @notice Calculates the amount of fees derived from the index growth due to interest accrual.
/// @param drawnIndex The current drawn index.
function getUnrealizedFees(
IHub.Asset storage asset,
uint256 drawnIndex
) internal view returns (uint256) {
uint256 previousIndex = asset.drawnIndex;
if (previousIndex == drawnIndex) {
return 0;
}
uint256 liquidityFee = asset.liquidityFee;
if (liquidityFee == 0) {
return 0;
}
uint120 drawnShares = asset.drawnShares;
uint120 premiumShares = asset.premiumShares;
int256 premiumOffsetRay = asset.premiumOffsetRay;
uint256 deficitRay = asset.deficitRay;
uint256 aggregatedOwedRayAfter = _calculateAggregatedOwedRay({
drawnShares: drawnShares,
premiumShares: premiumShares,
premiumOffsetRay: premiumOffsetRay,
deficitRay: deficitRay,
drawnIndex: drawnIndex
});
uint256 aggregatedOwedRayBefore = _calculateAggregatedOwedRay({
drawnShares: drawnShares,
premiumShares: premiumShares,
premiumOffsetRay: premiumOffsetRay,
deficitRay: deficitRay,
drawnIndex: previousIndex
});
return
(aggregatedOwedRayAfter.fromRayUp() - aggregatedOwedRayBefore.fromRayUp()).percentMulDown(
liquidityFee
);
}
/// @notice Calculates the aggregated owed amount for a specified asset, expressed in asset units and scaled by RAY.
function _calculateAggregatedOwedRay(
uint256 drawnShares,
uint256 premiumShares,
int256 premiumOffsetRay,
uint256 deficitRay,
uint256 drawnIndex
) internal pure returns (uint256) {
uint256 premiumRay = Premium.calculatePremiumRay({
premiumShares: premiumShares,
premiumOffsetRay: premiumOffsetRay,
drawnIndex: drawnIndex
});
return (drawnShares * drawnIndex) + premiumRay + deficitRay;
}
}