Contract 0x04706de6ce851a284b569ebae2e258225d952368

 
Txn Hash Method
Block
From
To
Value [Txn Fee]
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174cAdjust Collatera...576574622023-02-03 10:44:323 days 14 hrs ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00009047
0xd5fe101aab869a53430c90f705eef3ea1ad3ed1b6fa60d86f998a845c53e0239Adjust Collatera...576275812023-02-03 8:00:133 days 16 hrs ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.0010321
0x017b7b551aab75a0cd701c5c50b96feb878ba817c9599a589ef798c15432c62fAdjust Collatera...575591772023-02-03 0:36:324 days 20 mins ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00010885
0x67c28ed858e8b2df95570befbe4ce34cea20c5fd87216271e5f70060c0ef4d12Adjust Collatera...574949112023-02-02 18:25:334 days 6 hrs ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.0001327
0xe0b924a720b110e1d38bcee8b6fdddffd164f1d486ced6866eb438b2f6ee0fabAdjust Collatera...574929902023-02-02 18:16:334 days 6 hrs ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00015214
0xeb35a27bd9f48685b109083de7b5c0f0e838ebdc1eb829f75350b600229213c1Adjust Collatera...572916032023-02-02 0:42:325 days 14 mins ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00010523
0x269a2e092fa30d39381395308555b56a4b355290fa0a618eebcd7b468a44ab93Adjust Collatera...558053842023-01-27 8:00:1610 days 16 hrs ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00092755
0xe8918fb9da02f8428cd084017acbcc9ce47df1598a90aed789b8cfa298ffb1d1Adjust Collatera...558053752023-01-27 8:00:1310 days 16 hrs ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00092714
0x1f11f58397baa68e6f2702447e8a4cbd1beaea8dc68aef546ded3fece6baa86aAdjust Collatera...552331892023-01-24 22:32:1113 days 2 hrs ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00012336
0x6c5e56d9d3b3b97e39dd4412159627666e322659a68157fe4110e97f54237d89Adjust Collatera...552327262023-01-24 22:30:1213 days 2 hrs ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00085921
0xe3c73abe6f1125ff0cb3fb9651454454c4712874564193b9bef924f5a2b301f0Adjust Collatera...552327132023-01-24 22:30:0813 days 2 hrs ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00090862
0xc3f29c7100dec33fd63d11a1bc9bd5e4559a38f943316e1980da7f9ed8399ca2Adjust Collatera...543938892023-01-20 23:51:3117 days 1 hr ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00011141
0x3efd71903b18bef355f31cbe30455ec0de5c8a4d1fad4582f8abae742d343d7fAdjust Collatera...542438882023-01-20 8:00:1517 days 16 hrs ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00089037
0x9f2510f0978f6af101e7a7376727ddc62f67248dfce45fae1f0e798531e976eaAdjust Collatera...528911912023-01-14 0:46:1024 days 11 mins ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00023096
0xe8a67135f39c105651ffca61c4c282aa661ce00c5c8a0bb7645b7dd1460d67e6Adjust Collatera...528907312023-01-14 0:44:1124 days 13 mins ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00036133
0x1515b8f291693d1cf21f4c05ed061540d4a2d50c162f16e483f4f504129ea441Adjust Collatera...528904962023-01-14 0:43:1024 days 14 mins ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00074605
0x0900582927d89ddebbe93e2432acbfe5eaddfcca8b57bdaa136fc94ae64ca1e7Adjust Collatera...528898412023-01-14 0:40:2724 days 16 mins ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00089009
0xc7fd4d1a7cf216c2c417f6d7d53f5e8fef126e6e27a9e16970bcc5558101af5bAdjust Collatera...528898122023-01-14 0:40:2024 days 16 mins ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00088927
0x11bd1ea6091cb85ff332b66fbd3e2417f5d2c9dc36925e6fb123034daffebb87Adjust Collatera...528898002023-01-14 0:40:1724 days 16 mins ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00083181
0x18d0b53db63085f019ae12d910ae336d39c5f95ca162842cefd4d009ed6e9677Adjust Collatera...528897872023-01-14 0:40:1324 days 16 mins ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00089078
0x84a7a619da5c288b3846a7aeaba909447bf8267cf6a0c0f68cde3a8e2282e3c0Adjust Collatera...528894392023-01-14 0:38:4624 days 18 mins ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00010273
0xee48da940dd81333712b98de0537d1227901a4112f3d18709419e036b62126e6Adjust Collatera...528893862023-01-14 0:38:3324 days 18 mins ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.0000987
0xf8b151cb5d636816932365504ae4072d6481445241c6eed7cbaa8a231a20ae62Adjust Collatera...527223832023-01-13 8:00:1524 days 16 hrs ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00095079
0x4982896826a701e1b746848f2b019b218ebe7bde7670dfd0cbac2ca791a5b585Adjust Collatera...527223782023-01-13 8:00:1124 days 16 hrs ago0xbaa6faf7d34fa64b7813521aad0657767da04e3e IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00095079
0x5dd98671785afc3d513ec3c1094c7caddfbe54926f4ef20a434778293626a40bAdjust Collatera...526058822023-01-12 17:41:3325 days 7 hrs ago0xf308f68a42eb577133df915d995dd8c0aee97b42 IN  0x04706de6ce851a284b569ebae2e258225d9523680 ETH0.00008951
[ Download CSV Export 
Latest 25 internal transaction
Parent Txn Hash Block From To Value
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d9523680x594bd4ec29f7900ae29549c140ac53b5240d40190 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0x048603543a0fd41b56b831b80981addb19c1ea300 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xc10b976c671ce9bff0723611f01422acbae100a50 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0x749a3624ad2a001f935e3319743f53ecc74663580 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xba1880cffe38dd13771cb03de896460baf7da1e70 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xca19f26c52b11186b4b1e76a662a14da5149ea5a0 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xca19f26c52b11186b4b1e76a662a14da5149ea5a0 ETH
0x4b75b14a7d6cf471f5a278b54f9dddaa9a91ac5c12fe9811b65f59ffcdb4174c576574622023-02-03 10:44:323 days 14 hrs ago 0x04706de6ce851a284b569ebae2e258225d9523680x594bd4ec29f7900ae29549c140ac53b5240d40190 ETH
0x435160707f51726a35eaf7ff60b9ad529cac8a221c94e226cc1f1a092333664e576332482023-02-03 8:30:303 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
0x435160707f51726a35eaf7ff60b9ad529cac8a221c94e226cc1f1a092333664e576332482023-02-03 8:30:303 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
0x435160707f51726a35eaf7ff60b9ad529cac8a221c94e226cc1f1a092333664e576332482023-02-03 8:30:303 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d9523680x594bd4ec29f7900ae29549c140ac53b5240d40190 ETH
0x435160707f51726a35eaf7ff60b9ad529cac8a221c94e226cc1f1a092333664e576332482023-02-03 8:30:303 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
0x435160707f51726a35eaf7ff60b9ad529cac8a221c94e226cc1f1a092333664e576332482023-02-03 8:30:303 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d9523680x594bd4ec29f7900ae29549c140ac53b5240d40190 ETH
0x435160707f51726a35eaf7ff60b9ad529cac8a221c94e226cc1f1a092333664e576332482023-02-03 8:30:303 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0x048603543a0fd41b56b831b80981addb19c1ea300 ETH
0x435160707f51726a35eaf7ff60b9ad529cac8a221c94e226cc1f1a092333664e576332482023-02-03 8:30:303 days 16 hrs ago 0xc10b976c671ce9bff0723611f01422acbae100a5 0x04706de6ce851a284b569ebae2e258225d9523680 ETH
0x5df6f126fd550aef3eed4c42d5f3b8ae75f11dcad313950062d69f9f0eff017c576323062023-02-03 8:25:313 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
0x5df6f126fd550aef3eed4c42d5f3b8ae75f11dcad313950062d69f9f0eff017c576323062023-02-03 8:25:313 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
0x5df6f126fd550aef3eed4c42d5f3b8ae75f11dcad313950062d69f9f0eff017c576323062023-02-03 8:25:313 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d9523680x594bd4ec29f7900ae29549c140ac53b5240d40190 ETH
0x5df6f126fd550aef3eed4c42d5f3b8ae75f11dcad313950062d69f9f0eff017c576323062023-02-03 8:25:313 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
0x5df6f126fd550aef3eed4c42d5f3b8ae75f11dcad313950062d69f9f0eff017c576323062023-02-03 8:25:313 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d9523680x594bd4ec29f7900ae29549c140ac53b5240d40190 ETH
0x5df6f126fd550aef3eed4c42d5f3b8ae75f11dcad313950062d69f9f0eff017c576323062023-02-03 8:25:313 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0x048603543a0fd41b56b831b80981addb19c1ea300 ETH
0x5df6f126fd550aef3eed4c42d5f3b8ae75f11dcad313950062d69f9f0eff017c576323062023-02-03 8:25:313 days 16 hrs ago 0xc10b976c671ce9bff0723611f01422acbae100a5 0x04706de6ce851a284b569ebae2e258225d9523680 ETH
0xd5fe101aab869a53430c90f705eef3ea1ad3ed1b6fa60d86f998a845c53e0239576275812023-02-03 8:00:133 days 16 hrs ago 0x04706de6ce851a284b569ebae2e258225d952368 0xff970a61a04b1ca14834a43f5de4533ebddb5cc80 ETH
[ Download CSV Export 
Loading
This contract may be a proxy contract. Click on More Options and select Is this a proxy? to confirm and enable the "Read as Proxy" & "Write as Proxy" tabs.

Contract Source Code Verified (Exact Match)

Contract Name:
OptionRegistry

Compiler Version
v0.8.9+commit.e5eed63a

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion
File 1 of 31 : OptionRegistry.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;

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

import "./tokens/ERC20.sol";
import "./libraries/AccessControl.sol";
import { Types } from "./libraries/Types.sol";
import { CustomErrors } from "./libraries/CustomErrors.sol";
import { OptionsCompute } from "./libraries/OptionsCompute.sol";
import { SafeTransferLib } from "./libraries/SafeTransferLib.sol";
import { OpynInteractions } from "./libraries/OpynInteractions.sol";

import "./interfaces/IOracle.sol";
import "./interfaces/IMarginCalculator.sol";
import "./interfaces/AddressBookInterface.sol";
import { IController, GammaTypes } from "./interfaces/GammaInterface.sol";

/**
 *  @title Contract used for conducting options issuance and settlement as well as collateral management
 *  @dev Interacts with the opyn-rysk gamma protocol via OpynInteractions for options activity. Interacts with
 *       the liquidity pool for collateral and instructions.
 */
contract OptionRegistry is AccessControl {
	///////////////////////////
	/// immutable variables ///
	///////////////////////////

	// address of the opyn oTokenFactory for oToken minting
	address internal immutable oTokenFactory;
	// address of the gammaController for oToken operations
	address public immutable gammaController;
	// address of the collateralAsset
	address public immutable collateralAsset;
	// address of the opyn addressBook for accessing important opyn modules
	AddressBookInterface public immutable addressBook;
	// address of the marginPool, contract for storing options collateral
	address internal immutable marginPool;

	/////////////////////////
	/// dynamic variables ///
	/////////////////////////

	// information of a series
	mapping(address => Types.OptionSeries) public seriesInfo;
	// vaultId that is responsible for a specific series address
	mapping(address => uint256) public vaultIds;
	// issuance hash mapped against the series address
	mapping(bytes32 => address) seriesAddress;
	// vault counter
	uint64 public vaultCount;

	/////////////////////////////////////
	/// governance settable variables ///
	/////////////////////////////////////

	// address of the rysk liquidity pools
	address public liquidityPool;
	// max health threshold for calls
	uint64 public callUpperHealthFactor = 13_000;
	// min health threshold for calls
	uint64 public callLowerHealthFactor = 11_000;
	// max health threshold for puts
	uint64 public putUpperHealthFactor = 12_000;
	// min health threshold for puts
	uint64 public putLowerHealthFactor = 11_000;
	// keeper addresses for this contract
	mapping(address => bool) public keeper;

	//////////////////////////
	/// constant variables ///
	//////////////////////////

	// BIPS
	uint256 private constant MAX_BPS = 10_000;
	// used to convert e18 to e8
	uint256 private constant SCALE_FROM = 10**10;
	// oToken decimals
	uint8 private constant OPYN_DECIMALS = 8;

	/////////////////////////////////////
	/// events && errors && modifiers ///
	/////////////////////////////////////

	event OptionTokenCreated(address token);
	event SeriesRedeemed(address series, uint256 underlyingAmount, uint256 strikeAmount);
	event OptionsContractOpened(address indexed series, uint256 vaultId, uint256 optionsAmount);
	event OptionsContractClosed(address indexed series, uint256 vaultId, uint256 closedAmount);
	event OptionsContractSettled(
		address indexed series,
		uint256 collateralReturned,
		uint256 collateralLost,
		uint256 amountLost
	);
	event VaultLiquidationRegistered(
		address indexed series,
		uint256 vaultId,
		uint256 amountLiquidated,
		uint256 collateralLiquidated
	);

	error NoVault();
	error NotKeeper();
	error NotExpired();
	error HealthyVault();
	error AlreadyExpired();
	error NotLiquidityPool();
	error NonExistentSeries();
	error InvalidCollateral();
	error VaultNotLiquidated();
	error InsufficientBalance();

	constructor(
		address _collateralAsset,
		address _oTokenFactory,
		address _gammaController,
		address _marginPool,
		address _liquidityPool,
		address _addressBook,
		address _authority
	) AccessControl(IAuthority(_authority)) {
		collateralAsset = _collateralAsset;
		if (ERC20(_collateralAsset).decimals() > 18) {
			revert CustomErrors.InvalidDecimals();
		}
		oTokenFactory = _oTokenFactory;
		gammaController = _gammaController;
		marginPool = _marginPool;
		liquidityPool = _liquidityPool;
		addressBook = AddressBookInterface(_addressBook);
	}

	///////////////
	/// setters ///
	///////////////

	/**
	 * @notice Set the liquidity pool address
	 * @param  _newLiquidityPool set the liquidityPool address
	 */
	function setLiquidityPool(address _newLiquidityPool) external {
		_onlyGovernor();
		liquidityPool = _newLiquidityPool;
	}

	/**
	 * @notice Set or revoke a keeper
	 * @param  _target address to become a keeper
	 * @param  _auth accept or revoke
	 */
	function setKeeper(address _target, bool _auth) external {
		_onlyGovernor();
		keeper[_target] = _auth;
	}

	/**
	 * @notice Set the health thresholds of the pool
	 * @param  _putLower the lower health threshold for puts
	 * @param  _putUpper the upper health threshold for puts
	 * @param  _callLower the lower health threshold for calls
	 * @param  _callUpper the upper health threshold for calls
	 */
	function setHealthThresholds(
		uint64 _putLower,
		uint64 _putUpper,
		uint64 _callLower,
		uint64 _callUpper
	) external {
		_onlyGovernor();
		putLowerHealthFactor = _putLower;
		putUpperHealthFactor = _putUpper;
		callLowerHealthFactor = _callLower;
		callUpperHealthFactor = _callUpper;
	}

	//////////////////////////////////////////////////////
	/// access-controlled state changing functionality ///
	//////////////////////////////////////////////////////

	/**
	 * @notice Either retrieves the option token if it already exists, or deploy it
	 * @param  optionSeries the series used for the mint - strike passed in as e18
	 * @return the address of the option
	 */
	function issue(Types.OptionSeries memory optionSeries) external returns (address) {
		_isLiquidityPool();
		// deploy an oToken contract address
		if (optionSeries.expiration <= block.timestamp) {
			revert AlreadyExpired();
		}
		// assumes strike is passed in e18, converts to e8
		uint128 formattedStrike = uint128(
			formatStrikePrice(optionSeries.strike, optionSeries.collateral)
		);
		// create option storage hash
		bytes32 issuanceHash = getIssuanceHash(
			optionSeries.underlying,
			optionSeries.strikeAsset,
			optionSeries.collateral,
			optionSeries.expiration,
			optionSeries.isPut,
			formattedStrike
		);
		// check for an opyn oToken if it doesn't exist deploy it
		address series = OpynInteractions.getOrDeployOtoken(
			oTokenFactory,
			optionSeries.collateral,
			optionSeries.underlying,
			optionSeries.strikeAsset,
			formattedStrike,
			optionSeries.expiration,
			optionSeries.isPut
		);
		// store the option data as a hash
		seriesInfo[series] = Types.OptionSeries(
			optionSeries.expiration,
			formattedStrike,
			optionSeries.isPut,
			optionSeries.underlying,
			optionSeries.strikeAsset,
			optionSeries.collateral
		);
		seriesAddress[issuanceHash] = series;
		emit OptionTokenCreated(series);
		return series;
	}

	/**
	 * @notice Open an options contract using collateral from the liquidity pool
	 * @param  _series the address of the option token to be created
	 * @param  amount the amount of options to deploy - assume in e18
	 * @param  collateralAmount the collateral required for the option - assumes in collateral decimals
	 * @dev only callable by the liquidityPool
	 * @return if the transaction succeeded
	 * @return the amount of collateral taken from the liquidityPool
	 */
	function open(
		address _series,
		uint256 amount,
		uint256 collateralAmount
	) external returns (bool, uint256) {
		_isLiquidityPool();
		// make sure the options are ok to open
		Types.OptionSeries memory series = seriesInfo[_series];
		// assumes strike in e8
		if (series.expiration <= block.timestamp) {
			revert AlreadyExpired();
		}
		// transfer collateral to this contract, collateral will depend on the option type
		SafeTransferLib.safeTransferFrom(series.collateral, msg.sender, address(this), collateralAmount);
		// mint the option token following the opyn interface
		IController controller = IController(gammaController);
		// check if a vault for this option already exists
		uint256 vaultId_ = vaultIds[_series];
		if (vaultId_ == 0) {
			vaultId_ = (controller.getAccountVaultCounter(address(this))) + 1;
			vaultCount++;
		}
		uint256 mintAmount = OpynInteractions.createShort(
			gammaController,
			marginPool,
			_series,
			collateralAmount,
			vaultId_,
			amount,
			1
		);
		emit OptionsContractOpened(_series, vaultId_, mintAmount);
		// transfer the option to the liquidity pool
		SafeTransferLib.safeTransfer(ERC20(_series), msg.sender, mintAmount);
		vaultIds[_series] = vaultId_;
		// returns in collateral decimals
		return (true, collateralAmount);
	}

	/**
	 * @notice Close an options contract (oToken) before it has expired
	 * @param  _series the address of the option token to be burnt
	 * @param  amount the amount of options to burn - assumes in e18
	 * @dev only callable by the liquidityPool
	 * @return if the transaction succeeded
	 */
	function close(address _series, uint256 amount) external returns (bool, uint256) {
		_isLiquidityPool();
		// withdraw and burn
		Types.OptionSeries memory series = seriesInfo[_series];
		// assumes strike in e8
		// make sure the option hasnt expired yet
		if (series.expiration == 0) {
			revert NonExistentSeries();
		}
		if (series.expiration <= block.timestamp) {
			revert AlreadyExpired();
		}
		// get the vault id
		uint256 vaultId = vaultIds[_series];
		if (vaultId == 0) {
			revert NoVault();
		}
		uint256 convertedAmount = OptionsCompute.convertToDecimals(amount, ERC20(_series).decimals());
		// transfer the oToken back to this account
		SafeTransferLib.safeTransferFrom(_series, msg.sender, address(this), convertedAmount);
		// burn the oToken tracking the amount of collateral returned
		uint256 collatReturned = OpynInteractions.burnShort(
			gammaController,
			_series,
			convertedAmount,
			vaultId
		);
		SafeTransferLib.safeTransfer(ERC20(series.collateral), msg.sender, collatReturned);
		emit OptionsContractClosed(_series, vaultId, convertedAmount);
		// returns in collateral decimals
		return (true, collatReturned);
	}

	/**
	 * @notice Settle an options vault
	 * @param  _series the address of the option token to be burnt
	 * @return  if the transaction succeeded
	 * @return  the amount of collateral returned from the vault
	 * @return  the amount of collateral used to pay ITM options on vault settle
	 * @return  number of oTokens that the vault was short
	 * @dev callable by the liquidityPool so that local variables can also be updated
	 */
	function settle(address _series)
		external
		returns (
			bool,
			uint256,
			uint256,
			uint256
		)
	{
		_isLiquidityPool();
		Types.OptionSeries memory series = seriesInfo[_series];
		// strike will be in e8
		if (series.expiration == 0) {
			revert NonExistentSeries();
		}
		// check that the option has expired
		if (series.expiration >= block.timestamp) {
			revert NotExpired();
		}
		// get the vault
		uint256 vaultId = vaultIds[_series];
		// settle the vault
		(uint256 collatReturned, uint256 collatLost, uint256 amountShort) = OpynInteractions.settle(
			gammaController,
			vaultId
		);
		// transfer the collateral back to the liquidity pool
		SafeTransferLib.safeTransfer(ERC20(series.collateral), liquidityPool, collatReturned);
		emit OptionsContractSettled(_series, collatReturned, collatLost, amountShort);
		// assumes in collateral decimals, collateral decimals, e8
		return (true, collatReturned, collatLost, amountShort);
	}

	/**
	 * @notice adjust the collateral held in a specific vault because of health
	 * @param  vaultId the id of the vault to check
	 */
	function adjustCollateral(uint256 vaultId) external {
		_isKeeper();
		(
			bool isBelowMin,
			bool isAboveMax,
			,
			,
			uint256 collateralAmount,
			address _collateralAsset
		) = checkVaultHealth(vaultId);
		if (collateralAsset != _collateralAsset) {
			revert InvalidCollateral();
		}
		if (!isBelowMin && !isAboveMax) {
			revert HealthyVault();
		}
		if (isBelowMin) {
			LiquidityPool(liquidityPool).adjustCollateral(collateralAmount, false);
			if (LiquidityPool(liquidityPool).getBalance(collateralAsset) < collateralAmount) {
				revert CustomErrors.WithdrawExceedsLiquidity();
			}
			// transfer the needed collateral to this contract from the liquidityPool
			SafeTransferLib.safeTransferFrom(
				_collateralAsset,
				liquidityPool,
				address(this),
				collateralAmount
			);
			// increase the collateral in the vault (make sure balance change is recorded in the LiquidityPool)
			OpynInteractions.depositCollat(
				gammaController,
				marginPool,
				_collateralAsset,
				collateralAmount,
				vaultId
			);
		} else if (isAboveMax) {
			LiquidityPool(liquidityPool).adjustCollateral(collateralAmount, true);
			// decrease the collateral in the vault (make sure balance change is recorded in the LiquidityPool)
			OpynInteractions.withdrawCollat(gammaController, _collateralAsset, collateralAmount, vaultId);
			// transfer the excess collateral to the liquidityPool from this address
			SafeTransferLib.safeTransfer(ERC20(_collateralAsset), liquidityPool, collateralAmount);
		}
	}

	/**
	 * @notice adjust the collateral held in a specific vault because of health, using collateral from the caller. Only takes
	 *         from msg.sender, doesnt give them if vault is above the max.
	 * @param  vaultId the id of the vault to check
	 * @dev    this is a safety function, if worst comes to worse any caller can collateralise a vault to save it.
	 */
	function adjustCollateralCaller(uint256 vaultId) external {
		_onlyGuardian();
		(bool isBelowMin, , , , uint256 collateralAmount, address _collateralAsset) = checkVaultHealth(
			vaultId
		);
		if (collateralAsset != _collateralAsset) {
			revert InvalidCollateral();
		}
		if (!isBelowMin) {
			revert HealthyVault();
		}
		// transfer the needed collateral to this contract from the msg.sender
		SafeTransferLib.safeTransferFrom(_collateralAsset, msg.sender, address(this), collateralAmount);
		// increase the collateral in the vault
		OpynInteractions.depositCollat(
			gammaController,
			marginPool,
			_collateralAsset,
			collateralAmount,
			vaultId
		);
	}

	/**
	 * @notice withdraw collateral from a fully liquidated vault
	 * @param  vaultId the id of the vault to check
	 * @dev    this is a safety function, if a vault is liquidated.
	 */
	function wCollatLiquidatedVault(uint256 vaultId) external {
		_isKeeper();
		// get the vault details from the vaultId
		GammaTypes.Vault memory vault = IController(gammaController).getVault(address(this), vaultId);
		require(vault.shortAmounts[0] == 0, "Vault has short positions [amount]");
		require(vault.shortOtokens[0] == address(0), "Vault has short positions [token]");
		require(vault.collateralAmounts[0] > 0, "Vault has no collateral");
		// decrease the collateral in the vault (make sure balance change is recorded in the LiquidityPool)
		OpynInteractions.withdrawCollat(
			gammaController,
			vault.collateralAssets[0],
			vault.collateralAmounts[0],
			vaultId
		);
		// adjust the collateral in the liquidityPool
		LiquidityPool(liquidityPool).adjustCollateral(vault.collateralAmounts[0], true);
		// transfer the excess collateral to the liquidityPool from this address
		SafeTransferLib.safeTransfer(
			ERC20(vault.collateralAssets[0]),
			liquidityPool,
			vault.collateralAmounts[0]
		);
	}

	/**
	 * @notice register a liquidated vault so the collateral allocated is managed
	 * @param  vaultId the id of the vault to register liquidation for
	 * @dev    this is a safety function, if a vault is liquidated to update the collateral assets in the pool
	 */
	function registerLiquidatedVault(uint256 vaultId) external {
		_isKeeper();
		// get the vault liquidation details from the vaultId
		(address series, uint256 amount, uint256 collateralLiquidated) = IController(gammaController)
			.getVaultLiquidationDetails(address(this), vaultId);
		if (series == address(0)) {
			revert VaultNotLiquidated();
		}
		emit VaultLiquidationRegistered(series, vaultId, amount, collateralLiquidated);
		// adjust the collateral in the liquidity pool to reflect the loss
		LiquidityPool(liquidityPool).adjustCollateral(collateralLiquidated, true);
		// clear the liquidation record from gamma controller so as not to double count the liquidation
		IController(gammaController).clearVaultLiquidationDetails(vaultId);
	}

	/////////////////////////////////////////////
	/// external state changing functionality ///
	/////////////////////////////////////////////

	/**
	 * @notice Redeem oTokens for the locked collateral
	 * @param  _series the address of the option token to be burnt and redeemed
	 * @return amount returned
	 */
	function redeem(address _series) external returns (uint256) {
		Types.OptionSeries memory series = seriesInfo[_series];
		// strike will be in e8
		if (series.expiration == 0) {
			revert NonExistentSeries();
		}
		// check that the option has expired
		if (series.expiration >= block.timestamp) {
			revert NotExpired();
		}
		uint256 seriesBalance = ERC20(_series).balanceOf(msg.sender);
		if (seriesBalance == 0) {
			revert InsufficientBalance();
		}
		// transfer the oToken back to this account
		SafeTransferLib.safeTransferFrom(_series, msg.sender, address(this), seriesBalance);
		// redeem
		uint256 collatReturned = OpynInteractions.redeem(
			gammaController,
			marginPool,
			_series,
			seriesBalance
		);
		// assumes in collateral decimals
		return collatReturned;
	}

	///////////////////////
	/// complex getters ///
	///////////////////////

	/**
	 * @notice Send collateral funds for an option to be minted
	 * @dev series.strike should be scaled by 1e8.
	 * @param  series details of the option series
	 * @param  amount amount of options to mint always in e18
	 * @return amount transferred
	 */
	function getCollateral(Types.OptionSeries memory series, uint256 amount)
		external
		view
		returns (uint256)
	{
		IMarginCalculator marginCalc = IMarginCalculator(addressBook.getMarginCalculator());
		uint256 collateralAmount = marginCalc.getNakedMarginRequired(
			series.underlying,
			series.strikeAsset,
			series.collateral,
			amount / SCALE_FROM, // assumes that amount is always in e18
			series.strike, // assumes in e8
			IOracle(addressBook.getOracle()).getPrice(series.underlying),
			series.expiration,
			ERC20(series.collateral).decimals(),
			series.isPut
		);
		// based on this collateral requirement and the health factor get the amount to deposit
		uint256 upperHealthFactor = series.isPut ? putUpperHealthFactor : callUpperHealthFactor;
		collateralAmount = ((collateralAmount * upperHealthFactor) / MAX_BPS);
		// assumes in collateral decimals
		return collateralAmount;
	}

	/**
	 * @notice Retrieves the option token if it exists
	 * @param  underlying is the address of the underlying asset of the option
	 * @param  strikeAsset is the address of the collateral asset of the option
	 * @param  expiration is the expiry timestamp of the option
	 * @param  isPut the type of option
	 * @param  strike is the strike price of the option - 1e18 format
	 * @param  collateral is the address of the asset to collateralize the option with
	 * @return the address of the option
	 */
	function getOtoken(
		address underlying,
		address strikeAsset,
		uint256 expiration,
		bool isPut,
		uint256 strike,
		address collateral
	) external view returns (address) {
		// check for an opyn oToken
		address series = OpynInteractions.getOtoken(
			oTokenFactory,
			collateral,
			underlying,
			strikeAsset,
			formatStrikePrice(strike, collateral),
			expiration,
			isPut
		);
		return series;
	}

	/**
	 * @notice check the health of a specific vault to see if it requires collateral
	 * @param  vaultId the id of the vault to check
	 * @return isBelowMin bool to determine whether the vault needs topping up
	 * @return isAboveMax bool to determine whether the vault is too overcollateralised
	 * @return healthFactor the health factor of the vault in MAX_BPS format
	 * @return upperHealthFactor the upper bound of the acceptable health facor range in MAX_BPS format
	 * @return collatRequired the amount of collateral required to return the vault back to normal
	 * @return collatAsset the address of the collateral asset
	 */
	function checkVaultHealth(uint256 vaultId)
		public
		view
		returns (
			bool isBelowMin,
			bool isAboveMax,
			uint256 healthFactor,
			uint256 upperHealthFactor,
			uint256 collatRequired,
			address collatAsset
		)
	{
		// run checks on the vault health
		// get the vault details from the vaultId
		GammaTypes.Vault memory vault = IController(gammaController).getVault(address(this), vaultId);
		// get the series
		Types.OptionSeries memory series = seriesInfo[vault.shortOtokens[0]];
		if (series.expiration < block.timestamp) {
			revert CustomErrors.VaultExpired();
		}
		// get the MarginRequired
		IMarginCalculator marginCalc = IMarginCalculator(addressBook.getMarginCalculator());

		uint256 marginReq = marginCalc.getNakedMarginRequired(
			series.underlying,
			series.strikeAsset,
			series.collateral,
			vault.shortAmounts[0], // assumes in e8
			series.strike, // assumes in e8
			IOracle(addressBook.getOracle()).getPrice(series.underlying),
			series.expiration,
			ERC20(series.collateral).decimals(),
			series.isPut
		);
		// get the amount held in the vault
		uint256 collatAmount = vault.collateralAmounts[0];
		// divide the amount held in the vault by the margin requirements to get the health factor
		healthFactor = (collatAmount * MAX_BPS) / marginReq;
		// set the upper and lower health factor depending on if the series is a put or a call
		upperHealthFactor = series.isPut ? putUpperHealthFactor : callUpperHealthFactor;
		uint256 lowerHealthFactor = series.isPut ? putLowerHealthFactor : callLowerHealthFactor;
		// if the vault health is above a certain threshold then the vault is above safe margins and collateral can be withdrawn
		if (healthFactor > upperHealthFactor) {
			isAboveMax = true;
			// calculate the margin to remove from the vault
			collatRequired = collatAmount - ((marginReq * upperHealthFactor) / MAX_BPS);
		} else if (healthFactor < lowerHealthFactor) {
			isBelowMin = true;
			// calculate the margin to add to the vault
			collatRequired = ((marginReq * upperHealthFactor) / MAX_BPS) - collatAmount;
		}
		collatAsset = series.collateral;
	}

	///////////////////////////
	/// non-complex getters ///
	///////////////////////////

	function getSeriesAddress(bytes32 issuanceHash) external view returns (address) {
		return seriesAddress[issuanceHash];
	}

	function getSeries(Types.OptionSeries memory _series) external view returns (address) {
		return
			seriesAddress[
				getIssuanceHash(
					_series.underlying,
					_series.strikeAsset,
					_series.collateral,
					_series.expiration,
					_series.isPut,
					_series.strike
				)
			];
	}

	function getSeriesInfo(address series) external view returns (Types.OptionSeries memory) {
		return seriesInfo[series];
	}

	function getIssuanceHash(Types.OptionSeries memory _series) public pure returns (bytes32) {
		return
			getIssuanceHash(
				_series.underlying,
				_series.strikeAsset,
				_series.collateral,
				_series.expiration,
				_series.isPut,
				_series.strike
			);
	}

	/**
	 * Helper function for computing the hash of a given issuance.
	 */
	function getIssuanceHash(
		address underlying,
		address strikeAsset,
		address collateral,
		uint256 expiration,
		bool isPut,
		uint256 strike
	) internal pure returns (bytes32) {
		return
			keccak256(abi.encodePacked(underlying, strikeAsset, collateral, expiration, isPut, strike));
	}

	//////////////////////////
	/// internal utilities ///
	//////////////////////////

	/**
	 * @notice Converts strike price to 1e8 format and floors least significant digits if needed
	 * @param  strikePrice strikePrice in 1e18 format
	 * @param  collateral address of collateral asset
	 * @return if the transaction succeeded
	 */
	function formatStrikePrice(uint256 strikePrice, address collateral) public view returns (uint256) {
		// convert strike to 1e8 format
		uint256 price = strikePrice / (10**10);
		uint256 collateralDecimals = ERC20(collateral).decimals();
		if (collateralDecimals >= OPYN_DECIMALS) return price;
		uint256 difference = OPYN_DECIMALS - collateralDecimals;
		// round floor strike to prevent errors in Gamma protocol
		return (price / (10**difference)) * (10**difference);
	}

	function _isLiquidityPool() internal view {
		if (msg.sender != liquidityPool) {
			revert NotLiquidityPool();
		}
	}

	/// @dev keepers, managers or governors can access
	function _isKeeper() internal view {
		if (
			!keeper[msg.sender] && msg.sender != authority.governor() && msg.sender != authority.manager()
		) {
			revert NotKeeper();
		}
	}
}

File 2 of 31 : LiquidityPool.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "./Protocol.sol";
import "./PriceFeed.sol";
import "./VolatilityFeed.sol";

import "./tokens/ERC20.sol";
import "./utils/ReentrancyGuard.sol";

import "./libraries/BlackScholes.sol";
import "./libraries/CustomErrors.sol";
import "./libraries/AccessControl.sol";
import "./libraries/OptionsCompute.sol";
import "./libraries/SafeTransferLib.sol";

import "./interfaces/IAccounting.sol";
import "./interfaces/IOptionRegistry.sol";
import "./interfaces/IHedgingReactor.sol";
import "./interfaces/IPortfolioValuesFeed.sol";

import "@openzeppelin/contracts/security/Pausable.sol";

/**
 *  @title Contract used as the Dynamic Hedging Vault for storing funds, issuing shares and processing options transactions
 *  @dev Interacts with the OptionRegistry for options behaviour, Interacts with hedging reactors for alternative derivatives
 *       Interacts with Handlers for periphary user options interactions. Interacts with Chainlink price feeds throughout.
 *       Interacts with Volatility Feed via getImpliedVolatility(), interacts with a chainlink PortfolioValues external adaptor
 *       oracle via PortfolioValuesFeed.
 */
contract LiquidityPool is ERC20, AccessControl, ReentrancyGuard, Pausable {
	using PRBMathSD59x18 for int256;
	using PRBMathUD60x18 for uint256;

	///////////////////////////
	/// immutable variables ///
	///////////////////////////

	// Protocol management contract
	Protocol public immutable protocol;
	// asset that denominates the strike price
	address public immutable strikeAsset;
	// asset that is used as the reference asset
	address public immutable underlyingAsset;
	// asset that is used for collateral asset
	address public immutable collateralAsset;

	/////////////////////////
	/// dynamic variables ///
	/////////////////////////

	// amount of collateralAsset allocated as collateral
	uint256 public collateralAllocated;
	// ephemeral liabilities of the pool
	int256 public ephemeralLiabilities;
	// ephemeral delta of the pool
	int256 public ephemeralDelta;
	// epoch of the price per share round for deposits
	uint256 public depositEpoch;
	// epoch of the price per share round for withdrawals
	uint256 public withdrawalEpoch;
	// epoch PPS for deposits
	mapping(uint256 => uint256) public depositEpochPricePerShare;
	// epoch PPS for withdrawals
	mapping(uint256 => uint256) public withdrawalEpochPricePerShare;
	// deposit receipts for users
	mapping(address => IAccounting.DepositReceipt) public depositReceipts;
	// withdrawal receipts for users
	mapping(address => IAccounting.WithdrawalReceipt) public withdrawalReceipts;
	// pending deposits for a round - collateral denominated (collateral decimals)
	uint256 public pendingDeposits;
	// pending withdrawals for a round - DHV token e18 denominated
	uint256 public pendingWithdrawals;
	// withdrawal amount that has been executed and is pending completion. These funds are to be excluded from all book balances.
	uint256 public partitionedFunds;

	/////////////////////////////////////
	/// governance settable variables ///
	/////////////////////////////////////

	// buffer of funds to not be used to write new options in case of margin requirements (as percentage - for 20% enter 2000)
	uint256 public bufferPercentage = 5000;
	// list of addresses for hedging reactors
	address[] public hedgingReactors;
	// max total supply of collateral, denominated in e18
	uint256 public collateralCap = type(uint256).max;
	// Maximum discount that an option tilting factor can discount an option price
	uint256 public maxDiscount = (PRBMathUD60x18.SCALE * 10) / 100; // As a percentage. Init at 10%
	// The spread between the bid and ask on the IV skew;
	// Consider making this it's own volatility skew if more flexibility is needed
	uint256 public bidAskIVSpread;
	// option issuance parameters
	Types.OptionParams public optionParams;
	// riskFreeRate as a percentage PRBMath Float. IE: 3% -> 0.03 * 10**18
	uint256 public riskFreeRate;
	// handlers who are approved to interact with options functionality
	mapping(address => bool) public handler;
	// is the purchase and sale of options paused
	bool public isTradingPaused;
	// max time to allow between oracle updates for an underlying and strike
	uint256 public maxTimeDeviationThreshold = 600;
	// max price difference to allow between oracle updates for an underlying and strike
	uint256 public maxPriceDeviationThreshold = 1e18;
	// variables relating to the utilization skew function:
	// the gradient of the function where utiization is below function threshold. e18
	uint256 public belowThresholdGradient = 0; // 0
	// the gradient of the line above the utilization threshold. e18
	uint256 public aboveThresholdGradient = 1e18; // 1
	// the y-intercept of the line above the threshold. Needed to make the two lines meet at the threshold.  Will always be negative but enter the absolute value
	uint256 public aboveThresholdYIntercept = 6e17; //-0.6
	// the percentage utilization above which the function moves from its shallow line to its steep line. e18
	uint256 public utilizationFunctionThreshold = 6e17; // 60%
	// keeper mapping
	mapping(address => bool) public keeper;

	//////////////////////////
	/// constant variables ///
	//////////////////////////

	// BIPS
	uint256 private constant MAX_BPS = 10_000;

	/////////////////////////
	/// structs && events ///
	/////////////////////////

	event DepositEpochExecuted(uint256 epoch);
	event WithdrawalEpochExecuted(uint256 epoch);
	event Withdraw(address recipient, uint256 amount, uint256 shares);
	event Deposit(address recipient, uint256 amount, uint256 epoch);
	event Redeem(address recipient, uint256 amount, uint256 epoch);
	event InitiateWithdraw(address recipient, uint256 amount, uint256 epoch);
	event WriteOption(address series, uint256 amount, uint256 premium, uint256 escrow, address buyer);
	event RebalancePortfolioDelta(int256 deltaChange);
	event TradingPaused();
	event TradingUnpaused();
	event SettleVault(
		address series,
		uint256 collateralReturned,
		uint256 collateralLost,
		address closer
	);
	event BuybackOption(
		address series,
		uint256 amount,
		uint256 premium,
		uint256 escrowReturned,
		address seller
	);

	constructor(
		address _protocol,
		address _strikeAsset,
		address _underlyingAsset,
		address _collateralAsset,
		uint256 rfr,
		string memory name,
		string memory symbol,
		Types.OptionParams memory _optionParams,
		address _authority
	) ERC20(name, symbol, 18) AccessControl(IAuthority(_authority)) {
		if (ERC20(_collateralAsset).decimals() > 18) {
			revert CustomErrors.InvalidDecimals();
		}
		strikeAsset = _strikeAsset;
		riskFreeRate = rfr;
		underlyingAsset = _underlyingAsset;
		collateralAsset = _collateralAsset;
		protocol = Protocol(_protocol);
		optionParams = _optionParams;
		depositEpochPricePerShare[0] = 1e18;
		withdrawalEpochPricePerShare[0] = 1e18;
		depositEpoch++;
		withdrawalEpoch++;
	}

	///////////////
	/// setters ///
	///////////////

	function pause() external {
		_onlyGuardian();
		_pause();
	}

	function pauseUnpauseTrading(bool _pause) external {
		_onlyGuardian();
		isTradingPaused = _pause;
		if (_pause) {
			emit TradingPaused();
		} else {
			emit TradingUnpaused();
		}
	}

	function unpause() external {
		_onlyGuardian();
		_unpause();
	}

	/**
	 * @notice set a new hedging reactor
	 * @param _reactorAddress append a new hedging reactor
	 * @dev   only governance can call this function
	 */
	function setHedgingReactorAddress(address _reactorAddress) external {
		_onlyGovernor();
		if (_reactorAddress == address(0)) {
			revert CustomErrors.InvalidAddress();
		}
		uint256 arrayLength = hedgingReactors.length;
		for (uint256 i = 0; i < arrayLength; i++) {
			if (hedgingReactors[i] == _reactorAddress) {
				revert CustomErrors.ReactorAlreadyExists();
			}
		}
		hedgingReactors.push(_reactorAddress);
		SafeTransferLib.safeApprove(ERC20(collateralAsset), _reactorAddress, type(uint256).max);
	}

	/**
	 * @notice remove a new hedging reactor by index
	 * @param _index remove a hedging reactor
	 * @param _override whether to override whether the reactor is wound down 
	 		 			(THE REACTOR SHOULD BE WOUND DOWN SEPERATELY)
	 * @dev   only governance can call this function
	 */
	function removeHedgingReactorAddress(uint256 _index, bool _override) external {
		_onlyGovernor();
		address[] memory hedgingReactors_ = hedgingReactors;
		address reactorAddress = hedgingReactors_[_index];
		if (!_override) {
			IHedgingReactor reactor = IHedgingReactor(reactorAddress);
			int256 delta = reactor.getDelta();
			if (delta != 0) {
				reactor.hedgeDelta(delta);
			}
			reactor.withdraw(type(uint256).max);
		}
		SafeTransferLib.safeApprove(ERC20(collateralAsset), reactorAddress, 0);
		uint256 maxIndex = hedgingReactors_.length - 1;
		for (uint256 i = _index; i < maxIndex; i++) {
			hedgingReactors[i] = hedgingReactors_[i + 1];
		}
		hedgingReactors.pop();
	}

	function getHedgingReactors() external view returns (address[] memory) {
		return hedgingReactors;
	}
	/**
	 * @notice update all optionParam variables for max and min strikes and max and
	 *         min expiries for options that the DHV can issue
	 * @dev   only management or above can call this function
	 */
	function setNewOptionParams(
		uint128 _newMinCallStrike,
		uint128 _newMaxCallStrike,
		uint128 _newMinPutStrike,
		uint128 _newMaxPutStrike,
		uint128 _newMinExpiry,
		uint128 _newMaxExpiry
	) external {
		_onlyManager();
		optionParams.minCallStrikePrice = _newMinCallStrike;
		optionParams.maxCallStrikePrice = _newMaxCallStrike;
		optionParams.minPutStrikePrice = _newMinPutStrike;
		optionParams.maxPutStrikePrice = _newMaxPutStrike;
		optionParams.minExpiry = _newMinExpiry;
		optionParams.maxExpiry = _newMaxExpiry;
	}

	/**
	 * @notice set the bid ask spread used to price option buying
	 * @param _bidAskSpread the bid ask spread to update to
	 * @dev   only management or above can call this function
	 */
	function setBidAskSpread(uint256 _bidAskSpread) external {
		_onlyManager();
		bidAskIVSpread = _bidAskSpread;
	}

	/**
	 * @notice set the maximum percentage discount for an option
	 * @param _maxDiscount of the option as a percentage in 1e18 format. ie: 1*e18 == 1%
	 * @dev   only management or above can call this function
	 */
	function setMaxDiscount(uint256 _maxDiscount) external {
		_onlyManager();
		maxDiscount = _maxDiscount;
	}

	/**
	 * @notice set the maximum collateral amount allowed in the pool
	 * @param _collateralCap of the collateral held
	 * @dev   only governance can call this function
	 */
	function setCollateralCap(uint256 _collateralCap) external {
		_onlyGovernor();
		collateralCap = _collateralCap;
	}

	/**
	 * @notice update the liquidity pool buffer limit
	 * @param _bufferPercentage the minimum balance the liquidity pool must have as a percentage of collateral allocated to options. (for 20% enter 2000)
	 * @dev   only governance can call this function
	 */
	function setBufferPercentage(uint256 _bufferPercentage) external {
		_onlyGovernor();
		bufferPercentage = _bufferPercentage;
	}

	/**
	 * @notice update the liquidity pool risk free rate
	 * @param _riskFreeRate the risk free rate of the market
	 */
	function setRiskFreeRate(uint256 _riskFreeRate) external {
		_onlyGovernor();
		riskFreeRate = _riskFreeRate;
	}

	/**
	 * @notice update the max oracle time deviation threshold
	 */
	function setMaxTimeDeviationThreshold(uint256 _maxTimeDeviationThreshold) external {
		_onlyGovernor();
		maxTimeDeviationThreshold = _maxTimeDeviationThreshold;
	}

	/**
	 * @notice update the max oracle price deviation threshold
	 */
	function setMaxPriceDeviationThreshold(uint256 _maxPriceDeviationThreshold) external {
		_onlyGovernor();
		maxPriceDeviationThreshold = _maxPriceDeviationThreshold;
	}

	/**
	 * @notice change the status of a handler
	 */
	function changeHandler(address _handler, bool auth) external {
		_onlyGovernor();
		if (_handler == address(0)) {
			revert CustomErrors.InvalidAddress();
		}
		handler[_handler] = auth;
	}

	/**
	 * @notice change the status of a keeper
	 */
	function setKeeper(address _keeper, bool _auth) external {
		_onlyGovernor();
		if (_keeper == address(0)) {
			revert CustomErrors.InvalidAddress();
		}
		keeper[_keeper] = _auth;
	}

	/**
	 *  @notice sets the parameters for the function that determines the utilization price factor
	 *  The function is made up of two parts, both linear. The line to the left of the utilisation threshold has a low gradient
	 *  while the gradient to the right of the threshold is much steeper. The aim of this function is to make options much more
	 *  expensive near full utilization while not having much effect at low utilizations.
	 *  @param _belowThresholdGradient the gradient of the function where utiization is below function threshold. e18
	 *  @param _aboveThresholdGradient the gradient of the line above the utilization threshold. e18
	 *  @param _utilizationFunctionThreshold the percentage utilization above which the function moves from its shallow line to its steep line
	 */
	function setUtilizationSkewParams(
		uint256 _belowThresholdGradient,
		uint256 _aboveThresholdGradient,
		uint256 _utilizationFunctionThreshold
	) external {
		_onlyManager();
		belowThresholdGradient = _belowThresholdGradient;
		aboveThresholdGradient = _aboveThresholdGradient;
		aboveThresholdYIntercept = _utilizationFunctionThreshold.mul(
			_aboveThresholdGradient - _belowThresholdGradient // inverted the order of the subtraction to result in a positive uint
		);

		utilizationFunctionThreshold = _utilizationFunctionThreshold;
	}

	//////////////////////////////////////////////////////
	/// access-controlled state changing functionality ///
	//////////////////////////////////////////////////////

	/**
	 * @notice function for hedging portfolio delta through external means
	 * @param delta the current portfolio delta
	 * @param reactorIndex the index of the reactor in the hedgingReactors array to use
	 */
	function rebalancePortfolioDelta(int256 delta, uint256 reactorIndex) external {
		_onlyManager();
		IHedgingReactor(hedgingReactors[reactorIndex]).hedgeDelta(delta);
		emit RebalancePortfolioDelta(delta);
	}

	/**
	 * @notice adjust the collateral held in a specific vault because of health
	 * @param lpCollateralDifference amount of collateral taken from or given to the liquidity pool in collateral decimals
	 * @param addToLpBalance true if collateral is returned to liquidity pool, false if collateral is withdrawn from liquidity pool
	 * @dev   called by the option registry only
	 */
	function adjustCollateral(uint256 lpCollateralDifference, bool addToLpBalance) external {
		IOptionRegistry optionRegistry = _getOptionRegistry();
		require(msg.sender == address(optionRegistry));
		// assumes in collateral decimals
		if (addToLpBalance) {
			collateralAllocated -= lpCollateralDifference;
		} else {
			SafeTransferLib.safeApprove(
				ERC20(collateralAsset),
				address(optionRegistry),
				lpCollateralDifference
			);
			collateralAllocated += lpCollateralDifference;
		}
	}

	/**
	 * @notice closes an oToken vault, returning collateral (minus ITM option expiry value) back to the pool
	 * @param seriesAddress the address of the oToken vault to close
	 * @return collatReturned the amount of collateral returned to the liquidity pool, assumes in collateral decimals
	 */
	function settleVault(address seriesAddress) external returns (uint256) {
		_isKeeper();
		// get number of options in vault and collateral returned to recalculate our position without these options
		// returns in collat decimals, collat decimals and e8
		(, uint256 collatReturned, uint256 collatLost, ) = _getOptionRegistry().settle(seriesAddress);
		emit SettleVault(seriesAddress, collatReturned, collatLost, msg.sender);
		// if the vault expired ITM then when settled the oracle will still have accounted for it as a liability. When
		// the settle happens the liability is wiped off as it is now accounted for in collateralAllocated but because the
		// oracle doesn't know this yet we need to temporarily reduce the liability value.
		_adjustVariables(collatReturned, collatLost, 0, false);
		collateralAllocated -= collatLost;
		return collatReturned;
	}

	/**
	 * @notice issue an option
	 * @param optionSeries the series detail of the option - strike decimals in e18
	 * @dev only callable by a handler contract
	 */
	function handlerIssue(Types.OptionSeries memory optionSeries) external returns (address) {
		_isHandler();
		// series strike in e18
		return _issue(optionSeries, _getOptionRegistry());
	}

	/**
	 * @notice write an option that already exists
	 * @param optionSeries the series detail of the option - strike decimals in e8
	 * @param seriesAddress the series address of the oToken
	 * @param amount the number of options to write - in e18
	 * @param optionRegistry the registry used for options writing
	 * @param premium the premium of the option - in collateral decimals
	 * @param delta the delta of the option - in e18
	 * @param recipient the receiver of the option
	 * @dev only callable by a handler contract
	 */
	function handlerWriteOption(
		Types.OptionSeries memory optionSeries,
		address seriesAddress,
		uint256 amount,
		IOptionRegistry optionRegistry,
		uint256 premium,
		int256 delta,
		address recipient
	) external returns (uint256) {
		_isTradingNotPaused();
		_isHandler();
		return
			_writeOption(
				optionSeries, // series strike in e8
				seriesAddress,
				amount, // in e18
				optionRegistry,
				premium, // in collat decimals
				delta,
				checkBuffer(), // in e6
				recipient
			);
	}

	/**
	 * @notice write an option that doesnt exist
	 * @param optionSeries the series detail of the option - strike decimals in e18
	 * @param amount the number of options to write - in e18
	 * @param premium the premium of the option - in collateral decimals
	 * @param delta the delta of the option - in e18
	 * @param recipient the receiver of the option
	 * @dev only callable by a handler contract
	 */
	function handlerIssueAndWriteOption(
		Types.OptionSeries memory optionSeries,
		uint256 amount,
		uint256 premium,
		int256 delta,
		address recipient
	) external returns (uint256, address) {
		_isTradingNotPaused();
		_isHandler();
		IOptionRegistry optionRegistry = _getOptionRegistry();
		// series strike passed in as e18
		address seriesAddress = _issue(optionSeries, optionRegistry);
		// series strike received in e8, retrieved from the option registry instead of
		// using one in memory because formatStrikePrice might have slightly changed the
		// strike
		optionSeries = optionRegistry.getSeriesInfo(seriesAddress);
		return (
			_writeOption(
				optionSeries, // strike in e8
				seriesAddress,
				amount, // in e18
				optionRegistry,
				premium, // in collat decimals
				delta,
				checkBuffer(), // in e6
				recipient
			),
			seriesAddress
		);
	}

	/**
	 * @notice buy back an option that already exists
	 * @param optionSeries the series detail of the option - strike decimals in e8
	 * @param amount the number of options to buyback - in e18
	 * @param optionRegistry the registry used for options writing
	 * @param seriesAddress the series address of the oToken
	 * @param premium the premium of the option - in collateral decimals
	 * @param delta the delta of the option - in e18
	 * @param seller the receiver of the option
	 * @dev only callable by a handler contract
	 */
	function handlerBuybackOption(
		Types.OptionSeries memory optionSeries,
		uint256 amount,
		IOptionRegistry optionRegistry,
		address seriesAddress,
		uint256 premium,
		int256 delta,
		address seller
	) external returns (uint256) {
		_isTradingNotPaused();
		_isHandler();
		// strike passed in as e8
		return
			_buybackOption(optionSeries, amount, optionRegistry, seriesAddress, premium, delta, seller);
	}

	/**
	 * @notice reset the temporary portfolio and delta values that have been changed since the last oracle update
	 * @dev    only callable by the portfolio values feed oracle contract
	 */
	function resetEphemeralValues() external {
		require(msg.sender == address(_getPortfolioValuesFeed()));
		delete ephemeralLiabilities;
		delete ephemeralDelta;
	}

	/**
	 * @notice reset the temporary portfolio and delta values that have been changed since the last oracle update
	 * @dev    this function must be called in order to execute an epoch calculation
	 */
	function pauseTradingAndRequest() external returns (bytes32) {
		_isKeeper();
		// pause trading
		isTradingPaused = true;
		emit TradingPaused();
		// make an oracle request
		return _getPortfolioValuesFeed().requestPortfolioData(underlyingAsset, strikeAsset);
	}

	/**
	 * @notice execute the epoch and set all the price per shares
	 * @dev    this function must be called in order to execute an epoch calculation and batch a mutual fund epoch
	 */
	function executeEpochCalculation() external whenNotPaused {
		_isKeeper();
		if (!isTradingPaused) {
			revert CustomErrors.TradingNotPaused();
		}
		(
			uint256 newPricePerShareDeposit,
			uint256 newPricePerShareWithdrawal,
			uint256 sharesToMint,
			uint256 totalWithdrawAmount,
			uint256 amountNeeded
		) = _getAccounting().executeEpochCalculation(totalSupply, _getAssets(), _getLiabilities());
		// deposits always get executed
		depositEpochPricePerShare[depositEpoch] = newPricePerShareDeposit;
		delete pendingDeposits;
		emit DepositEpochExecuted(depositEpoch);
		depositEpoch++;
		isTradingPaused = false;
		emit TradingUnpaused();
		_mint(address(this), sharesToMint);
		// loop through the reactors and move funds if found
		if (amountNeeded > 0) {
			address[] memory hedgingReactors_ = hedgingReactors;
			for (uint8 i = 0; i < hedgingReactors_.length; i++) {
				amountNeeded -= IHedgingReactor(hedgingReactors_[i]).withdraw(amountNeeded);
				if (amountNeeded <= 0) {
					break;
				}
			}
			// if not enough funds in liquidity pool and reactors, dont process withdrawals this epoch
			if (amountNeeded > 0) {
				return;
			}
		}
		withdrawalEpochPricePerShare[withdrawalEpoch] = newPricePerShareWithdrawal;
		partitionedFunds += totalWithdrawAmount;
		emit WithdrawalEpochExecuted(withdrawalEpoch);
		_burn(address(this), pendingWithdrawals);
		delete pendingWithdrawals;
		withdrawalEpoch++;
	}

	/////////////////////////////////////////////
	/// external state changing functionality ///
	/////////////////////////////////////////////

	/**
	 * @notice function for adding liquidity to the options liquidity pool
	 * @param _amount    amount of the strike asset to deposit
	 * @return success
	 * @dev    entry point to provide liquidity to dynamic hedging vault
	 */
	function deposit(uint256 _amount) external whenNotPaused nonReentrant returns (bool) {
		if (_amount == 0) {
			revert CustomErrors.InvalidAmount();
		}
		(uint256 depositAmount, uint256 unredeemedShares) = _getAccounting().deposit(msg.sender, _amount);

		emit Deposit(msg.sender, _amount, depositEpoch);
		// create the deposit receipt
		depositReceipts[msg.sender] = IAccounting.DepositReceipt({
			epoch: uint128(depositEpoch),
			amount: uint128(depositAmount),
			unredeemedShares: unredeemedShares
		});
		pendingDeposits += _amount;
		// Pull in tokens from sender
		SafeTransferLib.safeTransferFrom(collateralAsset, msg.sender, address(this), _amount);
		return true;
	}

	/**
	 * @notice function for allowing a user to redeem their shares from a previous epoch
	 * @param _shares the number of shares to redeem
	 * @return the number of shares actually returned
	 */
	function redeem(uint256 _shares) external nonReentrant returns (uint256) {
		if (_shares == 0) {
			revert CustomErrors.InvalidShareAmount();
		}
		return _redeem(_shares);
	}

	/**
	 * @notice function for initiating a withdraw request from the pool
	 * @param _shares    amount of shares to return
	 * @dev    entry point to remove liquidity to dynamic hedging vault
	 */
	function initiateWithdraw(uint256 _shares) external whenNotPaused nonReentrant {
		if (_shares == 0) {
			revert CustomErrors.InvalidShareAmount();
		}
		IAccounting.DepositReceipt memory depositReceipt = depositReceipts[msg.sender];

		if (depositReceipt.amount > 0 || depositReceipt.unredeemedShares > 0) {
			// redeem so a user can use a completed deposit as shares for an initiation
			_redeem(type(uint256).max);
		}
		IAccounting.WithdrawalReceipt memory withdrawalReceipt = _getAccounting().initiateWithdraw(
			msg.sender,
			_shares
		);
		withdrawalReceipts[msg.sender] = withdrawalReceipt;
		pendingWithdrawals += _shares;
		emit InitiateWithdraw(msg.sender, _shares, withdrawalEpoch);
		transfer(address(this), _shares);
	}

	/**
	 * @notice function for completing the withdraw from a pool
	 * @dev    entry point to remove liquidity to dynamic hedging vault
	 */
	function completeWithdraw() external whenNotPaused nonReentrant returns (uint256) {
		(
			uint256 withdrawalAmount,
			uint256 withdrawalShares,
			IAccounting.WithdrawalReceipt memory withdrawalReceipt
		) = _getAccounting().completeWithdraw(msg.sender);
		withdrawalReceipts[msg.sender] = withdrawalReceipt;
		emit Withdraw(msg.sender, withdrawalAmount, withdrawalShares);
		// these funds are taken from the partitioned funds
		partitionedFunds -= withdrawalAmount;
		SafeTransferLib.safeTransfer(ERC20(collateralAsset), msg.sender, withdrawalAmount);
		return withdrawalAmount;
	}

	///////////////////////
	/// complex getters ///
	///////////////////////

	/**
	 * @notice Returning balance in 1e18 format
	 * @param asset address of the asset to get balance and normalize
	 * @return normalizedBalance balance in 1e18 format
	 */
	function _getNormalizedBalance(address asset) internal view returns (uint256 normalizedBalance) {
		normalizedBalance = OptionsCompute.convertFromDecimals(
			ERC20(asset).balanceOf(address(this)) - partitionedFunds,
			ERC20(asset).decimals()
		);
	}

	/**
	 * @notice Returning balance in 1e6 format
	 * @param asset address of the asset to get balance
	 * @return balance of the address accounting for partitionedFunds
	 */
	function getBalance(address asset) public view returns (uint256) {
		return ERC20(asset).balanceOf(address(this)) - partitionedFunds;
	}

	/**
	 * @notice get the delta of the hedging reactors
	 * @return externalDelta hedging reactor delta in e18 format
	 */
	function getExternalDelta() public view returns (int256 externalDelta) {
		address[] memory hedgingReactors_ = hedgingReactors;
		for (uint8 i = 0; i < hedgingReactors_.length; i++) {
			externalDelta += IHedgingReactor(hedgingReactors_[i]).getDelta();
		}
	}

	/**
	 * @notice get the delta of the portfolio
	 * @return portfolio delta
	 */
	function getPortfolioDelta() public view returns (int256) {
		// assumes in e18
		Types.PortfolioValues memory portfolioValues = _getPortfolioValuesFeed().getPortfolioValues(
			underlyingAsset,
			strikeAsset
		);
		// check that the portfolio values are acceptable
		OptionsCompute.validatePortfolioValues(
			_getUnderlyingPrice(underlyingAsset, strikeAsset),
			portfolioValues,
			maxTimeDeviationThreshold,
			maxPriceDeviationThreshold
		);
		return portfolioValues.delta + getExternalDelta() + ephemeralDelta;
	}

	/**
	 * @notice get the quote price and delta for a given option
	 * @param  optionSeries option type to quote - strike assumed in e18
	 * @param  amount the number of options to mint  - assumed in e18
	 * @param toBuy whether the protocol is buying the option
	 * @return quote the price of the options - returns in e18
	 * @return delta the delta of the options - returns in e18
	 */
	function quotePriceWithUtilizationGreeks(
		Types.OptionSeries memory optionSeries,
		uint256 amount,
		bool toBuy
	) external view returns (uint256 quote, int256 delta) {
		// using a struct to get around stack too deep issues
		Types.UtilizationState memory quoteState;
		quoteState.underlyingPrice = _getUnderlyingPrice(
			optionSeries.underlying,
			optionSeries.strikeAsset
		);
		quoteState.iv = getImpliedVolatility(
			optionSeries.isPut,
			quoteState.underlyingPrice,
			optionSeries.strike,
			optionSeries.expiration
		);
		(uint256 optionQuote, int256 deltaQuote) = OptionsCompute.quotePriceGreeks(
			optionSeries,
			toBuy,
			bidAskIVSpread,
			riskFreeRate,
			quoteState.iv,
			quoteState.underlyingPrice
		);
		// price of acquiring total amount of options (remains e18 due to PRBMath)
		quoteState.totalOptionPrice = optionQuote.mul(amount);
		quoteState.totalDelta = deltaQuote.mul(int256(amount));

		// will update quoteState.utilizationPrice
		addUtilizationPremium(quoteState, optionSeries, amount, toBuy);
		quote = applyDeltaPremium(quoteState, toBuy);

		quote = OptionsCompute.convertToCollateralDenominated(
			quote,
			quoteState.underlyingPrice,
			optionSeries
		);
		delta = quoteState.totalDelta;
		if (quote == 0 || delta == int256(0)) {
			revert CustomErrors.DeltaQuoteError(quote, delta);
		}
	}

	/**
	 *	@notice applies a utilization premium when the protocol is selling options.
	 *	Stores the utilization price in quoteState.utilizationPrice for use in quotePriceWithUtilizationGreeks
	 *	@param quoteState the struct created in quoteStateWithUtilizationGreeks to store memory variables
	 *	@param optionSeries the option type for which we are quoting a price
	 *	@param amount the amount of options. e18
	 *	@param toBuy whether we are buying an option. False if selling
	 */
	function addUtilizationPremium(
		Types.UtilizationState memory quoteState,
		Types.OptionSeries memory optionSeries,
		uint256 amount,
		bool toBuy
	) internal view {
		if (!toBuy) {
			uint256 collateralAllocated_ = collateralAllocated;
			// if selling options, we want to add the utilization premium
			// Work out the utilization of the pool as a percentage
			quoteState.utilizationBefore = collateralAllocated_.div(
				collateralAllocated_ + getBalance(collateralAsset)
			);
			// assumes strike is e18
			// strike is not being used again so we dont care if format changes
			optionSeries.strike = optionSeries.strike / 1e10;
			// returns collateral decimals
			quoteState.collateralToAllocate = _getOptionRegistry().getCollateral(optionSeries, amount);

			quoteState.utilizationAfter = (quoteState.collateralToAllocate + collateralAllocated_).div(
				collateralAllocated_ + getBalance(collateralAsset)
			);
			// get the price of the option with the utilization premium added
			quoteState.utilizationPrice = OptionsCompute.getUtilizationPrice(
				quoteState.utilizationBefore,
				quoteState.utilizationAfter,
				quoteState.totalOptionPrice,
				utilizationFunctionThreshold,
				belowThresholdGradient,
				aboveThresholdGradient,
				aboveThresholdYIntercept
			);
		} else {
			// do not use utlilization premium for buybacks
			quoteState.utilizationPrice = quoteState.totalOptionPrice;
		}
	}

	/**
	 *	@notice Applies a discount or premium based on the liquidity pool's delta exposure
	 *	Gives discount if the transaction results in a lower delta exposure for the liquidity pool.
	 *	Prices option more richly if the transaction results in higher delta exposure for liquidity pool.
	 *	@param quoteState the struct created in quoteStateWithUtilizationGreeks to store memory variables
	 *	@param toBuy whether we are buying an option. False if selling
	 *	@return quote the quote for the option with the delta skew applied
	 */
	function applyDeltaPremium(Types.UtilizationState memory quoteState, bool toBuy)
		internal
		view
		returns (uint256 quote)
	{
		// portfolio delta before writing option
		int256 portfolioDelta = getPortfolioDelta();
		// subtract totalDelta if buying as pool is taking on the negative of the option's delta
		int256 newDelta = toBuy
			? portfolioDelta + quoteState.totalDelta
			: portfolioDelta - quoteState.totalDelta;
		// Is delta moved closer to zero?
		quoteState.isDecreased = (PRBMathSD59x18.abs(newDelta) - PRBMathSD59x18.abs(portfolioDelta)) < 0;
		// delta exposure of the portolio per ETH equivalent value the portfolio holds.
		// This value is only used for tilting so we are only interested in its distance from 0 (its magnitude)
		uint256 normalizedDelta = uint256(PRBMathSD59x18.abs((portfolioDelta + newDelta).div(2e18))).div(
			_getNAV().div(quoteState.underlyingPrice)
		);
		// this is the percentage of the option price which is added to or subtracted from option price
		// according to whether portfolio delta is increased or decreased respectively
		quoteState.deltaTiltAmount = normalizedDelta > maxDiscount ? maxDiscount : normalizedDelta;

		if (quoteState.isDecreased) {
			quote = toBuy
				? quoteState.deltaTiltAmount.mul(quoteState.utilizationPrice) + quoteState.utilizationPrice
				: quoteState.utilizationPrice - quoteState.deltaTiltAmount.mul(quoteState.utilizationPrice);
		} else {
			// increase utilization by delta tilt factor for moving delta away from zero
			quote = toBuy
				? quoteState.utilizationPrice - quoteState.deltaTiltAmount.mul(quoteState.utilizationPrice)
				: quoteState.deltaTiltAmount.mul(quoteState.utilizationPrice) + quoteState.utilizationPrice;
		}
	}

	///////////////////////////
	/// non-complex getters ///
	///////////////////////////

	/**
	 * @notice get the current implied volatility from the feed
	 * @param isPut Is the option a call or put?
	 * @param underlyingPrice The underlying price - assumed in e18
	 * @param strikePrice The strike price of the option - assumed in e18
	 * @param expiration expiration timestamp of option as a PRBMath Float
	 * @return Implied volatility adjusted for volatility surface - assumed in e18
	 */
	function getImpliedVolatility(
		bool isPut,
		uint256 underlyingPrice,
		uint256 strikePrice,
		uint256 expiration
	) public view returns (uint256) {
		return _getVolatilityFeed().getImpliedVolatility(isPut, underlyingPrice, strikePrice, expiration);
	}

	function getAssets() external view returns (uint256) {
		return _getAssets();
	}

	function getNAV() external view returns (uint256) {
		return _getNAV();
	}

	//////////////////////////
	/// internal utilities ///
	//////////////////////////

	/**
	 * @notice functionality for allowing a user to redeem their shares from a previous epoch
	 * @param _shares the number of shares to redeem
	 * @return toRedeem the number of shares actually returned
	 */
	function _redeem(uint256 _shares) internal returns (uint256) {
		(uint256 toRedeem, IAccounting.DepositReceipt memory depositReceipt) = _getAccounting().redeem(
			msg.sender,
			_shares
		);
		if (toRedeem == 0) {
			return 0;
		}
		depositReceipts[msg.sender] = depositReceipt;
		allowance[address(this)][msg.sender] = toRedeem;
		emit Redeem(msg.sender, toRedeem, depositReceipt.epoch);
		// transfer as the shares will have been minted in the epoch execution
		transferFrom(address(this), msg.sender, toRedeem);
		return toRedeem;
	}

	/**
	 * @notice get the Net Asset Value
	 * @return Net Asset Value in e18 decimal format
	 */
	function _getNAV() internal view returns (uint256) {
		// equities = assets - liabilities
		// assets: Any token such as eth usd, collateral sent to OptionRegistry, hedging reactor stuff in e18
		// liabilities: Options that we wrote in e18
		uint256 assets = _getAssets();
		int256 liabilities = _getLiabilities();
		// if this ever happens then something has gone very wrong so throw here
		if (int256(assets) < liabilities) {
			revert CustomErrors.LiabilitiesGreaterThanAssets();
		}
		return uint256(int256(assets) - liabilities);
	}

	/**
	 * @notice get the Asset Value
	 * @return assets Asset Value in e18 decimal format
	 */
	function _getAssets() internal view returns (uint256 assets) {
		// assets: Any token such as eth usd, collateral sent to OptionRegistry, hedging reactor stuff in e18
		// liabilities: Options that we wrote in e18
		assets =
			_getNormalizedBalance(collateralAsset) +
			OptionsCompute.convertFromDecimals(collateralAllocated, ERC20(collateralAsset).decimals());
		address[] memory hedgingReactors_ = hedgingReactors;
		for (uint8 i = 0; i < hedgingReactors_.length; i++) {
			// should always return value in e18 decimals
			assets += IHedgingReactor(hedgingReactors_[i]).getPoolDenominatedValue();
		}
	}

	function _getLiabilities() internal view returns (int256 liabilities) {
		Types.PortfolioValues memory portfolioValues = _getPortfolioValuesFeed().getPortfolioValues(
			underlyingAsset,
			strikeAsset
		);
		// check that the portfolio values are acceptable
		OptionsCompute.validatePortfolioValues(
			_getUnderlyingPrice(underlyingAsset, strikeAsset),
			portfolioValues,
			maxTimeDeviationThreshold,
			maxPriceDeviationThreshold
		);
		// ephemeralLiabilities can be +/-, portfolioValues.callPutsValue could be +/-
		liabilities = portfolioValues.callPutsValue + ephemeralLiabilities;
	}

	/**
	 * @notice calculates amount of liquidity that can be used before hitting buffer
	 * @return bufferRemaining the amount of liquidity available before reaching buffer in e6
	 */
	function checkBuffer() public view returns (int256 bufferRemaining) {
		// calculate max amount of liquidity pool funds that can be used before reaching max buffer allowance
		uint256 collateralBalance = getBalance(collateralAsset);
		uint256 collateralBuffer = (collateralAllocated * bufferPercentage) / MAX_BPS;

		bufferRemaining = int256(collateralBalance) - int256(collateralBuffer);
	}

	/**
	 * @notice create the option contract in the options registry
	 * @param  optionSeries option type to mint - option series strike in e18
	 * @param  optionRegistry interface for the options issuer
	 * @return series the address of the option series minted
	 */
	function _issue(Types.OptionSeries memory optionSeries, IOptionRegistry optionRegistry)
		internal
		returns (address series)
	{
		// make sure option is being issued with correct assets
		if (optionSeries.collateral != collateralAsset) {
			revert CustomErrors.CollateralAssetInvalid();
		}
		if (optionSeries.underlying != underlyingAsset) {
			revert CustomErrors.UnderlyingAssetInvalid();
		}
		if (optionSeries.strikeAsset != strikeAsset) {
			revert CustomErrors.StrikeAssetInvalid();
		}
		// cache
		Types.OptionParams memory optionParams_ = optionParams;
		// check the expiry is within the allowed bounds
		if (
			block.timestamp + optionParams_.minExpiry > optionSeries.expiration ||
			optionSeries.expiration > block.timestamp + optionParams_.maxExpiry
		) {
			revert CustomErrors.OptionExpiryInvalid();
		}
		// check that the option strike is within the range of the min and max acceptable strikes of calls and puts
		if (optionSeries.isPut) {
			if (
				optionParams_.minPutStrikePrice > optionSeries.strike ||
				optionSeries.strike > optionParams_.maxPutStrikePrice
			) {
				revert CustomErrors.OptionStrikeInvalid();
			}
		} else {
			if (
				optionParams_.minCallStrikePrice > optionSeries.strike ||
				optionSeries.strike > optionParams_.maxCallStrikePrice
			) {
				revert CustomErrors.OptionStrikeInvalid();
			}
		}
		// issue the option from the option registry (its characteristics will be stored in the optionsRegistry)
		series = optionRegistry.issue(optionSeries);
		if (series == address(0)) {
			revert CustomErrors.IssuanceFailed();
		}
	}

	/**
	 * @notice write a number of options for a given OptionSeries
	 * @param  optionSeries option type to mint - strike in e8
	 * @param  seriesAddress the address of the options series
	 * @param  amount the amount to be written - in e18
	 * @param  optionRegistry the option registry of the pool
	 * @param  premium the premium to charge the user - in collateral decimals
	 * @param  delta the delta of the option position - in e18
	 * @param  bufferRemaining the amount of buffer that can be used - in e6
	 * @return the amount that was written
	 */
	function _writeOption(
		Types.OptionSeries memory optionSeries,
		address seriesAddress,
		uint256 amount,
		IOptionRegistry optionRegistry,
		uint256 premium,
		int256 delta,
		int256 bufferRemaining,
		address recipient
	) internal returns (uint256) {
		// strike decimals come into this function as e8
		uint256 collateralAmount = optionRegistry.getCollateral(optionSeries, amount);
		if (bufferRemaining < int256(collateralAmount)) {
			revert CustomErrors.MaxLiquidityBufferReached();
		}
		ERC20(collateralAsset).approve(address(optionRegistry), collateralAmount);
		(, collateralAmount) = optionRegistry.open(seriesAddress, amount, collateralAmount);
		emit WriteOption(seriesAddress, amount, premium, collateralAmount, recipient);
		// convert e8 strike to e18 strike
		optionSeries.strike = uint128(
			OptionsCompute.convertFromDecimals(optionSeries.strike, ERC20(seriesAddress).decimals())
		);
		_adjustVariables(collateralAmount, premium, delta, true);
		SafeTransferLib.safeTransfer(
			ERC20(seriesAddress),
			recipient,
			OptionsCompute.convertToDecimals(amount, ERC20(seriesAddress).decimals())
		);
		// returns in e18
		return amount;
	}

	/**
	 * @notice buys a number of options back and burns the tokens
	 * @param optionSeries the option token series to buyback - strike passed in as e8
	 * @param amount the number of options to buyback expressed in 1e18
	 * @param optionRegistry the registry
	 * @param seriesAddress the series being sold
	 * @param premium the premium to be sent back to the owner (in collat decimals)
	 * @param delta the delta of the option
	 * @param seller the address
	 * @return the number of options burned in e18
	 */
	function _buybackOption(
		Types.OptionSeries memory optionSeries,
		uint256 amount,
		IOptionRegistry optionRegistry,
		address seriesAddress,
		uint256 premium,
		int256 delta,
		address seller
	) internal returns (uint256) {
		SafeTransferLib.safeApprove(
			ERC20(seriesAddress),
			address(optionRegistry),
			OptionsCompute.convertToDecimals(amount, ERC20(seriesAddress).decimals())
		);
		(, uint256 collateralReturned) = optionRegistry.close(seriesAddress, amount);
		emit BuybackOption(seriesAddress, amount, premium, collateralReturned, seller);
		// convert e8 strike to e18 strike
		optionSeries.strike = uint128(
			OptionsCompute.convertFromDecimals(optionSeries.strike, ERC20(seriesAddress).decimals())
		);
		_adjustVariables(collateralReturned, premium, delta, false);
		if (getBalance(collateralAsset) < premium) {
			revert CustomErrors.WithdrawExceedsLiquidity();
		}
		SafeTransferLib.safeTransfer(ERC20(collateralAsset), seller, premium);
		return amount;
	}

	/**
	 * @notice adjust the variables of the pool
	 * @param  collateralAmount the amount of collateral transferred to change on collateral allocated in collateral decimals
	 * @param  optionsValue the value of the options in e18 decimals
	 * @param  delta the delta of the options in e18 decimals
	 * @param  isSale whether the action was an option sale or not
	 */
	function _adjustVariables(
		uint256 collateralAmount,
		uint256 optionsValue,
		int256 delta,
		bool isSale
	) internal {
		if (isSale) {
			collateralAllocated += collateralAmount;
			ephemeralLiabilities += int256(
				OptionsCompute.convertFromDecimals(optionsValue, ERC20(collateralAsset).decimals())
			);
			ephemeralDelta -= delta;
		} else {
			collateralAllocated -= collateralAmount;
			ephemeralLiabilities -= int256(
				OptionsCompute.convertFromDecimals(optionsValue, ERC20(collateralAsset).decimals())
			);
			ephemeralDelta += delta;
		}
	}

	/**
	 * @notice get the volatility feed used by the liquidity pool
	 * @return the volatility feed contract interface
	 */
	function _getVolatilityFeed() internal view returns (VolatilityFeed) {
		return VolatilityFeed(protocol.volatilityFeed());
	}

	/**
	 * @notice get the portfolio values feed used by the liquidity pool
	 * @return the portfolio values feed contract
	 */
	function _getPortfolioValuesFeed() internal view returns (IPortfolioValuesFeed) {
		return IPortfolioValuesFeed(protocol.portfolioValuesFeed());
	}

	/**
	 * @notice get the DHV accounting calculations contract used by the liquidity pool
	 * @return the Accounting contract
	 */
	function _getAccounting() internal view returns (IAccounting) {
		return IAccounting(protocol.accounting());
	}

	/**
	 * @notice get the option registry used for storing and managing the options
	 * @return the option registry contract
	 */
	function _getOptionRegistry() internal view returns (IOptionRegistry) {
		return IOptionRegistry(protocol.optionRegistry());
	}

	/**
	 * @notice get the underlying price with just the underlying asset and strike asset
	 * @param underlying   the asset that is used as the reference asset
	 * @param _strikeAsset the asset that the underlying value is denominated in
	 * @return the underlying price
	 */
	function _getUnderlyingPrice(address underlying, address _strikeAsset)
		internal
		view
		returns (uint256)
	{
		return PriceFeed(protocol.priceFeed()).getNormalizedRate(underlying, _strikeAsset);
	}

	function _isTradingNotPaused() internal view {
		if (isTradingPaused) {
			revert CustomErrors.TradingPaused();
		}
	}

	function _isHandler() internal view {
		if (!handler[msg.sender]) {
			revert CustomErrors.NotHandler();
		}
	}

	/// @dev keepers, managers or governors can access
	function _isKeeper() internal view {
		if (
			!keeper[msg.sender] && msg.sender != authority.governor() && msg.sender != authority.manager()
		) {
			revert CustomErrors.NotKeeper();
		}
	}
}

File 3 of 31 : CustomErrors.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

interface CustomErrors {
	error NotKeeper();
	error IVNotFound();
	error NotHandler();
	error VaultExpired();
	error InvalidInput();
	error InvalidPrice();
	error InvalidBuyer();
	error InvalidOrder();
	error OrderExpired();
	error InvalidAmount();
	error TradingPaused();
	error InvalidAddress();
	error IssuanceFailed();
	error EpochNotClosed();
	error InvalidDecimals();
	error TradingNotPaused();
	error NotLiquidityPool();
	error DeltaNotDecreased();
	error NonExistentOtoken();
	error OrderExpiryTooLong();
	error InvalidShareAmount();
	error ExistingWithdrawal();
	error TotalSupplyReached();
	error StrikeAssetInvalid();
	error OptionStrikeInvalid();
	error OptionExpiryInvalid();
	error NoExistingWithdrawal();
	error SpotMovedBeyondRange();
	error ReactorAlreadyExists();
	error CollateralAssetInvalid();
	error UnderlyingAssetInvalid();
	error CollateralAmountInvalid();
	error WithdrawExceedsLiquidity();
	error InsufficientShareBalance();
	error MaxLiquidityBufferReached();
	error LiabilitiesGreaterThanAssets();
	error CustomOrderInsufficientPrice();
	error CustomOrderInvalidDeltaValue();
	error DeltaQuoteError(uint256 quote, int256 delta);
	error TimeDeltaExceedsThreshold(uint256 timeDelta);
	error PriceDeltaExceedsThreshold(uint256 priceDelta);
	error StrikeAmountExceedsLiquidity(uint256 strikeAmount, uint256 strikeLiquidity);
	error MinStrikeAmountExceedsLiquidity(uint256 strikeAmount, uint256 strikeAmountMin);
	error UnderlyingAmountExceedsLiquidity(uint256 underlyingAmount, uint256 underlyingLiquidity);
	error MinUnderlyingAmountExceedsLiquidity(uint256 underlyingAmount, uint256 underlyingAmountMin);
}

File 4 of 31 : AccessControl.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "../interfaces/IAuthority.sol";

error UNAUTHORIZED();

/**
 *  @title Contract used for access control functionality, based off of OlympusDao Access Control
 */
abstract contract AccessControl {
	/* ========== EVENTS ========== */

	event AuthorityUpdated(IAuthority authority);

	/* ========== STATE VARIABLES ========== */

	IAuthority public authority;

	/* ========== Constructor ========== */

	constructor(IAuthority _authority) {
		authority = _authority;
		emit AuthorityUpdated(_authority);
	}

	/* ========== GOV ONLY ========== */

	function setAuthority(IAuthority _newAuthority) external {
		_onlyGovernor();
		authority = _newAuthority;
		emit AuthorityUpdated(_newAuthority);
	}

	/* ========== INTERNAL CHECKS ========== */

	function _onlyGovernor() internal view {
		if (msg.sender != authority.governor()) revert UNAUTHORIZED();
	}

	function _onlyGuardian() internal view {
		if (!authority.guardian(msg.sender) && msg.sender != authority.governor()) revert UNAUTHORIZED();
	}

	function _onlyManager() internal view {
		if (msg.sender != authority.manager() && msg.sender != authority.governor())
			revert UNAUTHORIZED();
	}
}

File 5 of 31 : OptionsCompute.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "./Types.sol";
import "./CustomErrors.sol";
import "./BlackScholes.sol";

import "prb-math/contracts/PRBMathUD60x18.sol";
import "prb-math/contracts/PRBMathSD59x18.sol";

/**
 *  @title Library used for various helper functionality for the Liquidity Pool
 */
library OptionsCompute {
	using PRBMathUD60x18 for uint256;
	using PRBMathSD59x18 for int256;

	uint8 private constant SCALE_DECIMALS = 18;

	/// @dev assumes decimals are coming in as e18
	function convertToDecimals(uint256 value, uint256 decimals) internal pure returns (uint256) {
		if (decimals > SCALE_DECIMALS) {
			revert();
		}
		uint256 difference = SCALE_DECIMALS - decimals;
		return value / (10**difference);
	}

	/// @dev converts from specified decimals to e18
	function convertFromDecimals(uint256 value, uint256 decimals) internal pure returns (uint256) {
		if (decimals > SCALE_DECIMALS) {
			revert();
		}
		uint256 difference = SCALE_DECIMALS - decimals;
		return value * (10**difference);
	}

	// doesnt allow for interest bearing collateral
	function convertToCollateralDenominated(
		uint256 quote,
		uint256 underlyingPrice,
		Types.OptionSeries memory optionSeries
	) internal pure returns (uint256 convertedQuote) {
		if (optionSeries.strikeAsset != optionSeries.collateral) {
			// convert value from strike asset to collateral asset
			return (quote * 1e18) / underlyingPrice;
		} else {
			return quote;
		}
	}

	/**
	 * @dev computes the percentage change between two integers
	 * @param n new value in e18
	 * @param o old value in e18
	 * @return pC uint256 the percentage change in e18
	 */
	function calculatePercentageChange(uint256 n, uint256 o) internal pure returns (uint256 pC) {
		// if new > old then its a percentage increase so do:
		// ((new - old) * 1e18) / old
		// if new < old then its a percentage decrease so do:
		// ((old - new) * 1e18) / old
		if (n > o) {
			pC = (n - o).div(o);
		} else {
			pC = (o - n).div(o);
		}
	}

	/**
	 * @notice get the latest oracle fed portfolio values and check when they were last updated and make sure this is within a reasonable window in
	 *		   terms of price and time
	 */
	function validatePortfolioValues(
		uint256 spotPrice,
		Types.PortfolioValues memory portfolioValues,
		uint256 maxTimeDeviationThreshold,
		uint256 maxPriceDeviationThreshold
	) public view {
		uint256 timeDelta = block.timestamp - portfolioValues.timestamp;
		// If too much time has passed we want to prevent a possible oracle attack
		if (timeDelta > maxTimeDeviationThreshold) {
			revert CustomErrors.TimeDeltaExceedsThreshold(timeDelta);
		}
		uint256 priceDelta = calculatePercentageChange(spotPrice, portfolioValues.spotPrice);
		// If price has deviated too much we want to prevent a possible oracle attack
		if (priceDelta > maxPriceDeviationThreshold) {
			revert CustomErrors.PriceDeltaExceedsThreshold(priceDelta);
		}
	}

	/**
	 *	@notice calculates the utilization price of an option using the liquidity pool's utilisation skew algorithm
	 */
	function getUtilizationPrice(
		uint256 _utilizationBefore,
		uint256 _utilizationAfter,
		uint256 _totalOptionPrice,
		uint256 _utilizationFunctionThreshold,
		uint256 _belowThresholdGradient,
		uint256 _aboveThresholdGradient,
		uint256 _aboveThresholdYIntercept
	) internal pure returns (uint256 utilizationPrice) {
		if (
			_utilizationBefore <= _utilizationFunctionThreshold &&
			_utilizationAfter <= _utilizationFunctionThreshold
		) {
			// linear function up to threshold utilization
			// take average of before and after utilization and multiply the average by belowThresholdGradient

			uint256 multiplicationFactor = (_utilizationBefore + _utilizationAfter)
				.mul(_belowThresholdGradient)
				.div(2e18);
			return _totalOptionPrice + _totalOptionPrice.mul(multiplicationFactor);
		} else if (
			_utilizationBefore >= _utilizationFunctionThreshold &&
			_utilizationAfter >= _utilizationFunctionThreshold
		) {
			// over threshold utilization the skew factor will follow a steeper line

			uint256 multiplicationFactor = _aboveThresholdGradient
				.mul(_utilizationBefore + _utilizationAfter)
				.div(2e18) - _aboveThresholdYIntercept;

			return _totalOptionPrice + _totalOptionPrice.mul(multiplicationFactor);
		} else {
			// in this case the utilization after is above the threshold and
			// utilization before is below it.
			// _utilizationAfter will always be greater than _utilizationBefore
			// finds the ratio of the distance below the threshold to the distance above the threshold
			uint256 weightingRatio = (_utilizationFunctionThreshold - _utilizationBefore).div(
				_utilizationAfter - _utilizationFunctionThreshold
			);
			// finds the average y value on the part of the function below threshold
			uint256 averageFactorBelow = (_utilizationFunctionThreshold + _utilizationBefore).div(2e18).mul(
				_belowThresholdGradient
			);
			// finds average y value on part of the function above threshold
			uint256 averageFactorAbove = (_utilizationAfter + _utilizationFunctionThreshold).div(2e18).mul(
				_aboveThresholdGradient
			) - _aboveThresholdYIntercept;
			// finds the weighted average of the two above averaged to find the average utilization skew over the range of utilization
			uint256 multiplicationFactor = (weightingRatio.mul(averageFactorBelow) + averageFactorAbove).div(
				1e18 + weightingRatio
			);
			return _totalOptionPrice + _totalOptionPrice.mul(multiplicationFactor);
		}
	}

	/**
	 * @notice get the greeks of a quotePrice for a given optionSeries
	 * @param  optionSeries Types.OptionSeries struct for describing the option to price greeks - strike in e18
	 * @return quote           Quote price of the option - in e18
	 * @return delta           delta of the option being priced - in e18
	 */
	function quotePriceGreeks(
		Types.OptionSeries memory optionSeries,
		bool isBuying,
		uint256 bidAskIVSpread,
		uint256 riskFreeRate,
		uint256 iv,
		uint256 underlyingPrice
	) internal view returns (uint256 quote, int256 delta) {
		if (iv == 0) {
			revert CustomErrors.IVNotFound();
		}
		// reduce IV by a factor of bidAskIVSpread if we are buying the options
		if (isBuying) {
			iv = (iv * (1e18 - (bidAskIVSpread))) / 1e18;
		}
		// revert CustomErrors.if the expiry is in the past
		if (optionSeries.expiration <= block.timestamp) {
			revert CustomErrors.OptionExpiryInvalid();
		}
		(quote, delta) = BlackScholes.blackScholesCalcGreeks(
			underlyingPrice,
			optionSeries.strike,
			optionSeries.expiration,
			iv,
			riskFreeRate,
			optionSeries.isPut
		);
	}
}

File 6 of 31 : AddressBookInterface.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

interface AddressBookInterface {
	/* Getters */

	function getOtokenImpl() external view returns (address);

	function getOtokenFactory() external view returns (address);

	function getWhitelist() external view returns (address);

	function getController() external view returns (address);

	function getOracle() external view returns (address);

	function getMarginPool() external view returns (address);

	function getMarginCalculator() external view returns (address);

	function getLiquidationManager() external view returns (address);

	function getAddress(bytes32 _id) external view returns (address);

	/* Setters */

	function setOtokenImpl(address _otokenImpl) external;

	function setOtokenFactory(address _factory) external;

	function setOracleImpl(address _otokenImpl) external;

	function setWhitelist(address _whitelist) external;

	function setController(address _controller) external;

	function setMarginPool(address _marginPool) external;

	function setMarginCalculator(address _calculator) external;

	function setLiquidationManager(address _liquidationManager) external;

	function setAddress(bytes32 _id, address _newImpl) external;
}

File 7 of 31 : Types.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

library Types {
	struct OptionSeries {
		uint64 expiration;
		uint128 strike;
		bool isPut;
		address underlying;
		address strikeAsset;
		address collateral;
	}
	struct PortfolioValues {
		int256 delta;
		int256 gamma;
		int256 vega;
		int256 theta;
		int256 callPutsValue;
		uint256 timestamp;
		uint256 spotPrice;
	}
	struct Order {
		OptionSeries optionSeries;
		uint256 amount;
		uint256 price;
		uint256 orderExpiry;
		address buyer;
		address seriesAddress;
		uint128 lowerSpotMovementRange;
		uint128 upperSpotMovementRange;
		bool isBuyBack;
	}
	// strike and expiry date range for options
	struct OptionParams {
		uint128 minCallStrikePrice;
		uint128 maxCallStrikePrice;
		uint128 minPutStrikePrice;
		uint128 maxPutStrikePrice;
		uint128 minExpiry;
		uint128 maxExpiry;
	}

	struct UtilizationState {
		uint256 totalOptionPrice; //e18
		int256 totalDelta; // e18
		uint256 collateralToAllocate; //collateral decimals
		uint256 utilizationBefore; // e18
		uint256 utilizationAfter; //e18
		uint256 utilizationPrice; //e18
		bool isDecreased;
		uint256 deltaTiltAmount; //e18
		uint256 underlyingPrice; // strike asset decimals
		uint256 iv; // e18
	}

}

File 8 of 31 : SafeTransferLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

import {ERC20} from "../tokens/ERC20.sol";

/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @author Modified from Gnosis (https://github.com/gnosis/gp-v2-contracts/blob/main/src/contracts/libraries/GPv2SafeERC20.sol)
/// @dev Use with caution! Some functions in this library knowingly create dirty bits at the destination of the free memory pointer.
library SafeTransferLib {
    /*///////////////////////////////////////////////////////////////
                            ETH OPERATIONS
    //////////////////////////////////////////////////////////////*/

    function safeTransferETH(address to, uint256 amount) internal {
        bool callStatus;

        assembly {
            // Transfer the ETH and store if it succeeded or not.
            callStatus := call(gas(), to, amount, 0, 0, 0, 0)
        }

        require(callStatus, "ETH_TRANSFER_FAILED");
    }

    /*///////////////////////////////////////////////////////////////
                           ERC20 OPERATIONS
    //////////////////////////////////////////////////////////////*/

    function safeTransferFrom(
        address tokenAddress,
        address from,
        address to,
        uint256 amount
    ) internal {
        ERC20 token = ERC20(tokenAddress);
        bool callStatus;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata to memory piece by piece:
            mstore(freeMemoryPointer, 0x23b872dd00000000000000000000000000000000000000000000000000000000) // Begin with the function selector.
            mstore(add(freeMemoryPointer, 4), and(from, 0xffffffffffffffffffffffffffffffffffffffff)) // Mask and append the "from" argument.
            mstore(add(freeMemoryPointer, 36), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Mask and append the "to" argument.
            mstore(add(freeMemoryPointer, 68), amount) // Finally append the "amount" argument. No mask as it's a full 32 byte value.

            // Call the token and store if it succeeded or not.
            // We use 100 because the calldata length is 4 + 32 * 3.
            callStatus := call(gas(), token, 0, freeMemoryPointer, 100, 0, 0)
        }

        require(didLastOptionalReturnCallSucceed(callStatus), "TRANSFER_FROM_FAILED");
    }

    function safeTransfer(
        ERC20 token,
        address to,
        uint256 amount
    ) internal {
        bool callStatus;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata to memory piece by piece:
            mstore(freeMemoryPointer, 0xa9059cbb00000000000000000000000000000000000000000000000000000000) // Begin with the function selector.
            mstore(add(freeMemoryPointer, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Mask and append the "to" argument.
            mstore(add(freeMemoryPointer, 36), amount) // Finally append the "amount" argument. No mask as it's a full 32 byte value.

            // Call the token and store if it succeeded or not.
            // We use 68 because the calldata length is 4 + 32 * 2.
            callStatus := call(gas(), token, 0, freeMemoryPointer, 68, 0, 0)
        }

        require(didLastOptionalReturnCallSucceed(callStatus), "TRANSFER_FAILED");
    }

    function safeApprove(
        ERC20 token,
        address to,
        uint256 amount
    ) internal {
        bool callStatus;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata to memory piece by piece:
            mstore(freeMemoryPointer, 0x095ea7b300000000000000000000000000000000000000000000000000000000) // Begin with the function selector.
            mstore(add(freeMemoryPointer, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Mask and append the "to" argument.
            mstore(add(freeMemoryPointer, 36), amount) // Finally append the "amount" argument. No mask as it's a full 32 byte value.

            // Call the token and store if it succeeded or not.
            // We use 68 because the calldata length is 4 + 32 * 2.
            callStatus := call(gas(), token, 0, freeMemoryPointer, 68, 0, 0)
        }

        require(didLastOptionalReturnCallSucceed(callStatus), "APPROVE_FAILED");
    }

    /*///////////////////////////////////////////////////////////////
                         INTERNAL HELPER LOGIC
    //////////////////////////////////////////////////////////////*/

    function didLastOptionalReturnCallSucceed(bool callStatus) private pure returns (bool success) {
        assembly {
            // Get how many bytes the call returned.
            let returnDataSize := returndatasize()

            // If the call reverted:
            if iszero(callStatus) {
                // Copy the revert message into memory.
                returndatacopy(0, 0, returnDataSize)

                // Revert with the same message.
                revert(0, returnDataSize)
            }

            switch returnDataSize
            case 32 {
                // Copy the return data into memory.
                returndatacopy(0, 0, returnDataSize)

                // Set success to whether it returned true.
                success := iszero(iszero(mload(0)))
            }
            case 0 {
                // There was no return data.
                success := 1
            }
            default {
                // It returned some malformed input.
                success := 0
            }
        }
    }
}

File 9 of 31 : OpynInteractions.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;

import "./SafeTransferLib.sol";

import { Types } from "./Types.sol";
import { IOtokenFactory, IOtoken, IController, GammaTypes } from "../interfaces/GammaInterface.sol";

/**
 *  @title Library used for standard interactions with the opyn-rysk gamma protocol
 *   @dev inherited by the options registry to complete base opyn-rysk gamma protocol interactions
 *        Interacts with the opyn-rysk gamma protocol in all functions
 */
library OpynInteractions {
	uint256 private constant SCALE_FROM = 10**10;
	error NoShort();

	/**
	 * @notice Either retrieves the option token if it already exists, or deploy it
	 * @param oTokenFactory is the address of the opyn oTokenFactory
	 * @param collateral asset that is held as collateral against short/written options
	 * @param underlying is the address of the underlying asset of the option
	 * @param strikeAsset is the address of the collateral asset of the option
	 * @param strike is the strike price of the option in 1e8 format
	 * @param expiration is the expiry timestamp of the option
	 * @param isPut the type of option
	 * @return the address of the option
	 */
	function getOrDeployOtoken(
		address oTokenFactory,
		address collateral,
		address underlying,
		address strikeAsset,
		uint256 strike,
		uint256 expiration,
		bool isPut
	) external returns (address) {
		IOtokenFactory factory = IOtokenFactory(oTokenFactory);

		address otokenFromFactory = factory.getOtoken(
			underlying,
			strikeAsset,
			collateral,
			strike,
			expiration,
			isPut
		);

		if (otokenFromFactory != address(0)) {
			return otokenFromFactory;
		}

		address otoken = factory.createOtoken(
			underlying,
			strikeAsset,
			collateral,
			strike,
			expiration,
			isPut
		);

		return otoken;
	}

	/**
	 * @notice Retrieves the option token if it already exists
	 * @param oTokenFactory is the address of the opyn oTokenFactory
	 * @param collateral asset that is held as collateral against short/written options
	 * @param underlying is the address of the underlying asset of the option
	 * @param strikeAsset is the address of the collateral asset of the option
	 * @param strike is the strike price of the option in 1e8 format
	 * @param expiration is the expiry timestamp of the option
	 * @param isPut the type of option
	 * @return otokenFromFactory the address of the option
	 */
	function getOtoken(
		address oTokenFactory,
		address collateral,
		address underlying,
		address strikeAsset,
		uint256 strike,
		uint256 expiration,
		bool isPut
	) external view returns (address otokenFromFactory) {
		IOtokenFactory factory = IOtokenFactory(oTokenFactory);
		otokenFromFactory = factory.getOtoken(
			underlying,
			strikeAsset,
			collateral,
			strike,
			expiration,
			isPut
		);
	}

	/**
	 * @notice Creates the actual Opyn short position by depositing collateral and minting otokens
	 * @param gammaController is the address of the opyn controller contract
	 * @param marginPool is the address of the opyn margin contract which holds the collateral
	 * @param oTokenAddress is the address of the otoken to mint
	 * @param depositAmount is the amount of collateral to deposit
	 * @param vaultId is the vault id to use for creating this short
	 * @param amount is the mint amount in 1e18 format
	 * @param vaultType is the type of vault to be created
	 * @return the otoken mint amount
	 */
	function createShort(
		address gammaController,
		address marginPool,
		address oTokenAddress,
		uint256 depositAmount,
		uint256 vaultId,
		uint256 amount,
		uint256 vaultType
	) external returns (uint256) {
		IController controller = IController(gammaController);
		amount = amount / SCALE_FROM;
		// An otoken's collateralAsset is the vault's `asset`
		// So in the context of performing Opyn short operations we call them collateralAsset
		IOtoken oToken = IOtoken(oTokenAddress);
		address collateralAsset = oToken.collateralAsset();

		// double approve to fix non-compliant ERC20s
		ERC20 collateralToken = ERC20(collateralAsset);
		SafeTransferLib.safeApprove(collateralToken, marginPool, depositAmount);
		// initialise the controller args with 2 incase the vault already exists
		IController.ActionArgs[] memory actions = new IController.ActionArgs[](2);
		// check if a new vault needs to be created
		uint256 newVaultID = (controller.getAccountVaultCounter(address(this))) + 1;
		if (newVaultID == vaultId) {
			actions = new IController.ActionArgs[](3);

			actions[0] = IController.ActionArgs(
				IController.ActionType.OpenVault,
				address(this), // owner
				address(this), // receiver
				address(0), // asset, otoken
				vaultId, // vaultId
				0, // amount
				0, //index
				abi.encode(vaultType) //data
			);

			actions[1] = IController.ActionArgs(
				IController.ActionType.DepositCollateral,
				address(this), // owner
				address(this), // address to transfer from
				collateralAsset, // deposited asset
				vaultId, // vaultId
				depositAmount, // amount
				0, //index
				"" //data
			);

			actions[2] = IController.ActionArgs(
				IController.ActionType.MintShortOption,
				address(this), // owner
				address(this), // address to transfer to
				oTokenAddress, // option address
				vaultId, // vaultId
				amount, // amount
				0, //index
				"" //data
			);
		} else {
			actions[0] = IController.ActionArgs(
				IController.ActionType.DepositCollateral,
				address(this), // owner
				address(this), // address to transfer from
				collateralAsset, // deposited asset
				vaultId, // vaultId
				depositAmount, // amount
				0, //index
				"" //data
			);

			actions[1] = IController.ActionArgs(
				IController.ActionType.MintShortOption,
				address(this), // owner
				address(this), // address to transfer to
				oTokenAddress, // option address
				vaultId, // vaultId
				amount, // amount
				0, //index
				"" //data
			);
		}

		controller.operate(actions);
		// returns in e8
		return amount;
	}

	/**
	 * @notice Deposits Collateral to a specific vault
	 * @param gammaController is the address of the opyn controller contract
	 * @param marginPool is the address of the opyn margin contract which holds the collateral
	 * @param collateralAsset is the address of the collateral asset to deposit
	 * @param depositAmount is the amount of collateral to deposit
	 * @param vaultId is the vault id to access
	 */
	function depositCollat(
		address gammaController,
		address marginPool,
		address collateralAsset,
		uint256 depositAmount,
		uint256 vaultId
	) external {
		IController controller = IController(gammaController);
		// double approve to fix non-compliant ERC20s
		ERC20 collateralToken = ERC20(collateralAsset);
		SafeTransferLib.safeApprove(collateralToken, marginPool, depositAmount);
		IController.ActionArgs[] memory actions = new IController.ActionArgs[](1);

		actions[0] = IController.ActionArgs(
			IController.ActionType.DepositCollateral,
			address(this), // owner
			address(this), // address to transfer from
			collateralAsset, // deposited asset
			vaultId, // vaultId
			depositAmount, // amount
			0, //index
			"" //data
		);

		controller.operate(actions);
	}

	/**
	 * @notice Withdraws Collateral from a specific vault
	 * @param gammaController is the address of the opyn controller contract
	 * @param collateralAsset is the address of the collateral asset to withdraw
	 * @param withdrawAmount is the amount of collateral to withdraw
	 * @param vaultId is the vault id to access
	 */
	function withdrawCollat(
		address gammaController,
		address collateralAsset,
		uint256 withdrawAmount,
		uint256 vaultId
	) external {
		IController controller = IController(gammaController);

		IController.ActionArgs[] memory actions = new IController.ActionArgs[](1);

		actions[0] = IController.ActionArgs(
			IController.ActionType.WithdrawCollateral,
			address(this), // owner
			address(this), // address to transfer to
			collateralAsset, // withdrawn asset
			vaultId, // vaultId
			withdrawAmount, // amount
			0, //index
			"" //data
		);

		controller.operate(actions);
	}

	/**
	 * @notice Burns an opyn short position and returns collateral back to OptionRegistry
	 * @param gammaController is the address of the opyn controller contract
	 * @param oTokenAddress is the address of the otoken to burn
	 * @param burnAmount is the amount of options to burn
	 * @param vaultId is the vault id used that holds the short
	 * @return the collateral returned amount
	 */
	function burnShort(
		address gammaController,
		address oTokenAddress,
		uint256 burnAmount,
		uint256 vaultId
	) external returns (uint256) {
		IController controller = IController(gammaController);
		// An otoken's collateralAsset is the vault's `asset`
		// So in the context of performing Opyn short operations we call them collateralAsset
		IOtoken oToken = IOtoken(oTokenAddress);
		ERC20 collateralAsset = ERC20(oToken.collateralAsset());
		uint256 startCollatBalance = collateralAsset.balanceOf(address(this));
		GammaTypes.Vault memory vault = controller.getVault(address(this), vaultId);
		// initialise the controller args with 2 incase the vault already exists
		IController.ActionArgs[] memory actions = new IController.ActionArgs[](2);

		actions[0] = IController.ActionArgs(
			IController.ActionType.BurnShortOption,
			address(this), // owner
			address(this), // address to transfer from
			oTokenAddress, // oToken address
			vaultId, // vaultId
			burnAmount, // amount to burn
			0, //index
			"" //data
		);

		actions[1] = IController.ActionArgs(
			IController.ActionType.WithdrawCollateral,
			address(this), // owner
			address(this), // address to transfer to
			address(collateralAsset), // withdrawn asset
			vaultId, // vaultId
			(vault.collateralAmounts[0] * burnAmount) / vault.shortAmounts[0], // amount
			0, //index
			"" //data
		);

		controller.operate(actions);
		// returns in collateral decimals
		return collateralAsset.balanceOf(address(this)) - startCollatBalance;
	}

	/**
	 * @notice Close the existing short otoken position.
	 * @param gammaController is the address of the opyn controller contract
	 * @param vaultId is the id of the vault to be settled
	 * @return collateralRedeemed collateral redeemed from the vault
	 * @return collateralLost collateral left behind in vault used to pay ITM expired options
	 * @return shortAmount number of options that were written
	 */
	function settle(address gammaController, uint256 vaultId)
		external
		returns (
			uint256 collateralRedeemed,
			uint256 collateralLost,
			uint256 shortAmount
		)
	{
		IController controller = IController(gammaController);

		GammaTypes.Vault memory vault = controller.getVault(address(this), vaultId);
		if (vault.shortOtokens.length == 0) {
			revert NoShort();
		}

		// An otoken's collateralAsset is the vault's `asset`
		// So in the context of performing Opyn short operations we call them collateralAsset
		ERC20 collateralToken = ERC20(vault.collateralAssets[0]);

		// This is equivalent to doing ERC20(vault.asset).balanceOf(address(this))
		uint256 startCollateralBalance = collateralToken.balanceOf(address(this));

		// If it is after expiry, we need to settle the short position using the normal way
		// Delete the vault and withdraw all remaining collateral from the vault
		IController.ActionArgs[] memory actions = new IController.ActionArgs[](1);

		actions[0] = IController.ActionArgs(
			IController.ActionType.SettleVault,
			address(this), // owner
			address(this), // address to transfer to
			address(0), // not used
			vaultId, // vaultId
			0, // not used
			0, // not used
			"" // not used
		);

		controller.operate(actions);

		uint256 endCollateralBalance = collateralToken.balanceOf(address(this));
		// calulate collateral redeemed and lost for collateral management in liquidity pool
		collateralRedeemed = endCollateralBalance - startCollateralBalance;
		// returns in collateral decimals, collateralDecimals, e8
		return (
			collateralRedeemed,
			vault.collateralAmounts[0] - collateralRedeemed,
			vault.shortAmounts[0]
		);
	}

	/**
	 * @notice Exercises an ITM option
	 * @param gammaController is the address of the opyn controller contract
	 * @param marginPool is the address of the opyn margin pool
	 * @param series is the address of the option to redeem
	 * @param amount is the number of oTokens to redeem - passed in as e8
	 * @return amount of asset received by exercising the option
	 */
	function redeem(
		address gammaController,
		address marginPool,
		address series,
		uint256 amount
	) external returns (uint256) {
		IController controller = IController(gammaController);
		address collateralAsset = IOtoken(series).collateralAsset();
		uint256 startAssetBalance = ERC20(collateralAsset).balanceOf(msg.sender);

		// If it is after expiry, we need to redeem the profits
		IController.ActionArgs[] memory actions = new IController.ActionArgs[](1);

		actions[0] = IController.ActionArgs(
			IController.ActionType.Redeem,
			address(0), // not used
			msg.sender, // address to send profits to
			series, // address of otoken
			0, // not used
			amount, // otoken balance
			0, // not used
			"" // not used
		);
		SafeTransferLib.safeApprove(ERC20(series), marginPool, amount);
		controller.operate(actions);

		uint256 endAssetBalance = ERC20(collateralAsset).balanceOf(msg.sender);
		// returns in collateral decimals
		return endAssetBalance - startAssetBalance;
	}
}

File 10 of 31 : IOracle.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

interface IOracle {
	function getPrice(address _asset) external view returns (uint256);
}

File 11 of 31 : IMarginCalculator.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

interface IMarginCalculator {
	function getNakedMarginRequired(
		address _underlying,
		address _strike,
		address _collateral,
		uint256 _shortAmount,
		uint256 _strikePrice,
		uint256 _underlyingPrice,
		uint256 _shortExpiryTimestamp,
		uint256 _collateralDecimals,
		bool _isPut
	) external view returns (uint256);
}

File 12 of 31 : GammaInterface.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;

library GammaTypes {
	// vault is a struct of 6 arrays that describe a position a user has, a user can have multiple vaults.
	struct Vault {
		// addresses of oTokens a user has shorted (i.e. written) against this vault
		address[] shortOtokens;
		// addresses of oTokens a user has bought and deposited in this vault
		// user can be long oTokens without opening a vault (e.g. by buying on a DEX)
		// generally, long oTokens will be 'deposited' in vaults to act as collateral
		// in order to write oTokens against (i.e. in spreads)
		address[] longOtokens;
		// addresses of other ERC-20s a user has deposited as collateral in this vault
		address[] collateralAssets;
		// quantity of oTokens minted/written for each oToken address in shortOtokens
		uint256[] shortAmounts;
		// quantity of oTokens owned and held in the vault for each oToken address in longOtokens
		uint256[] longAmounts;
		// quantity of ERC-20 deposited as collateral in the vault for each ERC-20 address in collateralAssets
		uint256[] collateralAmounts;
	}

	// vaultLiquidationDetails is a struct of 3 variables that store the series address, short amount liquidated and collateral transferred for
	// a given liquidation
	struct VaultLiquidationDetails {
		address series;
		uint128 shortAmount;
		uint128 collateralAmount;
	}
}

interface IOtoken {
	function underlyingAsset() external view returns (address);

	function strikeAsset() external view returns (address);

	function collateralAsset() external view returns (address);

	function strikePrice() external view returns (uint256);

	function expiryTimestamp() external view returns (uint256);

	function isPut() external view returns (bool);
}

interface IOtokenFactory {
	function getOtoken(
		address _underlyingAsset,
		address _strikeAsset,
		address _collateralAsset,
		uint256 _strikePrice,
		uint256 _expiry,
		bool _isPut
	) external view returns (address);

	function createOtoken(
		address _underlyingAsset,
		address _strikeAsset,
		address _collateralAsset,
		uint256 _strikePrice,
		uint256 _expiry,
		bool _isPut
	) external returns (address);

	function getTargetOtokenAddress(
		address _underlyingAsset,
		address _strikeAsset,
		address _collateralAsset,
		uint256 _strikePrice,
		uint256 _expiry,
		bool _isPut
	) external view returns (address);

	event OtokenCreated(
		address tokenAddress,
		address creator,
		address indexed underlying,
		address indexed strike,
		address indexed collateral,
		uint256 strikePrice,
		uint256 expiry,
		bool isPut
	);
}

interface IController {
	// possible actions that can be performed
	enum ActionType {
		OpenVault,
		MintShortOption,
		BurnShortOption,
		DepositLongOption,
		WithdrawLongOption,
		DepositCollateral,
		WithdrawCollateral,
		SettleVault,
		Redeem,
		Call,
		Liquidate
	}

	struct ActionArgs {
		// type of action that is being performed on the system
		ActionType actionType;
		// address of the account owner
		address owner;
		// address which we move assets from or to (depending on the action type)
		address secondAddress;
		// asset that is to be transfered
		address asset;
		// index of the vault that is to be modified (if any)
		uint256 vaultId;
		// amount of asset that is to be transfered
		uint256 amount;
		// each vault can hold multiple short / long / collateral assets
		// but we are restricting the scope to only 1 of each in this version
		// in future versions this would be the index of the short / long / collateral asset that needs to be modified
		uint256 index;
		// any other data that needs to be passed in for arbitrary function calls
		bytes data;
	}

	struct RedeemArgs {
		// address to which we pay out the oToken proceeds
		address receiver;
		// oToken that is to be redeemed
		address otoken;
		// amount of oTokens that is to be redeemed
		uint256 amount;
	}

	function getPayout(address _otoken, uint256 _amount) external view returns (uint256);

	function operate(ActionArgs[] calldata _actions) external;

	function getAccountVaultCounter(address owner) external view returns (uint256);

	function oracle() external view returns (address);

	function getVault(address _owner, uint256 _vaultId)
		external
		view
		returns (GammaTypes.Vault memory);

	function getProceed(address _owner, uint256 _vaultId) external view returns (uint256);

	function isSettlementAllowed(
		address _underlying,
		address _strike,
		address _collateral,
		uint256 _expiry
	) external view returns (bool);

	function clearVaultLiquidationDetails(uint256 _vaultId) external;

	function getVaultLiquidationDetails(address _owner, uint256 _vaultId)
		external
		view
		returns (
			address,
			uint256,
			uint256
		);
}

File 13 of 31 : ERC20.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
    /*///////////////////////////////////////////////////////////////
                                  EVENTS
    //////////////////////////////////////////////////////////////*/

    event Transfer(address indexed from, address indexed to, uint256 amount);

    event Approval(address indexed owner, address indexed spender, uint256 amount);

    /*///////////////////////////////////////////////////////////////
                             METADATA STORAGE
    //////////////////////////////////////////////////////////////*/

    string public name;

    string public symbol;

    uint8 public immutable decimals;

    /*///////////////////////////////////////////////////////////////
                              ERC20 STORAGE
    //////////////////////////////////////////////////////////////*/

    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;

    mapping(address => mapping(address => uint256)) public allowance;

    /*///////////////////////////////////////////////////////////////
                             EIP-2612 STORAGE
    //////////////////////////////////////////////////////////////*/

    uint256 internal immutable INITIAL_CHAIN_ID;

    bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;

    mapping(address => uint256) public nonces;

    /*///////////////////////////////////////////////////////////////
                               CONSTRUCTOR
    //////////////////////////////////////////////////////////////*/

    constructor(
        string memory _name,
        string memory _symbol,
        uint8 _decimals
    ) {
        name = _name;
        symbol = _symbol;
        decimals = _decimals;

        INITIAL_CHAIN_ID = block.chainid;
        INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
    }

    /*///////////////////////////////////////////////////////////////
                              ERC20 LOGIC
    //////////////////////////////////////////////////////////////*/

    function approve(address spender, uint256 amount) public virtual returns (bool) {
        allowance[msg.sender][spender] = amount;

        emit Approval(msg.sender, spender, amount);

        return true;
    }

    function transfer(address to, uint256 amount) public virtual returns (bool) {
        balanceOf[msg.sender] -= amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(msg.sender, to, amount);

        return true;
    }

    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual returns (bool) {
        uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.

        if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;

        balanceOf[from] -= amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(from, to, amount);

        return true;
    }

    /*///////////////////////////////////////////////////////////////
                              EIP-2612 LOGIC
    //////////////////////////////////////////////////////////////*/

    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) public virtual {
        require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");

        // Unchecked because the only math done is incrementing
        // the owner's nonce which cannot realistically overflow.
        unchecked {
            bytes32 digest = keccak256(
                abi.encodePacked(
                    "\x19\x01",
                    DOMAIN_SEPARATOR(),
                    keccak256(
                        abi.encode(
                            keccak256(
                                "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
                            ),
                            owner,
                            spender,
                            value,
                            nonces[owner]++,
                            deadline
                        )
                    )
                )
            );

            address recoveredAddress = ecrecover(digest, v, r, s);

            require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");

            allowance[recoveredAddress][spender] = value;
        }

        emit Approval(owner, spender, value);
    }

    function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
        return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
    }

    function computeDomainSeparator() internal view virtual returns (bytes32) {
        return
            keccak256(
                abi.encode(
                    keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
                    keccak256(bytes(name)),
                    keccak256("1"),
                    block.chainid,
                    address(this)
                )
            );
    }

    /*///////////////////////////////////////////////////////////////
                       INTERNAL MINT/BURN LOGIC
    //////////////////////////////////////////////////////////////*/

    function _mint(address to, uint256 amount) internal virtual {
        totalSupply += amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(address(0), to, amount);
    }

    function _burn(address from, uint256 amount) internal virtual {
        balanceOf[from] -= amount;

        // Cannot underflow because a user's balance
        // will never be larger than the total supply.
        unchecked {
            totalSupply -= amount;
        }

        emit Transfer(from, address(0), amount);
    }
}

File 14 of 31 : PriceFeed.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;

import "./interfaces/AggregatorV3Interface.sol";

import "./libraries/AccessControl.sol";

/**
 *  @title Contract used for accessing exchange rates using chainlink price feeds
 *  @dev Interacts with chainlink price feeds and services all contracts in the system for price data.
 */
contract PriceFeed is AccessControl {
	/////////////////////////////////////
	/// governance settable variables ///
	/////////////////////////////////////

	mapping(address => mapping(address => address)) public priceFeeds;

	//////////////////////////
	/// constant variables ///
	//////////////////////////

	uint8 private constant SCALE_DECIMALS = 18;
	// seconds since the last price feed update until we deem the data to be stale
	uint32 private constant STALE_PRICE_DELAY = 3600;

	constructor(address _authority) AccessControl(IAuthority(_authority)) {}

	///////////////
	/// setters ///
	///////////////

	function addPriceFeed(
		address underlying,
		address strike,
		address feed
	) public {
		_onlyGovernor();
		priceFeeds[underlying][strike] = feed;
	}

	///////////////////////
	/// complex getters ///
	///////////////////////

	function getRate(address underlying, address strike) external view returns (uint256) {
		address feedAddress = priceFeeds[underlying][strike];
		require(feedAddress != address(0), "Price feed does not exist");
		AggregatorV3Interface feed = AggregatorV3Interface(feedAddress);
		(uint80 roundId, int256 rate, , uint256 timestamp, uint80 answeredInRound) = feed
			.latestRoundData();
		require(rate > 0, "ChainLinkPricer: price is lower than 0");
		require(timestamp != 0, "ROUND_NOT_COMPLETE");
		require(block.timestamp <= timestamp + STALE_PRICE_DELAY, "STALE_PRICE");
		require(answeredInRound >= roundId, "STALE_PRICE");
		return uint256(rate);
	}

	/// @dev get the rate from chainlink and convert it to e18 decimals
	function getNormalizedRate(address underlying, address strike) external view returns (uint256) {
		address feedAddress = priceFeeds[underlying][strike];
		require(feedAddress != address(0), "Price feed does not exist");
		AggregatorV3Interface feed = AggregatorV3Interface(feedAddress);
		uint8 feedDecimals = feed.decimals();
		(uint80 roundId, int256 rate, , uint256 timestamp, uint80 answeredInRound) = feed
			.latestRoundData();
		require(rate > 0, "ChainLinkPricer: price is lower than 0");
		require(timestamp != 0, "ROUND_NOT_COMPLETE");
		require(block.timestamp <= timestamp + STALE_PRICE_DELAY, "STALE_PRICE");
		require(answeredInRound >= roundId, "STALE_PRICE_ROUND");
		uint8 difference;
		if (SCALE_DECIMALS > feedDecimals) {
			difference = SCALE_DECIMALS - feedDecimals;
			return uint256(rate) * (10**difference);
		}
		difference = feedDecimals - SCALE_DECIMALS;
		return uint256(rate) / (10**difference);
	}
}

File 15 of 31 : Protocol.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "./libraries/AccessControl.sol";

/**
 *  @title Contract used for storage of important contracts for the liquidity pool
 */
contract Protocol is AccessControl {
	////////////////////////
	/// static variables ///
	////////////////////////

	address public immutable optionRegistry;

	/////////////////////////////////////
	/// governance settable variables ///
	/////////////////////////////////////

	address public volatilityFeed;
	address public portfolioValuesFeed;
	address public accounting;
	address public priceFeed;

	constructor(
		address _optionRegistry,
		address _priceFeed,
		address _volatilityFeed,
		address _portfolioValuesFeed,
		address _authority
	) AccessControl(IAuthority(_authority)) {
		optionRegistry = _optionRegistry;
		priceFeed = _priceFeed;
		volatilityFeed = _volatilityFeed;
		portfolioValuesFeed = _portfolioValuesFeed;
	}

	///////////////
	/// setters ///
	///////////////

	function changeVolatilityFeed(address _volFeed) external {
		_onlyGovernor();
		volatilityFeed = _volFeed;
	}

	function changePortfolioValuesFeed(address _portfolioValuesFeed) external {
		_onlyGovernor();
		portfolioValuesFeed = _portfolioValuesFeed;
	}

	function changeAccounting(address _accounting) external {
		_onlyGovernor();
		accounting= _accounting;
	}
	
	function changePriceFeed(address _priceFeed) external {
		_onlyGovernor();
		priceFeed = _priceFeed;
	}
}

File 16 of 31 : BlackScholes.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "prb-math/contracts/PRBMathSD59x18.sol";
import "prb-math/contracts/PRBMathUD60x18.sol";

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

/**
 *  @title Library used to calculate an option price using Black Scholes
 */
library BlackScholes {
	using PRBMathSD59x18 for int256;
	using PRBMathSD59x18 for int8;
	using PRBMathUD60x18 for uint256;

	uint256 private constant ONE_YEAR_SECONDS = 31557600;
	uint256 private constant ONE = 1000000000000000000;
	uint256 private constant TWO = 2000000000000000000;

	struct Intermediates {
		uint256 d1Denominator;
		int256 d1;
		int256 eToNegRT;
	}

	function callOptionPrice(
		int256 d1,
		int256 d1Denominator,
		int256 price,
		int256 strike,
		int256 eToNegRT
	) public pure returns (uint256) {
		int256 d2 = d1 - d1Denominator;
		int256 cdfD1 = NormalDist.cdf(d1);
		int256 cdfD2 = NormalDist.cdf(d2);
		int256 priceCdf = price.mul(cdfD1);
		int256 strikeBy = strike.mul(eToNegRT).mul(cdfD2);
		assert(priceCdf >= strikeBy);
		return uint256(priceCdf - strikeBy);
	}

	function callOptionPriceGreeks(
		int256 d1,
		int256 d1Denominator,
		int256 price,
		int256 strike,
		int256 eToNegRT
	) public pure returns (uint256 quote, int256 delta) {
		int256 d2 = d1 - d1Denominator;
		int256 cdfD1 = NormalDist.cdf(d1);
		int256 cdfD2 = NormalDist.cdf(d2);
		int256 priceCdf = price.mul(cdfD1);
		int256 strikeBy = strike.mul(eToNegRT).mul(cdfD2);
		assert(priceCdf >= strikeBy);
		quote = uint256(priceCdf - strikeBy);
		delta = cdfD1;
	}

	function putOptionPriceGreeks(
		int256 d1,
		int256 d1Denominator,
		int256 price,
		int256 strike,
		int256 eToNegRT
	) public pure returns (uint256 quote, int256 delta) {
		int256 d2 = d1Denominator - d1;
		int256 cdfD1 = NormalDist.cdf(-d1);
		int256 cdfD2 = NormalDist.cdf(d2);
		int256 priceCdf = price.mul(cdfD1);
		int256 strikeBy = strike.mul(eToNegRT).mul(cdfD2);
		assert(strikeBy >= priceCdf);
		quote = uint256(strikeBy - priceCdf);
		delta = -cdfD1;
	}

	function putOptionPrice(
		int256 d1,
		int256 d1Denominator,
		int256 price,
		int256 strike,
		int256 eToNegRT
	) public pure returns (uint256) {
		int256 d2 = d1Denominator - d1;
		int256 cdfD1 = NormalDist.cdf(-d1);
		int256 cdfD2 = NormalDist.cdf(d2);
		int256 priceCdf = price.mul(cdfD1);
		int256 strikeBy = strike.mul(eToNegRT).mul(cdfD2);
		assert(strikeBy >= priceCdf);
		return uint256(strikeBy - priceCdf);
	}

	function getTimeStamp() private view returns (uint256) {
		return block.timestamp;
	}

	function getD1(
		uint256 price,
		uint256 strike,
		uint256 time,
		uint256 vol,
		uint256 rfr
	) private pure returns (int256 d1, uint256 d1Denominator) {
		uint256 d1Right = (vol.mul(vol).div(TWO) + rfr).mul(time);
		int256 d1Left = int256(price.div(strike)).ln();
		int256 d1Numerator = d1Left + int256(d1Right);
		d1Denominator = vol.mul(time.sqrt());
		d1 = d1Numerator.div(int256(d1Denominator));
	}

	function getIntermediates(
		uint256 price,
		uint256 strike,
		uint256 time,
		uint256 vol,
		uint256 rfr
	) private pure returns (Intermediates memory) {
		(int256 d1, uint256 d1Denominator) = getD1(price, strike, time, vol, rfr);
		return
			Intermediates({
				d1Denominator: d1Denominator,
				d1: d1,
				eToNegRT: (int256(rfr).mul(int256(time)).mul(-int256(ONE))).exp()
			});
	}

	function blackScholesCalc(
		uint256 price,
		uint256 strike,
		uint256 expiration,
		uint256 vol,
		uint256 rfr,
		bool isPut
	) public view returns (uint256) {
		uint256 time = (expiration - getTimeStamp()).div(ONE_YEAR_SECONDS);
		Intermediates memory i = getIntermediates(price, strike, time, vol, rfr);
		if (!isPut) {
			return
				callOptionPrice(
					int256(i.d1),
					int256(i.d1Denominator),
					int256(price),
					int256(strike),
					i.eToNegRT
				);
		} else {
			return
				putOptionPrice(
					int256(i.d1),
					int256(i.d1Denominator),
					int256(price),
					int256(strike),
					i.eToNegRT
				);
		}
	}

	function blackScholesCalcGreeks(
		uint256 price,
		uint256 strike,
		uint256 expiration,
		uint256 vol,
		uint256 rfr,
		bool isPut
	) public view returns (uint256 quote, int256 delta) {
		uint256 time = (expiration - getTimeStamp()).div(ONE_YEAR_SECONDS);
		Intermediates memory i = getIntermediates(price, strike, time, vol, rfr);
		if (!isPut) {
			return
				callOptionPriceGreeks(
					int256(i.d1),
					int256(i.d1Denominator),
					int256(price),
					int256(strike),
					i.eToNegRT
				);
		} else {
			return
				putOptionPriceGreeks(
					int256(i.d1),
					int256(i.d1Denominator),
					int256(price),
					int256(strike),
					i.eToNegRT
				);
		}
	}

	function getDelta(
		uint256 price,
		uint256 strike,
		uint256 expiration,
		uint256 vol,
		uint256 rfr,
		bool isPut
	) public view returns (int256) {
		uint256 time = (expiration - getTimeStamp()).div(ONE_YEAR_SECONDS);
		(int256 d1, ) = getD1(price, strike, time, vol, rfr);
		if (!isPut) {
			return NormalDist.cdf(d1);
		} else {
			return -NormalDist.cdf(-d1);
		}
	}
}

File 17 of 31 : ReentrancyGuard.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.8.9;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
contract ReentrancyGuard {
	// Booleans are more expensive than uint256 or any type that takes up a full
	// word because each write operation emits an extra SLOAD to first read the
	// slot's contents, replace the bits taken up by the boolean, and then write
	// back. This is the compiler's defense against contract upgrades and
	// pointer aliasing, and it cannot be disabled.

	// The values being non-zero value makes deployment a bit more expensive,
	// but in exchange the refund on every call to nonReentrant will be lower in
	// amount. Since refunds are capped to a percentage of the total
	// transaction's gas, it is best to keep them low in cases like this one, to
	// increase the likelihood of the full refund coming into effect.
	uint256 private constant _NOT_ENTERED = 1;
	uint256 private constant _ENTERED = 2;

	uint256 private _status;

	constructor() {
		_status = _NOT_ENTERED;
	}

	/**
	 * @dev Prevents a contract from calling itself, directly or indirectly.
	 * Calling a `nonReentrant` function from another `nonReentrant`
	 * function is not supported. It is possible to prevent this from happening
	 * by making the `nonReentrant` function external, and make it call a
	 * `private` function that does the actual work.
	 */
	modifier nonReentrant() {
		// On the first call to nonReentrant, _notEntered will be true
		require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

		// Any calls to nonReentrant after this point will fail
		_status = _ENTERED;

		_;

		// By storing the original value once again, a refund is triggered (see
		// https://eips.ethereum.org/EIPS/eip-2200)
		_status = _NOT_ENTERED;
	}
}

File 18 of 31 : IAccounting.sol
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.8.9;

/// @title Accounting contract to calculate the dhv token value and handle deposit/withdraw mechanics

interface IAccounting {
	struct DepositReceipt {
		uint128 epoch;
		uint128 amount; // collateral decimals
		uint256 unredeemedShares; // e18
	}

	struct WithdrawalReceipt {
		uint128 epoch;
		uint128 shares; // e18
	}

	/**
	 * @notice logic for adding liquidity to the options liquidity pool
	 * @param  depositor the address making the deposit
	 * @param  _amount amount of the collateral asset to deposit
	 * @return depositAmount the amount to deposit from the round
	 * @return unredeemedShares number of shares held in the deposit receipt that havent been redeemed
	 */
	function deposit(address depositor, uint256 _amount)
		external
		returns (uint256 depositAmount, uint256 unredeemedShares);

	/**
	 * @notice logic for allowing a user to redeem their shares from a previous epoch
	 * @param  redeemer the address making the deposit
	 * @param  shares amount of the collateral asset to deposit
	 * @return toRedeem the amount to actually redeem
	 * @return depositReceipt the updated deposit receipt after the redeem has completed
	 */
	function redeem(address redeemer, uint256 shares)
		external
		returns (uint256 toRedeem, DepositReceipt memory depositReceipt);

	/**
	 * @notice logic for accounting a user to initiate a withdraw request from the pool
	 * @param  withdrawer the address carrying out the withdrawal
	 * @param  shares the amount of shares to withdraw for
	 * @return withdrawalReceipt the new withdrawal receipt to pass to the liquidityPool
	 */
	function initiateWithdraw(address withdrawer, uint256 shares)
		external
		returns (WithdrawalReceipt memory withdrawalReceipt);

	/**
	 * @notice logic for accounting a user to complete a withdrawal
	 * @param  withdrawer the address carrying out the withdrawal
	 * @return withdrawalAmount  the amount of collateral to withdraw
	 * @return withdrawalShares  the number of shares to withdraw
	 * @return withdrawalReceipt the new withdrawal receipt to pass to the liquidityPool
	 */
	function completeWithdraw(address withdrawer)
		external
		returns (
			uint256 withdrawalAmount,
			uint256 withdrawalShares,
			WithdrawalReceipt memory withdrawalReceipt
		);

	/**
	 * @notice execute the next epoch
	 * @param totalSupply  the total number of share tokens
	 * @param assets the amount of collateral assets
	 * @param liabilities the amount of liabilities of the pool
	 * @return newPricePerShareDeposit the price per share for deposits
	 * @return newPricePerShareWithdrawal the price per share for withdrawals
	 * @return sharesToMint the number of shares to mint this epoch
	 * @return totalWithdrawAmount the amount of collateral to set aside for partitioning
	 * @return amountNeeded the amount needed to reach the total withdraw amount if collateral balance of lp is insufficient
	 */
	function executeEpochCalculation(
		uint256 totalSupply,
		uint256 assets,
		int256 liabilities
	)
		external
		view
		returns (
			uint256 newPricePerShareDeposit,
			uint256 newPricePerShareWithdrawal,
			uint256 sharesToMint,
			uint256 totalWithdrawAmount,
			uint256 amountNeeded
		);

	/**
	 * @notice get the number of shares for a given amount
	 * @param _amount  the amount to convert to shares - assumed in collateral decimals
	 * @param assetPerShare the amount of assets received per share
	 * @return shares the number of shares based on the amount - assumed in e18
	 */
	function sharesForAmount(uint256 _amount, uint256 assetPerShare)
		external
		view
		returns (uint256 shares);
}

File 19 of 31 : IHedgingReactor.sol
// SPDX-License-Identifier: UNLICENSED

pragma solidity >=0.8.9;

/// @title Reactors to hedge delta using means outside of the option pricing skew.

interface IHedgingReactor {
	/// @notice Execute a strategy to hedge delta exposure
	/// @param delta The exposure of the liquidity pool that the reactor needs to hedge against
	/// @return deltaChange The difference in delta exposure as a result of strategy execution
	function hedgeDelta(int256 delta) external returns (int256);

	/// @notice Returns the delta exposure of the reactor
	function getDelta() external view returns (int256 delta);

	/// @notice Returns the value of the reactor denominated in the liquidity pool asset
	/// @return value the value of the reactor in the liquidity pool asset
	function getPoolDenominatedValue() external view returns (uint256 value);

	/// @notice Withdraw a given asset from the hedging reactor to the calling liquidity pool.
	/// @param amount The amount to withdraw
	/// @return the amount actually withdrawn from the reactor denominated in the liquidity pool asset
	function withdraw(uint256 amount) external returns (uint256);

	/// @notice Handle events such as collateralisation rebalancing
	function update() external returns (uint256);
}

File 20 of 31 : VolatilityFeed.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;

import "./libraries/AccessControl.sol";
import "./libraries/CustomErrors.sol";
import "./libraries/SABR.sol";

import "prb-math/contracts/PRBMathSD59x18.sol";
import "prb-math/contracts/PRBMathUD60x18.sol";

/**
 *  @title Contract used as the Dynamic Hedging Vault for storing funds, issuing shares and processing options transactions
 *  @dev Interacts with liquidity pool to feed in volatility data.
 */
contract VolatilityFeed is AccessControl {
	using PRBMathSD59x18 for int256;
	using PRBMathUD60x18 for uint256;

	//////////////////////////
	/// settable variables ///
	//////////////////////////

	// Parameters for the sabr volatility model
	mapping(uint256 => SABRParams) public sabrParams;
	// keeper mapping
	mapping(address => bool) public keeper;
	// expiry array
	uint256[] public expiries;

	//////////////////////////
	/// constant variables ///
	//////////////////////////

	// number of seconds in a year used for calculations
	int256 private constant ONE_YEAR_SECONDS = 31557600;
	int256 private constant BIPS_SCALE = 1e12;
	int256 private constant BIPS = 1e6;

	struct SABRParams {
		int32 callAlpha; // not bigger or less than an int32 and above 0
		int32 callBeta; // greater than 0 and less than or equal to 1
		int32 callRho; // between 1 and -1
		int32 callVolvol; // not bigger or less than an int32 and above 0
		int32 putAlpha;
		int32 putBeta;
		int32 putRho;
		int32 putVolvol;
	}

	constructor(address _authority) AccessControl(IAuthority(_authority)) {}

	///////////////
	/// setters ///
	///////////////

	error AlphaError();
	error BetaError();
	error RhoError();
	error VolvolError();

	event SabrParamsSet(
		uint256 indexed _expiry,
		int32 callAlpha,
		int32 callBeta,
		int32 callRho,
		int32 callVolvol,
		int32 putAlpha,
		int32 putBeta,
		int32 putRho,
		int32 putVolvol
	);

	/**
	 * @notice set the sabr volatility params
	 * @param _sabrParams set the SABR parameters
	 * @param _expiry the expiry that the SABR parameters represent
	 * @dev   only keepers can call this function
	 */
	function setSabrParameters(SABRParams memory _sabrParams, uint256 _expiry) external {
		_isKeeper();
		if (_sabrParams.callAlpha <= 0 || _sabrParams.putAlpha <= 0) {
			revert AlphaError();
		}
		if (_sabrParams.callVolvol <= 0 || _sabrParams.putVolvol <= 0) {
			revert VolvolError();
		}
		if (
			_sabrParams.callBeta <= 0 ||
			_sabrParams.callBeta > BIPS ||
			_sabrParams.putBeta <= 0 ||
			_sabrParams.putBeta > BIPS
		) {
			revert BetaError();
		}
		if (
			_sabrParams.callRho <= -BIPS ||
			_sabrParams.callRho >= BIPS ||
			_sabrParams.putRho <= -BIPS ||
			_sabrParams.putRho >= BIPS
		) {
			revert RhoError();
		}
		// if the expiry is not already a registered expiry then add it to the expiry list
		if(sabrParams[_expiry].callAlpha == 0) {
			expiries.push(_expiry);
		}
		sabrParams[_expiry] = _sabrParams;
		emit SabrParamsSet(
			_expiry,
			_sabrParams.callAlpha,
			_sabrParams.callBeta,
			_sabrParams.callRho,
			_sabrParams.callVolvol,
			_sabrParams.putAlpha,
			_sabrParams.putBeta,
			_sabrParams.putRho,
			_sabrParams.putVolvol
		);
	}

	/// @notice update the keepers
	function setKeeper(address _keeper, bool _auth) external {
		_onlyGovernor();
		keeper[_keeper] = _auth;
	}

	///////////////////////
	/// complex getters ///
	///////////////////////

	/**
	 * @notice get the current implied volatility from the feed
	 * @param isPut Is the option a call or put?
	 * @param underlyingPrice The underlying price
	 * @param strikePrice The strike price of the option
	 * @param expiration expiration timestamp of option as a PRBMath Float
	 * @return Implied volatility adjusted for volatility surface
	 */
	function getImpliedVolatility(
		bool isPut,
		uint256 underlyingPrice,
		uint256 strikePrice,
		uint256 expiration
	) external view returns (uint256) {
		int256 time = (int256(expiration) - int256(block.timestamp)).div(ONE_YEAR_SECONDS);
		int256 vol;
		SABRParams memory sabrParams_ = sabrParams[expiration];
		if (sabrParams_.callAlpha == 0) {
			revert CustomErrors.IVNotFound();
		}
		if (!isPut) {
			vol = SABR.lognormalVol(
				int256(strikePrice),
				int256(underlyingPrice),
				time,
				sabrParams_.callAlpha * BIPS_SCALE,
				sabrParams_.callBeta * BIPS_SCALE,
				sabrParams_.callRho * BIPS_SCALE,
				sabrParams_.callVolvol * BIPS_SCALE
			);
		} else {
			vol = SABR.lognormalVol(
				int256(strikePrice),
				int256(underlyingPrice),
				time,
				sabrParams_.putAlpha * BIPS_SCALE,
				sabrParams_.putBeta * BIPS_SCALE,
				sabrParams_.putRho * BIPS_SCALE,
				sabrParams_.putVolvol * BIPS_SCALE
			);
		}
		if (vol <= 0) {
			revert CustomErrors.IVNotFound();
		}
		return uint256(vol);
	}

	/**
	 @notice get the expiry array
	 @return the expiry array
	 */
	function getExpiries() external view returns (uint256[] memory) {
		return expiries;
	}

	/// @dev keepers, managers or governors can access
	function _isKeeper() internal view {
		if (
			!keeper[msg.sender] && msg.sender != authority.governor() && msg.sender != authority.manager()
		) {
			revert CustomErrors.NotKeeper();
		}
	}
}

File 21 of 31 : IOptionRegistry.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.9;

import { Types } from "../libraries/Types.sol";

interface IOptionRegistry {
	//////////////////////////////////////////////////////
	/// access-controlled state changing functionality ///
	//////////////////////////////////////////////////////

	/**
	 * @notice Either retrieves the option token if it already exists, or deploy it
	 * @param  optionSeries option series to issue
	 * @return the address of the option
	 */
	function issue(Types.OptionSeries memory optionSeries) external returns (address);

	/**
	 * @notice Open an options contract using collateral from the liquidity pool
	 * @param  _series the address of the option token to be created
	 * @param  amount the amount of options to deploy
	 * @param  collateralAmount the collateral required for the option
	 * @dev only callable by the liquidityPool
	 * @return if the transaction succeeded
	 * @return the amount of collateral taken from the liquidityPool
	 */
	function open(
		address _series,
		uint256 amount,
		uint256 collateralAmount
	) external returns (bool, uint256);

	/**
	 * @notice Close an options contract (oToken) before it has expired
	 * @param  _series the address of the option token to be burnt
	 * @param  amount the amount of options to burn
	 * @dev only callable by the liquidityPool
	 * @return if the transaction succeeded
	 */
	function close(address _series, uint256 amount) external returns (bool, uint256);

	/////////////////////////////////////////////
	/// external state changing functionality ///
	/////////////////////////////////////////////

	/**
	 * @notice Settle an options vault
	 * @param  _series the address of the option token to be burnt
	 * @return success if the transaction succeeded
	 * @return collatReturned the amount of collateral returned from the vault
	 * @return collatLost the amount of collateral used to pay ITM options on vault settle
	 * @return amountShort number of oTokens that the vault was short
	 * @dev callable by anyone but returns funds to the liquidityPool
	 */
	function settle(address _series)
		external
		returns (
			bool success,
			uint256 collatReturned,
			uint256 collatLost,
			uint256 amountShort
		);

	///////////////////////
	/// complex getters ///
	///////////////////////

	/**
	 * @notice Send collateral funds for an option to be minted
	 * @dev series.strike should be scaled by 1e8.
	 * @param  series details of the option series
	 * @param  amount amount of options to mint
	 * @return amount transferred
	 */
	function getCollateral(Types.OptionSeries memory series, uint256 amount)
		external
		view
		returns (uint256);

	/**
	 * @notice Retrieves the option token if it exists
	 * @param  underlying is the address of the underlying asset of the option
	 * @param  strikeAsset is the address of the collateral asset of the option
	 * @param  expiration is the expiry timestamp of the option
	 * @param  isPut the type of option
	 * @param  strike is the strike price of the option - 1e18 format
	 * @param  collateral is the address of the asset to collateralize the option with
	 * @return the address of the option
	 */
	function getOtoken(
		address underlying,
		address strikeAsset,
		uint256 expiration,
		bool isPut,
		uint256 strike,
		address collateral
	) external view returns (address);

	///////////////////////////
	/// non-complex getters ///
	///////////////////////////

	function getSeriesInfo(address series) external view returns (Types.OptionSeries memory);
	function vaultIds(address series) external view returns (uint256);
	function gammaController() external view returns (address);
}

File 22 of 31 : IPortfolioValuesFeed.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.9;

import "../libraries/Types.sol";

interface IPortfolioValuesFeed {
	/////////////////////////////////////////////
	/// external state changing functionality ///
	/////////////////////////////////////////////

	/**
	 * @notice Creates a Chainlink request to update portfolio values
	 * data, then multiply by 1000000000000000000 (to remove decimal places from data).
	 *
	 * @return requestId - id of the request
	 */
	function requestPortfolioData(address _underlying, address _strike)
		external
		returns (bytes32 requestId);

	function updateStores(Types.OptionSeries memory _optionSeries, int256 _shortExposure, int256 _longExposure, address _seriesAddress) external;
	
	///////////////////////////
	/// non-complex getters ///
	///////////////////////////


	function getPortfolioValues(address underlying, address strike)
		external
		view
		returns (Types.PortfolioValues memory);
}

File 23 of 31 : Pausable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which allows children to implement an emergency stop
 * mechanism that can be triggered by an authorized account.
 *
 * This module is used through inheritance. It will make available the
 * modifiers `whenNotPaused` and `whenPaused`, which can be applied to
 * the functions of your contract. Note that they will not be pausable by
 * simply including this module, only once the modifiers are put in place.
 */
abstract contract Pausable is Context {
    /**
     * @dev Emitted when the pause is triggered by `account`.
     */
    event Paused(address account);

    /**
     * @dev Emitted when the pause is lifted by `account`.
     */
    event Unpaused(address account);

    bool private _paused;

    /**
     * @dev Initializes the contract in unpaused state.
     */
    constructor() {
        _paused = false;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is not paused.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    modifier whenNotPaused() {
        _requireNotPaused();
        _;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is paused.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    modifier whenPaused() {
        _requirePaused();
        _;
    }

    /**
     * @dev Returns true if the contract is paused, and false otherwise.
     */
    function paused() public view virtual returns (bool) {
        return _paused;
    }

    /**
     * @dev Throws if the contract is paused.
     */
    function _requireNotPaused() internal view virtual {
        require(!paused(), "Pausable: paused");
    }

    /**
     * @dev Throws if the contract is not paused.
     */
    function _requirePaused() internal view virtual {
        require(paused(), "Pausable: not paused");
    }

    /**
     * @dev Triggers stopped state.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    function _pause() internal virtual whenNotPaused {
        _paused = true;
        emit Paused(_msgSender());
    }

    /**
     * @dev Returns to normal state.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    function _unpause() internal virtual whenPaused {
        _paused = false;
        emit Unpaused(_msgSender());
    }
}

File 24 of 31 : AggregatorV3Interface.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.0;

interface AggregatorV3Interface {
	function decimals() external view returns (uint8);

	function description() external view returns (string memory);

	function version() external view returns (uint256);

	// getRoundData and latestRoundData should both raise "No data present"
	// if they do not have data to report, instead of returning unset values
	// which could be misinterpreted as actual reported values.
	function getRoundData(uint80 _roundId)
		external
		view
		returns (
			uint80 roundId,
			int256 answer,
			uint256 startedAt,
			uint256 updatedAt,
			uint80 answeredInRound
		);

	function latestRoundData()
		external
		view
		returns (
			uint80 roundId,
			int256 answer,
			uint256 startedAt,
			uint256 updatedAt,
			uint80 answeredInRound
		);
}

File 25 of 31 : IAuthority.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity >=0.8.0;

interface IAuthority {
	/* ========== EVENTS ========== */

	event GovernorPushed(address indexed from, address indexed to);
	event GuardianPushed(address indexed to);
	event ManagerPushed(address indexed from, address indexed to);

	event GovernorPulled(address indexed from, address indexed to);
	event GuardianRevoked(address indexed to);
	event ManagerPulled(address indexed from, address indexed to);

	/* ========== VIEW ========== */

	function governor() external view returns (address);

	function guardian(address _target) external view returns (bool);

	function manager() external view returns (address);
}

File 26 of 31 : PRBMathUD60x18.sol
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.4;

import "./PRBMath.sol";

/// @title PRBMathUD60x18
/// @author Paul Razvan Berg
/// @notice Smart contract library for advanced fixed-point math that works with uint256 numbers considered to have 18
/// trailing decimals. We call this number representation unsigned 60.18-decimal fixed-point, since there can be up to 60
/// digits in the integer part and up to 18 decimals in the fractional part. The numbers are bound by the minimum and the
/// maximum values permitted by the Solidity type uint256.
library PRBMathUD60x18 {
    /// @dev Half the SCALE number.
    uint256 internal constant HALF_SCALE = 5e17;

    /// @dev log2(e) as an unsigned 60.18-decimal fixed-point number.
    uint256 internal constant LOG2_E = 1_442695040888963407;

    /// @dev The maximum value an unsigned 60.18-decimal fixed-point number can have.
    uint256 internal constant MAX_UD60x18 =
        115792089237316195423570985008687907853269984665640564039457_584007913129639935;

    /// @dev The maximum whole value an unsigned 60.18-decimal fixed-point number can have.
    uint256 internal constant MAX_WHOLE_UD60x18 =
        115792089237316195423570985008687907853269984665640564039457_000000000000000000;

    /// @dev How many trailing decimals can be represented.
    uint256 internal constant SCALE = 1e18;

    /// @notice Calculates the arithmetic average of x and y, rounding down.
    /// @param x The first operand as an unsigned 60.18-decimal fixed-point number.
    /// @param y The second operand as an unsigned 60.18-decimal fixed-point number.
    /// @return result The arithmetic average as an unsigned 60.18-decimal fixed-point number.
    function avg(uint256 x, uint256 y) internal pure returns (uint256 result) {
        // The operations can never overflow.
        unchecked {
            // The last operand checks if both x and y are odd and if that is the case, we add 1 to the result. We need
            // to do this because if both numbers are odd, the 0.5 remainder gets truncated twice.
            result = (x >> 1) + (y >> 1) + (x & y & 1);
        }
    }

    /// @notice Yields the least unsigned 60.18 decimal fixed-point number greater than or equal to x.
    ///
    /// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts.
    /// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions.
    ///
    /// Requirements:
    /// - x must be less than or equal to MAX_WHOLE_UD60x18.
    ///
    /// @param x The unsigned 60.18-decimal fixed-point number to ceil.
    /// @param result The least integer greater than or equal to x, as an unsigned 60.18-decimal fixed-point number.
    function ceil(uint256 x) internal pure returns (uint256 result) {
        if (x > MAX_WHOLE_UD60x18) {
            revert PRBMathUD60x18__CeilOverflow(x);
        }
        assembly {
            // Equivalent to "x % SCALE" but faster.
            let remainder := mod(x, SCALE)

            // Equivalent to "SCALE - remainder" but faster.
            let delta := sub(SCALE, remainder)

            // Equivalent to "x + delta * (remainder > 0 ? 1 : 0)" but faster.
            result := add(x, mul(delta, gt(remainder, 0)))
        }
    }

    /// @notice Divides two unsigned 60.18-decimal fixed-point numbers, returning a new unsigned 60.18-decimal fixed-point number.
    ///
    /// @dev Uses mulDiv to enable overflow-safe multiplication and division.
    ///
    /// Requirements:
    /// - The denominator cannot be zero.
    ///
    /// @param x The numerator as an unsigned 60.18-decimal fixed-point number.
    /// @param y The denominator as an unsigned 60.18-decimal fixed-point number.
    /// @param result The quotient as an unsigned 60.18-decimal fixed-point number.
    function div(uint256 x, uint256 y) internal pure returns (uint256 result) {
        result = PRBMath.mulDiv(x, SCALE, y);
    }

    /// @notice Returns Euler's number as an unsigned 60.18-decimal fixed-point number.
    /// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant).
    function e() internal pure returns (uint256 result) {
        result = 2_718281828459045235;
    }

    /// @notice Calculates the natural exponent of x.
    ///
    /// @dev Based on the insight that e^x = 2^(x * log2(e)).
    ///
    /// Requirements:
    /// - All from "log2".
    /// - x must be less than 133.084258667509499441.
    ///
    /// @param x The exponent as an unsigned 60.18-decimal fixed-point number.
    /// @return result The result as an unsigned 60.18-decimal fixed-point number.
    function exp(uint256 x) internal pure returns (uint256 result) {
        // Without this check, the value passed to "exp2" would be greater than 192.
        if (x >= 133_084258667509499441) {
            revert PRBMathUD60x18__ExpInputTooBig(x);
        }

        // Do the fixed-point multiplication inline to save gas.
        unchecked {
            uint256 doubleScaleProduct = x * LOG2_E;
            result = exp2((doubleScaleProduct + HALF_SCALE) / SCALE);
        }
    }

    /// @notice Calculates the binary exponent of x using the binary fraction method.
    ///
    /// @dev See https://ethereum.stackexchange.com/q/79903/24693.
    ///
    /// Requirements:
    /// - x must be 192 or less.
    /// - The result must fit within MAX_UD60x18.
    ///
    /// @param x The exponent as an unsigned 60.18-decimal fixed-point number.
    /// @return result The result as an unsigned 60.18-decimal fixed-point number.
    function exp2(uint256 x) internal pure returns (uint256 result) {
        // 2^192 doesn't fit within the 192.64-bit format used internally in this function.
        if (x >= 192e18) {
            revert PRBMathUD60x18__Exp2InputTooBig(x);
        }

        unchecked {
            // Convert x to the 192.64-bit fixed-point format.
            uint256 x192x64 = (x << 64) / SCALE;

            // Pass x to the PRBMath.exp2 function, which uses the 192.64-bit fixed-point number representation.
            result = PRBMath.exp2(x192x64);
        }
    }

    /// @notice Yields the greatest unsigned 60.18 decimal fixed-point number less than or equal to x.
    /// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts.
    /// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions.
    /// @param x The unsigned 60.18-decimal fixed-point number to floor.
    /// @param result The greatest integer less than or equal to x, as an unsigned 60.18-decimal fixed-point number.
    function floor(uint256 x) internal pure returns (uint256 result) {
        assembly {
            // Equivalent to "x % SCALE" but faster.
            let remainder := mod(x, SCALE)

            // Equivalent to "x - remainder * (remainder > 0 ? 1 : 0)" but faster.
            result := sub(x, mul(remainder, gt(remainder, 0)))
        }
    }

    /// @notice Yields the excess beyond the floor of x.
    /// @dev Based on the odd function definition https://en.wikipedia.org/wiki/Fractional_part.
    /// @param x The unsigned 60.18-decimal fixed-point number to get the fractional part of.
    /// @param result The fractional part of x as an unsigned 60.18-decimal fixed-point number.
    function frac(uint256 x) internal pure returns (uint256 result) {
        assembly {
            result := mod(x, SCALE)
        }
    }

    /// @notice Converts a number from basic integer form to unsigned 60.18-decimal fixed-point representation.
    ///
    /// @dev Requirements:
    /// - x must be less than or equal to MAX_UD60x18 divided by SCALE.
    ///
    /// @param x The basic integer to convert.
    /// @param result The same number in unsigned 60.18-decimal fixed-point representation.
    function fromUint(uint256 x) internal pure returns (uint256 result) {
        unchecked {
            if (x > MAX_UD60x18 / SCALE) {
                revert PRBMathUD60x18__FromUintOverflow(x);
            }
            result = x * SCALE;
        }
    }

    /// @notice Calculates geometric mean of x and y, i.e. sqrt(x * y), rounding down.
    ///
    /// @dev Requirements:
    /// - x * y must fit within MAX_UD60x18, lest it overflows.
    ///
    /// @param x The first operand as an unsigned 60.18-decimal fixed-point number.
    /// @param y The second operand as an unsigned 60.18-decimal fixed-point number.
    /// @return result The result as an unsigned 60.18-decimal fixed-point number.
    function gm(uint256 x, uint256 y) internal pure returns (uint256 result) {
        if (x == 0) {
            return 0;
        }

        unchecked {
            // Checking for overflow this way is faster than letting Solidity do it.
            uint256 xy = x * y;
            if (xy / x != y) {
                revert PRBMathUD60x18__GmOverflow(x, y);
            }

            // We don't need to multiply by the SCALE here because the x*y product had already picked up a factor of SCALE
            // during multiplication. See the comments within the "sqrt" function.
            result = PRBMath.sqrt(xy);
        }
    }

    /// @notice Calculates 1 / x, rounding toward zero.
    ///
    /// @dev Requirements:
    /// - x cannot be zero.
    ///
    /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the inverse.
    /// @return result The inverse as an unsigned 60.18-decimal fixed-point number.
    function inv(uint256 x) internal pure returns (uint256 result) {
        unchecked {
            // 1e36 is SCALE * SCALE.
            result = 1e36 / x;
        }
    }

    /// @notice Calculates the natural logarithm of x.
    ///
    /// @dev Based on the insight that ln(x) = log2(x) / log2(e).
    ///
    /// Requirements:
    /// - All from "log2".
    ///
    /// Caveats:
    /// - All from "log2".
    /// - This doesn't return exactly 1 for 2.718281828459045235, for that we would need more fine-grained precision.
    ///
    /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the natural logarithm.
    /// @return result The natural logarithm as an unsigned 60.18-decimal fixed-point number.
    function ln(uint256 x) internal pure returns (uint256 result) {
        // Do the fixed-point multiplication inline to save gas. This is overflow-safe because the maximum value that log2(x)
        // can return is 196205294292027477728.
        unchecked {
            result = (log2(x) * SCALE) / LOG2_E;
        }
    }

    /// @notice Calculates the common logarithm of x.
    ///
    /// @dev First checks if x is an exact power of ten and it stops if yes. If it's not, calculates the common
    /// logarithm based on the insight that log10(x) = log2(x) / log2(10).
    ///
    /// Requirements:
    /// - All from "log2".
    ///
    /// Caveats:
    /// - All from "log2".
    ///
    /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the common logarithm.
    /// @return result The common logarithm as an unsigned 60.18-decimal fixed-point number.
    function log10(uint256 x) internal pure returns (uint256 result) {
        if (x < SCALE) {
            revert PRBMathUD60x18__LogInputTooSmall(x);
        }

        // Note that the "mul" in this block is the assembly multiplication operation, not the "mul" function defined
        // in this contract.
        // prettier-ignore
        assembly {
            switch x
            case 1 { result := mul(SCALE, sub(0, 18)) }
            case 10 { result := mul(SCALE, sub(1, 18)) }
            case 100 { result := mul(SCALE, sub(2, 18)) }
            case 1000 { result := mul(SCALE, sub(3, 18)) }
            case 10000 { result := mul(SCALE, sub(4, 18)) }
            case 100000 { result := mul(SCALE, sub(5, 18)) }
            case 1000000 { result := mul(SCALE, sub(6, 18)) }
            case 10000000 { result := mul(SCALE, sub(7, 18)) }
            case 100000000 { result := mul(SCALE, sub(8, 18)) }
            case 1000000000 { result := mul(SCALE, sub(9, 18)) }
            case 10000000000 { result := mul(SCALE, sub(10, 18)) }
            case 100000000000 { result := mul(SCALE, sub(11, 18)) }
            case 1000000000000 { result := mul(SCALE, sub(12, 18)) }
            case 10000000000000 { result := mul(SCALE, sub(13, 18)) }
            case 100000000000000 { result := mul(SCALE, sub(14, 18)) }
            case 1000000000000000 { result := mul(SCALE, sub(15, 18)) }
            case 10000000000000000 { result := mul(SCALE, sub(16, 18)) }
            case 100000000000000000 { result := mul(SCALE, sub(17, 18)) }
            case 1000000000000000000 { result := 0 }
            case 10000000000000000000 { result := SCALE }
            case 100000000000000000000 { result := mul(SCALE, 2) }
            case 1000000000000000000000 { result := mul(SCALE, 3) }
            case 10000000000000000000000 { result := mul(SCALE, 4) }
            case 100000000000000000000000 { result := mul(SCALE, 5) }
            case 1000000000000000000000000 { result := mul(SCALE, 6) }
            case 10000000000000000000000000 { result := mul(SCALE, 7) }
            case 100000000000000000000000000 { result := mul(SCALE, 8) }
            case 1000000000000000000000000000 { result := mul(SCALE, 9) }
            case 10000000000000000000000000000 { result := mul(SCALE, 10) }
            case 100000000000000000000000000000 { result := mul(SCALE, 11) }
            case 1000000000000000000000000000000 { result := mul(SCALE, 12) }
            case 10000000000000000000000000000000 { result := mul(SCALE, 13) }
            case 100000000000000000000000000000000 { result := mul(SCALE, 14) }
            case 1000000000000000000000000000000000 { result := mul(SCALE, 15) }
            case 10000000000000000000000000000000000 { result := mul(SCALE, 16) }
            case 100000000000000000000000000000000000 { result := mul(SCALE, 17) }
            case 1000000000000000000000000000000000000 { result := mul(SCALE, 18) }
            case 10000000000000000000000000000000000000 { result := mul(SCALE, 19) }
            case 100000000000000000000000000000000000000 { result := mul(SCALE, 20) }
            case 1000000000000000000000000000000000000000 { result := mul(SCALE, 21) }
            case 10000000000000000000000000000000000000000 { result := mul(SCALE, 22) }
            case 100000000000000000000000000000000000000000 { result := mul(SCALE, 23) }
            case 1000000000000000000000000000000000000000000 { result := mul(SCALE, 24) }
            case 10000000000000000000000000000000000000000000 { result := mul(SCALE, 25) }
            case 100000000000000000000000000000000000000000000 { result := mul(SCALE, 26) }
            case 1000000000000000000000000000000000000000000000 { result := mul(SCALE, 27) }
            case 10000000000000000000000000000000000000000000000 { result := mul(SCALE, 28) }
            case 100000000000000000000000000000000000000000000000 { result := mul(SCALE, 29) }
            case 1000000000000000000000000000000000000000000000000 { result := mul(SCALE, 30) }
            case 10000000000000000000000000000000000000000000000000 { result := mul(SCALE, 31) }
            case 100000000000000000000000000000000000000000000000000 { result := mul(SCALE, 32) }
            case 1000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 33) }
            case 10000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 34) }
            case 100000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 35) }
            case 1000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 36) }
            case 10000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 37) }
            case 100000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 38) }
            case 1000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 39) }
            case 10000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 40) }
            case 100000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 41) }
            case 1000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 42) }
            case 10000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 43) }
            case 100000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 44) }
            case 1000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 45) }
            case 10000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 46) }
            case 100000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 47) }
            case 1000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 48) }
            case 10000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 49) }
            case 100000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 50) }
            case 1000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 51) }
            case 10000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 52) }
            case 100000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 53) }
            case 1000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 54) }
            case 10000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 55) }
            case 100000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 56) }
            case 1000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 57) }
            case 10000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 58) }
            case 100000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 59) }
            default {
                result := MAX_UD60x18
            }
        }

        if (result == MAX_UD60x18) {
            // Do the fixed-point division inline to save gas. The denominator is log2(10).
            unchecked {
                result = (log2(x) * SCALE) / 3_321928094887362347;
            }
        }
    }

    /// @notice Calculates the binary logarithm of x.
    ///
    /// @dev Based on the iterative approximation algorithm.
    /// https://en.wikipedia.org/wiki/Binary_logarithm#Iterative_approximation
    ///
    /// Requirements:
    /// - x must be greater than or equal to SCALE, otherwise the result would be negative.
    ///
    /// Caveats:
    /// - The results are nor perfectly accurate to the last decimal, due to the lossy precision of the iterative approximation.
    ///
    /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the binary logarithm.
    /// @return result The binary logarithm as an unsigned 60.18-decimal fixed-point number.
    function log2(uint256 x) internal pure returns (uint256 result) {
        if (x < SCALE) {
            revert PRBMathUD60x18__LogInputTooSmall(x);
        }
        unchecked {
            // Calculate the integer part of the logarithm and add it to the result and finally calculate y = x * 2^(-n).
            uint256 n = PRBMath.mostSignificantBit(x / SCALE);

            // The integer part of the logarithm as an unsigned 60.18-decimal fixed-point number. The operation can't overflow
            // because n is maximum 255 and SCALE is 1e18.
            result = n * SCALE;

            // This is y = x * 2^(-n).
            uint256 y = x >> n;

            // If y = 1, the fractional part is zero.
            if (y == SCALE) {
                return result;
            }

            // Calculate the fractional part via the iterative approximation.
            // The "delta >>= 1" part is equivalent to "delta /= 2", but shifting bits is faster.
            for (uint256 delta = HALF_SCALE; delta > 0; delta >>= 1) {
                y = (y * y) / SCALE;

                // Is y^2 > 2 and so in the range [2,4)?
                if (y >= 2 * SCALE) {
                    // Add the 2^(-m) factor to the logarithm.
                    result += delta;

                    // Corresponds to z/2 on Wikipedia.
                    y >>= 1;
                }
            }
        }
    }

    /// @notice Multiplies two unsigned 60.18-decimal fixed-point numbers together, returning a new unsigned 60.18-decimal
    /// fixed-point number.
    /// @dev See the documentation for the "PRBMath.mulDivFixedPoint" function.
    /// @param x The multiplicand as an unsigned 60.18-decimal fixed-point number.
    /// @param y The multiplier as an unsigned 60.18-decimal fixed-point number.
    /// @return result The product as an unsigned 60.18-decimal fixed-point number.
    function mul(uint256 x, uint256 y) internal pure returns (uint256 result) {
        result = PRBMath.mulDivFixedPoint(x, y);
    }

    /// @notice Returns PI as an unsigned 60.18-decimal fixed-point number.
    function pi() internal pure returns (uint256 result) {
        result = 3_141592653589793238;
    }

    /// @notice Raises x to the power of y.
    ///
    /// @dev Based on the insight that x^y = 2^(log2(x) * y).
    ///
    /// Requirements:
    /// - All from "exp2", "log2" and "mul".
    ///
    /// Caveats:
    /// - All from "exp2", "log2" and "mul".
    /// - Assumes 0^0 is 1.
    ///
    /// @param x Number to raise to given power y, as an unsigned 60.18-decimal fixed-point number.
    /// @param y Exponent to raise x to, as an unsigned 60.18-decimal fixed-point number.
    /// @return result x raised to power y, as an unsigned 60.18-decimal fixed-point number.
    function pow(uint256 x, uint256 y) internal pure returns (uint256 result) {
        if (x == 0) {
            result = y == 0 ? SCALE : uint256(0);
        } else {
            result = exp2(mul(log2(x), y));
        }
    }

    /// @notice Raises x (unsigned 60.18-decimal fixed-point number) to the power of y (basic unsigned integer) using the
    /// famous algorithm "exponentiation by squaring".
    ///
    /// @dev See https://en.wikipedia.org/wiki/Exponentiation_by_squaring
    ///
    /// Requirements:
    /// - The result must fit within MAX_UD60x18.
    ///
    /// Caveats:
    /// - All from "mul".
    /// - Assumes 0^0 is 1.
    ///
    /// @param x The base as an unsigned 60.18-decimal fixed-point number.
    /// @param y The exponent as an uint256.
    /// @return result The result as an unsigned 60.18-decimal fixed-point number.
    function powu(uint256 x, uint256 y) internal pure returns (uint256 result) {
        // Calculate the first iteration of the loop in advance.
        result = y & 1 > 0 ? x : SCALE;

        // Equivalent to "for(y /= 2; y > 0; y /= 2)" but faster.
        for (y >>= 1; y > 0; y >>= 1) {
            x = PRBMath.mulDivFixedPoint(x, x);

            // Equivalent to "y % 2 == 1" but faster.
            if (y & 1 > 0) {
                result = PRBMath.mulDivFixedPoint(result, x);
            }
        }
    }

    /// @notice Returns 1 as an unsigned 60.18-decimal fixed-point number.
    function scale() internal pure returns (uint256 result) {
        result = SCALE;
    }

    /// @notice Calculates the square root of x, rounding down.
    /// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
    ///
    /// Requirements:
    /// - x must be less than MAX_UD60x18 / SCALE.
    ///
    /// @param x The unsigned 60.18-decimal fixed-point number for which to calculate the square root.
    /// @return result The result as an unsigned 60.18-decimal fixed-point .
    function sqrt(uint256 x) internal pure returns (uint256 result) {
        unchecked {
            if (x > MAX_UD60x18 / SCALE) {
                revert PRBMathUD60x18__SqrtOverflow(x);
            }
            // Multiply x by the SCALE to account for the factor of SCALE that is picked up when multiplying two unsigned
            // 60.18-decimal fixed-point numbers together (in this case, those two numbers are both the square root).
            result = PRBMath.sqrt(x * SCALE);
        }
    }

    /// @notice Converts a unsigned 60.18-decimal fixed-point number to basic integer form, rounding down in the process.
    /// @param x The unsigned 60.18-decimal fixed-point number to convert.
    /// @return result The same number in basic integer form.
    function toUint(uint256 x) internal pure returns (uint256 result) {
        unchecked {
            result = x / SCALE;
        }
    }
}

