Skip to content

Commit

Permalink
first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
kyzia551 committed Jan 16, 2024
1 parent 45deaeb commit 8dbcc32
Show file tree
Hide file tree
Showing 24 changed files with 944 additions and 47 deletions.
9 changes: 9 additions & 0 deletions .gitmodules
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
1 change: 1 addition & 0 deletions lib/aave-address-book
Submodule aave-address-book added at 74987e
1 change: 1 addition & 0 deletions lib/aave-v3-core
Submodule aave-v3-core added at 6070e8
1 change: 1 addition & 0 deletions lib/cl-synchronicity-price-adapter
4 changes: 4 additions & 0 deletions remappings.txt
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/
13 changes: 0 additions & 13 deletions scripts/Ghost.s.sol

This file was deleted.

52 changes: 52 additions & 0 deletions src/contracts/CLRatePriceCapAdapter.sol
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;
}
}
52 changes: 52 additions & 0 deletions src/contracts/CbETHPriceCapAdapter.sol
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;
}
}
10 changes: 0 additions & 10 deletions src/contracts/Ghost.sol

This file was deleted.

52 changes: 52 additions & 0 deletions src/contracts/MaticPriceCapAdapter.sol
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;
}
}
164 changes: 164 additions & 0 deletions src/contracts/PriceCapAdapterBase.sol
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);
}
Loading

0 comments on commit 8dbcc32

Please sign in to comment.