Skip to content

Commit e766c13

Browse files
committed
computeInterest
1 parent 334c805 commit e766c13

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

defi/Euler/contracts/BaseLogic.md

+52
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,58 @@ function resolveAssetConfig(address underlying) internal view returns (AssetConf
132132

133133
## Utils
134134

135+
### updateInterestRate
136+
137+
根据资金利用率 `utilisation` 计算利率,利率有最大值 `MAX_ALLOWED_INTEREST_RATE` 和最小值 `MIN_ALLOWED_INTEREST_RATE`
138+
139+
利率模型是分段函数 (折线)
140+
141+
```ts
142+
// BaseLogic.sol
143+
function updateInterestRate(AssetStorage storage assetStorage, AssetCache memory assetCache) internal {
144+
uint32 utilisation;
145+
146+
{
147+
uint totalBorrows = assetCache.totalBorrows / INTERNAL_DEBT_PRECISION;
148+
uint poolAssets = assetCache.poolSize + totalBorrows;
149+
if (poolAssets == 0) utilisation = 0; // empty pool arbitrarily given utilisation of 0
150+
else utilisation = uint32(totalBorrows * (uint(type(uint32).max) * 1e18) / poolAssets / 1e18);
151+
}
152+
153+
bytes memory result = callInternalModule(assetCache.interestRateModel,
154+
abi.encodeWithSelector(BaseIRM.computeInterestRate.selector, assetCache.underlying, utilisation));
155+
156+
(int96 newInterestRate) = abi.decode(result, (int96));
157+
158+
assetStorage.interestRate = assetCache.interestRate = newInterestRate;
159+
}
160+
161+
// BaseIRM.sol
162+
function computeInterestRate(address underlying, uint32 utilisation) external returns (int96) {
163+
int96 rate = computeInterestRateImpl(underlying, utilisation);
164+
165+
if (rate > MAX_ALLOWED_INTEREST_RATE) rate = MAX_ALLOWED_INTEREST_RATE;
166+
else if (rate < MIN_ALLOWED_INTEREST_RATE) rate = MIN_ALLOWED_INTEREST_RATE;
167+
168+
return rate;
169+
}
170+
171+
// BaseIRMLinearKink
172+
173+
function computeInterestRateImpl(address, uint32 utilisation) internal override view returns (int96) {
174+
uint ir = baseRate;
175+
176+
if (utilisation <= kink) {
177+
ir += utilisation * slope1;
178+
} else {
179+
ir += kink * slope1;
180+
ir += slope2 * (utilisation - kink);
181+
}
182+
183+
return int96(int(ir));
184+
}
185+
```
186+
135187
## Balances
136188

137189
### computeExchangeRate

0 commit comments

Comments
 (0)