File 27 of 31 : PRBMathSD59x18.sol
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.4;

import "./PRBMath.sol";

/// @title PRBMathSD59x18
/// @author Paul Razvan Berg
/// @notice Smart contract library for advanced fixed-point math that works with int256 numbers considered to have 18
/// trailing decimals. We call this number representation signed 59.18-decimal fixed-point, since the numbers can have
/// a sign and there can be up to 59 digits in the integer part and up to 18 decimals in the fractional part. The numbers
/// are bound by the minimum and the maximum values permitted by the Solidity type int256.
library PRBMathSD59x18 {
    /// @dev log2(e) as a signed 59.18-decimal fixed-point number.
    int256 internal constant LOG2_E = 1_442695040888963407;

    /// @dev Half the SCALE number.
    int256 internal constant HALF_SCALE = 5e17;

    /// @dev The maximum value a signed 59.18-decimal fixed-point number can have.
    int256 internal constant MAX_SD59x18 =
        57896044618658097711785492504343953926634992332820282019728_792003956564819967;

    /// @dev The maximum whole value a signed 59.18-decimal fixed-point number can have.
    int256 internal constant MAX_WHOLE_SD59x18 =
        57896044618658097711785492504343953926634992332820282019728_000000000000000000;

    /// @dev The minimum value a signed 59.18-decimal fixed-point number can have.
    int256 internal constant MIN_SD59x18 =
        -57896044618658097711785492504343953926634992332820282019728_792003956564819968;

    /// @dev The minimum whole value a signed 59.18-decimal fixed-point number can have.
    int256 internal constant MIN_WHOLE_SD59x18 =
        -57896044618658097711785492504343953926634992332820282019728_000000000000000000;

    /// @dev How many trailing decimals can be represented.
    int256 internal constant SCALE = 1e18;

    /// INTERNAL FUNCTIONS ///

    /// @notice Calculate the absolute value of x.
    ///
    /// @dev Requirements:
    /// - x must be greater than MIN_SD59x18.
    ///
    /// @param x The number to calculate the absolute value for.
    /// @param result The absolute value of x.
    function abs(int256 x) internal pure returns (int256 result) {
        unchecked {
            if (x == MIN_SD59x18) {
                revert PRBMathSD59x18__AbsInputTooSmall();
            }
            result = x < 0 ? -x : x;
        }
    }

    /// @notice Calculates the arithmetic average of x and y, rounding down.
    /// @param x The first operand as a signed 59.18-decimal fixed-point number.
    /// @param y The second operand as a signed 59.18-decimal fixed-point number.
    /// @return result The arithmetic average as a signed 59.18-decimal fixed-point number.
    function avg(int256 x, int256 y) internal pure returns (int256 result) {
        // The operations can never overflow.
        unchecked {
            int256 sum = (x >> 1) + (y >> 1);
            if (sum < 0) {
                // If at least one of x and y is odd, we add 1 to the result. This is because shifting negative numbers to the
                // right rounds down to infinity.
                assembly {
                    result := add(sum, and(or(x, y), 1))
                }
            } else {
                // If both x and y are odd, we add 1 to the result. This is because if both numbers are odd, the 0.5
                // remainder gets truncated twice.
                result = sum + (x & y & 1);
            }
        }
    }

    /// @notice Yields the least greatest signed 59.18 decimal fixed-point number greater than or equal to x.
    ///
    /// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts.
    /// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions.
    ///
    /// Requirements:
    /// - x must be less than or equal to MAX_WHOLE_SD59x18.
    ///
    /// @param x The signed 59.18-decimal fixed-point number to ceil.
    /// @param result The least integer greater than or equal to x, as a signed 58.18-decimal fixed-point number.
    function ceil(int256 x) internal pure returns (int256 result) {
        if (x > MAX_WHOLE_SD59x18) {
            revert PRBMathSD59x18__CeilOverflow(x);
        }
        unchecked {
            int256 remainder = x % SCALE;
            if (remainder == 0) {
                result = x;
            } else {
                // Solidity uses C fmod style, which returns a modulus with the same sign as x.
                result = x - remainder;
                if (x > 0) {
                    result += SCALE;
                }
            }
        }
    }

    /// @notice Divides two signed 59.18-decimal fixed-point numbers, returning a new signed 59.18-decimal fixed-point number.
    ///
    /// @dev Variant of "mulDiv" that works with signed numbers. Works by computing the signs and the absolute values separately.
    ///
    /// Requirements:
    /// - All from "PRBMath.mulDiv".
    /// - None of the inputs can be MIN_SD59x18.
    /// - The denominator cannot be zero.
    /// - The result must fit within int256.
    ///
    /// Caveats:
    /// - All from "PRBMath.mulDiv".
    ///
    /// @param x The numerator as a signed 59.18-decimal fixed-point number.
    /// @param y The denominator as a signed 59.18-decimal fixed-point number.
    /// @param result The quotient as a signed 59.18-decimal fixed-point number.
    function div(int256 x, int256 y) internal pure returns (int256 result) {
        if (x == MIN_SD59x18 || y == MIN_SD59x18) {
            revert PRBMathSD59x18__DivInputTooSmall();
        }

        // Get hold of the absolute values of x and y.
        uint256 ax;
        uint256 ay;
        unchecked {
            ax = x < 0 ? uint256(-x) : uint256(x);
            ay = y < 0 ? uint256(-y) : uint256(y);
        }

        // Compute the absolute value of (x*SCALE)÷y. The result must fit within int256.
        uint256 rAbs = PRBMath.mulDiv(ax, uint256(SCALE), ay);
        if (rAbs > uint256(MAX_SD59x18)) {
            revert PRBMathSD59x18__DivOverflow(rAbs);
        }

        // Get the signs of x and y.
        uint256 sx;
        uint256 sy;
        assembly {
            sx := sgt(x, sub(0, 1))
            sy := sgt(y, sub(0, 1))
        }

        // XOR over sx and sy. This is basically checking whether the inputs have the same sign. If yes, the result
        // should be positive. Otherwise, it should be negative.
        result = sx ^ sy == 1 ? -int256(rAbs) : int256(rAbs);
    }

    /// @notice Returns Euler's number as a signed 59.18-decimal fixed-point number.
    /// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant).
    function e() internal pure returns (int256 result) {
        result = 2_718281828459045235;
    }

    /// @notice Calculates the natural exponent of x.
    ///
    /// @dev Based on the insight that e^x = 2^(x * log2(e)).
    ///
    /// Requirements:
    /// - All from "log2".
    /// - x must be less than 133.084258667509499441.
    ///
    /// Caveats:
    /// - All from "exp2".
    /// - For any x less than -41.446531673892822322, the result is zero.
    ///
    /// @param x The exponent as a signed 59.18-decimal fixed-point number.
    /// @return result The result as a signed 59.18-decimal fixed-point number.
    function exp(int256 x) internal pure returns (int256 result) {
        // Without this check, the value passed to "exp2" would be less than -59.794705707972522261.
        if (x < -41_446531673892822322) {
            return 0;
        }

        // Without this check, the value passed to "exp2" would be greater than 192.
        if (x >= 133_084258667509499441) {
            revert PRBMathSD59x18__ExpInputTooBig(x);
        }

        // Do the fixed-point multiplication inline to save gas.
        unchecked {
            int256 doubleScaleProduct = x * LOG2_E;
            result = exp2((doubleScaleProduct + HALF_SCALE) / SCALE);
        }
    }

    /// @notice Calculates the binary exponent of x using the binary fraction method.
    ///
    /// @dev See https://ethereum.stackexchange.com/q/79903/24693.
    ///
    /// Requirements:
    /// - x must be 192 or less.
    /// - The result must fit within MAX_SD59x18.
    ///
    /// Caveats:
    /// - For any x less than -59.794705707972522261, the result is zero.
    ///
    /// @param x The exponent as a signed 59.18-decimal fixed-point number.
    /// @return result The result as a signed 59.18-decimal fixed-point number.
    function exp2(int256 x) internal pure returns (int256 result) {
        // This works because 2^(-x) = 1/2^x.
        if (x < 0) {
            // 2^59.794705707972522262 is the maximum number whose inverse does not truncate down to zero.
            if (x < -59_794705707972522261) {
                return 0;
            }

            // Do the fixed-point inversion inline to save gas. The numerator is SCALE * SCALE.
            unchecked {
                result = 1e36 / exp2(-x);
            }
        } else {
            // 2^192 doesn't fit within the 192.64-bit format used internally in this function.
            if (x >= 192e18) {
                revert PRBMathSD59x18__Exp2InputTooBig(x);
            }

            unchecked {
                // Convert x to the 192.64-bit fixed-point format.
                uint256 x192x64 = (uint256(x) << 64) / uint256(SCALE);

                // Safe to convert the result to int256 directly because the maximum input allowed is 192.
                result = int256(PRBMath.exp2(x192x64));
            }
        }
    }

    /// @notice Yields the greatest signed 59.18 decimal fixed-point number less than or equal to x.
    ///
    /// @dev Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts.
    /// See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions.
    ///
    /// Requirements:
    /// - x must be greater than or equal to MIN_WHOLE_SD59x18.
    ///
    /// @param x The signed 59.18-decimal fixed-point number to floor.
    /// @param result The greatest integer less than or equal to x, as a signed 58.18-decimal fixed-point number.
    function floor(int256 x) internal pure returns (int256 result) {
        if (x < MIN_WHOLE_SD59x18) {
            revert PRBMathSD59x18__FloorUnderflow(x);
        }
        unchecked {
            int256 remainder = x % SCALE;
            if (remainder == 0) {
                result = x;
            } else {
                // Solidity uses C fmod style, which returns a modulus with the same sign as x.
                result = x - remainder;
                if (x < 0) {
                    result -= SCALE;
                }
            }
        }
    }

    /// @notice Yields the excess beyond the floor of x for positive numbers and the part of the number to the right
    /// of the radix point for negative numbers.
    /// @dev Based on the odd function definition. https://en.wikipedia.org/wiki/Fractional_part
    /// @param x The signed 59.18-decimal fixed-point number to get the fractional part of.
    /// @param result The fractional part of x as a signed 59.18-decimal fixed-point number.
    function frac(int256 x) internal pure returns (int256 result) {
        unchecked {
            result = x % SCALE;
        }
    }

    /// @notice Converts a number from basic integer form to signed 59.18-decimal fixed-point representation.
    ///
    /// @dev Requirements:
    /// - x must be greater than or equal to MIN_SD59x18 divided by SCALE.
    /// - x must be less than or equal to MAX_SD59x18 divided by SCALE.
    ///
    /// @param x The basic integer to convert.
    /// @param result The same number in signed 59.18-decimal fixed-point representation.
    function fromInt(int256 x) internal pure returns (int256 result) {
        unchecked {
            if (x < MIN_SD59x18 / SCALE) {
                revert PRBMathSD59x18__FromIntUnderflow(x);
            }
            if (x > MAX_SD59x18 / SCALE) {
                revert PRBMathSD59x18__FromIntOverflow(x);
            }
            result = x * SCALE;
        }
    }

    /// @notice Calculates geometric mean of x and y, i.e. sqrt(x * y), rounding down.
    ///
    /// @dev Requirements:
    /// - x * y must fit within MAX_SD59x18, lest it overflows.
    /// - x * y cannot be negative.
    ///
    /// @param x The first operand as a signed 59.18-decimal fixed-point number.
    /// @param y The second operand as a signed 59.18-decimal fixed-point number.
    /// @return result The result as a signed 59.18-decimal fixed-point number.
    function gm(int256 x, int256 y) internal pure returns (int256 result) {
        if (x == 0) {
            return 0;
        }

        unchecked {
            // Checking for overflow this way is faster than letting Solidity do it.
            int256 xy = x * y;
            if (xy / x != y) {
                revert PRBMathSD59x18__GmOverflow(x, y);
            }

            // The product cannot be negative.
            if (xy < 0) {
                revert PRBMathSD59x18__GmNegativeProduct(x, y);
            }

            // We don't need to multiply by the SCALE here because the x*y product had already picked up a factor of SCALE
            // during multiplication. See the comments within the "sqrt" function.
            result = int256(PRBMath.sqrt(uint256(xy)));
        }
    }

    /// @notice Calculates 1 / x, rounding toward zero.
    ///
    /// @dev Requirements:
    /// - x cannot be zero.
    ///
    /// @param x The signed 59.18-decimal fixed-point number for which to calculate the inverse.
    /// @return result The inverse as a signed 59.18-decimal fixed-point number.
    function inv(int256 x) internal pure returns (int256 result) {
        unchecked {
            // 1e36 is SCALE * SCALE.
            result = 1e36 / x;
        }
    }

    /// @notice Calculates the natural logarithm of x.
    ///
    /// @dev Based on the insight that ln(x) = log2(x) / log2(e).
    ///
    /// Requirements:
    /// - All from "log2".
    ///
    /// Caveats:
    /// - All from "log2".
    /// - This doesn't return exactly 1 for 2718281828459045235, for that we would need more fine-grained precision.
    ///
    /// @param x The signed 59.18-decimal fixed-point number for which to calculate the natural logarithm.
    /// @return result The natural logarithm as a signed 59.18-decimal fixed-point number.
    function ln(int256 x) internal pure returns (int256 result) {
        // Do the fixed-point multiplication inline to save gas. This is overflow-safe because the maximum value that log2(x)
        // can return is 195205294292027477728.
        unchecked {
            result = (log2(x) * SCALE) / LOG2_E;
        }
    }

    /// @notice Calculates the common logarithm of x.
    ///
    /// @dev First checks if x is an exact power of ten and it stops if yes. If it's not, calculates the common
    /// logarithm based on the insight that log10(x) = log2(x) / log2(10).
    ///
    /// Requirements:
    /// - All from "log2".
    ///
    /// Caveats:
    /// - All from "log2".
    ///
    /// @param x The signed 59.18-decimal fixed-point number for which to calculate the common logarithm.
    /// @return result The common logarithm as a signed 59.18-decimal fixed-point number.
    function log10(int256 x) internal pure returns (int256 result) {
        if (x <= 0) {
            revert PRBMathSD59x18__LogInputTooSmall(x);
        }

        // Note that the "mul" in this block is the assembly mul operation, not the "mul" function defined in this contract.
        // prettier-ignore
        assembly {
            switch x
            case 1 { result := mul(SCALE, sub(0, 18)) }
            case 10 { result := mul(SCALE, sub(1, 18)) }
            case 100 { result := mul(SCALE, sub(2, 18)) }
            case 1000 { result := mul(SCALE, sub(3, 18)) }
            case 10000 { result := mul(SCALE, sub(4, 18)) }
            case 100000 { result := mul(SCALE, sub(5, 18)) }
            case 1000000 { result := mul(SCALE, sub(6, 18)) }
            case 10000000 { result := mul(SCALE, sub(7, 18)) }
            case 100000000 { result := mul(SCALE, sub(8, 18)) }
            case 1000000000 { result := mul(SCALE, sub(9, 18)) }
            case 10000000000 { result := mul(SCALE, sub(10, 18)) }
            case 100000000000 { result := mul(SCALE, sub(11, 18)) }
            case 1000000000000 { result := mul(SCALE, sub(12, 18)) }
            case 10000000000000 { result := mul(SCALE, sub(13, 18)) }
            case 100000000000000 { result := mul(SCALE, sub(14, 18)) }
            case 1000000000000000 { result := mul(SCALE, sub(15, 18)) }
            case 10000000000000000 { result := mul(SCALE, sub(16, 18)) }
            case 100000000000000000 { result := mul(SCALE, sub(17, 18)) }
            case 1000000000000000000 { result := 0 }
            case 10000000000000000000 { result := SCALE }
            case 100000000000000000000 { result := mul(SCALE, 2) }
            case 1000000000000000000000 { result := mul(SCALE, 3) }
            case 10000000000000000000000 { result := mul(SCALE, 4) }
            case 100000000000000000000000 { result := mul(SCALE, 5) }
            case 1000000000000000000000000 { result := mul(SCALE, 6) }
            case 10000000000000000000000000 { result := mul(SCALE, 7) }
            case 100000000000000000000000000 { result := mul(SCALE, 8) }
            case 1000000000000000000000000000 { result := mul(SCALE, 9) }
            case 10000000000000000000000000000 { result := mul(SCALE, 10) }
            case 100000000000000000000000000000 { result := mul(SCALE, 11) }
            case 1000000000000000000000000000000 { result := mul(SCALE, 12) }
            case 10000000000000000000000000000000 { result := mul(SCALE, 13) }
            case 100000000000000000000000000000000 { result := mul(SCALE, 14) }
            case 1000000000000000000000000000000000 { result := mul(SCALE, 15) }
            case 10000000000000000000000000000000000 { result := mul(SCALE, 16) }
            case 100000000000000000000000000000000000 { result := mul(SCALE, 17) }
            case 1000000000000000000000000000000000000 { result := mul(SCALE, 18) }
            case 10000000000000000000000000000000000000 { result := mul(SCALE, 19) }
            case 100000000000000000000000000000000000000 { result := mul(SCALE, 20) }
            case 1000000000000000000000000000000000000000 { result := mul(SCALE, 21) }
            case 10000000000000000000000000000000000000000 { result := mul(SCALE, 22) }
            case 100000000000000000000000000000000000000000 { result := mul(SCALE, 23) }
            case 1000000000000000000000000000000000000000000 { result := mul(SCALE, 24) }
            case 10000000000000000000000000000000000000000000 { result := mul(SCALE, 25) }
            case 100000000000000000000000000000000000000000000 { result := mul(SCALE, 26) }
            case 1000000000000000000000000000000000000000000000 { result := mul(SCALE, 27) }
            case 10000000000000000000000000000000000000000000000 { result := mul(SCALE, 28) }
            case 100000000000000000000000000000000000000000000000 { result := mul(SCALE, 29) }
            case 1000000000000000000000000000000000000000000000000 { result := mul(SCALE, 30) }
            case 10000000000000000000000000000000000000000000000000 { result := mul(SCALE, 31) }
            case 100000000000000000000000000000000000000000000000000 { result := mul(SCALE, 32) }
            case 1000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 33) }
            case 10000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 34) }
            case 100000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 35) }
            case 1000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 36) }
            case 10000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 37) }
            case 100000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 38) }
            case 1000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 39) }
            case 10000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 40) }
            case 100000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 41) }
            case 1000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 42) }
            case 10000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 43) }
            case 100000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 44) }
            case 1000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 45) }
            case 10000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 46) }
            case 100000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 47) }
            case 1000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 48) }
            case 10000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 49) }
            case 100000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 50) }
            case 1000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 51) }
            case 10000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 52) }
            case 100000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 53) }
            case 1000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 54) }
            case 10000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 55) }
            case 100000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 56) }
            case 1000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 57) }
            case 10000000000000000000000000000000000000000000000000000000000000000000000000000 { result := mul(SCALE, 58) }
            default {
                result := MAX_SD59x18
            }
        }

        if (result == MAX_SD59x18) {
            // Do the fixed-point division inline to save gas. The denominator is log2(10).
            unchecked {
                result = (log2(x) * SCALE) / 3_321928094887362347;
            }
        }
    }

    /// @notice Calculates the binary logarithm of x.
    ///
    /// @dev Based on the iterative approximation algorithm.
    /// https://en.wikipedia.org/wiki/Binary_logarithm#Iterative_approximation
    ///
    /// Requirements:
    /// - x must be greater than zero.
    ///
    /// Caveats:
    /// - The results are not perfectly accurate to the last decimal, due to the lossy precision of the iterative approximation.
    ///
    /// @param x The signed 59.18-decimal fixed-point number for which to calculate the binary logarithm.
    /// @return result The binary logarithm as a signed 59.18-decimal fixed-point number.
    function log2(int256 x) internal pure returns (int256 result) {
        if (x <= 0) {
            revert PRBMathSD59x18__LogInputTooSmall(x);
        }
        unchecked {
            // This works because log2(x) = -log2(1/x).
            int256 sign;
            if (x >= SCALE) {
                sign = 1;
            } else {
                sign = -1;
                // Do the fixed-point inversion inline to save gas. The numerator is SCALE * SCALE.
                assembly {
                    x := div(1000000000000000000000000000000000000, x)
                }
            }

            // Calculate the integer part of the logarithm and add it to the result and finally calculate y = x * 2^(-n).
            uint256 n = PRBMath.mostSignificantBit(uint256(x / SCALE));

            // The integer part of the logarithm as a signed 59.18-decimal fixed-point number. The operation can't overflow
            // because n is maximum 255, SCALE is 1e18 and sign is either 1 or -1.
            result = int256(n) * SCALE;

            // This is y = x * 2^(-n).
            int256 y = x >> n;

            // If y = 1, the fractional part is zero.
            if (y == SCALE) {
                return result * sign;
            }

            // Calculate the fractional part via the iterative approximation.
            // The "delta >>= 1" part is equivalent to "delta /= 2", but shifting bits is faster.
            for (int256 delta = int256(HALF_SCALE); delta > 0; delta >>= 1) {
                y = (y * y) / SCALE;

                // Is y^2 > 2 and so in the range [2,4)?
                if (y >= 2 * SCALE) {
                    // Add the 2^(-m) factor to the logarithm.
                    result += delta;

                    // Corresponds to z/2 on Wikipedia.
                    y >>= 1;
                }
            }
            result *= sign;
        }
    }

    /// @notice Multiplies two signed 59.18-decimal fixed-point numbers together, returning a new signed 59.18-decimal
    /// fixed-point number.
    ///
    /// @dev Variant of "mulDiv" that works with signed numbers and employs constant folding, i.e. the denominator is
    /// always 1e18.
    ///
    /// Requirements:
    /// - All from "PRBMath.mulDivFixedPoint".
    /// - None of the inputs can be MIN_SD59x18
    /// - The result must fit within MAX_SD59x18.
    ///
    /// Caveats:
    /// - The body is purposely left uncommented; see the NatSpec comments in "PRBMath.mulDiv" to understand how this works.
    ///
    /// @param x The multiplicand as a signed 59.18-decimal fixed-point number.
    /// @param y The multiplier as a signed 59.18-decimal fixed-point number.
    /// @return result The product as a signed 59.18-decimal fixed-point number.
    function mul(int256 x, int256 y) internal pure returns (int256 result) {
        if (x == MIN_SD59x18 || y == MIN_SD59x18) {
            revert PRBMathSD59x18__MulInputTooSmall();
        }

        unchecked {
            uint256 ax;
            uint256 ay;
            ax = x < 0 ? uint256(-x) : uint256(x);
            ay = y < 0 ? uint256(-y) : uint256(y);

            uint256 rAbs = PRBMath.mulDivFixedPoint(ax, ay);
            if (rAbs > uint256(MAX_SD59x18)) {
                revert PRBMathSD59x18__MulOverflow(rAbs);
            }

            uint256 sx;
            uint256 sy;
            assembly {
                sx := sgt(x, sub(0, 1))
                sy := sgt(y, sub(0, 1))
            }
            result = sx ^ sy == 1 ? -int256(rAbs) : int256(rAbs);
        }
    }

    /// @notice Returns PI as a signed 59.18-decimal fixed-point number.
    function pi() internal pure returns (int256 result) {
        result = 3_141592653589793238;
    }

    /// @notice Raises x to the power of y.
    ///
    /// @dev Based on the insight that x^y = 2^(log2(x) * y).
    ///
    /// Requirements:
    /// - All from "exp2", "log2" and "mul".
    /// - z cannot be zero.
    ///
    /// Caveats:
    /// - All from "exp2", "log2" and "mul".
    /// - Assumes 0^0 is 1.
    ///
    /// @param x Number to raise to given power y, as a signed 59.18-decimal fixed-point number.
    /// @param y Exponent to raise x to, as a signed 59.18-decimal fixed-point number.
    /// @return result x raised to power y, as a signed 59.18-decimal fixed-point number.
    function pow(int256 x, int256 y) internal pure returns (int256 result) {
        if (x == 0) {
            result = y == 0 ? SCALE : int256(0);
        } else {
            result = exp2(mul(log2(x), y));
        }
    }

    /// @notice Raises x (signed 59.18-decimal fixed-point number) to the power of y (basic unsigned integer) using the
    /// famous algorithm "exponentiation by squaring".
    ///
    /// @dev See https://en.wikipedia.org/wiki/Exponentiation_by_squaring
    ///
    /// Requirements:
    /// - All from "abs" and "PRBMath.mulDivFixedPoint".
    /// - The result must fit within MAX_SD59x18.
    ///
    /// Caveats:
    /// - All from "PRBMath.mulDivFixedPoint".
    /// - Assumes 0^0 is 1.
    ///
    /// @param x The base as a signed 59.18-decimal fixed-point number.
    /// @param y The exponent as an uint256.
    /// @return result The result as a signed 59.18-decimal fixed-point number.
    function powu(int256 x, uint256 y) internal pure returns (int256 result) {
        uint256 xAbs = uint256(abs(x));

        // Calculate the first iteration of the loop in advance.
        uint256 rAbs = y & 1 > 0 ? xAbs : uint256(SCALE);

        // Equivalent to "for(y /= 2; y > 0; y /= 2)" but faster.
        uint256 yAux = y;
        for (yAux >>= 1; yAux > 0; yAux >>= 1) {
            xAbs = PRBMath.mulDivFixedPoint(xAbs, xAbs);

            // Equivalent to "y % 2 == 1" but faster.
            if (yAux & 1 > 0) {
                rAbs = PRBMath.mulDivFixedPoint(rAbs, xAbs);
            }
        }

        // The result must fit within the 59.18-decimal fixed-point representation.
        if (rAbs > uint256(MAX_SD59x18)) {
            revert PRBMathSD59x18__PowuOverflow(rAbs);
        }

        // Is the base negative and the exponent an odd number?
        bool isNegative = x < 0 && y & 1 == 1;
        result = isNegative ? -int256(rAbs) : int256(rAbs);
    }

    /// @notice Returns 1 as a signed 59.18-decimal fixed-point number.
    function scale() internal pure returns (int256 result) {
        result = SCALE;
    }

    /// @notice Calculates the square root of x, rounding down.
    /// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
    ///
    /// Requirements:
    /// - x cannot be negative.
    /// - x must be less than MAX_SD59x18 / SCALE.
    ///
    /// @param x The signed 59.18-decimal fixed-point number for which to calculate the square root.
    /// @return result The result as a signed 59.18-decimal fixed-point .
    function sqrt(int256 x) internal pure returns (int256 result) {
        unchecked {
            if (x < 0) {
                revert PRBMathSD59x18__SqrtNegativeInput(x);
            }
            if (x > MAX_SD59x18 / SCALE) {
                revert PRBMathSD59x18__SqrtOverflow(x);
            }
            // Multiply x by the SCALE to account for the factor of SCALE that is picked up when multiplying two signed
            // 59.18-decimal fixed-point numbers together (in this case, those two numbers are both the square root).
            result = int256(PRBMath.sqrt(uint256(x * SCALE)));
        }
    }

    /// @notice Converts a signed 59.18-decimal fixed-point number to basic integer form, rounding down in the process.
    /// @param x The signed 59.18-decimal fixed-point number to convert.
    /// @return result The same number in basic integer form.
    function toInt(int256 x) internal pure returns (int256 result) {
        unchecked {
            result = x / SCALE;
        }
    }
}

