Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: chrismaree <christopher.maree@gmail.com>
  • Loading branch information
chrismaree committed Aug 28, 2023
1 parent 95533b2 commit 2f4845d
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 70 deletions.
18 changes: 9 additions & 9 deletions src/OevOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ abstract contract OevOracle is IOevOracle {
}

function updateAnswer() public {
require(_canUpdate(cachedLatestTimestamp));
require(canUpdate(msg.sender, cachedLatestTimestamp));
int256 _latestAnswer = sourceAdapter.latestAnswer();
uint256 _latestTimestamp = sourceAdapter.latestTimestamp();

require(_latestAnswer != cachedLatestAnswer || _latestTimestamp != cachedLatestTimestamp);

require(_validateUpdate(_latestAnswer, _latestTimestamp));
require(validateUpdate(_latestAnswer, _latestTimestamp));

cachedLatestAnswer = _latestAnswer;
cachedLatestTimestamp = _latestTimestamp;
}

function rawLatestAnswer() public view override returns (int256) {
if (_canReturnCachedValue(cachedLatestTimestamp)) return cachedLatestAnswer;
if (canReturnCachedValue(cachedLatestTimestamp)) return cachedLatestAnswer;
return sourceAdapter.latestAnswer();
}

function rawLatestTimestamp() public view override returns (uint256) {
if (_canReturnCachedValue(cachedLatestTimestamp)) return cachedLatestTimestamp;
if (canReturnCachedValue(cachedLatestTimestamp)) return cachedLatestTimestamp;
return sourceAdapter.latestTimestamp();
}

function setSourceOracleAdapter(IBaseOracleAdapter _sourceAdapter) external {
require(_canSetSourceOracleAdapter());
require(canSetSourceOracleAdapter(msg.sender));
sourceAdapter = _sourceAdapter;
}

function _canUpdate(uint256 cachedLatestTimestamp) internal view virtual returns (bool);
function canUpdate(address caller, uint256 cachedLatestTimestamp) public view virtual returns (bool);

function _validateUpdate(int256 _latestAnswer, uint256 _latestTimestamp) internal view virtual returns (bool);
function validateUpdate(int256 _latestAnswer, uint256 _latestTimestamp) public view virtual returns (bool);

function _canReturnCachedValue(uint256 _cachedValue) internal view virtual returns (bool);
function canReturnCachedValue(uint256 _cachedValue) public view virtual returns (bool);

function _canSetSourceOracleAdapter() internal view virtual returns (bool);
function canSetSourceOracleAdapter(address caller) public view virtual returns (bool);
}
6 changes: 3 additions & 3 deletions src/adapters/BaseDestinationOracleAdapter.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pragma solidity 0.8.17;

import "../controllers/BaseUpdateController.sol";
import "../controllers/BaseController.sol";
import "../interfaces/IBaseOracleAdapter.sol";

