Skip to content

Commit

Permalink
compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Fett authored and Nicholas Fett committed Sep 18, 2024
1 parent bdf80a5 commit d9e4c71
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 30 deletions.
36 changes: 28 additions & 8 deletions contracts/SampleCPIUser.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;
pragma solidity 0.8.24;

import "../interfaces/IBlobstreamO.sol";
import "./dependencies/IBlobstreamO.sol";


// For this contract, you have low risk in not changing a good value, inflation rarely sky rockets, but huge risk if it is uncapped in changes
// so you can pause it, the system limits an update to 10% change per day (in case even guardian fails)

// this contract has a pause button from a guardian
// it does not fallback to anything, but if the price is not updated for 48 hours, the guardian can change the oracle address
// the contract consensus or fallback to a 24 hour delay with no power threshold (if not sure on support)
//data can only go forward in time and must be within 5 minutes old, , must prove it's latest value
// system limited to 10% , can only update price once a day (inlfation numbers don't change that much)
//data can only go forward in time and must be within 15 minutes old, , must prove it's latest value

contract SampleCPIUser {
IBlobstreamO public blobstreamO;
Expand Down Expand Up @@ -43,19 +47,26 @@ contract SampleCPIUser {
Signature[] calldata _sigs
) external {
require(!paused, "contract paused");
require(block.timestamp - priceData[priceData.length - 1].timestamp > 1 days); //can only be updated once daily
require(_attestData.queryId == queryId, "Invalid queryId");
blobstreamO.verifyOracleData(_attestData, _currentValidatorSet, _sigs);
uint256 _price = abi.decode(_attestData.report.value, (uint256));
if(_attestData.report.aggregatePower < blobstreamO.powerThreshold()){//if not consensus data
require(_attestData.attestationTimestamp - _attestData.report.timestamp >= 15 minutes);//must be at least 15 minutes old
require(_attestData.report.aggregatePower > blobstreamO.powerThreshold()/2);//must have >1/3 aggregate power
require(_attestData.attestationTimestamp - _attestData.report.timestamp >= 24 hours);//must be at least one day old
require(_attestData.report.nextTimestamp == 0 ||
_attestData.attestationTimestamp - _attestData.report.nextTimestamp < 15 minutes);//cannot have newer data you can push
_attestData.attestationTimestamp - _attestData.report.nextTimestamp < 24 hours);//cannot have newer data you can push
}else{
require(_attestData.report.nextTimestamp == 0, "should be no newer timestamp"); // must push the newest data
}
require(block.timestamp - _attestData.attestationTimestamp < 10 minutes);//data cannot be more than 10 minutes old (the relayed attestation)
require(block.timestamp - _attestData.attestationTimestamp < 15 minutes);//data cannot be more than 10 minutes old (the relayed attestation)
require(_attestData.report.timestamp > priceData[priceData.length - 1].timestamp);//cannot go back in time
if(_percentChange(priceData[priceData.length - 1].price,_price) > 10){
if(priceData[priceData.length - 1].price > _price){
_price = 90 * priceData[priceData.length - 1].price / 100 ;
}else{
_price = 110 * priceData[priceData.length - 1].price / 100;
}
}
priceData.push(PriceData(
_price,
_attestData.report.timestamp,
Expand All @@ -67,6 +78,15 @@ contract SampleCPIUser {
);
}

function _percentChange(uint256 _a, uint256 _b) internal pure returns(uint256 _res){
if(_a > _b){
_res = (1000000 * _a - 1000000 * _b) / _a;
}
else{
_res = (1000000 * _b - 1000000 * _a) / _b;
}
_res = 100 * _res / 1000000;
}
function getCurrentPriceData() external view returns (PriceData memory) {
return priceData[priceData.length - 1];
}
Expand Down
55 changes: 41 additions & 14 deletions contracts/SampleEVMCallUser.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;
pragma solidity 0.8.24;

import "../interfaces/IBlobstreamO.sol";
import "./dependencies/IBlobstreamO.sol";


// this contract has a pause button from a guardian
// for our oracle, if its not updated for 24 hours, the guardian can change the oracle address
// a governance address can change the guardian or oracle with a 7 day delay
// the contract is always on a delay (no finality), so you have 1 hour delay with 1/3 aggregate power threshold

//example user - liquity
Expand All @@ -15,6 +15,13 @@ contract SampleEVMCalleUser {
bytes32 public queryId;
bool public paused;
address public guardian;
address public governance;

//for updating
address public proposedGuardian;
address public proposedOracle;
uint256 public updateGuardianTimestamp;
uint256 public updateOracleTimestamp;

event OracleUpdated(uint256 price, uint256 timestamp, uint256 aggregatePower);

Expand All @@ -27,17 +34,43 @@ contract SampleEVMCalleUser {
uint256 relayTimestamp;
}

constructor(address _blobstreamO, bytes32 _queryId, address _guardian) {
constructor(address _blobstreamO, bytes32 _queryId, address _guardian, address _governance) {
blobstreamO = IBlobstreamO(_blobstreamO);
queryId = _queryId;
guardian = _guardian;
governance = _governance;
}

function pauseContract() external{
function togglePause() external{
require(msg.sender == guardian, "should be guardian");
paused = true;
paused = !paused;
}

function changeGuardian(address _newGuardian) external{
require(msg.sender == governance);
if(proposedGuardian == address(0)){
proposedGuardian = _newGuardian;
updateGuardianTimestamp = block.timestamp;
}else{
require(block.timestamp - updateGuardianTimestamp > 7 days);
guardian = proposedGuardian;
proposedGuardian = address(0);
}
}

function changeOracle(address _newOracle) external{
require(msg.sender == governance);
if(proposedOracle == address(0)){
proposedOracle = _newOracle;
updateOracleTimestamp = block.timestamp;
}else{
require(block.timestamp - updateOracleTimestamp > 7 days);
blobstreamO = IBlobstreamO(proposedOracle);
proposedOracle = address(0);
}
}


function updateOracleData(
OracleAttestationData calldata _attestData,
Validator[] calldata _currentValidatorSet,
Expand All @@ -47,14 +80,8 @@ contract SampleEVMCalleUser {
require(_attestData.queryId == queryId, "Invalid queryId");
blobstreamO.verifyOracleData(_attestData, _currentValidatorSet, _sigs);
uint256 _price = abi.decode(_attestData.report.value, (uint256));
if(_attestData.report.aggregatePower < blobstreamO.powerThreshold()){//if not consensus data
require(_attestData.attestationTimestamp - _attestData.report.timestamp >= 15 minutes);//must be at least 15 minutes old
require(_attestData.report.aggregatePower > blobstreamO.powerThreshold()/2);//must have >1/3 aggregate power
require(_attestData.report.nextTimestamp == 0 ||
_attestData.attestationTimestamp - _attestData.report.nextTimestamp < 15 minutes);//cannot have newer data you can push
}else{
require(_attestData.report.nextTimestamp == 0, "should be no newer timestamp"); // must push the newest data
}
require(_attestData.attestationTimestamp - _attestData.report.timestamp >= 1 hours);//must be at least an hour old for finality
require(_attestData.report.aggregatePower > blobstreamO.powerThreshold()/2);//must have >1/3 aggregate power
require(block.timestamp - _attestData.attestationTimestamp < 10 minutes);//data cannot be more than 10 minutes old (the relayed attestation)
require(_attestData.report.timestamp > priceData[priceData.length - 1].timestamp);//cannot go back in time
priceData.push(PriceData(
Expand Down
12 changes: 6 additions & 6 deletions contracts/SampleFallbackOracleUser.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;
pragma solidity 0.8.24;

import "../interfaces/IBlobstreamO.sol";
import "./dependencies/IBlobstreamO.sol";


// this contract has a pause button from a guardian. Just pauses centralized oracle for 24 hours (then 24 hr before can pause again)
Expand All @@ -22,6 +22,7 @@ contract SampleFallbackOracleUser {
bool public paused;
uint256 public pauseTimestamp;
address public guardian;
address public centralizedOracle;

event OracleUpdated(uint256 price, uint256 timestamp, uint256 aggregatePower);

Expand All @@ -42,14 +43,13 @@ contract SampleFallbackOracleUser {
}

function pauseContract() external{
require(block.timestamp - pauseTimestamp > 2 days, "must be 24 hours between pauses")
require(block.timestamp - pauseTimestamp > 2 days, "must be 24 hours between pauses");
require(msg.sender == guardian, "should be guardian");

pauseTimestamp = block.timestamp;
}

function changeFallback(address newOracle) external{
if((block.timestamp - priceData[priceData.length - 1]) < 7 days){
if((block.timestamp - priceData[priceData.length - 1].timestamp) < 7 days){
blobstreamO = IBlobstreamO(newOracle);
}
}
Expand All @@ -62,7 +62,7 @@ contract SampleFallbackOracleUser {
) external {
require(_attestData.report.timestamp > priceData[priceData.length - 1].timestamp, "cannot go back in time");//cannot go back in time
uint256 _price = abi.decode(_attestData.report.value, (uint256));
if((block.timestamp - pauseTimestamp) < 24 hours && (block.timestamp - priceData[priceData.length - 1]) < 1 hours){
if((block.timestamp - pauseTimestamp) < 24 hours && (block.timestamp - priceData[priceData.length - 1].timestamp) < 1 hours){
require(msg.sender == centralizedOracle, "must be proper signer");
priceData.push(PriceData(
_price,
Expand Down
5 changes: 3 additions & 2 deletions contracts/SamplePredictionMarketUser.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;
pragma solidity 0.8.24;

import "./dependencies/IBlobstreamO.sol";

import "../interfaces/IBlobstreamO.sol";

// this contract has a pause button from a guardian (if paused, it should settle invalid or go to governance)
// the contract consensus or fallback to a 24 hour delay with no aggregate power threshold (since you don't know what the question is)
Expand Down

0 comments on commit d9e4c71

Please sign in to comment.