File 28 of 31 : NormalDist.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import "prb-math/contracts/PRBMathSD59x18.sol";

/**
 *  @title Library used for approximating a normal distribution
 */
library NormalDist {
	using PRBMathSD59x18 for int256;

	int256 private constant ONE = 1000000000000000000;
	int256 private constant ONE_HALF = 500000000000000000;
	int256 private constant SQRT_TWO = 1414213562373095048;
	// z-scores
	// A1 0.254829592
	int256 private constant A1 = 254829592000000000;
	// A2 -0.284496736
	int256 private constant A2 = -284496736000000000;
	// A3 1.421413741
	int256 private constant A3 = 1421413741000000000;
	// A4 -1.453152027
	int256 private constant A4 = -1453152027000000000;
	// A5 1.061405429
	int256 private constant A5 = 1061405429000000000;
	// P 0.3275911
	int256 private constant P = 327591100000000000;

	function cdf(int256 x) public pure returns (int256) {
		int256 phiParam = x.div(SQRT_TWO);
		int256 onePlusPhi = ONE + (phi(phiParam));
		return ONE_HALF.mul(onePlusPhi);
	}

	function phi(int256 x) public pure returns (int256) {
		int256 sign = x >= 0 ? ONE : -ONE;
		int256 abs = x.abs();

		// A&S formula 7.1.26
		int256 t = ONE.div(ONE + (P.mul(abs)));
		int256 scoresByT = getScoresFromT(t);
		int256 eToXs = abs.mul(-ONE).mul(abs).exp();
		int256 y = ONE - (scoresByT.mul(eToXs));
		return sign.mul(y);
	}

	function getScoresFromT(int256 t) public pure returns (int256) {
		int256 byA5T = A5.mul(t);
		int256 byA4T = (byA5T + A4).mul(t);
		int256 byA3T = (byA4T + A3).mul(t);
		int256 byA2T = (byA3T + A2).mul(t);
		int256 byA1T = (byA2T + A1).mul(t);
		return byA1T;
	}
}

