Skip to content

Commit

Permalink
feat: condition included in strike
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnyxos committed Oct 31, 2023
1 parent fd89989 commit 1264cf6
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 290 deletions.
2 changes: 1 addition & 1 deletion lib/redstone-oracles-monorepo
97 changes: 44 additions & 53 deletions script/v2/V2DeployConfig.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ contract V2DeployConfig is HelperV2 {
ConfigMarketV2 memory market = markets[i];
address controller = getController(market.isGenericController);
address depositAsset = getDepositAsset(market.depositAsset);
uint256 strkePrice = stringToUint(market.strikePrice);
uint256 strikePrice = stringToUint(market.strikePrice);
CarouselFactory localFactory = factory;
if (market.isGenericController) {
if (market.isDepeg && strikePrice % 2 ** 1 != 0)
strikePrice += 1;
else if (!market.isDepeg && strikePrice % 2 ** 1 != 1)
strikePrice += 1;
}
(address prem, address collat, uint256 marketId) = localFactory
.createNewCarouselMarket(
CarouselFactory.CarouselMarketConfigurationCalldata(
market.token,
strkePrice,
strikePrice,
market.oracle,
depositAsset,
market.name,
Expand All @@ -98,17 +104,6 @@ contract V2DeployConfig is HelperV2 {
"-----------------------NEXT MARKETS----------------------"
);
}
// if controller is generic, set depeg condition (depeg 2, repeg 1)
if (market.isGenericController) {
if(!market.isGenericController && !market.isDepeg) {
revert("Depeg only supported for generic controller");
}
setDepegCondition(
market.oracle,
marketId,
market.isDepeg
);
}
console2.log("\n");
}
}
Expand All @@ -128,16 +123,16 @@ contract V2DeployConfig is HelperV2 {
ConfigMarketV2 memory market = markets[i];
address controller = getController(market.isGenericController);
address depositAsset = getDepositAsset(market.depositAsset);
uint256 strkePrice = stringToUint(market.strikePrice);
uint256 strikePrice = stringToUint(market.strikePrice);
CarouselFactory localFactory = factory;
if (!localFactory.controllers(controller)) {
console2.log("Controller not whitelisted", controller);
revert("Controller not whitelisted");
}

uint256 previewMarketID = localFactory.getMarketId(
market.token,
strkePrice,
strikePrice,
depositAsset
);
address vault = localFactory.marketIdToVaults(previewMarketID, 0);
Expand All @@ -146,28 +141,14 @@ contract V2DeployConfig is HelperV2 {
console2.log(
"Market: ",
market.token,
strkePrice,
strikePrice,
depositAsset
);
revert("Market already deployed");
}
}
}

function setDepegCondition(
address _oracle,
uint256 _marketId,
bool _isDepegCondition
) public {
if (_isDepegCondition) {
console2.log("Set depeg condition");
IConditionProvider(_oracle).setConditionType(_marketId, 2);
} else {
console2.log("Set repeg condition");
IConditionProvider(_oracle).setConditionType(_marketId, 1);
}
}

function deployEpochs() public {
// ConfigAddressesV2 memory addresses = getConfigAddresses(false);
// IERC20(0x5D59e5837F7e5d0F710178Eda34d9eCF069B36D2).approve(0x16B8004f5440f95D3347e55776066032a701c0E8, type(uint256).max);
Expand Down Expand Up @@ -236,16 +217,16 @@ contract V2DeployConfig is HelperV2 {
);
revert("Not enough allowance");
}
// if (
// IERC20(y2k).allowance(configAddresses.policy, address(pausableFactory)) <
// configVariables.totalAmountOfEmittedTokens
// ) {
// console2.log(
// "Not enough allowance",
// IERC20(y2k).allowance(address(this), address(pausableFactory))
// );
// revert("Not enough allowance");
// }
// if (
// IERC20(y2k).allowance(configAddresses.policy, address(pausableFactory)) <
// configVariables.totalAmountOfEmittedTokens
// ) {
// console2.log(
// "Not enough allowance",
// IERC20(y2k).allowance(address(this), address(pausableFactory))
// );
// revert("Not enough allowance");
// }

