11// SPDX-License-Identifier: MIT
22pragma solidity 0.8.30 ;
33
4+ import { console } from "forge-std/console.sol " ;
45import { ERC20 } from "solady/tokens/ERC20.sol " ;
56import { IZToken } from "./interfaces/IZToken.sol " ;
67import { FixedPointMathLib } from "solady/utils/FixedPointMathLib.sol " ;
@@ -13,6 +14,7 @@ abstract contract ZToken is IZToken, ERC20 {
1314 uint256 internal totalBorrow;
1415 uint256 internal totalReserve;
1516 uint256 internal borrowIndex;
17+ uint256 internal accrualTimestamp;
1618
1719 address public immutable UNDERLYING_TOKEN;
1820
@@ -22,6 +24,7 @@ abstract contract ZToken is IZToken, ERC20 {
2224 uint256 public immutable BASE_BORROW_RATE;
2325 uint256 public immutable RESERVE_FACTOR;
2426 uint256 public immutable COLLATERAL_FACTOR;
27+ uint256 public immutable BASE_BORROW_RATE_PER_SEC;
2528
2629 uint256 public constant SECONDS_PER_YEAR = 365 days ;
2730 uint256 public constant INITIAL_EXCHANGE_RATE = 0.02e18 ;
@@ -32,13 +35,37 @@ abstract contract ZToken is IZToken, ERC20 {
3235 constructor (MarketConfig memory _config ) {
3336 UNDERLYING_TOKEN = _config.underlyingToken;
3437 UTILIZATION_THRESHOLD = _config.utilizationThreshold;
35- SLOPE_BEFORE_KINK = _config.slopeBeforeKink;
36- SLOPE_AFTER_KINK = _config.slopeAfterKink;
38+ SLOPE_BEFORE_KINK = _config.slopeBeforeKink / SECONDS_PER_YEAR ;
39+ SLOPE_AFTER_KINK = _config.slopeAfterKink / SECONDS_PER_YEAR ;
3740 BASE_BORROW_RATE = _config.baseBorrowRate;
3841 RESERVE_FACTOR = _config.reserveFactor;
3942 COLLATERAL_FACTOR = _config.collateralFactor;
43+ BASE_BORROW_RATE_PER_SEC = BASE_BORROW_RATE / SECONDS_PER_YEAR;
4044
4145 borrowIndex = 1e18 ;
46+ accrualTimestamp = block .timestamp ;
47+ }
48+
49+ function _accureInterest () internal {
50+ uint256 cash = _getCash ();
51+ uint256 currentTotalBorrowed = totalBorrow;
52+ uint256 utilization = cash.utilization (currentTotalBorrowed, totalReserve);
53+
54+ uint256 borrowRatePerSecond = utilization.borrowInterestRatePerSecond (
55+ UTILIZATION_THRESHOLD, BASE_BORROW_RATE_PER_SEC, SLOPE_BEFORE_KINK, SLOPE_AFTER_KINK
56+ );
57+
58+ console.log ("current borrow rate " , borrowRatePerSecond);
59+ uint256 time = block .timestamp - accrualTimestamp;
60+
61+ uint256 simpleInterestFactor = borrowRatePerSecond * time;
62+
63+ uint256 borrowInterestAccrued = currentTotalBorrowed.mulWad (simpleInterestFactor);
64+
65+ borrowIndex = borrowIndex.newBorrowIndex (borrowRatePerSecond, time);
66+ totalReserve += borrowInterestAccrued.mulWad (RESERVE_FACTOR);
67+ totalBorrow = currentTotalBorrowed + borrowInterestAccrued;
68+ accrualTimestamp = block .timestamp ;
4269 }
4370
4471 function _getCash () internal view virtual returns (uint256 ) { }
@@ -50,7 +77,7 @@ abstract contract ZToken is IZToken, ERC20 {
5077 return cash.exchangeRate (totalBorrow, totalReserve, totalSupply ());
5178 }
5279
53- function getUtilization () external view returns (uint256 ) {
80+ function getUtilization () public view returns (uint256 ) {
5481 uint256 cash = _getCash ();
5582 return cash.utilization (totalBorrow, totalReserve);
5683 }
0 commit comments