File 29 of 31 : PRBMath.sol
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.4;

/// @notice Emitted when the result overflows uint256.
error PRBMath__MulDivFixedPointOverflow(uint256 prod1);

/// @notice Emitted when the result overflows uint256.
error PRBMath__MulDivOverflow(uint256 prod1, uint256 denominator);

/// @notice Emitted when one of the inputs is type(int256).min.
error PRBMath__MulDivSignedInputTooSmall();

/// @notice Emitted when the intermediary absolute result overflows int256.
error PRBMath__MulDivSignedOverflow(uint256 rAbs);

/// @notice Emitted when the input is MIN_SD59x18.
error PRBMathSD59x18__AbsInputTooSmall();

/// @notice Emitted when ceiling a number overflows SD59x18.
error PRBMathSD59x18__CeilOverflow(int256 x);

/// @notice Emitted when one of the inputs is MIN_SD59x18.
error PRBMathSD59x18__DivInputTooSmall();

/// @notice Emitted when one of the intermediary unsigned results overflows SD59x18.
error PRBMathSD59x18__DivOverflow(uint256 rAbs);

/// @notice Emitted when the input is greater than 133.084258667509499441.
error PRBMathSD59x18__ExpInputTooBig(int256 x);

/// @notice Emitted when the input is greater than 192.
error PRBMathSD59x18__Exp2InputTooBig(int256 x);

