generated from bgd-labs/bgd-forge-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
944 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
[submodule "lib/forge-std"] | ||
path = lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std | ||
[submodule "lib/aave-address-book"] | ||
path = lib/aave-address-book | ||
url = https://github.com/bgd-labs/aave-address-book | ||
[submodule "lib/aave-v3-core"] | ||
path = lib/aave-v3-core | ||
url = https://github.com/aave/aave-v3-core | ||
[submodule "lib/cl-synchronicity-price-adapter"] | ||
path = lib/cl-synchronicity-price-adapter | ||
url = https://github.com/bgd-labs/cl-synchronicity-price-adapter |
Submodule aave-address-book
added at
74987e
Submodule aave-v3-core
added at
6070e8
Submodule cl-synchronicity-price-adapter
added at
0e800c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
forge-std/=lib/forge-std/src/ | ||
aave-address-book/=lib/aave-address-book/src/ | ||
cl-synchronicity-price-adapter/=lib/cl-synchronicity-price-adapter/src/ | ||
aave-v3-core/=lib/aave-address-book/lib/aave-v3-core/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import {IACLManager} from 'aave-address-book/AaveV3.sol'; | ||
import {PriceCapAdapterBase} from './PriceCapAdapterBase.sol'; | ||
import {IChainlinkAggregator} from 'cl-synchronicity-price-adapter/interfaces/IChainlinkAggregator.sol'; | ||
|
||
/** | ||
* @title CLRatePriceCapAdapter | ||
* @author BGD Labs | ||
* @notice Price capped adapter to calculate price of (Asset / USD) pair by using | ||
* @notice Chainlink data feeds for (PEG / USD) and (ASSET / PEG). | ||
*/ | ||
contract CLRatePriceCapAdapter is PriceCapAdapterBase { | ||
/** | ||
* @notice Price feed for (Asset / Peg) pair | ||
*/ | ||
IChainlinkAggregator public immutable RATE_PROVIDER; | ||
|
||
/** | ||
* @param pegToBaseAggregatorAddress the address of PEG / BASE feed | ||
* @param rateProviderAddress the address of the ASSET / PEG feed | ||
* @param pairName name identifier | ||
*/ | ||
constructor( | ||
IACLManager aclManager, | ||
address pegToBaseAggregatorAddress, | ||
address rateProviderAddress, | ||
string memory pairName, | ||
uint256 snapshotRatio, | ||
uint256 snapshotTimestamp, | ||
uint16 maxYearlyRatioGrowth | ||
) | ||
PriceCapAdapterBase( | ||
aclManager, | ||
pegToBaseAggregatorAddress, | ||
pairName, | ||
IChainlinkAggregator(rateProviderAddress).decimals(), | ||
snapshotRatio, | ||
snapshotTimestamp, | ||
maxYearlyRatioGrowth | ||
) | ||
{ | ||
RATE_PROVIDER = IChainlinkAggregator(rateProviderAddress); | ||
} | ||
|
||
function _getRatio() internal view override returns (int256) { | ||
int256 ratio = RATE_PROVIDER.latestAnswer(); | ||
|
||
return ratio; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import {IACLManager} from 'aave-address-book/AaveV3.sol'; | ||
import {PriceCapAdapterBase} from './PriceCapAdapterBase.sol'; | ||
import {ICbEthRateProvider} from 'cl-synchronicity-price-adapter/interfaces/ICbEthRateProvider.sol'; | ||
|
||
/** | ||
* @title CbETHPriceCapAdapter | ||
* @author BGD Labs | ||
* @notice Price capped adapter to calculate price of (cbETH / USD) pair by using | ||
* @notice Chainlink data feed for (ETH / USD) and (cbETH / ETH) ratio. | ||
*/ | ||
contract CbETHPriceCapAdapter is PriceCapAdapterBase { | ||
/** | ||
* @notice rate provider for (cbETH / Base) | ||
*/ | ||
ICbEthRateProvider public immutable RATE_PROVIDER; | ||
|
||
/** | ||
* @param cbETHToBaseAggregatorAddress the address of cbETH / BASE feed | ||
* @param rateProviderAddress the address of the rate provider | ||
* @param pairName name identifier | ||
*/ | ||
constructor( | ||
IACLManager aclManager, | ||
address cbETHToBaseAggregatorAddress, | ||
address rateProviderAddress, | ||
string memory pairName, | ||
uint256 snapshotRatio, | ||
uint256 snapshotTimestamp, | ||
uint16 maxYearlyRatioGrowth | ||
) | ||
PriceCapAdapterBase( | ||
aclManager, | ||
cbETHToBaseAggregatorAddress, | ||
pairName, | ||
18, | ||
snapshotRatio, | ||
snapshotTimestamp, | ||
maxYearlyRatioGrowth | ||
) | ||
{ | ||
RATE_PROVIDER = ICbEthRateProvider(rateProviderAddress); | ||
} | ||
|
||
function _getRatio() internal view override returns (int256) { | ||
int256 ratio = int256(RATE_PROVIDER.exchangeRate()); | ||
|
||
return ratio; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import {IACLManager} from 'aave-address-book/AaveV3.sol'; | ||
import {PriceCapAdapterBase} from './PriceCapAdapterBase.sol'; | ||
import {IMaticRateProvider} from 'cl-synchronicity-price-adapter/interfaces/IMaticRateProvider.sol'; | ||
|
||
/** | ||
* @title MaticPriceCapAdapter | ||
* @author BGD Labs | ||
* @notice Price capped adapter to calculate price of (lst matic / USD) pair by using | ||
* @notice Chainlink data feed for (MATIC / USD) and (liquid staked matic / MATIC) ratio. | ||
*/ | ||
contract MaticPriceCapAdapter is PriceCapAdapterBase { | ||
/** | ||
* @notice Price feed for (MATIC / Base) pair | ||
*/ | ||
IMaticRateProvider public immutable RATE_PROVIDER; | ||
|
||
/** | ||
* @param maticToBaseAggregatorAddress the address of cbETH / BASE feed | ||
* @param rateProviderAddress the address of the rETH token | ||
* @param pairName name identifier | ||
*/ | ||
constructor( | ||
IACLManager aclManager, | ||
address maticToBaseAggregatorAddress, | ||
address rateProviderAddress, | ||
string memory pairName, | ||
uint256 snapshotRatio, | ||
uint256 snapshotTimestamp, | ||
uint16 maxYearlyRatioGrowth | ||
) | ||
PriceCapAdapterBase( | ||
aclManager, | ||
maticToBaseAggregatorAddress, | ||
pairName, | ||
18, | ||
snapshotRatio, | ||
snapshotTimestamp, | ||
maxYearlyRatioGrowth | ||
) | ||
{ | ||
RATE_PROVIDER = IMaticRateProvider(rateProviderAddress); | ||
} | ||
|
||
function _getRatio() internal view override returns (int256) { | ||
int256 ratio = int256(RATE_PROVIDER.getRate()); | ||
|
||
return ratio; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import {PercentageMath} from 'aave-v3-core/contracts/protocol/libraries/math/PercentageMath.sol'; | ||
import {IACLManager} from 'aave-address-book/AaveV3.sol'; | ||
|
||
import {IChainlinkAggregator} from 'cl-synchronicity-price-adapter/interfaces/IChainlinkAggregator.sol'; | ||
import {IPriceCapAdapter, ICLSynchronicityPriceAdapter} from '../interfaces/IPriceCapAdapter.sol'; | ||
|
||
/** | ||
* @title PriceCapAdapterBase | ||
* @author BGD Labs | ||
* @notice Price adapter to cap the price of the underlying asset. | ||
*/ | ||
abstract contract PriceCapAdapterBase is IPriceCapAdapter { | ||
using PercentageMath for uint256; | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
uint256 public constant SECONDS_PER_YEAR = 365 days; | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
IChainlinkAggregator public immutable BASE_TO_USD; | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
IACLManager public immutable ACL_MANAGER; | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
uint8 public immutable DECIMALS; | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
uint8 public immutable RATIO_DECIMALS; | ||
|
||
/** | ||
* @notice Description of the pair | ||
*/ | ||
string private _description; | ||
|
||
/** | ||
* @notice Ratio at the time of snapshot | ||
*/ | ||
uint256 private _snapshotRatio; | ||
|
||
/** | ||
* @notice Timestamp at the time of snapshot | ||
*/ | ||
uint256 private _snapshotTimestamp; | ||
|
||
/** | ||
* @notice Ratio growth per second | ||
*/ | ||
uint256 private _maxRatioGrowthPerSecond; | ||
|
||
/** | ||
* @param baseAggregatorAddress the address of BASE_CURRENCY / USD feed | ||
* @param pairDescription description | ||
*/ | ||
constructor( | ||
IACLManager aclManager, | ||
address baseAggregatorAddress, | ||
string memory pairDescription, | ||
uint8 ratioDecimals, | ||
uint256 snapshotRatio, | ||
uint256 snapshotTimestamp, | ||
uint16 maxYearlyRatioGrowthPercent | ||
) { | ||
ACL_MANAGER = aclManager; | ||
BASE_TO_USD = IChainlinkAggregator(baseAggregatorAddress); | ||
DECIMALS = BASE_TO_USD.decimals(); | ||
RATIO_DECIMALS = ratioDecimals; | ||
|
||
_description = pairDescription; | ||
|
||
_setCapParameters(snapshotRatio, snapshotTimestamp, maxYearlyRatioGrowthPercent); | ||
} | ||
|
||
/// @inheritdoc ICLSynchronicityPriceAdapter | ||
function description() external view returns (string memory) { | ||
return _description; | ||
} | ||
|
||
/// @inheritdoc ICLSynchronicityPriceAdapter | ||
function decimals() external view returns (uint8) { | ||
return DECIMALS; | ||
} | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
function getSnapshotRatio() external view returns (uint256) { | ||
return _snapshotRatio; | ||
} | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
function getSnapshotTimestamp() external view returns (uint256) { | ||
return _snapshotTimestamp; | ||
} | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
function getMaxYearlyGrowthRatePercent() external view returns (uint256) { | ||
return _maxRatioGrowthPerSecond * SECONDS_PER_YEAR; | ||
} | ||
|
||
/// @inheritdoc IPriceCapAdapter | ||
function setCapParameters( | ||
uint256 snapshotRatio, | ||
uint256 snapshotTimestamp, | ||
uint16 maxYearlyRatioGrowthPercent | ||
) external { | ||
if (!ACL_MANAGER.isRiskAdmin(msg.sender)) { | ||
revert CallerIsNotRiskAdmin(); | ||
} | ||
|
||
_setCapParameters(snapshotRatio, snapshotTimestamp, maxYearlyRatioGrowthPercent); | ||
} | ||
|
||
/// @inheritdoc ICLSynchronicityPriceAdapter | ||
function latestAnswer() external view override returns (int256) { | ||
// get the current ratio | ||
int256 currentRatio = _getRatio(); | ||
|
||
// calculate the ratio based on snapshot ratio and max growth rate | ||
int256 maxRatio = int256( | ||
_snapshotRatio + _maxRatioGrowthPerSecond * (block.timestamp - _snapshotTimestamp) | ||
); | ||
|
||
if (maxRatio < currentRatio) { | ||
currentRatio = maxRatio; | ||
} | ||
|
||
// get the base price | ||
int256 basePrice = BASE_TO_USD.latestAnswer(); | ||
|
||
// calculate the price of the underlying asset | ||
int256 price = (basePrice * currentRatio) / int256(10 ** RATIO_DECIMALS); | ||
|
||
return price; | ||
} | ||
|
||
function _setCapParameters( | ||
uint256 snapshotRatio, | ||
uint256 snapshotTimestamp, | ||
uint16 maxYearlyRatioGrowthPercent | ||
) internal { | ||
if (snapshotRatio == 0) { | ||
revert SnapshotRatioIsZero(); | ||
} | ||
_snapshotRatio = snapshotRatio; | ||
_snapshotTimestamp = snapshotTimestamp; | ||
|
||
_maxRatioGrowthPerSecond = | ||
(_snapshotRatio.percentMul(maxYearlyRatioGrowthPercent)) / | ||
(SECONDS_PER_YEAR); | ||
|
||
emit CapParametersUpdated( | ||
snapshotRatio, | ||
snapshotTimestamp, | ||
_maxRatioGrowthPerSecond, | ||
maxYearlyRatioGrowthPercent | ||
); | ||
} | ||
|
||
/** | ||
* @notice Returns the current exchange ratio to the underlying(base) asset | ||
*/ | ||
function _getRatio() internal view virtual returns (int256); | ||
} |
Oops, something went wrong.