Verified contractPartialLicenseRef-BUSL
AssetInterestRateStrategy
Address0xdcd924047a4bdbfef9ccdde845e5d45373ad276d
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.28
- Same code
7 more contracts
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 44,444,444 runs
- EVM version
- cancun
- License
- LicenseRef-BUSL
- Match
- Partialcreation code: no, runtime code: yes
- Verified on Sourcify
- Created in block
- 24,720,887
src/hub/AssetInterestRateStrategy.sol
// SPDX-License-Identifier: LicenseRef-BUSL
pragma solidity 0.8.28;
import {WadRayMath} from 'src/libraries/math/WadRayMath.sol';
import {
IAssetInterestRateStrategy,
IBasicInterestRateStrategy
} from 'src/hub/interfaces/IAssetInterestRateStrategy.sol';
/// @title AssetInterestRateStrategy
/// @author Aave Labs
/// @notice Manages the optimal-usage-based interest rate strategy for an asset.
/// @dev Strategies are Hub-specific, due to the usage of asset identifier as index of the `_interestRateData` mapping.
contract AssetInterestRateStrategy is IAssetInterestRateStrategy {
using WadRayMath for *;
/// @inheritdoc IAssetInterestRateStrategy
uint256 public constant MAX_ALLOWED_DRAWN_RATE = 1000_00;
/// @inheritdoc IAssetInterestRateStrategy
uint256 public constant MIN_OPTIMAL_RATIO = 1_00;
/// @inheritdoc IAssetInterestRateStrategy
uint256 public constant MAX_OPTIMAL_RATIO = 99_00;
/// @inheritdoc IAssetInterestRateStrategy
address public immutable HUB;
/// @dev Map of asset identifiers to their interest rate data.
mapping(uint256 assetId => InterestRateData) internal _interestRateData;
/// @dev Constructor.
/// @param hub_ The address of the associated Hub.
constructor(address hub_) {
require(hub_ != address(0), InvalidAddress());
HUB = hub_;
}
/// @notice Sets the interest rate parameters for a specified asset.
/// @param assetId The identifier of the asset.
/// @param data The encoded parameters containing BPS data used to configure the interest rate of the asset.
function setInterestRateData(uint256 assetId, bytes calldata data) external {
require(HUB == msg.sender, OnlyHub());
InterestRateData memory rateData = abi.decode(data, (InterestRateData));
require(
MIN_OPTIMAL_RATIO <= rateData.optimalUsageRatio &&
rateData.optimalUsageRatio <= MAX_OPTIMAL_RATIO,
InvalidOptimalUsageRatio()
);
require(
rateData.baseDrawnRate + rateData.rateGrowthBeforeOptimal + rateData.rateGrowthAfterOptimal <=
MAX_ALLOWED_DRAWN_RATE,
InvalidMaxDrawnRate()
);
_interestRateData[assetId] = rateData;
emit UpdateInterestRateData(
HUB,
assetId,
rateData.optimalUsageRatio,
rateData.baseDrawnRate,
rateData.rateGrowthBeforeOptimal,
rateData.rateGrowthAfterOptimal
);
}
/// @inheritdoc IAssetInterestRateStrategy
function getInterestRateData(uint256 assetId) external view returns (InterestRateData memory) {
return _interestRateData[assetId];
}
/// @inheritdoc IAssetInterestRateStrategy
function getOptimalUsageRatio(uint256 assetId) external view returns (uint256) {
return _interestRateData[assetId].optimalUsageRatio;
}
/// @inheritdoc IAssetInterestRateStrategy
function getBaseDrawnRate(uint256 assetId) external view returns (uint256) {
return _interestRateData[assetId].baseDrawnRate;
}
/// @inheritdoc IAssetInterestRateStrategy
function getRateGrowthBeforeOptimal(uint256 assetId) external view returns (uint256) {
return _interestRateData[assetId].rateGrowthBeforeOptimal;
}
/// @inheritdoc IAssetInterestRateStrategy
function getRateGrowthAfterOptimal(uint256 assetId) external view returns (uint256) {
return _interestRateData[assetId].rateGrowthAfterOptimal;
}
/// @inheritdoc IAssetInterestRateStrategy
function getMaxDrawnRate(uint256 assetId) external view returns (uint256) {
return
_interestRateData[assetId].baseDrawnRate +
_interestRateData[assetId].rateGrowthBeforeOptimal +
_interestRateData[assetId].rateGrowthAfterOptimal;
}
/// @inheritdoc IBasicInterestRateStrategy
function calculateInterestRate(
uint256 assetId,
uint256 liquidity,
uint256 drawn,
uint256 /* deficit */,
uint256 swept
) external view returns (uint256) {
InterestRateData memory rateData = _interestRateData[assetId];
require(rateData.optimalUsageRatio > 0, InterestRateDataNotSet(assetId));
uint256 currentDrawnRateRay = rateData.baseDrawnRate.bpsToRay();
if (drawn == 0) {
return currentDrawnRateRay;
}
uint256 usageRatioRay = drawn.rayDivUp(liquidity + drawn + swept);
uint256 optimalUsageRatioRay = rateData.optimalUsageRatio.bpsToRay();
if (usageRatioRay <= optimalUsageRatioRay) {
currentDrawnRateRay += rateData
.rateGrowthBeforeOptimal
.bpsToRay()
.rayMulUp(usageRatioRay)
.rayDivUp(optimalUsageRatioRay);
} else {
currentDrawnRateRay +=
rateData.rateGrowthBeforeOptimal.bpsToRay() +
rateData
.rateGrowthAfterOptimal
.bpsToRay()
.rayMulUp(usageRatioRay - optimalUsageRatioRay)
.rayDivUp(WadRayMath.RAY - optimalUsageRatioRay);
}
return currentDrawnRateRay;
}
}
src/hub/interfaces/IAssetInterestRateStrategy.sol
// SPDX-License-Identifier: LicenseRef-BUSL
pragma solidity ^0.8.0;
import {IBasicInterestRateStrategy} from 'src/hub/interfaces/IBasicInterestRateStrategy.sol';
/// @title IAssetInterestRateStrategy
/// @author Aave Labs
/// @notice Interface of the optimal-usage-based asset interest rate strategy.
interface IAssetInterestRateStrategy is IBasicInterestRateStrategy {
/// @notice Holds the interest rate data for a given asset.
/// @dev optimalUsageRatio The optimal usage ratio, in BPS. Maximum and minimum values are defined by `MAX_OPTIMAL_RATIO` and `MIN_OPTIMAL_RATIO`.
/// @dev baseDrawnRate The base drawn rate, in BPS.
/// @dev rateGrowthBeforeOptimal The rate growth before the optimal usage ratio, in BPS.
/// @dev rateGrowthAfterOptimal The rate growth after the optimal usage ratio, in BPS.
struct InterestRateData {
uint16 optimalUsageRatio;
uint32 baseDrawnRate;
uint32 rateGrowthBeforeOptimal;
uint32 rateGrowthAfterOptimal;
}
/// @notice Emitted when interest rate data is updated for an asset.
/// @param hub The address of the associated Hub.
/// @param assetId The identifier of the asset whose interest rate data is updated.
/// @param optimalUsageRatio The optimal usage ratio, in BPS.
/// @param baseDrawnRate The base drawn rate, in BPS.
/// @param rateGrowthBeforeOptimal The rate growth before the optimal usage ratio, in BPS.
/// @param rateGrowthAfterOptimal The rate growth after the optimal usage ratio, in BPS.
event UpdateInterestRateData(
address indexed hub,
uint256 indexed assetId,
uint256 optimalUsageRatio,
uint256 baseDrawnRate,
uint256 rateGrowthBeforeOptimal,
uint256 rateGrowthAfterOptimal
);
/// @notice Thrown when the given address is invalid.
error InvalidAddress();
/// @notice Thrown when the caller is not the Hub.
error OnlyHub();
/// @notice Thrown when the max possible rate is greater than `MAX_ALLOWED_DRAWN_RATE`.
error InvalidMaxDrawnRate();
/// @notice Thrown when the optimal usage ratio is less than `MIN_OPTIMAL_RATIO` or greater than `MAX_OPTIMAL_RATIO`.
error InvalidOptimalUsageRatio();
/// @notice Returns the full InterestRateData struct for the given asset.
/// @param assetId The identifier of the asset for which to get the data.
/// @return The InterestRateData struct for the given asset, all in BPS.
function getInterestRateData(uint256 assetId) external view returns (InterestRateData memory);
/// @notice Returns the optimal usage rate for the given asset.
/// @param assetId The identifier of the asset for which to get the optimal usage ratio.
/// @return The optimal usage ratio, in BPS.
function getOptimalUsageRatio(uint256 assetId) external view returns (uint256);
/// @notice Returns the base drawn rate.
/// @param assetId The identifier of the asset for which to get the base drawn rate.
/// @return The base drawn rate, in BPS.
function getBaseDrawnRate(uint256 assetId) external view returns (uint256);
/// @notice Returns the rate growth before the optimal usage ratio.
/// @dev Applicable when usage ratio > 0 and <= OPTIMAL_USAGE_RATIO.
/// @param assetId The identifier of the asset for which to get the rate growth before the optimal usage ratio.
/// @return The rate growth, in BPS.
function getRateGrowthBeforeOptimal(uint256 assetId) external view returns (uint256);
/// @notice Returns the rate growth after the optimal usage ratio.
/// @dev Applicable when usage ratio > OPTIMAL_USAGE_RATIO.
/// @param assetId The identifier of the asset for which to get the rate growth after the optimal usage ratio.
/// @return The rate growth, in BPS.
function getRateGrowthAfterOptimal(uint256 assetId) external view returns (uint256);
/// @notice Returns the maximum drawn rate.
/// @param assetId The identifier of the asset for which to get the maximum drawn rate.
/// @return The maximum drawn rate, in BPS.
function getMaxDrawnRate(uint256 assetId) external view returns (uint256);
/// @notice Returns the maximum allowed value for a drawn rate.
/// @return The maximum drawn rate, in BPS.
function MAX_ALLOWED_DRAWN_RATE() external view returns (uint256);
/// @notice Returns the minimum optimal usage ratio.
/// @return The minimum optimal usage ratio, expressed in BPS.
function MIN_OPTIMAL_RATIO() external view returns (uint256);
/// @notice Returns the maximum optimal usage ratio.
/// @return The maximum optimal usage ratio, expressed in BPS.
function MAX_OPTIMAL_RATIO() external view returns (uint256);
/// @notice Returns the address of the Hub.
function HUB() external view returns (address);
}
src/hub/interfaces/IBasicInterestRateStrategy.sol
// SPDX-License-Identifier: LicenseRef-BUSL
pragma solidity ^0.8.0;
/// @title IBasicInterestRateStrategy
/// @author Aave Labs
/// @notice Basic interface for any interest rate strategy.
interface IBasicInterestRateStrategy {
/// @notice Thrown when the interest rate data is not set for the asset.
/// @param assetId The identifier of the asset with no interest rate data set.
error InterestRateDataNotSet(uint256 assetId);
/// @notice Sets the interest rate parameters for a specified asset.
/// @param assetId The identifier of the asset.
/// @param data The encoded parameters used to configure the interest rate of the asset.
function setInterestRateData(uint256 assetId, bytes calldata data) external;
/// @notice Calculates the interest rate depending on the asset's state and configurations.
/// @param assetId The identifier of the asset.
/// @param liquidity The current available liquidity of the asset.
/// @param drawn The current drawn amount of the asset.
/// @param deficit The current deficit of the asset.
/// @param swept The current swept (reinvested) amount of the asset.
/// @return The interest rate, expressed in RAY.
function calculateInterestRate(
uint256 assetId,
uint256 liquidity,
uint256 drawn,
uint256 deficit,
uint256 swept
) external view returns (uint256);
}
src/libraries/math/WadRayMath.sol
// SPDX-License-Identifier: LicenseRef-BUSL
pragma solidity ^0.8.20;
/// @title WadRayMath library
/// @author Aave Labs
/// @notice Provides utility functions to work with WAD and RAY units with explicit rounding.
library WadRayMath {
uint256 internal constant WAD_DECIMALS = 18;
uint256 internal constant WAD = 1e18;
uint256 internal constant RAY = 1e27;
uint256 internal constant PERCENTAGE_FACTOR = 1e4;
/// @notice Multiplies two WAD numbers, rounding down.
/// @dev Reverts if intermediate multiplication overflows.
/// @return c = floor(a * b / WAD), expressed in WAD.
function wadMulDown(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly ('memory-safe') {
// to avoid overflow, a <= type(uint256).max / b
if iszero(or(iszero(b), iszero(gt(a, div(not(0), b))))) {
revert(0, 0)
}
c := div(mul(a, b), WAD)
}
}
/// @notice Multiplies two WAD numbers, rounding up.
/// @dev Reverts if intermediate multiplication overflows.
/// @return c = ceil(a * b / WAD), expressed in WAD.
function wadMulUp(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly ('memory-safe') {
// to avoid overflow, a <= type(uint256).max / b
if iszero(or(iszero(b), iszero(gt(a, div(not(0), b))))) {
revert(0, 0)
}
c := mul(a, b)
// Add 1 if (a * b) % WAD > 0 to round up the division of (a * b) by WAD
c := add(div(c, WAD), gt(mod(c, WAD), 0))
}
}
/// @notice Divides two WAD numbers, rounding down.
/// @dev Reverts if division by zero or intermediate multiplication overflows.
/// @return c = floor(a * WAD / b), expressed in WAD.
function wadDivDown(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly ('memory-safe') {
// to avoid overflow, a <= type(uint256).max / WAD
if or(iszero(b), iszero(iszero(gt(a, div(not(0), WAD))))) {
revert(0, 0)
}
c := div(mul(a, WAD), b)
}
}
/// @notice Divides two WAD numbers, rounding up.
/// @dev Reverts if division by zero or intermediate multiplication overflows.
/// @return c = ceil(a * WAD / b), expressed in WAD.
function wadDivUp(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly ('memory-safe') {
// to avoid overflow, a <= type(uint256).max / WAD
if or(iszero(b), iszero(iszero(gt(a, div(not(0), WAD))))) {
revert(0, 0)
}
c := mul(a, WAD)
// Add 1 if (a * WAD) % b > 0 to round up the division of (a * WAD) by b
c := add(div(c, b), gt(mod(c, b), 0))
}
}
/// @notice Multiplies two RAY numbers, rounding down.
/// @dev Reverts if intermediate multiplication overflows.
/// @return c = floor(a * b / RAY), expressed in RAY.
function rayMulDown(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly ('memory-safe') {
// to avoid overflow, a <= type(uint256).max / b
if iszero(or(iszero(b), iszero(gt(a, div(not(0), b))))) {
revert(0, 0)
}
c := div(mul(a, b), RAY)
}
}
/// @notice Multiplies two RAY numbers, rounding up.
/// @dev Reverts if intermediate multiplication overflows.
/// @return c = ceil(a * b / RAY), expressed in RAY.
function rayMulUp(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly ('memory-safe') {
// to avoid overflow, a <= type(uint256).max / b
if iszero(or(iszero(b), iszero(gt(a, div(not(0), b))))) {
revert(0, 0)
}
c := mul(a, b)
// Add 1 if (a * b) % RAY > 0 to round up the division of (a * b) by RAY
c := add(div(c, RAY), gt(mod(c, RAY), 0))
}
}
/// @notice Divides two RAY numbers, rounding down.
/// @dev Reverts if division by zero or intermediate multiplication overflows.
/// @return c = floor(a * RAY / b), expressed in RAY.
function rayDivDown(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly ('memory-safe') {
// to avoid overflow, a <= type(uint256).max / RAY
if or(iszero(b), iszero(iszero(gt(a, div(not(0), RAY))))) {
revert(0, 0)
}
c := div(mul(a, RAY), b)
}
}
/// @notice Divides two RAY numbers, rounding up.
/// @dev Reverts if division by zero or intermediate multiplication overflows.
/// @return c = ceil(a * RAY / b), expressed in RAY.
function rayDivUp(uint256 a, uint256 b) internal pure returns (uint256 c) {
assembly ('memory-safe') {
// to avoid overflow, a <= type(uint256).max / RAY
if or(iszero(b), iszero(iszero(gt(a, div(not(0), RAY))))) {
revert(0, 0)
}
c := mul(a, RAY)
// Add 1 if (a * RAY) % b > 0 to round up the division of (a * RAY) by b
c := add(div(c, b), gt(mod(c, b), 0))
}
}
/// @notice Casts value to WAD, adding 18 digits of precision.
/// @dev Reverts if intermediate multiplication overflows.
/// @return b = a * WAD, expressed in WAD.
function toWad(uint256 a) internal pure returns (uint256 b) {
assembly ('memory-safe') {
b := mul(a, WAD)
// to avoid overflow, b/WAD == a
if iszero(eq(div(b, WAD), a)) {
revert(0, 0)
}
}
}
/// @notice Casts value to RAY, adding 27 digits of precision.
/// @dev Reverts if intermediate multiplication overflows.
/// @return b = a * RAY, expressed in RAY.
function toRay(uint256 a) internal pure returns (uint256 b) {
assembly ('memory-safe') {
b := mul(a, RAY)
// to avoid overflow, b/RAY == a
if iszero(eq(div(b, RAY), a)) {
revert(0, 0)
}
}
}
/// @notice Removes WAD precision from a given value, rounding down.
/// @return b = a / WAD.
function fromWadDown(uint256 a) internal pure returns (uint256 b) {
assembly ('memory-safe') {
b := div(a, WAD)
}
}
/// @notice Removes RAY precision from a given value, rounding up.
/// @return b = ceil(a / RAY).
function fromRayUp(uint256 a) internal pure returns (uint256 b) {
assembly ('memory-safe') {
// add 1 if (a % RAY) > 0 to round up the division of a by RAY
b := add(div(a, RAY), gt(mod(a, RAY), 0))
}
}
/// @notice Converts value from basis points to WAD.
/// @dev Reverts if result overflows.
/// @return b = a * (WAD / PERCENTAGE_FACTOR), expressed in WAD.
function bpsToWad(uint256 a) internal pure returns (uint256 b) {
assembly ('memory-safe') {
let factor := div(WAD, PERCENTAGE_FACTOR)
b := mul(a, factor)
// to avoid overflow, b/factor == a
if iszero(eq(div(b, factor), a)) {
revert(0, 0)
}
}
}
/// @notice Converts value from basis points to RAY.
/// @dev Reverts if result overflows.
/// @return b = a * (RAY / PERCENTAGE_FACTOR), expressed in RAY.
function bpsToRay(uint256 a) internal pure returns (uint256 b) {
assembly ('memory-safe') {
let factor := div(RAY, PERCENTAGE_FACTOR)
b := mul(a, factor)
// to avoid overflow, b/factor == a
if iszero(eq(div(b, factor), a)) {
revert(0, 0)
}
}
}
/// @notice Rounds up a RAY value to the nearest RAY.
/// @dev Reverts if result overflows.
/// @return b = ceil(a / RAY) * RAY.
function roundRayUp(uint256 a) internal pure returns (uint256 b) {
assembly ('memory-safe') {
// add 1 if (a % RAY) > 0 to round up the division of a by RAY
let c := add(div(a, RAY), gt(mod(a, RAY), 0))
b := mul(c, RAY)
// to avoid overflow, b/RAY == c
if iszero(eq(div(b, RAY), c)) {
revert(0, 0)
}
}
}
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "none",
"useLiteralContent": false
},
"optimizer": {
"runs": 44444444,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"erc4626-tests/=lib/erc4626-tests/",
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.