/// @notice Emitted when flooring a number underflows SD59x18.
error PRBMathSD59x18__FloorUnderflow(int256 x);

/// @notice Emitted when converting a basic integer to the fixed-point format overflows SD59x18.
error PRBMathSD59x18__FromIntOverflow(int256 x);

/// @notice Emitted when converting a basic integer to the fixed-point format underflows SD59x18.
error PRBMathSD59x18__FromIntUnderflow(int256 x);

/// @notice Emitted when the product of the inputs is negative.
error PRBMathSD59x18__GmNegativeProduct(int256 x, int256 y);

/// @notice Emitted when multiplying the inputs overflows SD59x18.
error PRBMathSD59x18__GmOverflow(int256 x, int256 y);

/// @notice Emitted when the input is less than or equal to zero.
error PRBMathSD59x18__LogInputTooSmall(int256 x);

/// @notice Emitted when one of the inputs is MIN_SD59x18.
error PRBMathSD59x18__MulInputTooSmall();

/// @notice Emitted when the intermediary absolute result overflows SD59x18.
error PRBMathSD59x18__MulOverflow(uint256 rAbs);

/// @notice Emitted when the intermediary absolute result overflows SD59x18.
error PRBMathSD59x18__PowuOverflow(uint256 rAbs);

/// @notice Emitted when the input is negative.
error PRBMathSD59x18__SqrtNegativeInput(int256 x);