if (epochs.length != configVariables.amountOfNewEpochs) {
console.log("epochs.length", epochs.length);
Expand All @@ -259,7 +240,7 @@ contract V2DeployConfig is HelperV2 {
ConfigEpochWithEmission memory epoch = epochs[i];
CarouselFactory localFactory = factory;
// epoch.isGenericController
// ?
// ?
// keccak256(abi.encodePacked(epoch.name)) == keccak256(abi.encodePacked("y2kVST_984_WETH*")) ?
// factory :
// pausableFactory
Expand Down Expand Up @@ -335,22 +316,32 @@ contract V2DeployConfig is HelperV2 {
);
}

uint256[] public marketIds =
[
102062669946436220800282965814418861703520361600036198831171353773735437582898,
19463494430787374236800916328272982194211552759358049028102840161626064590799,
43365822659564324551460842388001340228617494417519860688859686053122333904688,
2331975465739044783693470748756652505041645629776698023032314532662570347594
];

uint256[] public marketIds = [
102062669946436220800282965814418861703520361600036198831171353773735437582898,
19463494430787374236800916328272982194211552759358049028102840161626064590799,
43365822659564324551460842388001340228617494417519860688859686053122333904688,
2331975465739044783693470748756652505041645629776698023032314532662570347594
];

function deployNullEpoch() public {
// loop thwourh marketIds
for (uint256 i = 0; i < marketIds.length; ++i) {
uint256 marketId = marketIds[i];
(uint256 epochId, address[2] memory vaults) = factory.createEpochWithEmissions(marketId, uint40(block.timestamp + uint256(10 minutes)), uint40(block.timestamp + uint256(15 minutes)), 1, 0, 0);
Carousel(vaults[0]).getDepositQueueLength() > 0 ? Carousel(vaults[0]).mintDepositInQueue(epochId, 100) : console2.log("No deposit in queue");
Carousel(vaults[1]).getDepositQueueLength() > 0 ? Carousel(vaults[1]).mintDepositInQueue(epochId, 100) : console2.log("No deposit in queue");
(uint256 epochId, address[2] memory vaults) = factory
.createEpochWithEmissions(
marketId,
uint40(block.timestamp + uint256(10 minutes)),
uint40(block.timestamp + uint256(15 minutes)),
1,
0,
0
);
Carousel(vaults[0]).getDepositQueueLength() > 0
? Carousel(vaults[0]).mintDepositInQueue(epochId, 100)
: console2.log("No deposit in queue");
Carousel(vaults[1]).getDepositQueueLength() > 0
? Carousel(vaults[1]).mintDepositInQueue(epochId, 100)
: console2.log("No deposit in queue");
}
}
}
7 changes: 0 additions & 7 deletions src/v2/interfaces/IConditionProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,4 @@ interface IConditionProvider {
external
view
returns (uint80, int256, uint256, uint256, uint80);

function marketIdToConditionType(uint256 _marketId)
external
view
returns (uint256);

function setConditionType(uint256 _marketId, uint256 _condition) external;
}
32 changes: 14 additions & 18 deletions src/v2/oracles/individual/CVIPriceProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ contract CVIPriceProvider is Ownable, IConditionProvider {
uint256 public immutable decimals;
string public description;

mapping(uint256 => uint256) public marketIdToConditionType;

event MarketConditionSet(uint256 indexed marketId, uint256 conditionType);

constructor(address _priceFeed, uint256 _timeOut, uint256 _decimals) {
Expand All @@ -25,19 +23,6 @@ contract CVIPriceProvider is Ownable, IConditionProvider {
description = "CVI";
}

/*//////////////////////////////////////////////////////////////
ADMIN
//////////////////////////////////////////////////////////////*/
function setConditionType(
uint256 _marketId,
uint256 _condition
) external onlyOwner {
if (marketIdToConditionType[_marketId] != 0) revert ConditionTypeSet();
if (_condition != 1 && _condition != 2) revert InvalidInput();
marketIdToConditionType[_marketId] = _condition;
emit MarketConditionSet(_marketId, _condition);
}

/*//////////////////////////////////////////////////////////////
PUBLIC
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -91,12 +76,23 @@ contract CVIPriceProvider is Ownable, IConditionProvider {
*/
function conditionMet(
uint256 _strike,
uint256 _marketId
uint256 /* _marketId */
) public view virtual returns (bool, int256 price) {
uint256 conditionType = marketIdToConditionType[_marketId];
uint256 conditionType = _strike % 2 ** 1;
assembly {
// conditionType := and(
// 0x000000000000000000000000000000000000000000000000000000000000000f,
// _strike
// )
_strike := and(
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0,
_strike
)
}

price = getLatestPrice();
if (conditionType == 1) return (int256(_strike) < price, price);
else if (conditionType == 2) return (int256(_strike) > price, price);
else if (conditionType == 0) return (int256(_strike) > price, price);
else revert ConditionTypeNotSet();
}

Expand Down
32 changes: 15 additions & 17 deletions src/v2/oracles/individual/ChainlinkPriceProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {IVaultFactoryV2} from "../../interfaces/IVaultFactoryV2.sol";
import {IConditionProvider} from "../../interfaces/IConditionProvider.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

import "forge-std/console.sol";

contract ChainlinkPriceProvider is Ownable, IConditionProvider {
uint16 private constant _GRACE_PERIOD_TIME = 3600;
uint256 public immutable timeOut;
Expand All @@ -20,8 +22,6 @@ contract ChainlinkPriceProvider is Ownable, IConditionProvider {
uint256 public immutable decimals;
string public description;

mapping(uint256 => uint256) public marketIdToConditionType;

event MarketConditionSet(uint256 indexed marketId, uint256 conditionType);

constructor(
Expand All @@ -42,19 +42,6 @@ contract ChainlinkPriceProvider is Ownable, IConditionProvider {
description = priceFeed.description();
}

/*//////////////////////////////////////////////////////////////
ADMIN
//////////////////////////////////////////////////////////////*/
function setConditionType(
uint256 _marketId,
uint256 _condition
) external onlyOwner {
if (marketIdToConditionType[_marketId] != 0) revert ConditionTypeSet();
if (_condition != 1 && _condition != 2) revert InvalidInput();
marketIdToConditionType[_marketId] = _condition;
emit MarketConditionSet(_marketId, _condition);
}

/*//////////////////////////////////////////////////////////////
PUBLIC
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -122,10 +109,21 @@ contract ChainlinkPriceProvider is Ownable, IConditionProvider {
uint256 _strike,
uint256 _marketId
) public view virtual returns (bool, int256 price) {
uint256 conditionType = marketIdToConditionType[_marketId];
uint256 conditionType = _strike % 2 ** 1;
assembly {
// conditionType := and(
// 0x000000000000000000000000000000000000000000000000000000000000000f,
// _strike
// )
_strike := and(
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0,
_strike
)
}

price = getLatestPrice();
if (conditionType == 1) return (int256(_strike) < price, price);
else if (conditionType == 2) return (int256(_strike) > price, price);
else if (conditionType == 0) return (int256(_strike) > price, price);
else revert ConditionTypeNotSet();
}

Expand Down
35 changes: 15 additions & 20 deletions src/v2/oracles/individual/DIAPriceProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ contract DIAPriceProvider is Ownable, IConditionProvider {
uint256 public immutable decimals;
string public constant description = "BTC/USD";

mapping(uint256 => uint256) public marketIdToConditionType;

event MarketConditionSet(uint256 indexed marketId, uint256 conditionType);

constructor(address _priceFeed, uint256 _decimals) {
Expand All @@ -23,19 +21,6 @@ contract DIAPriceProvider is Ownable, IConditionProvider {
decimals = _decimals;
}

/*//////////////////////////////////////////////////////////////
ADMIN
//////////////////////////////////////////////////////////////*/
function setConditionType(
uint256 _marketId,
uint256 _condition
) external onlyOwner {
if (marketIdToConditionType[_marketId] != 0) revert ConditionTypeSet();
if (_condition != 1 && _condition != 2) revert InvalidInput();
marketIdToConditionType[_marketId] = _condition;
emit MarketConditionSet(_marketId, _condition);
}

/*//////////////////////////////////////////////////////////////
PUBLIC
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -67,19 +52,29 @@ contract DIAPriceProvider is Ownable, IConditionProvider {
* @dev The strike is hashed as an int256 to enable comparison vs. price for earthquake
and conditional check vs. strike to ensure vaidity
* @param _strike Strike price
* @param _marketId Market ID
* @return condition boolean If condition is met i.e. strike > price
* @return price current price for token
*/
function conditionMet(
uint256 _strike,
uint256 _marketId
uint256 /* _marketId */
) public view virtual returns (bool condition, int256 price) {
uint256 conditionType = marketIdToConditionType[_marketId];
(price, ) = _getLatestPrice();
uint256 conditionType = _strike % 2 ** 1;

assembly {
// conditionType := and(
// 0x00000000000000000000000000000000000000000000000000000000000000f,
// _strike
// )
_strike := and(
0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0,
_strike
)
}

(price, ) = _getLatestPrice();
if (conditionType == 1) return (int256(_strike) < price, price);
else if (conditionType == 2) return (int256(_strike) > price, price);
else if (conditionType == 0) return (int256(_strike) > price, price);
else revert ConditionTypeNotSet();
}

Expand Down
Loading

0 comments on commit 1264cf6

Please sign in to comment.