Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2ca524f
Added AggregatorV3Interface diagram
naddison36 Sep 12, 2023
876fd06
Updated OETH Oracle contracts diagram
naddison36 Sep 12, 2023
c7dc5b4
Added price Oracle to vault
naddison36 Sep 12, 2023
4c6361c
Added reference implementation of OETH Oracle
naddison36 Sep 13, 2023
08824ef
Refactor OETH Oracle
naddison36 Sep 13, 2023
3be9b7a
Fixed warnings of price override
naddison36 Sep 13, 2023
0d55c81
Fix unit tests
naddison36 Sep 13, 2023
90e8e8c
Fix fork tests
naddison36 Sep 13, 2023
db6fa51
Implements two floorPrice functions to compare
naddison36 Sep 14, 2023
dbd9525
Remove test floorPrice2
naddison36 Sep 14, 2023
714e393
Added price cap on OETH Oracle
naddison36 Sep 14, 2023
06a6691
Merge remote-tracking branch 'origin/master' into nicka/oeth-oracle
naddison36 Sep 14, 2023
8e03c2d
Only get OETH floor price from the vault if the market price is < 0.995
naddison36 Oct 4, 2023
3eb58da
Merge remote-tracking branch 'origin/master' into nicka/oeth-oracle
naddison36 Nov 7, 2023
d08572e
Updated OETH Oracle Natspec and code comments
naddison36 Nov 7, 2023
0805d43
Update Vault Natspec for floorPrice
naddison36 Nov 7, 2023
7e21054
fixed OETH Oracle fork tests
naddison36 Nov 8, 2023
b7e118a
Merge remote-tracking branch 'origin/master' into nicka/oeth-oracle
naddison36 Nov 8, 2023
f52cd40
fix unit tests after merge
naddison36 Nov 8, 2023
d8e34a1
regenerated OETH Oracle contract diagrams
naddison36 Nov 8, 2023
a393ffd
Updated Oracle docs
naddison36 Nov 8, 2023
2bd9e0a
Fix Slither error
naddison36 Nov 8, 2023
2789e13
Fixed OETHOracle deploy script
naddison36 Nov 8, 2023
f31dcca
Added OETH Oracle unit tests
naddison36 Nov 8, 2023
85bf20e
Removed transferToken from governable behaviour as that is strategy r…
naddison36 Nov 8, 2023
b6af118
Internal review updates to the OETH Oracle
naddison36 Nov 9, 2023
467a0d2
Merge remote-tracking branch 'origin/master' into nicka/oeth-oracle
naddison36 Nov 9, 2023
2b83ec6
Merge remote-tracking branch 'origin/master' into nicka/oeth-oracle
naddison36 Nov 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contracts/contracts/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ interface IVault {

function priceUnitRedeem(address asset) external view returns (uint256);

function floorPrice() external view returns (uint256);

function withdrawAllFromStrategy(address _strategyAddr) external;

function withdrawAllFromStrategies() external;
Expand Down
3 changes: 3 additions & 0 deletions contracts/contracts/interfaces/chainlink/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Diagrams

![AggregatorV3Interface](../../../docs/AggregatorV3Interface.svg)
5 changes: 5 additions & 0 deletions contracts/contracts/mocks/MockVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contract MockVault is VaultCore {
using StableMath for uint256;

uint256 storedTotalValue;
uint256 public override floorPrice;

function setTotalValue(uint256 _value) public {
storedTotalValue = _value;
Expand Down Expand Up @@ -42,4 +43,8 @@ contract MockVault is VaultCore {
function setMaxSupplyDiff(uint256 _maxSupplyDiff) external onlyGovernor {
maxSupplyDiff = _maxSupplyDiff;
}

function setFloorPrice(uint256 _floorPrice) external {
floorPrice = _floorPrice;
}
}
20 changes: 20 additions & 0 deletions contracts/contracts/mocks/curve/MockCurveOethEthPool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { MockCurveAbstractMetapool } from "./MockCurveAbstractMetapool.sol";
import "../MintableERC20.sol";

contract MockCurveOethEthPool is MockCurveAbstractMetapool {
constructor(address[2] memory _coins)
ERC20("Curve.fi Factory Pool: OETH", "OETHCRV-f")
{
coins = _coins;
}

// Simulate pool's EMA Oracle price
uint256 public price_oracle = 9995e14; // 0.9995

function setOraclePrice(uint256 _price) public {
price_oracle = _price;
}
}
202 changes: 202 additions & 0 deletions contracts/contracts/oracle/BaseOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { IOracleReceiver } from "./IOracleReceiver.sol";
import { AggregatorV3Interface } from "../interfaces/chainlink/AggregatorV3Interface.sol";
import { Governable } from "../governance/Governable.sol";

/**
* @title BaseOracle
* @notice Generic Chainlink style oracle
* @author Origin Protocol Inc
*/
abstract contract BaseOracle is
AggregatorV3Interface,
IOracleReceiver,
Governable
{
/// @notice Contract or account that can call addRoundData() to update the Oracle prices
address public oracleUpdater;

/// @notice Last round ID where isBadData is false and price is within maximum deviation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find any reference to isBadData does this comment need updating?

uint80 public lastCorrectRoundId;

/// @notice Historical Oracle prices
Round[] public rounds;

/// @notice Packed Round data struct
/// @notice price Oracle price to 18 decimals
/// @notice timestamp timestamp in seconds of price
struct Round {
uint128 price;
uint40 timestamp;
}

event SetOracleUpdater(address oracleUpdater);

constructor(address _oracleUpdater) Governable() {
_setOracleUpdater(_oracleUpdater);
}

/***************************************
Internal Setters
****************************************/

/// @notice Sets the contract or account that can add Oracle prices
/// @param _oracleUpdater Address of the contract or account that can update the Oracle prices
function _setOracleUpdater(address _oracleUpdater) internal {
emit SetOracleUpdater(_oracleUpdater);
oracleUpdater = _oracleUpdater;
}

/***************************************
External Setters
****************************************/

/// @notice Sets the contract or account that can update the Oracle prices
/// @param _oracleUpdater Address of the contract or account that can update the Oracle prices
function setOracleUpdater(address _oracleUpdater) external onlyGovernor {
_setOracleUpdater(_oracleUpdater);
}

/***************************************
Metadata
****************************************/

/// @notice The number of decimals in the Oracle price.
function decimals()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be expressed as:

uint8 public constant override decimals = 18;

external
pure
virtual
override
returns (uint8 _decimals)
{
_decimals = 18;
}

/// @notice The version number for the AggregatorV3Interface.
/// @dev Adheres to AggregatorV3Interface, which is different than typical semver
function version()
external
view
virtual
override
returns (uint256 _version)
{
_version = 1;
}

/***************************************
Oracle Receiver
****************************************/

/// @notice Adds a new Oracle price by the Oracle updater.
/// Can not be run twice in the same block.
/// @param _price is the Oracle price with 18 decimals
function addPrice(uint128 _price) external override {
if (msg.sender != oracleUpdater) revert OnlyOracleUpdater();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on using custom errors

if (_price == 0) revert NoPriceData();

// Can not add price in the same or previous blocks
uint256 _roundsLength = rounds.length;
if (
_roundsLength > 0 &&
block.timestamp <= rounds[_roundsLength - 1].timestamp
) {
revert AddPriceSameBlock();
}

lastCorrectRoundId = uint80(_roundsLength);

rounds.push(
Round({ price: _price, timestamp: uint40(block.timestamp) })
);
}

/***************************************
Prices
****************************************/

function _getRoundData(uint80 _roundId)
internal
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
if (rounds.length <= _roundId) revert NoPriceData();

Round memory _round = rounds[_roundId];
answer = int256(uint256(_round.price));

roundId = answeredInRound = _roundId;
startedAt = updatedAt = _round.timestamp;
}

/// @notice Returns the Oracle price data for a specific round.
/// @param _roundId The round ID
/// @return roundId The round ID
/// @return answer The Oracle price
/// @return startedAt Timestamp of when the round started
/// @return updatedAt Timestamp of when the round was updated
/// @return answeredInRound The round ID in which the answer was computed
function getRoundData(uint80 _roundId)
external
view
override
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
(
roundId,
answer,
startedAt,
updatedAt,
answeredInRound
) = _getRoundData(_roundId);
}

/// @notice Returns the latest Oracle price data.
/// @return roundId The round ID
/// @return answer The Oracle price
/// @return startedAt Timestamp of when the round started
/// @return updatedAt Timestamp of when the round was updated
/// @return answeredInRound The round ID in which the answer was computed
function latestRoundData()
external
view
override
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
(
roundId,
answer,
startedAt,
updatedAt,
answeredInRound
) = _getRoundData(lastCorrectRoundId);
}

/***************************************
Errors
****************************************/

error AddPriceSameBlock();
error NoPriceData();
error OnlyOracleUpdater();
}
6 changes: 6 additions & 0 deletions contracts/contracts/oracle/IOracleReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IOracleReceiver {
function addPrice(uint128 price) external;
}
18 changes: 18 additions & 0 deletions contracts/contracts/oracle/OETHOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { BaseOracle } from "./BaseOracle.sol";

/**
* @title OETH Oracle
* @notice Chainlink style oracle for OETH/ETH
* @author Origin Protocol Inc
*/
contract OETHOracle is BaseOracle {
string public constant override description = "OETH / ETH";

/**
* @param _oracleUpdater Address of the contract that is authorized to add prices
*/
constructor(address _oracleUpdater) BaseOracle(_oracleUpdater) {}
}
Loading