/// @notice Emitted when the calculating the square root overflows SD59x18.
error PRBMathSD59x18__SqrtOverflow(int256 x);

/// @notice Emitted when addition overflows UD60x18.
error PRBMathUD60x18__AddOverflow(uint256 x, uint256 y);

/// @notice Emitted when ceiling a number overflows UD60x18.
error PRBMathUD60x18__CeilOverflow(uint256 x);

/// @notice Emitted when the input is greater than 133.084258667509499441.
error PRBMathUD60x18__ExpInputTooBig(uint256 x);

/// @notice Emitted when the input is greater than 192.
error PRBMathUD60x18__Exp2InputTooBig(uint256 x);

/// @notice Emitted when converting a basic integer to the fixed-point format format overflows UD60x18.
error PRBMathUD60x18__FromUintOverflow(uint256 x);

/// @notice Emitted when multiplying the inputs overflows UD60x18.
error PRBMathUD60x18__GmOverflow(uint256 x, uint256 y);

/// @notice Emitted when the input is less than 1.
error PRBMathUD60x18__LogInputTooSmall(uint256 x);

/// @notice Emitted when the calculating the square root overflows UD60x18.
error PRBMathUD60x18__SqrtOverflow(uint256 x);

/// @notice Emitted when subtraction underflows UD60x18.
error PRBMathUD60x18__SubUnderflow(uint256 x, uint256 y);

/// @dev Common mathematical functions used in both PRBMathSD59x18 and PRBMathUD60x18. Note that this shared library
/// does not always assume the signed 59.18-decimal fixed-point or the unsigned 60.18-decimal fixed-point
/// representation. When it does not, it is explicitly mentioned in the NatSpec documentation.
library PRBMath {
    /// STRUCTS ///

    struct SD59x18 {
        int256 value;
    }

    struct UD60x18 {
        uint256 value;
    }

    /// STORAGE ///

    /// @dev How many trailing decimals can be represented.
    uint256 internal constant SCALE = 1e18;

    /// @dev Largest power of two divisor of SCALE.
    uint256 internal constant SCALE_LPOTD = 262144;

    /// @dev SCALE inverted mod 2^256.
    uint256 internal constant SCALE_INVERSE =
        78156646155174841979727994598816262306175212592076161876661_508869554232690281;

    /// FUNCTIONS ///

    /// @notice Calculates the binary exponent of x using the binary fraction method.
    /// @dev Has to use 192.64-bit fixed-point numbers.
    /// See https://ethereum.stackexchange.com/a/96594/24693.
    /// @param x The exponent as an unsigned 192.64-bit fixed-point number.
    /// @return result The result as an unsigned 60.18-decimal fixed-point number.
    function exp2(uint256 x) internal pure returns (uint256 result) {
        unchecked {
            // Start from 0.5 in the 192.64-bit fixed-point format.
            result = 0x800000000000000000000000000000000000000000000000;

            // Multiply the result by root(2, 2^-i) when the bit at position i is 1. None of the intermediary results overflows
            // because the initial result is 2^191 and all magic factors are less than 2^65.
            if (x & 0x8000000000000000 > 0) {
                result = (result * 0x16A09E667F3BCC909) >> 64;
            }
            if (x & 0x4000000000000000 > 0) {
                result = (result * 0x1306FE0A31B7152DF) >> 64;
            }
            if (x & 0x2000000000000000 > 0) {
                result = (result * 0x1172B83C7D517ADCE) >> 64;
            }
            if (x & 0x1000000000000000 > 0) {
                result = (result * 0x10B5586CF9890F62A) >> 64;
            }
            if (x & 0x800000000000000 > 0) {
                result = (result * 0x1059B0D31585743AE) >> 64;
            }
            if (x & 0x400000000000000 > 0) {
                result = (result * 0x102C9A3E778060EE7) >> 64;
            }
            if (x & 0x200000000000000 > 0) {
                result = (result * 0x10163DA9FB33356D8) >> 64;
            }
            if (x & 0x100000000000000 > 0) {
                result = (result * 0x100B1AFA5ABCBED61) >> 64;
            }
            if (x & 0x80000000000000 > 0) {
                result = (result * 0x10058C86DA1C09EA2) >> 64;
            }
            if (x & 0x40000000000000 > 0) {
                result = (result * 0x1002C605E2E8CEC50) >> 64;
            }
            if (x & 0x20000000000000 > 0) {
                result = (result * 0x100162F3904051FA1) >> 64;
            }
            if (x & 0x10000000000000 > 0) {
                result = (result * 0x1000B175EFFDC76BA) >> 64;
            }
            if (x & 0x8000000000000 > 0) {
                result = (result * 0x100058BA01FB9F96D) >> 64;
            }
            if (x & 0x4000000000000 > 0) {
                result = (result * 0x10002C5CC37DA9492) >> 64;
            }
            if (x & 0x2000000000000 > 0) {
                result = (result * 0x1000162E525EE0547) >> 64;
            }
            if (x & 0x1000000000000 > 0) {
                result = (result * 0x10000B17255775C04) >> 64;
            }
            if (x & 0x800000000000 > 0) {
                result = (result * 0x1000058B91B5BC9AE) >> 64;
            }
            if (x & 0x400000000000 > 0) {
                result = (result * 0x100002C5C89D5EC6D) >> 64;
            }
            if (x & 0x200000000000 > 0) {
                result = (result * 0x10000162E43F4F831) >> 64;
            }
            if (x & 0x100000000000 > 0) {
                result = (result * 0x100000B1721BCFC9A) >> 64;
            }
            if (x & 0x80000000000 > 0) {
                result = (result * 0x10000058B90CF1E6E) >> 64;
            }
            if (x & 0x40000000000 > 0) {
                result = (result * 0x1000002C5C863B73F) >> 64;
            }
            if (x & 0x20000000000 > 0) {
                result = (result * 0x100000162E430E5A2) >> 64;
            }
            if (x & 0x10000000000 > 0) {
                result = (result * 0x1000000B172183551) >> 64;
            }
            if (x & 0x8000000000 > 0) {
                result = (result * 0x100000058B90C0B49) >> 64;
            }
            if (x & 0x4000000000 > 0) {
                result = (result * 0x10000002C5C8601CC) >> 64;
            }
            if (x & 0x2000000000 > 0) {
                result = (result * 0x1000000162E42FFF0) >> 64;
            }
            if (x & 0x1000000000 > 0) {
                result = (result * 0x10000000B17217FBB) >> 64;
            }
            if (x & 0x800000000 > 0) {
                result = (result * 0x1000000058B90BFCE) >> 64;
            }
            if (x & 0x400000000 > 0) {
                result = (result * 0x100000002C5C85FE3) >> 64;
            }
            if (x & 0x200000000 > 0) {
                result = (result * 0x10000000162E42FF1) >> 64;
            }
            if (x & 0x100000000 > 0) {
                result = (result * 0x100000000B17217F8) >> 64;
            }
            if (x & 0x80000000 > 0) {
                result = (result * 0x10000000058B90BFC) >> 64;
            }
            if (x & 0x40000000 > 0) {
                result = (result * 0x1000000002C5C85FE) >> 64;
            }
            if (x & 0x20000000 > 0) {
                result = (result * 0x100000000162E42FF) >> 64;
            }
            if (x & 0x10000000 > 0) {
                result = (result * 0x1000000000B17217F) >> 64;
            }
            if (x & 0x8000000 > 0) {
                result = (result * 0x100000000058B90C0) >> 64;
            }
            if (x & 0x4000000 > 0) {
                result = (result * 0x10000000002C5C860) >> 64;
            }
            if (x & 0x2000000 > 0) {
                result = (result * 0x1000000000162E430) >> 64;
            }
            if (x & 0x1000000 > 0) {
                result = (result * 0x10000000000B17218) >> 64;
            }
            if (x & 0x800000 > 0) {
                result = (result * 0x1000000000058B90C) >> 64;
            }
            if (x & 0x400000 > 0) {
                result = (result * 0x100000000002C5C86) >> 64;
            }
            if (x & 0x200000 > 0) {
                result = (result * 0x10000000000162E43) >> 64;
            }
            if (x & 0x100000 > 0) {
                result = (result * 0x100000000000B1721) >> 64;
            }
            if (x & 0x80000 > 0) {
                result = (result * 0x10000000000058B91) >> 64;
            }
            if (x & 0x40000 > 0) {
                result = (result * 0x1000000000002C5C8) >> 64;
            }
            if (x & 0x20000 > 0) {
                result = (result * 0x100000000000162E4) >> 64;
            }
            if (x & 0x10000 > 0) {
                result = (result * 0x1000000000000B172) >> 64;
            }
            if (x & 0x8000 > 0) {
                result = (result * 0x100000000000058B9) >> 64;
            }
            if (x & 0x4000 > 0) {
                result = (result * 0x10000000000002C5D) >> 64;
            }
            if (x & 0x2000 > 0) {
                result = (result * 0x1000000000000162E) >> 64;
            }
            if (x & 0x1000 > 0) {
                result = (result * 0x10000000000000B17) >> 64;
            }
            if (x & 0x800 > 0) {
                result = (result * 0x1000000000000058C) >> 64;
            }
            if (x & 0x400 > 0) {
                result = (result * 0x100000000000002C6) >> 64;
            }
            if (x & 0x200 > 0) {
                result = (result * 0x10000000000000163) >> 64;
            }
            if (x & 0x100 > 0) {
                result = (result * 0x100000000000000B1) >> 64;
            }
            if (x & 0x80 > 0) {
                result = (result * 0x10000000000000059) >> 64;
            }
            if (x & 0x40 > 0) {
                result = (result * 0x1000000000000002C) >> 64;
            }
            if (x & 0x20 > 0) {
                result = (result * 0x10000000000000016) >> 64;
            }
            if (x & 0x10 > 0) {
                result = (result * 0x1000000000000000B) >> 64;
            }
            if (x & 0x8 > 0) {
                result = (result * 0x10000000000000006) >> 64;
            }
            if (x & 0x4 > 0) {
                result = (result * 0x10000000000000003) >> 64;
            }
            if (x & 0x2 > 0) {
                result = (result * 0x10000000000000001) >> 64;
            }
            if (x & 0x1 > 0) {
                result = (result * 0x10000000000000001) >> 64;
            }

            // We're doing two things at the same time:
            //
            //   1. Multiply the result by 2^n + 1, where "2^n" is the integer part and the one is added to account for
            //      the fact that we initially set the result to 0.5. This is accomplished by subtracting from 191
            //      rather than 192.
            //   2. Convert the result to the unsigned 60.18-decimal fixed-point format.
            //
            // This works because 2^(191-ip) = 2^ip / 2^191, where "ip" is the integer part "2^n".
            result *= SCALE;
            result >>= (191 - (x >> 64));
        }
    }

    /// @notice Finds the zero-based index of the first one in the binary representation of x.
    /// @dev See the note on msb in the "Find First Set" Wikipedia article https://en.wikipedia.org/wiki/Find_first_set
    /// @param x The uint256 number for which to find the index of the most significant bit.
    /// @return msb The index of the most significant bit as an uint256.
    function mostSignificantBit(uint256 x) internal pure returns (uint256 msb) {
        if (x >= 2**128) {
            x >>= 128;
            msb += 128;
        }
        if (x >= 2**64) {
            x >>= 64;
            msb += 64;
        }
        if (x >= 2**32) {
            x >>= 32;
            msb += 32;
        }
        if (x >= 2**16) {
            x >>= 16;
            msb += 16;
        }
        if (x >= 2**8) {
            x >>= 8;
            msb += 8;
        }
        if (x >= 2**4) {
            x >>= 4;
            msb += 4;
        }
        if (x >= 2**2) {
            x >>= 2;
            msb += 2;
        }
        if (x >= 2**1) {
            // No need to shift x any more.
            msb += 1;
        }
    }

    /// @notice Calculates floor(x*y÷denominator) with full precision.
    ///
    /// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv.
    ///
    /// Requirements:
    /// - The denominator cannot be zero.
    /// - The result must fit within uint256.
    ///
    /// Caveats:
    /// - This function does not work with fixed-point numbers.
    ///
    /// @param x The multiplicand as an uint256.
    /// @param y The multiplier as an uint256.
    /// @param denominator The divisor as an uint256.
    /// @return result The result as an uint256.
    function mulDiv(
        uint256 x,
        uint256 y,
        uint256 denominator
    ) internal pure returns (uint256 result) {
        // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
        // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
        // variables such that product = prod1 * 2^256 + prod0.
        uint256 prod0; // Least significant 256 bits of the product
        uint256 prod1; // Most significant 256 bits of the product
        assembly {
            let mm := mulmod(x, y, not(0))
            prod0 := mul(x, y)
            prod1 := sub(sub(mm, prod0), lt(mm, prod0))
        }

        // Handle non-overflow cases, 256 by 256 division.
        if (prod1 == 0) {
            unchecked {
                result = prod0 / denominator;
            }
            return result;
        }

        // Make sure the result is less than 2^256. Also prevents denominator == 0.
        if (prod1 >= denominator) {
            revert PRBMath__MulDivOverflow(prod1, denominator);
        }

        ///////////////////////////////////////////////
        // 512 by 256 division.
        ///////////////////////////////////////////////

        // Make division exact by subtracting the remainder from [prod1 prod0].
        uint256 remainder;
        assembly {
            // Compute remainder using mulmod.
            remainder := mulmod(x, y, denominator)

            // Subtract 256 bit number from 512 bit number.
            prod1 := sub(prod1, gt(remainder, prod0))
            prod0 := sub(prod0, remainder)
        }

        // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
        // See https://cs.stackexchange.com/q/138556/92363.
        unchecked {
            // Does not overflow because the denominator cannot be zero at this stage in the function.
            uint256 lpotdod = denominator & (~denominator + 1);
            assembly {
                // Divide denominator by lpotdod.
                denominator := div(denominator, lpotdod)

                // Divide [prod1 prod0] by lpotdod.
                prod0 := div(prod0, lpotdod)

                // Flip lpotdod such that it is 2^256 / lpotdod. If lpotdod is zero, then it becomes one.
                lpotdod := add(div(sub(0, lpotdod), lpotdod), 1)
            }

            // Shift in bits from prod1 into prod0.
            prod0 |= prod1 * lpotdod;

            // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
            // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
            // four bits. That is, denominator * inv = 1 mod 2^4.
            uint256 inverse = (3 * denominator) ^ 2;

            // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
            // in modular arithmetic, doubling the correct bits in each step.
            inverse *= 2 - denominator * inverse; // inverse mod 2^8
            inverse *= 2 - denominator * inverse; // inverse mod 2^16
            inverse *= 2 - denominator * inverse; // inverse mod 2^32
            inverse *= 2 - denominator * inverse; // inverse mod 2^64
            inverse *= 2 - denominator * inverse; // inverse mod 2^128
            inverse *= 2 - denominator * inverse; // inverse mod 2^256

            // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
            // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
            // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
            // is no longer required.
            result = prod0 * inverse;
            return result;
        }
    }

    /// @notice Calculates floor(x*y÷1e18) with full precision.
    ///
    /// @dev Variant of "mulDiv" with constant folding, i.e. in which the denominator is always 1e18. Before returning the
    /// final result, we add 1 if (x * y) % SCALE >= HALF_SCALE. Without this, 6.6e-19 would be truncated to 0 instead of
    /// being rounded to 1e-18.  See "Listing 6" and text above it at https://accu.org/index.php/journals/1717.
    ///
    /// Requirements:
    /// - The result must fit within uint256.
    ///
    /// Caveats:
    /// - The body is purposely left uncommented; see the NatSpec comments in "PRBMath.mulDiv" to understand how this works.
    /// - It is assumed that the result can never be type(uint256).max when x and y solve the following two equations:
    ///     1. x * y = type(uint256).max * SCALE
    ///     2. (x * y) % SCALE >= SCALE / 2
    ///
    /// @param x The multiplicand as an unsigned 60.18-decimal fixed-point number.
    /// @param y The multiplier as an unsigned 60.18-decimal fixed-point number.
    /// @return result The result as an unsigned 60.18-decimal fixed-point number.
    function mulDivFixedPoint(uint256 x, uint256 y) internal pure returns (uint256 result) {
        uint256 prod0;
        uint256 prod1;
        assembly {
            let mm := mulmod(x, y, not(0))
            prod0 := mul(x, y)
            prod1 := sub(sub(mm, prod0), lt(mm, prod0))
        }

        if (prod1 >= SCALE) {
            revert PRBMath__MulDivFixedPointOverflow(prod1);
        }

        uint256 remainder;
        uint256 roundUpUnit;
        assembly {
            remainder := mulmod(x, y, SCALE)
            roundUpUnit := gt(remainder, 499999999999999999)
        }

        if (prod1 == 0) {
            unchecked {
                result = (prod0 / SCALE) + roundUpUnit;
                return result;
            }
        }

        assembly {
            result := add(
                mul(
                    or(
                        div(sub(prod0, remainder), SCALE_LPOTD),
                        mul(sub(prod1, gt(remainder, prod0)), add(div(sub(0, SCALE_LPOTD), SCALE_LPOTD), 1))
                    ),
                    SCALE_INVERSE
                ),
                roundUpUnit
            )
        }
    }

    /// @notice Calculates floor(x*y÷denominator) with full precision.
    ///
    /// @dev An extension of "mulDiv" for signed numbers. Works by computing the signs and the absolute values separately.
    ///
    /// Requirements:
    /// - None of the inputs can be type(int256).min.
    /// - The result must fit within int256.
    ///
    /// @param x The multiplicand as an int256.
    /// @param y The multiplier as an int256.
    /// @param denominator The divisor as an int256.
    /// @return result The result as an int256.
    function mulDivSigned(
        int256 x,
        int256 y,
        int256 denominator
    ) internal pure returns (int256 result) {
        if (x == type(int256).min || y == type(int256).min || denominator == type(int256).min) {
            revert PRBMath__MulDivSignedInputTooSmall();
        }

        // Get hold of the absolute values of x, y and the denominator.
        uint256 ax;
        uint256 ay;
        uint256 ad;
        unchecked {
            ax = x < 0 ? uint256(-x) : uint256(x);
            ay = y < 0 ? uint256(-y) : uint256(y);
            ad = denominator < 0 ? uint256(-denominator) : uint256(denominator);
        }

        // Compute the absolute value of (x*y)÷denominator. The result must fit within int256.
        uint256 rAbs = mulDiv(ax, ay, ad);
        if (rAbs > uint256(type(int256).max)) {
            revert PRBMath__MulDivSignedOverflow(rAbs);
        }

        // Get the signs of x, y and the denominator.
        uint256 sx;
        uint256 sy;
        uint256 sd;
        assembly {
            sx := sgt(x, sub(0, 1))
            sy := sgt(y, sub(0, 1))
            sd := sgt(denominator, sub(0, 1))
        }

        // XOR over sx, sy and sd. This is checking whether there are one or three negative signs in the inputs.
        // If yes, the result should be negative.
        result = sx ^ sy ^ sd == 0 ? -int256(rAbs) : int256(rAbs);
    }

    /// @notice Calculates the square root of x, rounding down.
    /// @dev Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
    ///
    /// Caveats:
    /// - This function does not work with fixed-point numbers.
    ///
    /// @param x The uint256 number for which to calculate the square root.
    /// @return result The result as an uint256.
    function sqrt(uint256 x) internal pure returns (uint256 result) {
        if (x == 0) {
            return 0;
        }

        // Set the initial guess to the least power of two that is greater than or equal to sqrt(x).
        uint256 xAux = uint256(x);
        result = 1;
        if (xAux >= 0x100000000000000000000000000000000) {
            xAux >>= 128;
            result <<= 64;
        }
        if (xAux >= 0x10000000000000000) {
            xAux >>= 64;
            result <<= 32;
        }
        if (xAux >= 0x100000000) {
            xAux >>= 32;
            result <<= 16;
        }
        if (xAux >= 0x10000) {
            xAux >>= 16;
            result <<= 8;
        }
        if (xAux >= 0x100) {
            xAux >>= 8;
            result <<= 4;
        }
        if (xAux >= 0x10) {
            xAux >>= 4;
            result <<= 2;
        }
        if (xAux >= 0x8) {
            result <<= 1;
        }

        // The operations can never overflow because the result is max 2^127 when it enters this block.
        unchecked {
            result = (result + x / result) >> 1;
            result = (result + x / result) >> 1;
            result = (result + x / result) >> 1;
            result = (result + x / result) >> 1;
            result = (result + x / result) >> 1;
            result = (result + x / result) >> 1;
            result = (result + x / result) >> 1; // Seven iterations should be enough
            uint256 roundedDownResult = x / result;
            return result >= roundedDownResult ? roundedDownResult : result;
        }
    }
}

File 30 of 31 : SABR.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import "prb-math/contracts/PRBMath.sol";
import "prb-math/contracts/PRBMathSD59x18.sol";

library SABR {
	using PRBMathSD59x18 for int256;

	int256 private constant eps = 1e11;

	struct IntermediateVariables {
		int256 a;
		int256 b;
		int256 c;
		int256 d;
		int256 v;
		int256 w;
		int256 z;
		int256 k;
		int256 f;
		int256 t;
	}

	function lognormalVol(
		int256 k,
		int256 f,
		int256 t,
		int256 alpha,
		int256 beta,
		int256 rho,
		int256 volvol
	) internal pure returns (int256 iv) {
		// Hagan's 2002 SABR lognormal vol expansion.

		// negative strikes or forwards
		if (k <= 0 || f <= 0) {
			return 0;
		}

		IntermediateVariables memory vars;

		vars.k = k;
		vars.f = f;
		vars.t = t;
		if (beta == 1e18) {
			vars.a = 0;
			vars.v = 0;
			vars.w = 0;
		} else {
			vars.a = ((1e18 - beta).pow(2e18)).mul(alpha.pow(2e18)).div(
				int256(24e18).mul(_fkbeta(vars.f, vars.k, beta))
			);
			vars.v = ((1e18 - beta).pow(2e18)).mul(_logfk(vars.f, vars.k).powu(2)).div(24e18);
			vars.w = ((1e18 - beta).pow(4e18)).mul(_logfk(vars.f, vars.k).powu(4)).div(1920e18);
		}
		vars.b = int256(25e16).mul(rho).mul(beta).mul(volvol).mul(alpha).div(
			_fkbeta(vars.f, vars.k, beta).sqrt()
		);
		vars.c = (2e18 - int256(3e18).mul(rho.powu(2))).mul(volvol.pow(2e18)).div(24e18);
		vars.d = _fkbeta(vars.f, vars.k, beta).sqrt();
		vars.z = volvol.mul(_fkbeta(vars.f, vars.k, beta).sqrt()).mul(_logfk(vars.f, vars.k)).div(alpha);

		// if |z| > eps
		if (vars.z.abs() > eps) {
			int256 vz = alpha.mul(vars.z).mul(1e18 + (vars.a + vars.b + vars.c).mul(vars.t)).div(
				vars.d.mul(1e18 + vars.v + vars.w).mul(_x(rho, vars.z))
			);
			return vz;
			// if |z| <= eps
		} else {
			int256 v0 = alpha.mul(1e18 + (vars.a + vars.b + vars.c).mul(vars.t)).div(
				vars.d.mul(1e18 + vars.v + vars.w)
			);
			return v0;
		}
	}

	function _logfk(int256 f, int256 k) internal pure returns (int256) {
		return (f.div(k)).ln();
	}

	function _fkbeta(
		int256 f,
		int256 k,
		int256 beta
	) internal pure returns (int256) {
		return (f.mul(k)).pow(1e18 - beta);
	}

	function _x(int256 rho, int256 z) internal pure returns (int256) {
		int256 a = (1e18 - 2 * rho.mul(z) + z.powu(2)).sqrt() + z - rho;
		int256 b = 1e18 - rho;
		return (a.div(b)).ln();
	}
}

File 31 of 31 : Context.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

Settings
{
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "metadata": {
    "useLiteralContent": true
  },
  "libraries": {
    "contracts/libraries/OpynInteractions.sol": {
      "OpynInteractions": "0x048603543a0fd41b56b831b80981addb19c1ea30"
    }
  }
}

Contract ABI

[{"inputs":[{"internalType":"address","name":"_collateralAsset","type":"address"},{"internalType":"address","name":"_oTokenFactory","type":"address"},{"internalType":"address","name":"_gammaController","type":"address"},{"internalType":"address","name":"_marginPool","type":"address"},{"internalType":"address","name":"_liquidityPool","type":"address"},{"internalType":"address","name":"_addressBook","type":"address"},{"internalType":"address","name":"_authority","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"AlreadyExpired","type":"error"},{"inputs":[],"name":"HealthyVault","type":"error"},{"inputs":[],"name":"InsufficientBalance","type":"error"},{"inputs":[],"name":"InvalidCollateral","type":"error"},{"inputs":[],"name":"InvalidDecimals","type":"error"},{"inputs":[],"name":"NoVault","type":"error"},{"inputs":[],"name":"NonExistentSeries","type":"error"},{"inputs":[],"name":"NotExpired","type":"error"},{"inputs":[],"name":"NotKeeper","type":"error"},{"inputs":[],"name":"NotLiquidityPool","type":"error"},{"inputs":[],"name":"UNAUTHORIZED","type":"error"},{"inputs":[],"name":"VaultExpired","type":"error"},{"inputs":[],"name":"VaultNotLiquidated","type":"error"},{"inputs":[],"name":"WithdrawExceedsLiquidity","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract IAuthority","name":"authority","type":"address"}],"name":"AuthorityUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"token","type":"address"}],"name":"OptionTokenCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"series","type":"address"},{"indexed":false,"internalType":"uint256","name":"vaultId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"closedAmount","type":"uint256"}],"name":"OptionsContractClosed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"series","type":"address"},{"indexed":false,"internalType":"uint256","name":"vaultId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"optionsAmount","type":"uint256"}],"name":"OptionsContractOpened","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"series","type":"address"},{"indexed":false,"internalType":"uint256","name":"collateralReturned","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"collateralLost","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountLost","type":"uint256"}],"name":"OptionsContractSettled","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"series","type":"address"},{"indexed":false,"internalType":"uint256","name":"underlyingAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"strikeAmount","type":"uint256"}],"name":"SeriesRedeemed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"series","type":"address"},{"indexed":false,"internalType":"uint256","name":"vaultId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountLiquidated","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"collateralLiquidated","type":"uint256"}],"name":"VaultLiquidationRegistered","type":"event"},{"inputs":[],"name":"addressBook","outputs":[{"internalType":"contract AddressBookInterface","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"vaultId","type":"uint256"}],"name":"adjustCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"vaultId","type":"uint256"}],"name":"adjustCollateralCaller","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"authority","outputs":[{"internalType":"contract IAuthority","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"callLowerHealthFactor","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"callUpperHealthFactor","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"vaultId","type":"uint256"}],"name":"checkVaultHealth","outputs":[{"internalType":"bool","name":"isBelowMin","type":"bool"},{"internalType":"bool","name":"isAboveMax","type":"bool"},{"internalType":"uint256","name":"healthFactor","type":"uint256"},{"internalType":"uint256","name":"upperHealthFactor","type":"uint256"},{"internalType":"uint256","name":"collatRequired","type":"uint256"},{"internalType":"address","name":"collatAsset","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_series","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"close","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"collateralAsset","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"strikePrice","type":"uint256"},{"internalType":"address","name":"collateral","type":"address"}],"name":"formatStrikePrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gammaController","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint64","name":"expiration","type":"uint64"},{"internalType":"uint128","name":"strike","type":"uint128"},{"internalType":"bool","name":"isPut","type":"bool"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"strikeAsset","type":"address"},{"internalType":"address","name":"collateral","type":"address"}],"internalType":"struct Types.OptionSeries","name":"series","type":"tuple"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getCollateral","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint64","name":"expiration","type":"uint64"},{"internalType":"uint128","name":"strike","type":"uint128"},{"internalType":"bool","name":"isPut","type":"bool"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"strikeAsset","type":"address"},{"internalType":"address","name":"collateral","type":"address"}],"internalType":"struct Types.OptionSeries","name":"_series","type":"tuple"}],"name":"getIssuanceHash","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"strikeAsset","type":"address"},{"internalType":"uint256","name":"expiration","type":"uint256"},{"internalType":"bool","name":"isPut","type":"bool"},{"internalType":"uint256","name":"strike","type":"uint256"},{"internalType":"address","name":"collateral","type":"address"}],"name":"getOtoken","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint64","name":"expiration","type":"uint64"},{"internalType":"uint128","name":"strike","type":"uint128"},{"internalType":"bool","name":"isPut","type":"bool"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"strikeAsset","type":"address"},{"internalType":"address","name":"collateral","type":"address"}],"internalType":"struct Types.OptionSeries","name":"_series","type":"tuple"}],"name":"getSeries","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"issuanceHash","type":"bytes32"}],"name":"getSeriesAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"series","type":"address"}],"name":"getSeriesInfo","outputs":[{"components":[{"internalType":"uint64","name":"expiration","type":"uint64"},{"internalType":"uint128","name":"strike","type":"uint128"},{"internalType":"bool","name":"isPut","type":"bool"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"strikeAsset","type":"address"},{"internalType":"address","name":"collateral","type":"address"}],"internalType":"struct Types.OptionSeries","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint64","name":"expiration","type":"uint64"},{"internalType":"uint128","name":"strike","type":"uint128"},{"internalType":"bool","name":"isPut","type":"bool"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"strikeAsset","type":"address"},{"internalType":"address","name":"collateral","type":"address"}],"internalType":"struct Types.OptionSeries","name":"optionSeries","type":"tuple"}],"name":"issue","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"keeper","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"liquidityPool","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_series","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"collateralAmount","type":"uint256"}],"name":"open","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"putLowerHealthFactor","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"putUpperHealthFactor","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_series","type":"address"}],"name":"redeem","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"vaultId","type":"uint256"}],"name":"registerLiquidatedVault","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"seriesInfo","outputs":[{"internalType":"uint64","name":"expiration","type":"uint64"},{"internalType":"uint128","name":"strike","type":"uint128"},{"internalType":"bool","name":"isPut","type":"bool"},{"internalType":"address","name":"underlying","type":"address"},{"internalType":"address","name":"strikeAsset","type":"address"},{"internalType":"address","name":"collateral","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IAuthority","name":"_newAuthority","type":"address"}],"name":"setAuthority","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint64","name":"_putLower","type":"uint64"},{"internalType":"uint64","name":"_putUpper","type":"uint64"},{"internalType":"uint64","name":"_callLower","type":"uint64"},{"internalType":"uint64","name":"_callUpper","type":"uint64"}],"name":"setHealthThresholds","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_target","type":"address"},{"internalType":"bool","name":"_auth","type":"bool"}],"name":"setKeeper","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newLiquidityPool","type":"address"}],"name":"setLiquidityPool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_series","type":"address"}],"name":"settle","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"vaultCount","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"vaultIds","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"vaultId","type":"uint256"}],"name":"wCollatLiquidatedVault","outputs":[],"stateMutability":"nonpayable","type":"function"}]

610120604052792af80000000000002ee00000000000002af800000000000032c86005553480156200003057600080fd5b5060405162003f5138038062003f518339810160408190526200005391620001c6565b600080546001600160a01b0319166001600160a01b03831690811790915560405190815281907f2f658b440c35314f52658ea8a740e05b284cdc84dc9ae01e891f21b8933e7cad9060200160405180910390a150866001600160a01b031660c0816001600160a01b0316815250506012876001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b158015620000fd57600080fd5b505afa15801562000112573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906200013891906200025b565b60ff1611156200015b57604051630692acc560e51b815260040160405180910390fd5b506001600160a01b0394851660805292841660a05290831661010052600480549184166801000000000000000002600160401b600160e01b03199092169190911790551660e0525062000287565b80516001600160a01b0381168114620001c157600080fd5b919050565b600080600080600080600060e0888a031215620001e257600080fd5b620001ed88620001a9565b9650620001fd60208901620001a9565b95506200020d60408901620001a9565b94506200021d60608901620001a9565b93506200022d60808901620001a9565b92506200023d60a08901620001a9565b91506200024d60c08901620001a9565b905092959891949750929550565b6000602082840312156200026e57600080fd5b815160ff811681146200028057600080fd5b9392505050565b60805160a05160c05160e05161010051613bdd620003746000396000818161169401528181611cd40152818161201d0152612a4f01526000818161078a01528181610927015281816109f00152818161224f01526123260152600081816104840152818161148c0152818161158501526129a001526000818161036f01528181610cf201528181610f000152818161166c015281816117c20152818161194b01528181611baa01528181611cac01528181611ff5015281816120ff01528181612764015281816128d601528181612a270152612ca401526000818161084401526111650152613bdd6000f3fe608060405234801561001057600080fd5b50600436106102065760003560e01c806395a2251f1161011a578063c7bc163b116100ad578063dfeddb7e1161007c578063dfeddb7e14610703578063e341c12c1461072c578063e49280cb1461073f578063e8cf860814610752578063f5887cdd1461078557600080fd5b8063c7bc163b146104e6578063c84feaca146105a3578063d1b9e853146105bd578063dac9675b146105d057600080fd5b8063aabaecd6116100e9578063aabaecd61461047f578063b0b96a35146104a6578063b9402079146104c0578063bf7e214f146104d357600080fd5b806395a2251f146103fb5780639b8c8dc21461040e578063a3895ce614610459578063a7c6a1001461046c57600080fd5b80635fb708cf1161019d5780636abf4c5e1161016c5780636abf4c5e1461036a5780637a9e5e4b1461039157806389a86ad3146103a457806389f92871146103ce5780638cbb6511146103e857600080fd5b80635fb708cf146102f557806363009ac314610308578063665a11ca1461031b5780636a256b291461033557600080fd5b80632e79bf42116101d95780632e79bf421461029c57806332c926cb146102af57806341849afc146102c25780634c9a1c61146102e257600080fd5b8063018770201461020b578063164e6ea9146102205780631c19975914610250578063210fc56a14610271575b600080fd5b61021e610219366004613343565b6107ac565b005b600554610233906001600160401b031681565b6040516001600160401b0390911681526020015b60405180910390f35b61026361025e366004613497565b6107e5565b604051908152602001610247565b61028461027f3660046134b3565b610825565b6040516001600160a01b039091168152602001610247565b6102636102aa366004613523565b610922565b61021e6102bd36600461354e565b610c4e565b6102636102d0366004613343565b60026020526000908152604090205481565b61021e6102f03660046135a2565b610ccb565b610284610303366004613497565b6110ac565b61021e6103163660046135a2565b61145f565b60045461028490600160401b90046001600160a01b031681565b610348610343366004613343565b611846565b6040805194151585526020850193909352918301526060820152608001610247565b6102847f000000000000000000000000000000000000000000000000000000000000000081565b61021e61039f366004613343565b611a75565b6103b76103b23660046135bb565b611ad1565b604080519215158352602083019190915201610247565b60055461023390600160401b90046001600160401b031681565b6102846103f6366004613497565b611e0c565b610263610409366004613343565b611e6a565b61042161041c3660046135a2565b6120cc565b604080519615158752941515602087015293850192909252606084015260808301526001600160a01b031660a082015260c001610247565b6102636104673660046135f0565b61265c565b600454610233906001600160401b031681565b6102847f000000000000000000000000000000000000000000000000000000000000000081565b60055461023390600160c01b90046001600160401b031681565b61021e6104ce3660046135a2565b612731565b600054610284906001600160a01b031681565b6105526104f4366004613343565b600160208190526000918252604090912080549181015460028201546003909201546001600160401b03841693600160401b81046001600160801b031693600160c01b90910460ff16926001600160a01b0390811692918116911686565b604080516001600160401b0390971687526001600160801b039095166020870152921515938501939093526001600160a01b03908116606085015291821660808401521660a082015260c001610247565b60055461023390600160801b90046001600160401b031681565b61021e6105cb366004613620565b612942565b6106976105de366004613343565b6040805160c081018252600080825260208201819052918101829052606081018290526080810182905260a0810191909152506001600160a01b03908116600090815260016020818152604092839020835160c08101855281546001600160401b0381168252600160401b81046001600160801b031693820193909352600160c01b90920460ff16151593820193909352908201548316606082015260028201548316608082015260039091015490911660a082015290565b604051610247919081516001600160401b031681526020808301516001600160801b0316908201526040808301511515908201526060808301516001600160a01b039081169183019190915260808084015182169083015260a092830151169181019190915260c00190565b6102846107113660046135a2565b6000908152600360205260409020546001600160a01b031690565b61021e61073a3660046135a2565b612975565b6103b761074d36600461364e565b612ad5565b610775610760366004613343565b60066020526000908152604090205460ff1681565b6040519015158152602001610247565b6102847f000000000000000000000000000000000000000000000000000000000000000081565b6107b4612d86565b600480546001600160a01b03909216600160401b0268010000000000000000600160e01b0319909216919091179055565b600061081f826060015183608001518460a0015185600001516001600160401b0316866040015187602001516001600160801b0316612e3d565b92915050565b60008073048603543a0fd41b56b831b80981addb19c1ea3063f7b98e1b7f0000000000000000000000000000000000000000000000000000000000000000858b8b6108708a8a61265c565b60405160e087901b6001600160e01b03191681526001600160a01b039586166004820152938516602485015291841660448401529092166064820152608481019190915260a4810189905287151560c482015260e40160206040518083038186803b1580156108de57600080fd5b505af41580156108f2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610916919061367a565b98975050505050505050565b6000807f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663cf28493f6040518163ffffffff1660e01b815260040160206040518083038186803b15801561097e57600080fd5b505afa158015610992573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906109b6919061367a565b90506000816001600160a01b0316630b0509fb866060015187608001518860a001516402540be400896109e991906136ad565b8a602001517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663833b1fce6040518163ffffffff1660e01b815260040160206040518083038186803b158015610a4757600080fd5b505afa158015610a5b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610a7f919061367a565b60608d01516040516341976e0960e01b81526001600160a01b0391821660048201529116906341976e099060240160206040518083038186803b158015610ac557600080fd5b505afa158015610ad9573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610afd91906136cf565b8c600001518d60a001516001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b158015610b3f57600080fd5b505afa158015610b53573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b7791906136e8565b8e604001516040518a63ffffffff1660e01b8152600401610ba09998979695949392919061370b565b60206040518083038186803b158015610bb857600080fd5b505afa158015610bcc573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610bf091906136cf565b905060008560400151610c0e576005546001600160401b0316610c22565b600554600160801b90046001600160401b03165b6001600160401b03169050612710610c3a828461376f565b610c4491906136ad565b9695505050505050565b610c56612d86565b600580546001600160801b0316600160c01b6001600160401b039687160267ffffffffffffffff60801b191617600160801b94861694909402939093176fffffffffffffffffffffffffffffffff1916600160401b9285169290920267ffffffffffffffff1916919091179216919091179055565b610cd3612eae565b60405163d99d13f560e01b8152306004820152602481018290526000907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169063d99d13f59060440160006040518083038186803b158015610d3c57600080fd5b505afa158015610d50573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052610d789190810190613880565b90508060600151600081518110610d9157610d916139a0565b6020026020010151600014610df85760405162461bcd60e51b815260206004820152602260248201527f5661756c74206861732073686f727420706f736974696f6e73205b616d6f756e604482015261745d60f01b60648201526084015b60405180910390fd5b80518051600091908290610e0e57610e0e6139a0565b60200260200101516001600160a01b031614610e765760405162461bcd60e51b815260206004820152602160248201527f5661756c74206861732073686f727420706f736974696f6e73205b746f6b656e6044820152605d60f81b6064820152608401610def565b60008160a00151600081518110610e8f57610e8f6139a0565b602002602001015111610ee45760405162461bcd60e51b815260206004820152601760248201527f5661756c7420686173206e6f20636f6c6c61746572616c0000000000000000006044820152606401610def565b73048603543a0fd41b56b831b80981addb19c1ea3063fd5b6dd57f00000000000000000000000000000000000000000000000000000000000000008360400151600081518110610f3657610f366139a0565b60200260200101518460a00151600081518110610f5557610f556139a0565b6020026020010151866040518563ffffffff1660e01b8152600401610f7d94939291906139b6565b60006040518083038186803b158015610f9557600080fd5b505af4158015610fa9573d6000803e3d6000fd5b50505050600460089054906101000a90046001600160a01b03166001600160a01b0316631e4bc5578260a00151600081518110610fe857610fe86139a0565b602002602001015160016040518363ffffffff1660e01b815260040161101a9291909182521515602082015260400190565b600060405180830381600087803b15801561103457600080fd5b505af1158015611048573d6000803e3d6000fd5b505050506110a88160400151600081518110611066576110666139a0565b6020026020010151600460089054906101000a90046001600160a01b03168360a0015160008151811061109b5761109b6139a0565b6020026020010151613022565b5050565b60006110b66130a1565b4282600001516001600160401b0316116110e35760405163506b27c560e11b815260040160405180910390fd5b600061110083602001516001600160801b03168460a0015161265c565b90506000611138846060015185608001518660a0015187600001516001600160401b03168860400151876001600160801b0316612e3d565b60a08501516060860151608087015187516040808a01519051630e4e99e360e01b81526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000081166004830152958616602482015293851660448501529390911660648301526001600160801b03861660848301526001600160401b031660a482015290151560c482015290915060009073048603543a0fd41b56b831b80981addb19c1ea3090630e4e99e39060e40160206040518083038186803b15801561120657600080fd5b505af415801561121a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061123e919061367a565b90506040518060c0016040528086600001516001600160401b03168152602001846001600160801b0316815260200186604001511515815260200186606001516001600160a01b0316815260200186608001516001600160a01b031681526020018660a001516001600160a01b031681525060016000836001600160a01b03166001600160a01b0316815260200190815260200160002060008201518160000160006101000a8154816001600160401b0302191690836001600160401b0316021790555060208201518160000160086101000a8154816001600160801b0302191690836001600160801b0316021790555060408201518160000160186101000a81548160ff02191690831515021790555060608201518160010160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060808201518160020160006101000a8154816001600160a01b0302191690836001600160a01b0316021790555060a08201518160030160006101000a8154816001600160a01b0302191690836001600160a01b03160217905550905050806003600084815260200190815260200160002060006101000a8154816001600160a01b0302191690836001600160a01b031602179055507ff434a81a703b3b06f1dd290881e157c47d663389196c88db9f57819ec6f6ae9f8160405161144f91906001600160a01b0391909116815260200190565b60405180910390a1949350505050565b611467612eae565b600080600080611476856120cc565b95509550505093509350806001600160a01b03167f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316146114d2576040516368f7a67560e11b815260040160405180910390fd5b831580156114de575082155b156114fc576040516315fe9b6160e21b815260040160405180910390fd5b83156117235760048054604051631e4bc55760e01b815291820184905260006024830152600160401b90046001600160a01b031690631e4bc55790604401600060405180830381600087803b15801561155457600080fd5b505af1158015611568573d6000803e3d6000fd5b50506004805460405163f8b2cb4f60e01b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000811693820193909352869450600160401b909104909116915063f8b2cb4f9060240160206040518083038186803b1580156115de57600080fd5b505afa1580156115f2573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061161691906136cf565b10156116355760405163b84beb5d60e01b815260040160405180910390fd5b600454611655908290600160401b90046001600160a01b031630856130d3565b6040516347e200ad60e11b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000811660048301527f00000000000000000000000000000000000000000000000000000000000000008116602483015282166044820152606481018390526084810186905273048603543a0fd41b56b831b80981addb19c1ea3090638fc4015a9060a40160006040518083038186803b15801561170657600080fd5b505af415801561171a573d6000803e3d6000fd5b5050505061183f565b821561183f5760048054604051631e4bc55760e01b815291820184905260016024830152600160401b90046001600160a01b031690631e4bc55790604401600060405180830381600087803b15801561177b57600080fd5b505af115801561178f573d6000803e3d6000fd5b505060405163fd5b6dd560e01b815273048603543a0fd41b56b831b80981addb19c1ea30925063fd5b6dd591506117f0907f000000000000000000000000000000000000000000000000000000000000000090859087908b906004016139b6565b60006040518083038186803b15801561180857600080fd5b505af415801561181c573d6000803e3d6000fd5b505060045461183f9250839150600160401b90046001600160a01b031684613022565b5050505050565b6000806000806118546130a1565b6001600160a01b03858116600090815260016020818152604092839020835160c08101855281546001600160401b038116808352600160401b82046001600160801b031694830194909452600160c01b900460ff16151594810194909452918201548416606084015260028201548416608084015260039091015490921660a0820152906118f55760405163117af06360e31b815260040160405180910390fd5b4281600001516001600160401b0316106119225760405163d0404f8560e01b815260040160405180910390fd5b6001600160a01b038681166000908152600260205260408082205490516315afd40960e01b81527f0000000000000000000000000000000000000000000000000000000000000000909316600484015260248301819052918190819073048603543a0fd41b56b831b80981addb19c1ea30906315afd4099060440160606040518083038186803b1580156119b557600080fd5b505af41580156119c9573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906119ed91906139df565b925092509250611a178560a00151600460089054906101000a90046001600160a01b031685613022565b60408051848152602081018490529081018290526001600160a01b038b16907ffd7eb2273d5660864e5ad7b3a1afd8ff16166a5c5fb2fe03c29f9fdbdc8930d49060600160405180910390a260019a92995090975095509350505050565b611a7d612d86565b600080546001600160a01b0319166001600160a01b0383169081179091556040519081527f2f658b440c35314f52658ea8a740e05b284cdc84dc9ae01e891f21b8933e7cad9060200160405180910390a150565b600080611adc6130a1565b6001600160a01b03858116600090815260016020818152604092839020835160c08101855281546001600160401b038116808352600160401b82046001600160801b031694830194909452600160c01b900460ff16151594810194909452918201548416606084015260028201548416608084015260039091015490921660a0820152904210611b7f5760405163506b27c560e11b815260040160405180910390fd5b611b8f8160a001513330876130d3565b6001600160a01b0386166000908152600260205260409020547f00000000000000000000000000000000000000000000000000000000000000009080611c9557604051636553690d60e11b81523060048201526001600160a01b0383169063caa6d21a9060240160206040518083038186803b158015611c0e57600080fd5b505afa158015611c22573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611c4691906136cf565b611c51906001613a0d565b600480549192506001600160401b03909116906000611c6f83613a25565b91906101000a8154816001600160401b0302191690836001600160401b03160217905550505b604051633718f97560e01b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000811660048301527f00000000000000000000000000000000000000000000000000000000000000008116602483015289166044820152606481018790526084810182905260a48101889052600160c482015260009073048603543a0fd41b56b831b80981addb19c1ea3090633718f9759060e40160206040518083038186803b158015611d5757600080fd5b505af4158015611d6b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611d8f91906136cf565b60408051848152602081018390529192506001600160a01b038b16917f242b1ddd06a984a28045d1a261fcddca274c5f29be7c894def55d7b938d4c7ce910160405180910390a2611de1893383613022565b506001600160a01b038816600090815260026020526040902055506001925083915050935093915050565b600060036000611e4a846060015185608001518660a0015187600001516001600160401b0316886040015189602001516001600160801b0316612e3d565b81526020810191909152604001600020546001600160a01b031692915050565b6001600160a01b038181166000908152600160208181526040808420815160c08101835281546001600160401b038116808352600160401b82046001600160801b031695830195909552600160c01b900460ff16151592810192909252928301548516606082015260028301548516608082015260039092015490931660a08201529091611f0b5760405163117af06360e31b815260040160405180910390fd5b4281600001516001600160401b031610611f385760405163d0404f8560e01b815260040160405180910390fd5b6040516370a0823160e01b81523360048201526000906001600160a01b038516906370a082319060240160206040518083038186803b158015611f7a57600080fd5b505afa158015611f8e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611fb291906136cf565b905080611fd257604051631e9acf1760e31b815260040160405180910390fd5b611fde843330846130d3565b6040516302e80dad60e61b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000811660048301527f000000000000000000000000000000000000000000000000000000000000000081166024830152851660448201526064810182905260009073048603543a0fd41b56b831b80981addb19c1ea309063ba036b409060840160206040518083038186803b15801561208b57600080fd5b505af415801561209f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906120c391906136cf565b95945050505050565b60405163d99d13f560e01b8152306004820152602481018290526000908190819081908190819081906001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169063d99d13f59060440160006040518083038186803b15801561214157600080fd5b505afa158015612155573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405261217d9190810190613880565b9050600060016000836000015160008151811061219c5761219c6139a0565b6020908102919091018101516001600160a01b039081168352828201939093526040918201600020825160c08101845281546001600160401b038116808352600160401b82046001600160801b031694830194909452600160c01b900460ff161515938101939093526001810154841660608401526002810154841660808401526003015490921660a0820152915042111561224b57604051631267999960e31b815260040160405180910390fd5b60007f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663cf28493f6040518163ffffffff1660e01b815260040160206040518083038186803b1580156122a657600080fd5b505afa1580156122ba573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906122de919061367a565b90506000816001600160a01b0316630b0509fb846060015185608001518660a001518860600151600081518110612317576123176139a0565b602002602001015188602001517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663833b1fce6040518163ffffffff1660e01b815260040160206040518083038186803b15801561237d57600080fd5b505afa158015612391573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123b5919061367a565b60608b01516040516341976e0960e01b81526001600160a01b0391821660048201529116906341976e099060240160206040518083038186803b1580156123fb57600080fd5b505afa15801561240f573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061243391906136cf565b8a600001518b60a001516001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b15801561247557600080fd5b505afa158015612489573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906124ad91906136e8565b8c604001516040518a63ffffffff1660e01b81526004016124d69998979695949392919061370b565b60206040518083038186803b1580156124ee57600080fd5b505afa158015612502573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061252691906136cf565b905060008460a00151600081518110612541576125416139a0565b60200260200101519050816127108261255a919061376f565b61256491906136ad565b98508360400151612580576005546001600160401b0316612594565b600554600160801b90046001600160401b03165b6001600160401b03169750600084604001516125c257600554600160401b90046001600160401b03166125d6565b600554600160c01b90046001600160401b03165b6001600160401b03169050888a11156126155760019a506127106125fa8a8561376f565b61260491906136ad565b61260e9083613a4c565b9750612646565b808a10156126465760019b508161271061262f8b8661376f565b61263991906136ad565b6126439190613a4c565b97505b8460a00151965050505050505091939550919395565b60008061266e6402540be400856136ad565b90506000836001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b1580156126ab57600080fd5b505afa1580156126bf573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906126e391906136e8565b60ff169050600881106126f85750905061081f565b6000612705826008613a4c565b905061271281600a613b47565b61271d82600a613b47565b61272790856136ad565b610c44919061376f565b612739612eae565b60405163f62fc0b360e01b815230600482015260248101829052600090819081906001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169063f62fc0b39060440160606040518083038186803b1580156127a657600080fd5b505afa1580156127ba573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906127de9190613b53565b919450925090506001600160a01b03831661280c5760405163df66968160e01b815260040160405180910390fd5b60408051858152602081018490529081018290526001600160a01b038416907f47aa675faf14c5e3b5da8d235f13812988463fbe9e8d3da67741f8c068e8132d9060600160405180910390a260048054604051631e4bc55760e01b815291820183905260016024830152600160401b90046001600160a01b031690631e4bc55790604401600060405180830381600087803b1580156128aa57600080fd5b505af11580156128be573d6000803e3d6000fd5b5050604051630fb0af3f60e31b8152600481018790527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03169250637d8579f89150602401600060405180830381600087803b15801561292457600080fd5b505af1158015612938573d6000803e3d6000fd5b5050505050505050565b61294a612d86565b6001600160a01b03919091166000908152600660205260409020805460ff1916911515919091179055565b61297d61316d565b600080600061298b846120cc565b955095505050509250806001600160a01b03167f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316146129e6576040516368f7a67560e11b815260040160405180910390fd5b82612a04576040516315fe9b6160e21b815260040160405180910390fd5b612a10813330856130d3565b6040516347e200ad60e11b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000811660048301527f00000000000000000000000000000000000000000000000000000000000000008116602483015282166044820152606481018390526084810185905273048603543a0fd41b56b831b80981addb19c1ea3090638fc4015a9060a40160006040518083038186803b158015612ac157600080fd5b505af4158015612938573d6000803e3d6000fd5b600080612ae06130a1565b6001600160a01b03848116600090815260016020818152604092839020835160c08101855281546001600160401b038116808352600160401b82046001600160801b031694830194909452600160c01b900460ff16151594810194909452918201548416606084015260028201548416608084015260039091015490921660a082015290612b815760405163117af06360e31b815260040160405180910390fd5b4281600001516001600160401b031611612bae5760405163506b27c560e11b815260040160405180910390fd5b6001600160a01b03851660009081526002602052604090205480612be55760405163a1b86ccf60e01b815260040160405180910390fd5b6000612c6486886001600160a01b031663313ce5676040518163ffffffff1660e01b815260040160206040518083038186803b158015612c2457600080fd5b505afa158015612c38573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612c5c91906136e8565b60ff166132a8565b9050612c72873330846130d3565b604051633f83640160e21b815260009073048603543a0fd41b56b831b80981addb19c1ea309063fe0d900490612cd2907f0000000000000000000000000000000000000000000000000000000000000000908c90879089906004016139b6565b60206040518083038186803b158015612cea57600080fd5b505af4158015612cfe573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612d2291906136cf565b9050612d338460a001513383613022565b60408051848152602081018490526001600160a01b038a16917f8b307ec2cdec0ea71518d1d82e315a365c5789f3e02e4466b9d886b3474b1793910160405180910390a260019890975095505050505050565b60008054906101000a90046001600160a01b03166001600160a01b0316630c340a246040518163ffffffff1660e01b815260040160206040518083038186803b158015612dd257600080fd5b505afa158015612de6573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612e0a919061367a565b6001600160a01b0316336001600160a01b031614612e3b5760405163075fd2b160e01b815260040160405180910390fd5b565b6040516bffffffffffffffffffffffff19606088811b8216602084015287811b8216603484015286901b166048820152605c810184905282151560f81b607c820152607d8101829052600090609d016040516020818303038152906040528051906020012090509695505050505050565b3360009081526006602052604090205460ff16158015612f63575060008054906101000a90046001600160a01b03166001600160a01b0316630c340a246040518163ffffffff1660e01b815260040160206040518083038186803b158015612f1557600080fd5b505afa158015612f29573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612f4d919061367a565b6001600160a01b0316336001600160a01b031614155b8015613004575060008054906101000a90046001600160a01b03166001600160a01b031663481c6a756040518163ffffffff1660e01b815260040160206040518083038186803b158015612fb657600080fd5b505afa158015612fca573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190612fee919061367a565b6001600160a01b0316336001600160a01b031614155b15612e3b57604051631ea2564f60e31b815260040160405180910390fd5b600060405163a9059cbb60e01b81526001600160a01b03841660048201528260248201526000806044836000895af191505061305d816132e4565b61309b5760405162461bcd60e51b815260206004820152600f60248201526e1514905394d1915497d19052531151608a1b6044820152606401610def565b50505050565b600454600160401b90046001600160a01b03163314612e3b5760405163f539349760e01b815260040160405180910390fd5b600084905060006040516323b872dd60e01b81526001600160a01b03861660048201526001600160a01b03851660248201528360448201526000806064836000875af1915050613122816132e4565b6131655760405162461bcd60e51b81526020600482015260146024820152731514905394d1915497d19493d357d1905253115160621b6044820152606401610def565b505050505050565b60005460405163794f111b60e11b81523360048201526001600160a01b039091169063f29e22369060240160206040518083038186803b1580156131b057600080fd5b505afa1580156131c4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906131e89190613b8a565b15801561328a575060008054906101000a90046001600160a01b03166001600160a01b0316630c340a246040518163ffffffff1660e01b815260040160206040518083038186803b15801561323c57600080fd5b505afa158015613250573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613274919061367a565b6001600160a01b0316336001600160a01b031614155b15612e3b5760405163075fd2b160e01b815260040160405180910390fd5b600060128211156132b857600080fd5b60006132c5836012613a4c565b90506132d281600a613b47565b6132dc90856136ad565b949350505050565b60003d826132f657806000803e806000fd5b806020811461330e57801561331f5760009250613324565b816000803e60005115159250613324565b600192505b5050919050565b6001600160a01b038116811461334057600080fd5b50565b60006020828403121561335557600080fd5b81356133608161332b565b9392505050565b634e487b7160e01b600052604160045260246000fd5b60405160c081016001600160401b038111828210171561339f5761339f613367565b60405290565b604051601f8201601f191681016001600160401b03811182821017156133cd576133cd613367565b604052919050565b80356001600160401b03811681146133ec57600080fd5b919050565b801515811461334057600080fd5b600060c0828403121561341157600080fd5b61341961337d565b9050613424826133d5565b815260208201356001600160801b038116811461344057600080fd5b60208201526040820135613453816133f1565b604082015260608201356134668161332b565b606082015260808201356134798161332b565b608082015260a082013561348c8161332b565b60a082015292915050565b600060c082840312156134a957600080fd5b61336083836133ff565b60008060008060008060c087890312156134cc57600080fd5b86356134d78161332b565b955060208701356134e78161332b565b94506040870135935060608701356134fe816133f1565b92506080870135915060a08701356135158161332b565b809150509295509295509295565b60008060e0838503121561353657600080fd5b61354084846133ff565b9460c0939093013593505050565b6000806000806080858703121561356457600080fd5b61356d856133d5565b935061357b602086016133d5565b9250613589604086016133d5565b9150613597606086016133d5565b905092959194509250565b6000602082840312156135b457600080fd5b5035919050565b6000806000606084860312156135d057600080fd5b83356135db8161332b565b95602085013595506040909401359392505050565b6000806040838503121561360357600080fd5b8235915060208301356136158161332b565b809150509250929050565b6000806040838503121561363357600080fd5b823561363e8161332b565b91506020830135613615816133f1565b6000806040838503121561366157600080fd5b823561366c8161332b565b946020939093013593505050565b60006020828403121561368c57600080fd5b81516133608161332b565b634e487b7160e01b600052601160045260246000fd5b6000826136ca57634e487b7160e01b600052601260045260246000fd5b500490565b6000602082840312156136e157600080fd5b5051919050565b6000602082840312156136fa57600080fd5b815160ff8116811461336057600080fd5b6001600160a01b03998a168152978916602089015295909716604087015260608601939093526001600160801b0391909116608085015260a08401526001600160401b031660c083015260ff90921660e08201529015156101008201526101200190565b600081600019048311821515161561378957613789613697565b500290565b60006001600160401b038211156137a7576137a7613367565b5060051b60200190565b600082601f8301126137c257600080fd5b815160206137d76137d28361378e565b6133a5565b82815260059290921b840181019181810190868411156137f657600080fd5b8286015b8481101561381a57805161380d8161332b565b83529183019183016137fa565b509695505050505050565b600082601f83011261383657600080fd5b815160206138466137d28361378e565b82815260059290921b8401810191818101908684111561386557600080fd5b8286015b8481101561381a5780518352918301918301613869565b60006020828403121561389257600080fd5b81516001600160401b03808211156138a957600080fd5b9083019060c082860312156138bd57600080fd5b6138c561337d565b8251828111156138d457600080fd5b6138e0878286016137b1565b8252506020830151828111156138f557600080fd5b613901878286016137b1565b60208301525060408301518281111561391957600080fd5b613925878286016137b1565b60408301525060608301518281111561393d57600080fd5b61394987828601613825565b60608301525060808301518281111561396157600080fd5b61396d87828601613825565b60808301525060a08301518281111561398557600080fd5b61399187828601613825565b60a08301525095945050505050565b634e487b7160e01b600052603260045260246000fd5b6001600160a01b0394851681529290931660208301526040820152606081019190915260800190565b6000806000606084860312156139f457600080fd5b8351925060208401519150604084015190509250925092565b60008219821115613a2057613a20613697565b500190565b60006001600160401b0380831681811415613a4257613a42613697565b6001019392505050565b600082821015613a5e57613a5e613697565b500390565b600181815b80851115613a9e578160001904821115613a8457613a84613697565b80851615613a9157918102915b93841c9390800290613a68565b509250929050565b600082613ab55750600161081f565b81613ac25750600061081f565b8160018114613ad85760028114613ae257613afe565b600191505061081f565b60ff841115613af357613af3613697565b50506001821b61081f565b5060208310610133831016604e8410600b8410161715613b21575081810a61081f565b613b2b8383613a63565b8060001904821115613b3f57613b3f613697565b029392505050565b60006133608383613aa6565b600080600060608486031215613b6857600080fd5b8351613b738161332b565b602085015160409095015190969495509392505050565b600060208284031215613b9c57600080fd5b8151613360816133f156fea26469706673582212202653e9aa8a1965c2c41a440146e010c7d9d38e5ed9b7de26e8c54a867c69a20164736f6c63430008090033000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc8000000000000000000000000ba1952ecdba02de66fcf73f29068e8cf072644ec000000000000000000000000594bd4ec29f7900ae29549c140ac53b5240d4019000000000000000000000000b9f33349db1d0711d95c1198acba9511b8269626000000000000000000000000a67d0c1180e0e183f482304a9b5436a3478f0674000000000000000000000000ca19f26c52b11186b4b1e76a662a14da5149ea5a0000000000000000000000000c83e447dc7f4045b8717d5321056d4e9e86dcd2

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc8000000000000000000000000ba1952ecdba02de66fcf73f29068e8cf072644ec000000000000000000000000594bd4ec29f7900ae29549c140ac53b5240d4019000000000000000000000000b9f33349db1d0711d95c1198acba9511b8269626000000000000000000000000a67d0c1180e0e183f482304a9b5436a3478f0674000000000000000000000000ca19f26c52b11186b4b1e76a662a14da5149ea5a0000000000000000000000000c83e447dc7f4045b8717d5321056d4e9e86dcd2

-----Decoded View---------------
Arg [0] : _collateralAsset (address): 0xff970a61a04b1ca14834a43f5de4533ebddb5cc8
Arg [1] : _oTokenFactory (address): 0xba1952ecdba02de66fcf73f29068e8cf072644ec
Arg [2] : _gammaController (address): 0x594bd4ec29f7900ae29549c140ac53b5240d4019
Arg [3] : _marginPool (address): 0xb9f33349db1d0711d95c1198acba9511b8269626
Arg [4] : _liquidityPool (address): 0xa67d0c1180e0e183f482304a9b5436a3478f0674
Arg [5] : _addressBook (address): 0xca19f26c52b11186b4b1e76a662a14da5149ea5a
Arg [6] : _authority (address): 0x0c83e447dc7f4045b8717d5321056d4e9e86dcd2

-----Encoded View---------------
7 Constructor Arguments found :
Arg [0] : 000000000000000000000000ff970a61a04b1ca14834a43f5de4533ebddb5cc8
Arg [1] : 000000000000000000000000ba1952ecdba02de66fcf73f29068e8cf072644ec
Arg [2] : 000000000000000000000000594bd4ec29f7900ae29549c140ac53b5240d4019
Arg [3] : 000000000000000000000000b9f33349db1d0711d95c1198acba9511b8269626
Arg [4] : 000000000000000000000000a67d0c1180e0e183f482304a9b5436a3478f0674
Arg [5] : 000000000000000000000000ca19f26c52b11186b4b1e76a662a14da5149ea5a
Arg [6] : 0000000000000000000000000c83e447dc7f4045b8717d5321056d4e9e86dcd2


Block Transaction Gas Used Reward
Age Block Fee Address BC Fee Address Voting Power Jailed Incoming
Block Uncle Number Difficulty Gas Used Reward
Loading
Make sure to use the "Vote Down" button for any spammy posts, and the "Vote Up" for interesting conversations.