-
Notifications
You must be signed in to change notification settings - Fork 113
OToken Vault Oracles #2953
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
Open
naddison36
wants to merge
4
commits into
master
Choose a base branch
from
nicka/vault-oracle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+459
−0
Open
OToken Vault Oracles #2953
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5f0fcf8
OToken Vault Oracles
naddison36 44169d4
Merge remote-tracking branch 'origin/master' into nicka/vault-oracle
naddison36 87eb039
Merge remote-tracking branch 'origin/master' into nicka/vault-oracle
naddison36 739d67b
Merge remote-tracking branch 'origin/master' into nicka/vault-oracle
naddison36 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import { AggregatorV3Interface } from "./chainlink/AggregatorV3Interface.sol"; | ||
| import { IOToken } from "./IOToken.sol"; | ||
| import { IVault } from "./IVault.sol"; | ||
|
|
||
| /** | ||
| * @title OToken Vault Oracle Interface | ||
| * @author Origin Protocol Inc | ||
| */ | ||
| interface IOTokenVaultOracle is AggregatorV3Interface { | ||
| /** | ||
| * @notice Returns the Vault used to calculate the OToken price. | ||
| * @return The Vault contract. | ||
| */ | ||
| function vault() external view returns (IVault); | ||
|
|
||
| /** | ||
| * @notice Returns the OToken whose price is reported. | ||
| * @return The OToken contract. | ||
| */ | ||
| function oToken() external view returns (IOToken); | ||
|
|
||
| /** | ||
| * @notice Returns the value of one OToken in the Vault's underlying asset. | ||
| * @return The price with 18 decimals. | ||
| */ | ||
| function price() external view returns (uint256); | ||
|
|
||
| /** | ||
| * @notice Returns the latest price using the legacy Chainlink interface. | ||
| * @return The price with 18 decimals. | ||
| */ | ||
| function latestAnswer() external view returns (int256); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import { IOToken } from "../interfaces/IOToken.sol"; | ||
| import { IOTokenVaultOracle } from "../interfaces/IOTokenVaultOracle.sol"; | ||
| import { IVault } from "../interfaces/IVault.sol"; | ||
|
|
||
| /** | ||
| * @title OToken Vault Oracle | ||
| * @notice Reports the value of one OToken in the Vault's underlying asset. | ||
| * @dev The price has 18 decimals and is calculated from the Vault's total value | ||
| * divided by the OToken's total supply. | ||
| * @author Origin Protocol Inc | ||
| */ | ||
| contract OTokenVaultOracle is IOTokenVaultOracle { | ||
| /// @notice The number of decimals in the reported price. | ||
| uint8 public constant override decimals = 18; | ||
|
|
||
| /// @notice The Chainlink-compatible oracle version. | ||
| uint256 public constant override version = 1; | ||
|
|
||
| /// @notice The Vault used to calculate the OToken price. | ||
| IVault public immutable vault; | ||
|
|
||
| /// @notice The OToken whose price is reported. | ||
| IOToken public immutable oToken; | ||
|
|
||
| /// @notice A human-readable description of the price pair. | ||
| string public override description; | ||
|
|
||
| /** | ||
| * @notice Constructs an OToken Vault Oracle. | ||
| * @param _vault The Vault used to calculate the price and resolve the OToken. | ||
| * @param _description A human-readable description of the price pair. | ||
| */ | ||
| constructor(address _vault, string memory _description) { | ||
| require(_vault != address(0), "Vault is zero address"); | ||
|
|
||
| vault = IVault(_vault); | ||
| address _oToken = vault.oToken(); | ||
| require(_oToken != address(0), "OToken is zero address"); | ||
| oToken = IOToken(_oToken); | ||
| description = _description; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Returns the value of one OToken in the Vault's underlying asset. | ||
| * @return The price with 18 decimals. | ||
| */ | ||
| function price() public view override returns (uint256) { | ||
| uint256 supply = oToken.totalSupply(); | ||
| require(supply > 0, "No data present"); | ||
|
|
||
| return (vault.totalValue() * 1e18) / supply; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Legacy Chainlink-compatible latest price accessor. | ||
| * @return The latest price with 18 decimals. | ||
| */ | ||
| function latestAnswer() external view override returns (int256) { | ||
| return int256(price()); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Returns data for the current synthetic round. | ||
| * @dev This is a live calculated feed, so only round 1 exists. | ||
| * @param _roundId The requested round ID, which must be 1. | ||
| * @return roundId The synthetic round ID. | ||
| * @return answer The current price with 18 decimals. | ||
| * @return startedAt The timestamp at which the price was read. | ||
| * @return updatedAt The timestamp at which the price was read. | ||
| * @return answeredInRound The synthetic round ID in which the answer was calculated. | ||
| */ | ||
| function getRoundData(uint80 _roundId) | ||
| external | ||
| view | ||
| override | ||
| returns ( | ||
| uint80 roundId, | ||
| int256 answer, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ) | ||
| { | ||
| require(_roundId == 1, "No data present"); | ||
| return _latestRoundData(); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Returns the latest calculated price as Chainlink round data. | ||
| * @return roundId The synthetic round ID. | ||
| * @return answer The current price with 18 decimals. | ||
| * @return startedAt The timestamp at which the price was read. | ||
| * @return updatedAt The timestamp at which the price was read. | ||
| * @return answeredInRound The synthetic round ID in which the answer was calculated. | ||
| */ | ||
| function latestRoundData() | ||
| external | ||
| view | ||
| override | ||
| returns ( | ||
| uint80 roundId, | ||
| int256 answer, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ) | ||
| { | ||
| return _latestRoundData(); | ||
| } | ||
|
|
||
| function _latestRoundData() | ||
| internal | ||
| view | ||
| returns ( | ||
| uint80 roundId, | ||
| int256 answer, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ) | ||
| { | ||
| return (1, int256(price()), block.timestamp, block.timestamp, 1); | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
contracts/scripts/deploy/base/001_DeploySuperOETHVaultOracle.s.sol
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import { OTokenVaultOracle } from "contracts/oracle/OTokenVaultOracle.sol"; | ||
| import { AbstractDeployScript } from "scripts/deploy/helpers/AbstractDeployScript.s.sol"; | ||
|
|
||
| /** | ||
| * @title 001_DeploySuperOETHVaultOracle | ||
| * @notice Deploys the superOETHb/WETH Vault Oracle on Base. | ||
| * @author Origin Protocol Inc | ||
| */ | ||
| contract $001_DeploySuperOETHVaultOracle is | ||
| AbstractDeployScript("001_DeploySuperOETHVaultOracle") | ||
| { | ||
| function _execute() internal override { | ||
| OTokenVaultOracle superOETHVaultOracle = new OTokenVaultOracle( | ||
| resolver.resolve("OETHBASE_VAULT_PROXY"), | ||
| "superOETHb / WETH" | ||
| ); | ||
|
|
||
| _recordDeployment( | ||
| "OETHBASE_VAULT_ORACLE", | ||
| address(superOETHVaultOracle) | ||
| ); | ||
| } | ||
|
|
||
| function _fork() internal override { | ||
| OTokenVaultOracle oracle = OTokenVaultOracle( | ||
| resolver.resolve("OETHBASE_VAULT_ORACLE") | ||
| ); | ||
|
|
||
| require( | ||
| address(oracle.vault()) == resolver.resolve("OETHBASE_VAULT_PROXY"), | ||
| "Unexpected Vault" | ||
| ); | ||
| require( | ||
| address(oracle.oToken()) == resolver.resolve("OETHBASE_PROXY"), | ||
| "Unexpected OToken" | ||
| ); | ||
| require(oracle.decimals() == 18, "Unexpected decimals"); | ||
| require(oracle.version() == 1, "Unexpected version"); | ||
| require( | ||
| keccak256(bytes(oracle.description())) == | ||
| keccak256(bytes("superOETHb / WETH")), | ||
| "Unexpected description" | ||
| ); | ||
| require(oracle.price() > 0, "Invalid price"); | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
contracts/scripts/deploy/mainnet/004_DeployOTokenVaultOracles.s.sol
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import { OTokenVaultOracle } from "contracts/oracle/OTokenVaultOracle.sol"; | ||
| import { AbstractDeployScript } from "scripts/deploy/helpers/AbstractDeployScript.s.sol"; | ||
|
|
||
| /** | ||
| * @title 004_DeployOTokenVaultOracles | ||
| * @notice Deploys the OETH/WETH and OUSD/USDC Vault Oracles on mainnet. | ||
| * @author Origin Protocol Inc | ||
| */ | ||
| contract $004_DeployOTokenVaultOracles is | ||
| AbstractDeployScript("004_DeployOTokenVaultOracles") | ||
| { | ||
| function _execute() internal override { | ||
| OTokenVaultOracle oethVaultOracle = new OTokenVaultOracle( | ||
| resolver.resolve("OETH_VAULT_PROXY"), | ||
| "OETH / WETH" | ||
| ); | ||
| OTokenVaultOracle ousdVaultOracle = new OTokenVaultOracle( | ||
| resolver.resolve("OUSD_VAULT_PROXY"), | ||
| "OUSD / USDC" | ||
| ); | ||
|
|
||
| _recordDeployment("OETH_VAULT_ORACLE", address(oethVaultOracle)); | ||
| _recordDeployment("OUSD_VAULT_ORACLE", address(ousdVaultOracle)); | ||
| } | ||
|
|
||
| function _fork() internal override { | ||
| _verifyOracle( | ||
| resolver.resolve("OETH_VAULT_ORACLE"), | ||
| resolver.resolve("OETH_VAULT_PROXY"), | ||
| resolver.resolve("OETH_PROXY"), | ||
| "OETH / WETH" | ||
| ); | ||
| _verifyOracle( | ||
| resolver.resolve("OUSD_VAULT_ORACLE"), | ||
| resolver.resolve("OUSD_VAULT_PROXY"), | ||
| resolver.resolve("OUSD_PROXY"), | ||
| "OUSD / USDC" | ||
| ); | ||
| } | ||
|
|
||
| function _verifyOracle( | ||
| address oracleAddress, | ||
| address vaultAddress, | ||
| address oTokenAddress, | ||
| string memory expectedDescription | ||
| ) internal view { | ||
| OTokenVaultOracle oracle = OTokenVaultOracle(oracleAddress); | ||
|
|
||
| require(address(oracle.vault()) == vaultAddress, "Unexpected Vault"); | ||
| require(address(oracle.oToken()) == oTokenAddress, "Unexpected OToken"); | ||
| require(oracle.decimals() == 18, "Unexpected decimals"); | ||
| require(oracle.version() == 1, "Unexpected version"); | ||
| require( | ||
| keccak256(bytes(oracle.description())) == | ||
| keccak256(bytes(expectedDescription)), | ||
| "Unexpected description" | ||
| ); | ||
| require(oracle.price() > 0, "Invalid price"); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
contracts/tests/mocks/MockOTokenVaultOracleDependencies.sol
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| contract MockOTokenVaultOracleVault { | ||
| uint256 public totalValue; | ||
| address public oToken; | ||
|
|
||
| function setTotalValue(uint256 _totalValue) external { | ||
| totalValue = _totalValue; | ||
| } | ||
|
|
||
| function setOToken(address _oToken) external { | ||
| oToken = _oToken; | ||
| } | ||
| } | ||
|
|
||
| contract MockOTokenVaultOracleToken { | ||
| uint256 public totalSupply; | ||
|
|
||
| function setTotalSupply(uint256 _totalSupply) external { | ||
| totalSupply = _totalSupply; | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
contracts/tests/unit/oracle/OTokenVaultOracle/concrete/ViewFunctions.t.sol
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import { Unit_OTokenVaultOracle_Shared_Test } from "../shared/Shared.t.sol"; | ||
|
|
||
| contract Unit_Concrete_OTokenVaultOracle_ViewFunctions_Test is | ||
| Unit_OTokenVaultOracle_Shared_Test | ||
| { | ||
| function test_constructor_setsConfiguration() public view { | ||
| assertEq(address(oracle.vault()), address(mockVault)); | ||
| assertEq(address(oracle.oToken()), address(mockOToken)); | ||
| assertEq(oracle.decimals(), 18); | ||
| assertEq(oracle.description(), "OToken / underlying asset"); | ||
| assertEq(oracle.version(), 1); | ||
| } | ||
|
|
||
| function test_price_isOneAtRebase() public view { | ||
| assertEq(oracle.price(), 1e18); | ||
| } | ||
|
|
||
| function test_price_increasesWithUnrebasedYield() public { | ||
| mockVault.setTotalValue(105e18); | ||
| assertEq(oracle.price(), 1.05e18); | ||
| } | ||
|
|
||
| function test_price_returnsFractionalPrice() public { | ||
| mockVault.setTotalValue(101e18); | ||
| mockOToken.setTotalSupply(100e18); | ||
| assertEq(oracle.price(), 1.01e18); | ||
| } | ||
|
|
||
| function test_price_RevertWhen_supplyIsZero() public { | ||
| mockOToken.setTotalSupply(0); | ||
| vm.expectRevert("No data present"); | ||
| oracle.price(); | ||
| } | ||
|
|
||
| function test_latestAnswer_returnsPrice() public view { | ||
| assertEq(oracle.latestAnswer(), int256(1e18)); | ||
| } | ||
|
|
||
| function test_latestRoundData_returnsCurrentPrice() public view { | ||
| ( | ||
| uint80 roundId, | ||
| int256 answer, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ) = oracle.latestRoundData(); | ||
|
|
||
| assertEq(roundId, 1); | ||
| assertEq(answer, int256(1e18)); | ||
| assertEq(startedAt, block.timestamp); | ||
| assertEq(updatedAt, block.timestamp); | ||
| assertEq(answeredInRound, 1); | ||
| } | ||
|
|
||
| function test_getRoundData_returnsCurrentPriceForSyntheticRound() | ||
| public | ||
| view | ||
| { | ||
| (uint80 roundId, int256 answer, , , uint80 answeredInRound) = oracle | ||
| .getRoundData(1); | ||
|
|
||
| assertEq(roundId, 1); | ||
| assertEq(answer, int256(1e18)); | ||
| assertEq(answeredInRound, 1); | ||
| } | ||
|
|
||
| function test_getRoundData_RevertWhen_roundDoesNotExist() public { | ||
| vm.expectRevert("No data present"); | ||
| oracle.getRoundData(2); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
The Vault's
totalValuemight contain funds that are unreachable - redeemable by the token holders. This is enforced by different yield/rebase caps which prevent the token from capturing all of the VaultValue in a single rebase.Also this has me slightly worried that offering such an on-chain oracle that reads immediate data opens us up for a read-only oracle attack.
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.
Oh regarding nominator of vault value what I had in mind was:
This fixes the donation lever problem, where one could donate to the Vault and inflate the price value instantly.
The downside is that this operation becomes super costly gas wise:
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.
I guess we could achieve a similar outcome by just upper bounding it. And thus prevent any donation lever: