generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 17
[VEN-3002]: Cap Underlying Exchange Rate in Capped Oracle #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eccc4b0
fix: wip - fix capping exchange rate
web3rover e22fe87
fix: implemented capped oracle for SFrxETHOracle
web3rover 6a9616b
fix: fixed tests
web3rover bdb8550
fix: optimisation
web3rover 518a8a3
fix: revert sfrxETH oracle
web3rover 860b31b
fix: removed CappedOracle abstract contract
web3rover c39ce2c
fix: added check for invalid growth rate
web3rover 6254078
fix: use cached price as exchange rate not final price
web3rover 4dc0ab2
fix: updated var name
web3rover File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
|
@@ -2,15 +2,18 @@ | |
pragma solidity 0.8.25; | ||
|
||
import { OracleInterface } from "../../interfaces/OracleInterface.sol"; | ||
import { ensureNonzeroAddress, ensureNonzeroValue } from "@venusprotocol/solidity-utilities/contracts/validators.sol"; | ||
import { ensureNonzeroAddress } from "@venusprotocol/solidity-utilities/contracts/validators.sol"; | ||
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; | ||
import { CappedOracle } from "./CappedOracle.sol"; | ||
import { Transient } from "../../lib/Transient.sol"; | ||
|
||
/** | ||
* @title CorrelatedTokenOracle | ||
* @notice This oracle fetches the price of a token that is correlated to another token. | ||
*/ | ||
abstract contract CorrelatedTokenOracle is CappedOracle { | ||
abstract contract CorrelatedTokenOracle { | ||
/// Slot to cache the asset's price, used for transient storage | ||
bytes32 public constant CACHE_SLOT = keccak256(abi.encode("venus-protocol/oracle/common/CappedOracle/cache")); | ||
|
||
/// @notice Address of the correlated token | ||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
address public immutable CORRELATED_TOKEN; | ||
|
@@ -19,13 +22,33 @@ abstract contract CorrelatedTokenOracle is CappedOracle { | |
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
address public immutable UNDERLYING_TOKEN; | ||
|
||
//// @notice Growth rate percentage in seconds. Ex: 1e18 is 100% | ||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
uint256 public immutable GROWTH_RATE_PER_SECOND; | ||
|
||
/// @notice Snapshot update interval | ||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
uint256 public immutable SNAPSHOT_INTERVAL; | ||
|
||
/// @notice Address of Resilient Oracle | ||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
OracleInterface public immutable RESILIENT_ORACLE; | ||
|
||
/// @notice Last stored snapshot exchange rate | ||
uint256 public snapshotExchangeRate; | ||
|
||
/// @notice Last stored snapshot timestamp | ||
uint256 public snapshotTimestamp; | ||
|
||
/// @notice Emitted when the snapshot is updated | ||
event SnapshotUpdated(uint256 exchangeRate, uint256 timestamp); | ||
|
||
/// @notice Thrown if the token address is invalid | ||
error InvalidTokenAddress(); | ||
|
||
/// @notice Thrown if the growth rate is invalid | ||
error InvalidGrowthRate(); | ||
|
||
/// @notice Constructor for the implementation contract. | ||
/// @custom:oz-upgrades-unsafe-allow constructor | ||
constructor( | ||
|
@@ -34,39 +57,106 @@ abstract contract CorrelatedTokenOracle is CappedOracle { | |
address resilientOracle, | ||
uint256 annualGrowthRate, | ||
uint256 snapshotInterval | ||
) CappedOracle(annualGrowthRate, snapshotInterval) { | ||
) { | ||
if ((annualGrowthRate == 0 && snapshotInterval > 0) || (annualGrowthRate > 0 && snapshotInterval == 0)) | ||
revert InvalidGrowthRate(); | ||
|
||
ensureNonzeroAddress(correlatedToken); | ||
ensureNonzeroAddress(underlyingToken); | ||
ensureNonzeroAddress(resilientOracle); | ||
|
||
CORRELATED_TOKEN = correlatedToken; | ||
UNDERLYING_TOKEN = underlyingToken; | ||
RESILIENT_ORACLE = OracleInterface(resilientOracle); | ||
SNAPSHOT_INTERVAL = snapshotInterval; | ||
GROWTH_RATE_PER_SECOND = (annualGrowthRate) / (365 * 24 * 60 * 60); | ||
} | ||
|
||
/** | ||
* @notice Returns if the price is capped | ||
* @return isCapped Boolean indicating if the price is capped | ||
*/ | ||
function isCapped() external view virtual returns (bool) { | ||
uint256 maxAllowedExchangeRate = _getMaxAllowedExchangeRate(); | ||
if (maxAllowedExchangeRate == 0) { | ||
return false; | ||
} | ||
|
||
uint256 exchangeRate = _getUnderlyingAmount(); | ||
|
||
return exchangeRate > maxAllowedExchangeRate; | ||
} | ||
|
||
/** | ||
* @notice Updates the snapshot price and timestamp | ||
*/ | ||
function updateSnapshot() public { | ||
if (Transient.readCachedPrice(CACHE_SLOT, CORRELATED_TOKEN) != 0) { | ||
return; | ||
} | ||
if (block.timestamp - snapshotTimestamp < SNAPSHOT_INTERVAL || SNAPSHOT_INTERVAL == 0) return; | ||
|
||
uint256 exchangeRate = _getUnderlyingAmount(); | ||
uint256 maxAllowedExchangeRate = _getMaxAllowedExchangeRate(); | ||
|
||
snapshotExchangeRate = exchangeRate > maxAllowedExchangeRate ? maxAllowedExchangeRate : exchangeRate; | ||
snapshotTimestamp = block.timestamp; | ||
Transient.cachePrice(CACHE_SLOT, CORRELATED_TOKEN, snapshotExchangeRate); | ||
emit SnapshotUpdated(snapshotExchangeRate, snapshotTimestamp); | ||
} | ||
|
||
/** | ||
* @notice Fetches the price of the token | ||
* @param asset Address of the token | ||
* @return price The price of the token in scaled decimal places. It can be capped | ||
* to a maximum value taking into account the growth rate | ||
*/ | ||
function getPrice(address asset) public view returns (uint256) { | ||
uint256 exchangeRate = Transient.readCachedPrice(CACHE_SLOT, asset); | ||
if (exchangeRate != 0) { | ||
return calculatePrice(asset, exchangeRate); | ||
Comment on lines
+115
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @web3rover can you please add a UT for the changes? |
||
} | ||
|
||
exchangeRate = _getUnderlyingAmount(); | ||
|
||
if (SNAPSHOT_INTERVAL == 0) { | ||
return calculatePrice(asset, exchangeRate); | ||
} | ||
|
||
uint256 maxAllowedExchangeRate = _getMaxAllowedExchangeRate(); | ||
|
||
if ((exchangeRate > maxAllowedExchangeRate) && (maxAllowedExchangeRate != 0)) { | ||
return calculatePrice(asset, maxAllowedExchangeRate); | ||
} else { | ||
return calculatePrice(asset, exchangeRate); | ||
} | ||
} | ||
|
||
/** | ||
* @notice Fetches the uncapped price of the correlated token | ||
* @param asset Address of the correlated token | ||
* @return price The price of the correlated token in scaled decimal places | ||
* @notice Fetches price of the token based on an underlying exchange rate | ||
* @param asset The address of the asset | ||
* @param exchangeRate The underlying exchange rate to use | ||
* @return price The price of the token in scaled decimal places | ||
*/ | ||
function getUncappedPrice(address asset) internal view override returns (uint256) { | ||
function calculatePrice(address asset, uint256 exchangeRate) internal view returns (uint256) { | ||
if (asset != CORRELATED_TOKEN) revert InvalidTokenAddress(); | ||
|
||
uint256 underlyingAmount = _getUnderlyingAmount(); | ||
uint256 underlyingUSDPrice = RESILIENT_ORACLE.getPrice(UNDERLYING_TOKEN); | ||
|
||
IERC20Metadata token = IERC20Metadata(CORRELATED_TOKEN); | ||
uint256 decimals = token.decimals(); | ||
|
||
return (underlyingAmount * underlyingUSDPrice) / (10 ** decimals); | ||
return (exchangeRate * underlyingUSDPrice) / (10 ** decimals); | ||
} | ||
|
||
/** | ||
* @notice Address of the correlated token | ||
* @return address Address of the correlated token | ||
* @notice Gets the maximum allowed exchange rate for token | ||
* @return maxPrice Maximum allowed price | ||
*/ | ||
function token() internal view override returns (address) { | ||
return CORRELATED_TOKEN; | ||
function _getMaxAllowedExchangeRate() internal view returns (uint256) { | ||
uint256 timeElapsed = block.timestamp - snapshotTimestamp; | ||
uint256 maxPrice = snapshotExchangeRate + (snapshotExchangeRate * GROWTH_RATE_PER_SECOND * timeElapsed) / 1e18; | ||
return maxPrice; | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused
ensureNonzeroValue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed