文件 1 的 25:AccessControl.sol
pragma solidity >=0.8.0;
import "../interfaces/IAuthority.sol";
error UNAUTHORIZED();
abstract contract AccessControl {
event AuthorityUpdated(IAuthority authority);
IAuthority public authority;
constructor(IAuthority _authority) {
authority = _authority;
emit AuthorityUpdated(_authority);
}
function setAuthority(IAuthority _newAuthority) external {
_onlyGovernor();
authority = _newAuthority;
emit AuthorityUpdated(_newAuthority);
}
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();
}
}
文件 2 的 25:AggregatorV3Interface.sol
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);
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
);
}
文件 3 的 25:BlackScholes.sol
pragma solidity >=0.8.0;
import "prb-math/contracts/PRBMathSD59x18.sol";
import "prb-math/contracts/PRBMathUD60x18.sol";
import { NormalDist } from "./NormalDist.sol";
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);
}
}
}
文件 4 的 25:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 5 的 25:CustomErrors.sol
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);
}
文件 6 的 25:ERC20.sol
pragma solidity >=0.8.0;
abstract contract ERC20 {
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
string public name;
string public symbol;
uint8 public immutable decimals;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
uint256 internal immutable INITIAL_CHAIN_ID;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
mapping(address => uint256) public nonces;
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
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;
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];
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
balanceOf[from] -= amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(from, to, amount);
return true;
}
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 {
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)
)
);
}
function _mint(address to, uint256 amount) internal virtual {
totalSupply += amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(address(0), to, amount);
}
function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;
unchecked {
totalSupply -= amount;
}
emit Transfer(from, address(0), amount);
}
}
文件 7 的 25:IAccounting.sol
pragma solidity >=0.8.9;
interface IAccounting {
struct DepositReceipt {
uint128 epoch;
uint128 amount;
uint256 unredeemedShares;
}
struct WithdrawalReceipt {
uint128 epoch;
uint128 shares;
}
function deposit(address depositor, uint256 _amount)
external
returns (uint256 depositAmount, uint256 unredeemedShares);
function redeem(address redeemer, uint256 shares)
external
returns (uint256 toRedeem, DepositReceipt memory depositReceipt);
function initiateWithdraw(address withdrawer, uint256 shares)
external
returns (WithdrawalReceipt memory withdrawalReceipt);
function completeWithdraw(address withdrawer)
external
returns (
uint256 withdrawalAmount,
uint256 withdrawalShares,
WithdrawalReceipt memory withdrawalReceipt
);
function executeEpochCalculation(
uint256 totalSupply,
uint256 assets,
int256 liabilities
)
external
view
returns (
uint256 newPricePerShareDeposit,
uint256 newPricePerShareWithdrawal,
uint256 sharesToMint,
uint256 totalWithdrawAmount,
uint256 amountNeeded
);
function sharesForAmount(uint256 _amount, uint256 assetPerShare)
external
view
returns (uint256 shares);
}
文件 8 的 25:IAuthority.sol
pragma solidity >=0.8.0;
interface IAuthority {
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);
function governor() external view returns (address);
function guardian(address _target) external view returns (bool);
function manager() external view returns (address);
}
文件 9 的 25:IHedgingReactor.sol
pragma solidity >=0.8.9;
interface IHedgingReactor {
function hedgeDelta(int256 delta) external returns (int256);
function getDelta() external view returns (int256 delta);
function getPoolDenominatedValue() external view returns (uint256 value);
function withdraw(uint256 amount) external returns (uint256);
function update() external returns (uint256);
}
文件 10 的 25:IOptionRegistry.sol
pragma solidity >=0.8.9;
import { Types } from "../libraries/Types.sol";
interface IOptionRegistry {
function issue(Types.OptionSeries memory optionSeries) external returns (address);
function open(
address _series,
uint256 amount,
uint256 collateralAmount
) external returns (bool, uint256);
function close(address _series, uint256 amount) external returns (bool, uint256);
function settle(address _series)
external
returns (
bool success,
uint256 collatReturned,
uint256 collatLost,
uint256 amountShort
);
function getCollateral(Types.OptionSeries memory series, uint256 amount)
external
view
returns (uint256);
function getOtoken(
address underlying,
address strikeAsset,
uint256 expiration,
bool isPut,
uint256 strike,
address collateral
) external view returns (address);
function getSeriesInfo(address series) external view returns (Types.OptionSeries memory);
function vaultIds(address series) external view returns (uint256);
function gammaController() external view returns (address);
}
文件 11 的 25:IPortfolioValuesFeed.sol
pragma solidity 0.8.9;
import "../libraries/Types.sol";
interface IPortfolioValuesFeed {
function requestPortfolioData(address _underlying, address _strike)
external
returns (bytes32 requestId);
function updateStores(Types.OptionSeries memory _optionSeries, int256 _shortExposure, int256 _longExposure, address _seriesAddress) external;
function getPortfolioValues(address underlying, address strike)
external
view
returns (Types.PortfolioValues memory);
}
文件 12 的 25:LiquidityPool.sol
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";
contract LiquidityPool is ERC20, AccessControl, ReentrancyGuard, Pausable {
using PRBMathSD59x18 for int256;
using PRBMathUD60x18 for uint256;
Protocol public immutable protocol;
address public immutable strikeAsset;
address public immutable underlyingAsset;
address public immutable collateralAsset;
uint256 public collateralAllocated;
int256 public ephemeralLiabilities;
int256 public ephemeralDelta;
uint256 public depositEpoch;
uint256 public withdrawalEpoch;
mapping(uint256 => uint256) public depositEpochPricePerShare;
mapping(uint256 => uint256) public withdrawalEpochPricePerShare;
mapping(address => IAccounting.DepositReceipt) public depositReceipts;
mapping(address => IAccounting.WithdrawalReceipt) public withdrawalReceipts;
uint256 public pendingDeposits;
uint256 public pendingWithdrawals;
uint256 public partitionedFunds;
uint256 public bufferPercentage = 5000;
address[] public hedgingReactors;
uint256 public collateralCap = type(uint256).max;
uint256 public maxDiscount = (PRBMathUD60x18.SCALE * 10) / 100;
uint256 public bidAskIVSpread;
Types.OptionParams public optionParams;
uint256 public riskFreeRate;
mapping(address => bool) public handler;
bool public isTradingPaused;
uint256 public maxTimeDeviationThreshold = 600;
uint256 public maxPriceDeviationThreshold = 1e18;
uint256 public belowThresholdGradient = 0;
uint256 public aboveThresholdGradient = 1e18;
uint256 public aboveThresholdYIntercept = 6e17;
uint256 public utilizationFunctionThreshold = 6e17;
mapping(address => bool) public keeper;
uint256 private constant MAX_BPS = 10_000;
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++;
}
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();
}
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);
}
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;
}
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;
}
function setBidAskSpread(uint256 _bidAskSpread) external {
_onlyManager();
bidAskIVSpread = _bidAskSpread;
}
function setMaxDiscount(uint256 _maxDiscount) external {
_onlyManager();
maxDiscount = _maxDiscount;
}
function setCollateralCap(uint256 _collateralCap) external {
_onlyGovernor();
collateralCap = _collateralCap;
}
function setBufferPercentage(uint256 _bufferPercentage) external {
_onlyGovernor();
bufferPercentage = _bufferPercentage;
}
function setRiskFreeRate(uint256 _riskFreeRate) external {
_onlyGovernor();
riskFreeRate = _riskFreeRate;
}
function setMaxTimeDeviationThreshold(uint256 _maxTimeDeviationThreshold) external {
_onlyGovernor();
maxTimeDeviationThreshold = _maxTimeDeviationThreshold;
}
function setMaxPriceDeviationThreshold(uint256 _maxPriceDeviationThreshold) external {
_onlyGovernor();
maxPriceDeviationThreshold = _maxPriceDeviationThreshold;
}
function changeHandler(address _handler, bool auth) external {
_onlyGovernor();
if (_handler == address(0)) {
revert CustomErrors.InvalidAddress();
}
handler[_handler] = auth;
}
function setKeeper(address _keeper, bool _auth) external {
_onlyGovernor();
if (_keeper == address(0)) {
revert CustomErrors.InvalidAddress();
}
keeper[_keeper] = _auth;
}
function setUtilizationSkewParams(
uint256 _belowThresholdGradient,
uint256 _aboveThresholdGradient,
uint256 _utilizationFunctionThreshold
) external {
_onlyManager();
belowThresholdGradient = _belowThresholdGradient;
aboveThresholdGradient = _aboveThresholdGradient;
aboveThresholdYIntercept = _utilizationFunctionThreshold.mul(
_aboveThresholdGradient - _belowThresholdGradient
);
utilizationFunctionThreshold = _utilizationFunctionThreshold;
}
function rebalancePortfolioDelta(int256 delta, uint256 reactorIndex) external {
_onlyManager();
IHedgingReactor(hedgingReactors[reactorIndex]).hedgeDelta(delta);
emit RebalancePortfolioDelta(delta);
}
function adjustCollateral(uint256 lpCollateralDifference, bool addToLpBalance) external {
IOptionRegistry optionRegistry = _getOptionRegistry();
require(msg.sender == address(optionRegistry));
if (addToLpBalance) {
collateralAllocated -= lpCollateralDifference;
} else {
SafeTransferLib.safeApprove(
ERC20(collateralAsset),
address(optionRegistry),
lpCollateralDifference
);
collateralAllocated += lpCollateralDifference;
}
}
function settleVault(address seriesAddress) external returns (uint256) {
_isKeeper();
(, uint256 collatReturned, uint256 collatLost, ) = _getOptionRegistry().settle(seriesAddress);
emit SettleVault(seriesAddress, collatReturned, collatLost, msg.sender);
_adjustVariables(collatReturned, collatLost, 0, false);
collateralAllocated -= collatLost;
return collatReturned;
}
function handlerIssue(Types.OptionSeries memory optionSeries) external returns (address) {
_isHandler();
return _issue(optionSeries, _getOptionRegistry());
}
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,
seriesAddress,
amount,
optionRegistry,
premium,
delta,
checkBuffer(),
recipient
);
}
function handlerIssueAndWriteOption(
Types.OptionSeries memory optionSeries,
uint256 amount,
uint256 premium,
int256 delta,
address recipient
) external returns (uint256, address) {
_isTradingNotPaused();
_isHandler();
IOptionRegistry optionRegistry = _getOptionRegistry();
address seriesAddress = _issue(optionSeries, optionRegistry);
optionSeries = optionRegistry.getSeriesInfo(seriesAddress);
return (
_writeOption(
optionSeries,
seriesAddress,
amount,
optionRegistry,
premium,
delta,
checkBuffer(),
recipient
),
seriesAddress
);
}
function handlerBuybackOption(
Types.OptionSeries memory optionSeries,
uint256 amount,
IOptionRegistry optionRegistry,
address seriesAddress,
uint256 premium,
int256 delta,
address seller
) external returns (uint256) {
_isTradingNotPaused();
_isHandler();
return
_buybackOption(optionSeries, amount, optionRegistry, seriesAddress, premium, delta, seller);
}
function resetEphemeralValues() external {
require(msg.sender == address(_getPortfolioValuesFeed()));
delete ephemeralLiabilities;
delete ephemeralDelta;
}
function pauseTradingAndRequest() external returns (bytes32) {
_isKeeper();
isTradingPaused = true;
emit TradingPaused();
return _getPortfolioValuesFeed().requestPortfolioData(underlyingAsset, strikeAsset);
}
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());
depositEpochPricePerShare[depositEpoch] = newPricePerShareDeposit;
delete pendingDeposits;
emit DepositEpochExecuted(depositEpoch);
depositEpoch++;
isTradingPaused = false;
emit TradingUnpaused();
_mint(address(this), sharesToMint);
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 (amountNeeded > 0) {
return;
}
}
withdrawalEpochPricePerShare[withdrawalEpoch] = newPricePerShareWithdrawal;
partitionedFunds += totalWithdrawAmount;
emit WithdrawalEpochExecuted(withdrawalEpoch);
_burn(address(this), pendingWithdrawals);
delete pendingWithdrawals;
withdrawalEpoch++;
}
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);
depositReceipts[msg.sender] = IAccounting.DepositReceipt({
epoch: uint128(depositEpoch),
amount: uint128(depositAmount),
unredeemedShares: unredeemedShares
});
pendingDeposits += _amount;
SafeTransferLib.safeTransferFrom(collateralAsset, msg.sender, address(this), _amount);
return true;
}
function redeem(uint256 _shares) external nonReentrant returns (uint256) {
if (_shares == 0) {
revert CustomErrors.InvalidShareAmount();
}
return _redeem(_shares);
}
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(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);
}
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);
partitionedFunds -= withdrawalAmount;
SafeTransferLib.safeTransfer(ERC20(collateralAsset), msg.sender, withdrawalAmount);
return withdrawalAmount;
}
function _getNormalizedBalance(address asset) internal view returns (uint256 normalizedBalance) {
normalizedBalance = OptionsCompute.convertFromDecimals(
ERC20(asset).balanceOf(address(this)) - partitionedFunds,
ERC20(asset).decimals()
);
}
function getBalance(address asset) public view returns (uint256) {
return ERC20(asset).balanceOf(address(this)) - partitionedFunds;
}
function getExternalDelta() public view returns (int256 externalDelta) {
address[] memory hedgingReactors_ = hedgingReactors;
for (uint8 i = 0; i < hedgingReactors_.length; i++) {
externalDelta += IHedgingReactor(hedgingReactors_[i]).getDelta();
}
}
function getPortfolioDelta() public view returns (int256) {
Types.PortfolioValues memory portfolioValues = _getPortfolioValuesFeed().getPortfolioValues(
underlyingAsset,
strikeAsset
);
OptionsCompute.validatePortfolioValues(
_getUnderlyingPrice(underlyingAsset, strikeAsset),
portfolioValues,
maxTimeDeviationThreshold,
maxPriceDeviationThreshold
);
return portfolioValues.delta + getExternalDelta() + ephemeralDelta;
}
function quotePriceWithUtilizationGreeks(
Types.OptionSeries memory optionSeries,
uint256 amount,
bool toBuy
) external view returns (uint256 quote, int256 delta) {
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
);
quoteState.totalOptionPrice = optionQuote.mul(amount);
quoteState.totalDelta = deltaQuote.mul(int256(amount));
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);
}
}
function addUtilizationPremium(
Types.UtilizationState memory quoteState,
Types.OptionSeries memory optionSeries,
uint256 amount,
bool toBuy
) internal view {
if (!toBuy) {
uint256 collateralAllocated_ = collateralAllocated;
quoteState.utilizationBefore = collateralAllocated_.div(
collateralAllocated_ + getBalance(collateralAsset)
);
optionSeries.strike = optionSeries.strike / 1e10;
quoteState.collateralToAllocate = _getOptionRegistry().getCollateral(optionSeries, amount);
quoteState.utilizationAfter = (quoteState.collateralToAllocate + collateralAllocated_).div(
collateralAllocated_ + getBalance(collateralAsset)
);
quoteState.utilizationPrice = OptionsCompute.getUtilizationPrice(
quoteState.utilizationBefore,
quoteState.utilizationAfter,
quoteState.totalOptionPrice,
utilizationFunctionThreshold,
belowThresholdGradient,
aboveThresholdGradient,
aboveThresholdYIntercept
);
} else {
quoteState.utilizationPrice = quoteState.totalOptionPrice;
}
}
function applyDeltaPremium(Types.UtilizationState memory quoteState, bool toBuy)
internal
view
returns (uint256 quote)
{
int256 portfolioDelta = getPortfolioDelta();
int256 newDelta = toBuy
? portfolioDelta + quoteState.totalDelta
: portfolioDelta - quoteState.totalDelta;
quoteState.isDecreased = (PRBMathSD59x18.abs(newDelta) - PRBMathSD59x18.abs(portfolioDelta)) < 0;
uint256 normalizedDelta = uint256(PRBMathSD59x18.abs((portfolioDelta + newDelta).div(2e18))).div(
_getNAV().div(quoteState.underlyingPrice)
);
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 {
quote = toBuy
? quoteState.utilizationPrice - quoteState.deltaTiltAmount.mul(quoteState.utilizationPrice)
: quoteState.deltaTiltAmount.mul(quoteState.utilizationPrice) + quoteState.utilizationPrice;
}
}
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();
}
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);
transferFrom(address(this), msg.sender, toRedeem);
return toRedeem;
}
function _getNAV() internal view returns (uint256) {
uint256 assets = _getAssets();
int256 liabilities = _getLiabilities();
if (int256(assets) < liabilities) {
revert CustomErrors.LiabilitiesGreaterThanAssets();
}
return uint256(int256(assets) - liabilities);
}
function _getAssets() internal view returns (uint256 assets) {
assets =
_getNormalizedBalance(collateralAsset) +
OptionsCompute.convertFromDecimals(collateralAllocated, ERC20(collateralAsset).decimals());
address[] memory hedgingReactors_ = hedgingReactors;
for (uint8 i = 0; i < hedgingReactors_.length; i++) {
assets += IHedgingReactor(hedgingReactors_[i]).getPoolDenominatedValue();
}
}
function _getLiabilities() internal view returns (int256 liabilities) {
Types.PortfolioValues memory portfolioValues = _getPortfolioValuesFeed().getPortfolioValues(
underlyingAsset,
strikeAsset
);
OptionsCompute.validatePortfolioValues(
_getUnderlyingPrice(underlyingAsset, strikeAsset),
portfolioValues,
maxTimeDeviationThreshold,
maxPriceDeviationThreshold
);
liabilities = portfolioValues.callPutsValue + ephemeralLiabilities;
}
function checkBuffer() public view returns (int256 bufferRemaining) {
uint256 collateralBalance = getBalance(collateralAsset);
uint256 collateralBuffer = (collateralAllocated * bufferPercentage) / MAX_BPS;
bufferRemaining = int256(collateralBalance) - int256(collateralBuffer);
}
function _issue(Types.OptionSeries memory optionSeries, IOptionRegistry optionRegistry)
internal
returns (address series)
{
if (optionSeries.collateral != collateralAsset) {
revert CustomErrors.CollateralAssetInvalid();
}
if (optionSeries.underlying != underlyingAsset) {
revert CustomErrors.UnderlyingAssetInvalid();
}
if (optionSeries.strikeAsset != strikeAsset) {
revert CustomErrors.StrikeAssetInvalid();
}
Types.OptionParams memory optionParams_ = optionParams;
if (
block.timestamp + optionParams_.minExpiry > optionSeries.expiration ||
optionSeries.expiration > block.timestamp + optionParams_.maxExpiry
) {
revert CustomErrors.OptionExpiryInvalid();
}
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();
}
}
series = optionRegistry.issue(optionSeries);
if (series == address(0)) {
revert CustomErrors.IssuanceFailed();
}
}
function _writeOption(
Types.OptionSeries memory optionSeries,
address seriesAddress,
uint256 amount,
IOptionRegistry optionRegistry,
uint256 premium,
int256 delta,
int256 bufferRemaining,
address recipient
) internal returns (uint256) {
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);
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())
);
return amount;
}
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);
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;
}
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;
}
}
function _getVolatilityFeed() internal view returns (VolatilityFeed) {
return VolatilityFeed(protocol.volatilityFeed());
}
function _getPortfolioValuesFeed() internal view returns (IPortfolioValuesFeed) {
return IPortfolioValuesFeed(protocol.portfolioValuesFeed());
}
function _getAccounting() internal view returns (IAccounting) {
return IAccounting(protocol.accounting());
}
function _getOptionRegistry() internal view returns (IOptionRegistry) {
return IOptionRegistry(protocol.optionRegistry());
}
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();
}
}
function _isKeeper() internal view {
if (
!keeper[msg.sender] && msg.sender != authority.governor() && msg.sender != authority.manager()
) {
revert CustomErrors.NotKeeper();
}
}
}
文件 13 的 25:NormalDist.sol
pragma solidity >=0.8.0;
import "prb-math/contracts/PRBMathSD59x18.sol";
library NormalDist {
using PRBMathSD59x18 for int256;
int256 private constant ONE = 1000000000000000000;
int256 private constant ONE_HALF = 500000000000000000;
int256 private constant SQRT_TWO = 1414213562373095048;
int256 private constant A1 = 254829592000000000;
int256 private constant A2 = -284496736000000000;
int256 private constant A3 = 1421413741000000000;
int256 private constant A4 = -1453152027000000000;
int256 private constant A5 = 1061405429000000000;
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();
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;
}
}
文件 14 的 25:OptionsCompute.sol
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";
library OptionsCompute {
using PRBMathUD60x18 for uint256;
using PRBMathSD59x18 for int256;
uint8 private constant SCALE_DECIMALS = 18;
function convertToDecimals(uint256 value, uint256 decimals) internal pure returns (uint256) {
if (decimals > SCALE_DECIMALS) {
revert();
}
uint256 difference = SCALE_DECIMALS - decimals;
return value / (10**difference);
}
function convertFromDecimals(uint256 value, uint256 decimals) internal pure returns (uint256) {
if (decimals > SCALE_DECIMALS) {
revert();
}
uint256 difference = SCALE_DECIMALS - decimals;
return value * (10**difference);
}
function convertToCollateralDenominated(
uint256 quote,
uint256 underlyingPrice,
Types.OptionSeries memory optionSeries
) internal pure returns (uint256 convertedQuote) {
if (optionSeries.strikeAsset != optionSeries.collateral) {
return (quote * 1e18) / underlyingPrice;
} else {
return quote;
}
}
function calculatePercentageChange(uint256 n, uint256 o) internal pure returns (uint256 pC) {
if (n > o) {
pC = (n - o).div(o);
} else {
pC = (o - n).div(o);
}
}
function validatePortfolioValues(
uint256 spotPrice,
Types.PortfolioValues memory portfolioValues,
uint256 maxTimeDeviationThreshold,
uint256 maxPriceDeviationThreshold
) public view {
uint256 timeDelta = block.timestamp - portfolioValues.timestamp;
if (timeDelta > maxTimeDeviationThreshold) {
revert CustomErrors.TimeDeltaExceedsThreshold(timeDelta);
}
uint256 priceDelta = calculatePercentageChange(spotPrice, portfolioValues.spotPrice);
if (priceDelta > maxPriceDeviationThreshold) {
revert CustomErrors.PriceDeltaExceedsThreshold(priceDelta);
}
}
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
) {
uint256 multiplicationFactor = (_utilizationBefore + _utilizationAfter)
.mul(_belowThresholdGradient)
.div(2e18);
return _totalOptionPrice + _totalOptionPrice.mul(multiplicationFactor);
} else if (
_utilizationBefore >= _utilizationFunctionThreshold &&
_utilizationAfter >= _utilizationFunctionThreshold
) {
uint256 multiplicationFactor = _aboveThresholdGradient
.mul(_utilizationBefore + _utilizationAfter)
.div(2e18) - _aboveThresholdYIntercept;
return _totalOptionPrice + _totalOptionPrice.mul(multiplicationFactor);
} else {
uint256 weightingRatio = (_utilizationFunctionThreshold - _utilizationBefore).div(
_utilizationAfter - _utilizationFunctionThreshold
);
uint256 averageFactorBelow = (_utilizationFunctionThreshold + _utilizationBefore).div(2e18).mul(
_belowThresholdGradient
);
uint256 averageFactorAbove = (_utilizationAfter + _utilizationFunctionThreshold).div(2e18).mul(
_aboveThresholdGradient
) - _aboveThresholdYIntercept;
uint256 multiplicationFactor = (weightingRatio.mul(averageFactorBelow) + averageFactorAbove).div(
1e18 + weightingRatio
);
return _totalOptionPrice + _totalOptionPrice.mul(multiplicationFactor);
}
}
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();
}
if (isBuying) {
iv = (iv * (1e18 - (bidAskIVSpread))) / 1e18;
}
if (optionSeries.expiration <= block.timestamp) {
revert CustomErrors.OptionExpiryInvalid();
}
(quote, delta) = BlackScholes.blackScholesCalcGreeks(
underlyingPrice,
optionSeries.strike,
optionSeries.expiration,
iv,
riskFreeRate,
optionSeries.isPut
);
}
}
文件 15 的 25:PRBMath.sol
pragma solidity >=0.8.4;
error PRBMath__MulDivFixedPointOverflow(uint256 prod1);
error PRBMath__MulDivOverflow(uint256 prod1, uint256 denominator);
error PRBMath__MulDivSignedInputTooSmall();
error PRBMath__MulDivSignedOverflow(uint256 rAbs);
error PRBMathSD59x18__AbsInputTooSmall();
error PRBMathSD59x18__CeilOverflow(int256 x);
error PRBMathSD59x18__DivInputTooSmall();
error PRBMathSD59x18__DivOverflow(uint256 rAbs);
error PRBMathSD59x18__ExpInputTooBig(int256 x);
error PRBMathSD59x18__Exp2InputTooBig(int256 x);
error PRBMathSD59x18__FloorUnderflow(int256 x);
error PRBMathSD59x18__FromIntOverflow(int256 x);
error PRBMathSD59x18__FromIntUnderflow(int256 x);
error PRBMathSD59x18__GmNegativeProduct(int256 x, int256 y);
error PRBMathSD59x18__GmOverflow(int256 x, int256 y);
error PRBMathSD59x18__LogInputTooSmall(int256 x);
error PRBMathSD59x18__MulInputTooSmall();
error PRBMathSD59x18__MulOverflow(uint256 rAbs);
error PRBMathSD59x18__PowuOverflow(uint256 rAbs);
error PRBMathSD59x18__SqrtNegativeInput(int256 x);
error PRBMathSD59x18__SqrtOverflow(int256 x);
error PRBMathUD60x18__AddOverflow(uint256 x, uint256 y);
error PRBMathUD60x18__CeilOverflow(uint256 x);
error PRBMathUD60x18__ExpInputTooBig(uint256 x);
error PRBMathUD60x18__Exp2InputTooBig(uint256 x);
error PRBMathUD60x18__FromUintOverflow(uint256 x);
error PRBMathUD60x18__GmOverflow(uint256 x, uint256 y);
error PRBMathUD60x18__LogInputTooSmall(uint256 x);
error PRBMathUD60x18__SqrtOverflow(uint256 x);
error PRBMathUD60x18__SubUnderflow(uint256 x, uint256 y);
library PRBMath {
struct SD59x18 {
int256 value;
}
struct UD60x18 {
uint256 value;
}
uint256 internal constant SCALE = 1e18;
uint256 internal constant SCALE_LPOTD = 262144;
uint256 internal constant SCALE_INVERSE =
78156646155174841979727994598816262306175212592076161876661_508869554232690281;
function exp2(uint256 x) internal pure returns (uint256 result) {
unchecked {
result = 0x800000000000000000000000000000000000000000000000;
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;
}
result *= SCALE;
result >>= (191 - (x >> 64));
}
}
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) {
msb += 1;
}
}
function mulDiv(
uint256 x,
uint256 y,
uint256 denominator
) 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 == 0) {
unchecked {
result = prod0 / denominator;
}
return result;
}
if (prod1 >= denominator) {
revert PRBMath__MulDivOverflow(prod1, denominator);
}
uint256 remainder;
assembly {
remainder := mulmod(x, y, denominator)
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
unchecked {
uint256 lpotdod = denominator & (~denominator + 1);
assembly {
denominator := div(denominator, lpotdod)
prod0 := div(prod0, lpotdod)
lpotdod := add(div(sub(0, lpotdod), lpotdod), 1)
}
prod0 |= prod1 * lpotdod;
uint256 inverse = (3 * denominator) ^ 2;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
result = prod0 * inverse;
return result;
}
}
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
)
}
}
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();
}
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);
}
uint256 rAbs = mulDiv(ax, ay, ad);
if (rAbs > uint256(type(int256).max)) {
revert PRBMath__MulDivSignedOverflow(rAbs);
}
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))
}
result = sx ^ sy ^ sd == 0 ? -int256(rAbs) : int256(rAbs);
}
function sqrt(uint256 x) internal pure returns (uint256 result) {
if (x == 0) {
return 0;
}
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;
}
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;
uint256 roundedDownResult = x / result;
return result >= roundedDownResult ? roundedDownResult : result;
}
}
}
文件 16 的 25:PRBMathSD59x18.sol
pragma solidity >=0.8.4;
import "./PRBMath.sol";
library PRBMathSD59x18 {
int256 internal constant LOG2_E = 1_442695040888963407;
int256 internal constant HALF_SCALE = 5e17;
int256 internal constant MAX_SD59x18 =
57896044618658097711785492504343953926634992332820282019728_792003956564819967;
int256 internal constant MAX_WHOLE_SD59x18 =
57896044618658097711785492504343953926634992332820282019728_000000000000000000;
int256 internal constant MIN_SD59x18 =
-57896044618658097711785492504343953926634992332820282019728_792003956564819968;
int256 internal constant MIN_WHOLE_SD59x18 =
-57896044618658097711785492504343953926634992332820282019728_000000000000000000;
int256 internal constant SCALE = 1e18;
function abs(int256 x) internal pure returns (int256 result) {
unchecked {
if (x == MIN_SD59x18) {
revert PRBMathSD59x18__AbsInputTooSmall();
}
result = x < 0 ? -x : x;
}
}
function avg(int256 x, int256 y) internal pure returns (int256 result) {
unchecked {
int256 sum = (x >> 1) + (y >> 1);
if (sum < 0) {
assembly {
result := add(sum, and(or(x, y), 1))
}
} else {
result = sum + (x & y & 1);
}
}
}
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 {
result = x - remainder;
if (x > 0) {
result += SCALE;
}
}
}
}
function div(int256 x, int256 y) internal pure returns (int256 result) {
if (x == MIN_SD59x18 || y == MIN_SD59x18) {
revert PRBMathSD59x18__DivInputTooSmall();
}
uint256 ax;
uint256 ay;
unchecked {
ax = x < 0 ? uint256(-x) : uint256(x);
ay = y < 0 ? uint256(-y) : uint256(y);
}
uint256 rAbs = PRBMath.mulDiv(ax, uint256(SCALE), ay);
if (rAbs > uint256(MAX_SD59x18)) {
revert PRBMathSD59x18__DivOverflow(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);
}
function e() internal pure returns (int256 result) {
result = 2_718281828459045235;
}
function exp(int256 x) internal pure returns (int256 result) {
if (x < -41_446531673892822322) {
return 0;
}
if (x >= 133_084258667509499441) {
revert PRBMathSD59x18__ExpInputTooBig(x);
}
unchecked {
int256 doubleScaleProduct = x * LOG2_E;
result = exp2((doubleScaleProduct + HALF_SCALE) / SCALE);
}
}
function exp2(int256 x) internal pure returns (int256 result) {
if (x < 0) {
if (x < -59_794705707972522261) {
return 0;
}
unchecked {
result = 1e36 / exp2(-x);
}
} else {
if (x >= 192e18) {
revert PRBMathSD59x18__Exp2InputTooBig(x);
}
unchecked {
uint256 x192x64 = (uint256(x) << 64) / uint256(SCALE);
result = int256(PRBMath.exp2(x192x64));
}
}
}
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 {
result = x - remainder;
if (x < 0) {
result -= SCALE;
}
}
}
}
function frac(int256 x) internal pure returns (int256 result) {
unchecked {
result = x % SCALE;
}
}
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;
}
}
function gm(int256 x, int256 y) internal pure returns (int256 result) {
if (x == 0) {
return 0;
}
unchecked {
int256 xy = x * y;
if (xy / x != y) {
revert PRBMathSD59x18__GmOverflow(x, y);
}
if (xy < 0) {
revert PRBMathSD59x18__GmNegativeProduct(x, y);
}
result = int256(PRBMath.sqrt(uint256(xy)));
}
}
function inv(int256 x) internal pure returns (int256 result) {
unchecked {
result = 1e36 / x;
}
}
function ln(int256 x) internal pure returns (int256 result) {
unchecked {
result = (log2(x) * SCALE) / LOG2_E;
}
}
function log10(int256 x) internal pure returns (int256 result) {
if (x <= 0) {
revert PRBMathSD59x18__LogInputTooSmall(x);
}
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) {
unchecked {
result = (log2(x) * SCALE) / 3_321928094887362347;
}
}
}
function log2(int256 x) internal pure returns (int256 result) {
if (x <= 0) {
revert PRBMathSD59x18__LogInputTooSmall(x);
}
unchecked {
int256 sign;
if (x >= SCALE) {
sign = 1;
} else {
sign = -1;
assembly {
x := div(1000000000000000000000000000000000000, x)
}
}
uint256 n = PRBMath.mostSignificantBit(uint256(x / SCALE));
result = int256(n) * SCALE;
int256 y = x >> n;
if (y == SCALE) {
return result * sign;
}
for (int256 delta = int256(HALF_SCALE); delta > 0; delta >>= 1) {
y = (y * y) / SCALE;
if (y >= 2 * SCALE) {
result += delta;
y >>= 1;
}
}
result *= sign;
}
}
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);
}
}
function pi() internal pure returns (int256 result) {
result = 3_141592653589793238;
}
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));
}
}
function powu(int256 x, uint256 y) internal pure returns (int256 result) {
uint256 xAbs = uint256(abs(x));
uint256 rAbs = y & 1 > 0 ? xAbs : uint256(SCALE);
uint256 yAux = y;
for (yAux >>= 1; yAux > 0; yAux >>= 1) {
xAbs = PRBMath.mulDivFixedPoint(xAbs, xAbs);
if (yAux & 1 > 0) {
rAbs = PRBMath.mulDivFixedPoint(rAbs, xAbs);
}
}
if (rAbs > uint256(MAX_SD59x18)) {
revert PRBMathSD59x18__PowuOverflow(rAbs);
}
bool isNegative = x < 0 && y & 1 == 1;
result = isNegative ? -int256(rAbs) : int256(rAbs);
}
function scale() internal pure returns (int256 result) {
result = SCALE;
}
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);
}
result = int256(PRBMath.sqrt(uint256(x * SCALE)));
}
}
function toInt(int256 x) internal pure returns (int256 result) {
unchecked {
result = x / SCALE;
}
}
}
文件 17 的 25:PRBMathUD60x18.sol
pragma solidity >=0.8.4;
import "./PRBMath.sol";
library PRBMathUD60x18 {
uint256 internal constant HALF_SCALE = 5e17;
uint256 internal constant LOG2_E = 1_442695040888963407;
uint256 internal constant MAX_UD60x18 =
115792089237316195423570985008687907853269984665640564039457_584007913129639935;
uint256 internal constant MAX_WHOLE_UD60x18 =
115792089237316195423570985008687907853269984665640564039457_000000000000000000;
uint256 internal constant SCALE = 1e18;
function avg(uint256 x, uint256 y) internal pure returns (uint256 result) {
unchecked {
result = (x >> 1) + (y >> 1) + (x & y & 1);
}
}
function ceil(uint256 x) internal pure returns (uint256 result) {
if (x > MAX_WHOLE_UD60x18) {
revert PRBMathUD60x18__CeilOverflow(x);
}
assembly {
let remainder := mod(x, SCALE)
let delta := sub(SCALE, remainder)
result := add(x, mul(delta, gt(remainder, 0)))
}
}
function div(uint256 x, uint256 y) internal pure returns (uint256 result) {
result = PRBMath.mulDiv(x, SCALE, y);
}
function e() internal pure returns (uint256 result) {
result = 2_718281828459045235;
}
function exp(uint256 x) internal pure returns (uint256 result) {
if (x >= 133_084258667509499441) {
revert PRBMathUD60x18__ExpInputTooBig(x);
}
unchecked {
uint256 doubleScaleProduct = x * LOG2_E;
result = exp2((doubleScaleProduct + HALF_SCALE) / SCALE);
}
}
function exp2(uint256 x) internal pure returns (uint256 result) {
if (x >= 192e18) {
revert PRBMathUD60x18__Exp2InputTooBig(x);
}
unchecked {
uint256 x192x64 = (x << 64) / SCALE;
result = PRBMath.exp2(x192x64);
}
}
function floor(uint256 x) internal pure returns (uint256 result) {
assembly {
let remainder := mod(x, SCALE)
result := sub(x, mul(remainder, gt(remainder, 0)))
}
}
function frac(uint256 x) internal pure returns (uint256 result) {
assembly {
result := mod(x, SCALE)
}
}
function fromUint(uint256 x) internal pure returns (uint256 result) {
unchecked {
if (x > MAX_UD60x18 / SCALE) {
revert PRBMathUD60x18__FromUintOverflow(x);
}
result = x * SCALE;
}
}
function gm(uint256 x, uint256 y) internal pure returns (uint256 result) {
if (x == 0) {
return 0;
}
unchecked {
uint256 xy = x * y;
if (xy / x != y) {
revert PRBMathUD60x18__GmOverflow(x, y);
}
result = PRBMath.sqrt(xy);
}
}
function inv(uint256 x) internal pure returns (uint256 result) {
unchecked {
result = 1e36 / x;
}
}
function ln(uint256 x) internal pure returns (uint256 result) {
unchecked {
result = (log2(x) * SCALE) / LOG2_E;
}
}
function log10(uint256 x) internal pure returns (uint256 result) {
if (x < SCALE) {
revert PRBMathUD60x18__LogInputTooSmall(x);
}
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) {
unchecked {
result = (log2(x) * SCALE) / 3_321928094887362347;
}
}
}
function log2(uint256 x) internal pure returns (uint256 result) {
if (x < SCALE) {
revert PRBMathUD60x18__LogInputTooSmall(x);
}
unchecked {
uint256 n = PRBMath.mostSignificantBit(x / SCALE);
result = n * SCALE;
uint256 y = x >> n;
if (y == SCALE) {
return result;
}
for (uint256 delta = HALF_SCALE; delta > 0; delta >>= 1) {
y = (y * y) / SCALE;
if (y >= 2 * SCALE) {
result += delta;
y >>= 1;
}
}
}
}
function mul(uint256 x, uint256 y) internal pure returns (uint256 result) {
result = PRBMath.mulDivFixedPoint(x, y);
}
function pi() internal pure returns (uint256 result) {
result = 3_141592653589793238;
}
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));
}
}
function powu(uint256 x, uint256 y) internal pure returns (uint256 result) {
result = y & 1 > 0 ? x : SCALE;
for (y >>= 1; y > 0; y >>= 1) {
x = PRBMath.mulDivFixedPoint(x, x);
if (y & 1 > 0) {
result = PRBMath.mulDivFixedPoint(result, x);
}
}
}
function scale() internal pure returns (uint256 result) {
result = SCALE;
}
function sqrt(uint256 x) internal pure returns (uint256 result) {
unchecked {
if (x > MAX_UD60x18 / SCALE) {
revert PRBMathUD60x18__SqrtOverflow(x);
}
result = PRBMath.sqrt(x * SCALE);
}
}
function toUint(uint256 x) internal pure returns (uint256 result) {
unchecked {
result = x / SCALE;
}
}
}
文件 18 的 25:Pausable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Pausable is Context {
event Paused(address account);
event Unpaused(address account);
bool private _paused;
constructor() {
_paused = false;
}
modifier whenNotPaused() {
_requireNotPaused();
_;
}
modifier whenPaused() {
_requirePaused();
_;
}
function paused() public view virtual returns (bool) {
return _paused;
}
function _requireNotPaused() internal view virtual {
require(!paused(), "Pausable: paused");
}
function _requirePaused() internal view virtual {
require(paused(), "Pausable: not paused");
}
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
}
文件 19 的 25:PriceFeed.sol
pragma solidity >=0.8.9;
import "./interfaces/AggregatorV3Interface.sol";
import "./libraries/AccessControl.sol";
contract PriceFeed is AccessControl {
mapping(address => mapping(address => address)) public priceFeeds;
uint8 private constant SCALE_DECIMALS = 18;
uint32 private constant STALE_PRICE_DELAY = 3600;
constructor(address _authority) AccessControl(IAuthority(_authority)) {}
function addPriceFeed(
address underlying,
address strike,
address feed
) public {
_onlyGovernor();
priceFeeds[underlying][strike] = feed;
}
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);
}
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);
}
}
文件 20 的 25:Protocol.sol
pragma solidity >=0.8.0;
import "./libraries/AccessControl.sol";
contract Protocol is AccessControl {
address public immutable optionRegistry;
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;
}
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;
}
}
文件 21 的 25:ReentrancyGuard.sol
pragma solidity >=0.8.9;
contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
文件 22 的 25:SABR.sol
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) {
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 (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;
} 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();
}
}
文件 23 的 25:SafeTransferLib.sol
pragma solidity >=0.8.0;
import {ERC20} from "../tokens/ERC20.sol";
library SafeTransferLib {
function safeTransferETH(address to, uint256 amount) internal {
bool callStatus;
assembly {
callStatus := call(gas(), to, amount, 0, 0, 0, 0)
}
require(callStatus, "ETH_TRANSFER_FAILED");
}
function safeTransferFrom(
address tokenAddress,
address from,
address to,
uint256 amount
) internal {
ERC20 token = ERC20(tokenAddress);
bool callStatus;
assembly {
let freeMemoryPointer := mload(0x40)
mstore(freeMemoryPointer, 0x23b872dd00000000000000000000000000000000000000000000000000000000)
mstore(add(freeMemoryPointer, 4), and(from, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(freeMemoryPointer, 36), and(to, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(freeMemoryPointer, 68), amount)
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 {
let freeMemoryPointer := mload(0x40)
mstore(freeMemoryPointer, 0xa9059cbb00000000000000000000000000000000000000000000000000000000)
mstore(add(freeMemoryPointer, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(freeMemoryPointer, 36), amount)
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 {
let freeMemoryPointer := mload(0x40)
mstore(freeMemoryPointer, 0x095ea7b300000000000000000000000000000000000000000000000000000000)
mstore(add(freeMemoryPointer, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(freeMemoryPointer, 36), amount)
callStatus := call(gas(), token, 0, freeMemoryPointer, 68, 0, 0)
}
require(didLastOptionalReturnCallSucceed(callStatus), "APPROVE_FAILED");
}
function didLastOptionalReturnCallSucceed(bool callStatus) private pure returns (bool success) {
assembly {
let returnDataSize := returndatasize()
if iszero(callStatus) {
returndatacopy(0, 0, returnDataSize)
revert(0, returnDataSize)
}
switch returnDataSize
case 32 {
returndatacopy(0, 0, returnDataSize)
success := iszero(iszero(mload(0)))
}
case 0 {
success := 1
}
default {
success := 0
}
}
}
}
文件 24 的 25:Types.sol
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;
}
struct OptionParams {
uint128 minCallStrikePrice;
uint128 maxCallStrikePrice;
uint128 minPutStrikePrice;
uint128 maxPutStrikePrice;
uint128 minExpiry;
uint128 maxExpiry;
}
struct UtilizationState {
uint256 totalOptionPrice;
int256 totalDelta;
uint256 collateralToAllocate;
uint256 utilizationBefore;
uint256 utilizationAfter;
uint256 utilizationPrice;
bool isDecreased;
uint256 deltaTiltAmount;
uint256 underlyingPrice;
uint256 iv;
}
}
文件 25 的 25:VolatilityFeed.sol
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";
contract VolatilityFeed is AccessControl {
using PRBMathSD59x18 for int256;
using PRBMathUD60x18 for uint256;
mapping(uint256 => SABRParams) public sabrParams;
mapping(address => bool) public keeper;
uint256[] public expiries;
int256 private constant ONE_YEAR_SECONDS = 31557600;
int256 private constant BIPS_SCALE = 1e12;
int256 private constant BIPS = 1e6;
struct SABRParams {
int32 callAlpha;
int32 callBeta;
int32 callRho;
int32 callVolvol;
int32 putAlpha;
int32 putBeta;
int32 putRho;
int32 putVolvol;
}
constructor(address _authority) AccessControl(IAuthority(_authority)) {}
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
);
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(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
);
}
function setKeeper(address _keeper, bool _auth) external {
_onlyGovernor();
keeper[_keeper] = _auth;
}
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);
}
function getExpiries() external view returns (uint256[] memory) {
return expiries;
}
function _isKeeper() internal view {
if (
!keeper[msg.sender] && msg.sender != authority.governor() && msg.sender != authority.manager()
) {
revert CustomErrors.NotKeeper();
}
}
}
{
"compilationTarget": {
"contracts/LiquidityPool.sol": "LiquidityPool"
},
"evmVersion": "london",
"libraries": {
"contracts/libraries/BlackScholes.sol:BlackScholes": "0x2c215b6bac6a4871c2e58669f0437853da500020",
"contracts/libraries/OptionsCompute.sol:OptionsCompute": "0x303956bcc420b3b74b861874d39bad5d5ee341f0"
},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_protocol","type":"address"},{"internalType":"address","name":"_strikeAsset","type":"address"},{"internalType":"address","name":"_underlyingAsset","type":"address"},{"internalType":"address","name":"_collateralAsset","type":"address"},{"internalType":"uint256","name":"rfr","type":"uint256"},{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"components":[{"internalType":"uint128","name":"minCallStrikePrice","type":"uint128"},{"internalType":"uint128","name":"maxCallStrikePrice","type":"uint128"},{"internalType":"uint128","name":"minPutStrikePrice","type":"uint128"},{"internalType":"uint128","name":"maxPutStrikePrice","type":"uint128"},{"internalType":"uint128","name":"minExpiry","type":"uint128"},{"internalType":"uint128","name":"maxExpiry","type":"uint128"}],"internalType":"struct Types.OptionParams","name":"_optionParams","type":"tuple"},{"internalType":"address","name":"_authority","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"CollateralAssetInvalid","type":"error"},{"inputs":[{"internalType":"uint256","name":"quote","type":"uint256"},{"internalType":"int256","name":"delta","type":"int256"}],"name":"DeltaQuoteError","type":"error"},{"inputs":[],"name":"IVNotFound","type":"error"},{"inputs":[],"name":"InvalidAddress","type":"error"},{"inputs":[],"name":"InvalidAmount","type":"error"},{"inputs":[],"name":"InvalidDecimals","type":"error"},{"inputs":[],"name":"InvalidShareAmount","type":"error"},{"inputs":[],"name":"IssuanceFailed","type":"error"},{"inputs":[],"name":"LiabilitiesGreaterThanAssets","type":"error"},{"inputs":[],"name":"MaxLiquidityBufferReached","type":"error"},{"inputs":[],"name":"NotHandler","type":"error"},{"inputs":[],"name":"NotKeeper","type":"error"},{"inputs":[],"name":"OptionExpiryInvalid","type":"error"},{"inputs":[],"name":"OptionStrikeInvalid","type":"error"},{"inputs":[],"name":"PRBMathSD59x18__AbsInputTooSmall","type":"error"},{"inputs":[],"name":"PRBMathSD59x18__DivInputTooSmall","type":"error"},{"inputs":[{"internalType":"uint256","name":"rAbs","type":"uint256"}],"name":"PRBMathSD59x18__DivOverflow","type":"error"},{"inputs":[],"name":"PRBMathSD59x18__MulInputTooSmall","type":"error"},{"inputs":[{"internalType":"uint256","name":"rAbs","type":"uint256"}],"name":"PRBMathSD59x18__MulOverflow","type":"error"},{"inputs":[{"internalType":"uint256","name":"prod1","type":"uint256"}],"name":"PRBMath__MulDivFixedPointOverflow","type":"error"},{"inputs":[{"internalType":"uint256","name":"prod1","type":"uint256"},{"internalType":"uint256","name":"denominator","type":"uint256"}],"name":"PRBMath__MulDivOverflow","type":"error"},{"inputs":[],"name":"ReactorAlreadyExists","type":"error"},{"inputs":[],"name":"StrikeAssetInvalid","type":"error"},{"inputs":[],"name":"TradingNotPaused","type":"error"},{"inputs":[],"name":"TradingPaused","type":"error"},{"inputs":[],"name":"UNAUTHORIZED","type":"error"},{"inputs":[],"name":"UnderlyingAssetInvalid","type":"error"},{"inputs":[],"name":"WithdrawExceedsLiquidity","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract IAuthority","name":"authority","type":"address"}],"name":"AuthorityUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"series","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"premium","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"escrowReturned","type":"uint256"},{"indexed":false,"internalType":"address","name":"seller","type":"address"}],"name":"BuybackOption","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"DepositEpochExecuted","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"InitiateWithdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Paused","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"int256","name":"deltaChange","type":"int256"}],"name":"RebalancePortfolioDelta","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"Redeem","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"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":"address","name":"closer","type":"address"}],"name":"SettleVault","type":"event"},{"anonymous":false,"inputs":[],"name":"TradingPaused","type":"event"},{"anonymous":false,"inputs":[],"name":"TradingUnpaused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Unpaused","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"shares","type":"uint256"}],"name":"Withdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"epoch","type":"uint256"}],"name":"WithdrawalEpochExecuted","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"series","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"premium","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"escrow","type":"uint256"},{"indexed":false,"internalType":"address","name":"buyer","type":"address"}],"name":"WriteOption","type":"event"},{"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"aboveThresholdGradient","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"aboveThresholdYIntercept","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"lpCollateralDifference","type":"uint256"},{"internalType":"bool","name":"addToLpBalance","type":"bool"}],"name":"adjustCollateral","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"authority","outputs":[{"internalType":"contract IAuthority","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"belowThresholdGradient","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"bidAskIVSpread","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"bufferPercentage","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_handler","type":"address"},{"internalType":"bool","name":"auth","type":"bool"}],"name":"changeHandler","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"checkBuffer","outputs":[{"internalType":"int256","name":"bufferRemaining","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"collateralAllocated","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"collateralAsset","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"collateralCap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"completeWithdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"deposit","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"depositEpoch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"depositEpochPricePerShare","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"depositReceipts","outputs":[{"internalType":"uint128","name":"epoch","type":"uint128"},{"internalType":"uint128","name":"amount","type":"uint128"},{"internalType":"uint256","name":"unredeemedShares","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ephemeralDelta","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ephemeralLiabilities","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"executeEpochCalculation","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getAssets","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"asset","type":"address"}],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getExternalDelta","outputs":[{"internalType":"int256","name":"externalDelta","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getHedgingReactors","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bool","name":"isPut","type":"bool"},{"internalType":"uint256","name":"underlyingPrice","type":"uint256"},{"internalType":"uint256","name":"strikePrice","type":"uint256"},{"internalType":"uint256","name":"expiration","type":"uint256"}],"name":"getImpliedVolatility","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getNAV","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPortfolioDelta","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"handler","outputs":[{"internalType":"bool","name":"","type":"bool"}],"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"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"contract IOptionRegistry","name":"optionRegistry","type":"address"},{"internalType":"address","name":"seriesAddress","type":"address"},{"internalType":"uint256","name":"premium","type":"uint256"},{"internalType":"int256","name":"delta","type":"int256"},{"internalType":"address","name":"seller","type":"address"}],"name":"handlerBuybackOption","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","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":"handlerIssue","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","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"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"premium","type":"uint256"},{"internalType":"int256","name":"delta","type":"int256"},{"internalType":"address","name":"recipient","type":"address"}],"name":"handlerIssueAndWriteOption","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"}],"stateMutability":"nonpayable","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"},{"internalType":"address","name":"seriesAddress","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"contract IOptionRegistry","name":"optionRegistry","type":"address"},{"internalType":"uint256","name":"premium","type":"uint256"},{"internalType":"int256","name":"delta","type":"int256"},{"internalType":"address","name":"recipient","type":"address"}],"name":"handlerWriteOption","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"hedgingReactors","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_shares","type":"uint256"}],"name":"initiateWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"isTradingPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"keeper","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxDiscount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxPriceDeviationThreshold","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxTimeDeviationThreshold","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"optionParams","outputs":[{"internalType":"uint128","name":"minCallStrikePrice","type":"uint128"},{"internalType":"uint128","name":"maxCallStrikePrice","type":"uint128"},{"internalType":"uint128","name":"minPutStrikePrice","type":"uint128"},{"internalType":"uint128","name":"maxPutStrikePrice","type":"uint128"},{"internalType":"uint128","name":"minExpiry","type":"uint128"},{"internalType":"uint128","name":"maxExpiry","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"partitionedFunds","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pauseTradingAndRequest","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"_pause","type":"bool"}],"name":"pauseUnpauseTrading","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingDeposits","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingWithdrawals","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"protocol","outputs":[{"internalType":"contract Protocol","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":"optionSeries","type":"tuple"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bool","name":"toBuy","type":"bool"}],"name":"quotePriceWithUtilizationGreeks","outputs":[{"internalType":"uint256","name":"quote","type":"uint256"},{"internalType":"int256","name":"delta","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"int256","name":"delta","type":"int256"},{"internalType":"uint256","name":"reactorIndex","type":"uint256"}],"name":"rebalancePortfolioDelta","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_shares","type":"uint256"}],"name":"redeem","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_index","type":"uint256"},{"internalType":"bool","name":"_override","type":"bool"}],"name":"removeHedgingReactorAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"resetEphemeralValues","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"riskFreeRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IAuthority","name":"_newAuthority","type":"address"}],"name":"setAuthority","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_bidAskSpread","type":"uint256"}],"name":"setBidAskSpread","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_bufferPercentage","type":"uint256"}],"name":"setBufferPercentage","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_collateralCap","type":"uint256"}],"name":"setCollateralCap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_reactorAddress","type":"address"}],"name":"setHedgingReactorAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_keeper","type":"address"},{"internalType":"bool","name":"_auth","type":"bool"}],"name":"setKeeper","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_maxDiscount","type":"uint256"}],"name":"setMaxDiscount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_maxPriceDeviationThreshold","type":"uint256"}],"name":"setMaxPriceDeviationThreshold","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_maxTimeDeviationThreshold","type":"uint256"}],"name":"setMaxTimeDeviationThreshold","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint128","name":"_newMinCallStrike","type":"uint128"},{"internalType":"uint128","name":"_newMaxCallStrike","type":"uint128"},{"internalType":"uint128","name":"_newMinPutStrike","type":"uint128"},{"internalType":"uint128","name":"_newMaxPutStrike","type":"uint128"},{"internalType":"uint128","name":"_newMinExpiry","type":"uint128"},{"internalType":"uint128","name":"_newMaxExpiry","type":"uint128"}],"name":"setNewOptionParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_riskFreeRate","type":"uint256"}],"name":"setRiskFreeRate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_belowThresholdGradient","type":"uint256"},{"internalType":"uint256","name":"_aboveThresholdGradient","type":"uint256"},{"internalType":"uint256","name":"_utilizationFunctionThreshold","type":"uint256"}],"name":"setUtilizationSkewParams","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"seriesAddress","type":"address"}],"name":"settleVault","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"strikeAsset","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"underlyingAsset","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"utilizationFunctionThreshold","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"withdrawalEpoch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"withdrawalEpochPricePerShare","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"withdrawalReceipts","outputs":[{"internalType":"uint128","name":"epoch","type":"uint128"},{"internalType":"uint128","name":"shares","type":"uint128"}],"stateMutability":"view","type":"function"}]