contract BaseDestinationOracleAdapter is BaseUpdateController, IBaseOracleAdapter {
constructor(address _sourceAdapter) BaseUpdateController(_sourceAdapter) {}
contract BaseDestinationOracleAdapter is BaseController, IBaseOracleAdapter {
constructor(address _sourceAdapter) BaseController(_sourceAdapter) {}

function decimals() public view override returns (uint8) {
return 18;
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/chainlink/ChainlinkDestinationOracleAdapter.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;

import "../../controllers/BaseUpdateController.sol";
import "../../controllers/BaseController.sol";
import "../../interfaces/IBaseOracleAdapter.sol";
import "../../interfaces/chainlink/IAggregatorV3.sol";
import "../../OevOracle.sol";

contract ChainlinkDestinationOracleAdapter is IAggregatorV3, BaseUpdateController {
contract ChainlinkDestinationOracleAdapter is IAggregatorV3, BaseController {
uint8 public override decimals;

// This should sit between OSM and spotter.
constructor(uint8 _decimals, address _sourceAdapter) BaseUpdateController(_sourceAdapter) {
constructor(uint8 _decimals, address _sourceAdapter) BaseController(_sourceAdapter) {
decimals = _decimals;
}

Expand Down
6 changes: 3 additions & 3 deletions src/adapters/makerdao/ChronicleDestinationOracleAdapter.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;

import "../../controllers/BaseUpdateController.sol";
import "../../controllers/BaseController.sol";
import "../../interfaces/chronicle/IMedian.sol";

contract ChronicleDestinationOracleAdapter is BaseUpdateController, IMedian {
contract ChronicleDestinationOracleAdapter is BaseController, IMedian {
uint8 public decimals;

constructor(uint8 _decimals, address _sourceAdapter) BaseUpdateController(_sourceAdapter) {
constructor(uint8 _decimals, address _sourceAdapter) BaseController(_sourceAdapter) {
decimals = _decimals;
}

Expand Down
59 changes: 59 additions & 0 deletions src/controllers/BaseController.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;

import "openzeppelin-contracts/contracts/access/Ownable.sol";
import "../OevOracle.sol";
import "../interfaces/IBaseController.sol";

contract BaseController is IBaseController, OevOracle, Ownable {
constructor(address _sourceAdapter) OevOracle(_sourceAdapter) {}

mapping(address => bool) public updaters;

uint256 public permissionWindow = 10 minutes;

function setUpdater(address _updater, bool allowed) public onlyOwner {
updaters[_updater] = allowed;
}

function setPermissionWindow(uint256 _permissionWindow) public onlyOwner {
permissionWindow = _permissionWindow;
}

function canUpdate(address caller, uint256 cachedLatestTimestamp)
public
view
override(IBaseController, OevOracle)
returns (bool)
{
return updaters[caller] || (block.timestamp - cachedLatestTimestamp) > permissionWindow;
}

function validateUpdate(int256 rawLatestAnswer, uint256 rawLatestTimestamp)
public
view
override(IBaseController, OevOracle)
returns (bool)
{
// sample dummy check.
return rawLatestAnswer > 0 && rawLatestTimestamp > 0;
}

function canReturnCachedValue(uint256 cachedLatestTimestamp)
public
view
override(IBaseController, OevOracle)
returns (bool)
{
return (block.timestamp - cachedLatestTimestamp < permissionWindow);
}

function canSetSourceOracleAdapter(address caller)
public
view
override(IBaseController, OevOracle)
returns (bool)
{
return caller == owner();
}
}
44 changes: 0 additions & 44 deletions src/controllers/BaseUpdateController.sol

This file was deleted.

13 changes: 13 additions & 0 deletions src/interfaces/IBaseController.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;

interface IBaseController {
function canUpdate(address caller, uint256 cachedLatestTimestamp) external view returns (bool);

//TODO: think if we actually want this validation here. this means we have 2 forms of validation, one within the source adapter and the second here. is this needed?
function validateUpdate(int256 rawLatestAnswer, uint256 rawLatestTimestamp) external view returns (bool);

function canReturnCachedValue(uint256 cachedLatestTimestamp) external view returns (bool);

function canSetSourceOracleAdapter(address caller) external view returns (bool);
}
3 changes: 0 additions & 3 deletions src/interfaces/IBaseDestinationOracleAdapter.sol

This file was deleted.

3 changes: 0 additions & 3 deletions src/interfaces/IBaseIntegrationController.sol

This file was deleted.

1 change: 1 addition & 0 deletions src/interfaces/chainlink/IAggregatorV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface IAggregatorV3 {

function decimals() external view returns (uint8);

// Other chainlink functions we dont need.
// function latestRoundData()
// external
// view
Expand Down
6 changes: 4 additions & 2 deletions src/interfaces/chronicle/IMedian.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ interface IMedian {
function bar() external view returns (uint256);

function read() external view returns (uint256);

function peek() external view returns (uint256, bool);

// Other Medianer functions we dont need.
// function bud(address) external view returns (uint256);

// function orcl(address) external view returns (uint256);

function peek() external view returns (uint256, bool);

// function slot(uint8) external view returns (address);

// function wards(address) external view returns (uint256);
Expand Down

0 comments on commit 2f4845d

Please sign in to comment.