OptionMath

Git Source

State Variables

ALPHA

SD59x18 internal constant ALPHA = SD59x18.wrap(-6.37309208e18);

LAMBDA

SD59x18 internal constant LAMBDA = SD59x18.wrap(-0.61228883e18);

S1

SD59x18 internal constant S1 = SD59x18.wrap(-0.11105481e18);

S2

SD59x18 internal constant S2 = SD59x18.wrap(0.44334159e18);

SQRT_2PI

int256 internal constant SQRT_2PI = 2_506628274631000502;

MIN_INPUT_PRICE

UD60x18 internal constant MIN_INPUT_PRICE = UD60x18.wrap(1e1);

MAX_INPUT_PRICE

UD60x18 internal constant MAX_INPUT_PRICE = UD60x18.wrap(1e34);

Functions

helperNormal

Helper function to evaluate used to compute the normal CDF approximation

function helperNormal(SD59x18 x) internal pure returns (SD59x18 result);

Parameters

NameTypeDescription
xSD59x18The input to the normal CDF (18 decimals)

Returns

NameTypeDescription
resultSD59x18The value of the evaluated helper function (18 decimals)

normalCdf

Approximation of the normal CDF

The approximation implemented is based on the paper 'Accurate RMM-Based Approximations for the CDF of the Normal Distribution' by Haim Shore

function normalCdf(SD59x18 x) internal pure returns (SD59x18 result);

Parameters

NameTypeDescription
xSD59x18input value to evaluate the normal CDF on, F(Z<=x) (18 decimals)

Returns

NameTypeDescription
resultSD59x18The normal CDF evaluated at x (18 decimals)

normalPdf

Normal Distribution Probability Density Function.

Equal to Z(x) = (1 / σ√2π)e^( (-(x - µ)^2) / 2σ^2 ). Only computes pdf of a distribution with µ = 0 and σ = 1.

function normalPdf(SD59x18 x) internal pure returns (SD59x18 z);

Parameters

NameTypeDescription
xSD59x18Number to get PDF for (18 decimals)

Returns

NameTypeDescription
zSD59x18z-number (18 decimals)

relu

Implementation of the ReLu function f(x)=(x)^+ to compute call / put payoffs

function relu(SD59x18 x) internal pure returns (UD60x18);

Parameters

NameTypeDescription
xSD59x18Input value (18 decimals)

Returns

NameTypeDescription
<none>UD60x18result Output of the relu function (18 decimals)

d1d2

Returns the terms d1 and d2 from the Black-Scholes formula that are used to compute the price of a call / put option.

function d1d2(UD60x18 spot, UD60x18 strike, UD60x18 timeToMaturity, UD60x18 volAnnualized, UD60x18 riskFreeRate)
    internal
    pure
    returns (SD59x18 d1, SD59x18 d2);

Parameters

NameTypeDescription
spotUD60x18The spot price. (18 decimals)
strikeUD60x18The strike price of the option. (18 decimals)
timeToMaturityUD60x18The time until the option expires. (18 decimals)
volAnnualizedUD60x18The percentage volatility of the geometric Brownian motion. (18 decimals)
riskFreeRateUD60x18The rate of the risk-less asset, i.e. the risk-free interest rate. (18 decimals)

Returns

NameTypeDescription
d1SD59x18The term d1 from the Black-Scholes formula. (18 decimals)
d2SD59x18The term d2 from the Black-Scholes formula. (18 decimals)

optionDelta

Calculate option delta

function optionDelta(
    UD60x18 spot,
    UD60x18 strike,
    UD60x18 timeToMaturity,
    UD60x18 volAnnualized,
    UD60x18 riskFreeRate,
    bool isCall
) internal pure returns (SD59x18);

Parameters

NameTypeDescription
spotUD60x18Spot price
strikeUD60x18Strike price
timeToMaturityUD60x18Duration of option contract (in years)
volAnnualizedUD60x18Annualized volatility
riskFreeRateUD60x18
isCallboolwhether to price "call" or "put" option

Returns

NameTypeDescription
<none>SD59x18price Option delta

blackScholesPrice

Calculate the price of an option using the Black-Scholes model

this implementation assumes zero interest

function blackScholesPrice(
    UD60x18 spot,
    UD60x18 strike,
    UD60x18 timeToMaturity,
    UD60x18 volAnnualized,
    UD60x18 riskFreeRate,
    bool isCall
) internal pure returns (UD60x18);

Parameters

NameTypeDescription
spotUD60x18Spot price (18 decimals)
strikeUD60x18Strike price (18 decimals)
timeToMaturityUD60x18Duration of option contract (in years) (18 decimals)
volAnnualizedUD60x18Annualized volatility (18 decimals)
riskFreeRateUD60x18The risk-free rate (18 decimals)
isCallboolwhether to price "call" or "put" option

Returns

NameTypeDescription
<none>UD60x18price The Black-Scholes option price (18 decimals)

is8AMUTC

Returns true if the maturity time is 8AM UTC

function is8AMUTC(uint256 maturity) internal pure returns (bool);

Parameters

NameTypeDescription
maturityuint256The maturity timestamp of the option

Returns

NameTypeDescription
<none>boolTrue if the maturity time is 8AM UTC, false otherwise

isFriday

Returns true if the maturity day is Friday

function isFriday(uint256 maturity) internal pure returns (bool);

Parameters

NameTypeDescription
maturityuint256The maturity timestamp of the option

Returns

NameTypeDescription
<none>boolTrue if the maturity day is Friday, false otherwise

isLastFriday

Returns true if the maturity day is the last Friday of the month

function isLastFriday(uint256 maturity) internal pure returns (bool);

Parameters

NameTypeDescription
maturityuint256The maturity timestamp of the option

Returns

NameTypeDescription
<none>boolTrue if the maturity day is the last Friday of the month, false otherwise

calculateTimeToMaturity

Calculates the time to maturity in seconds

function calculateTimeToMaturity(uint256 maturity) internal view returns (uint256);

Parameters

NameTypeDescription
maturityuint256The maturity timestamp of the option

Returns

NameTypeDescription
<none>uint256Time to maturity in seconds

calculateStrikeInterval

Calculates the strike interval for strike

function calculateStrikeInterval(UD60x18 strike) internal pure returns (UD60x18);

Parameters

NameTypeDescription
strikeUD60x18The price to calculate strike interval for (18 decimals)

Returns

NameTypeDescription
<none>UD60x18The strike interval (18 decimals)

roundToStrikeInterval

Rounds strike using the calculated strike interval

function roundToStrikeInterval(UD60x18 strike) internal pure returns (UD60x18);

Parameters

NameTypeDescription
strikeUD60x18The price to round (18 decimals)

Returns

NameTypeDescription
<none>UD60x18The rounded strike price (18 decimals)

scaleDecimals

Converts a number with inputDecimals, to a number with given amount of decimals

function scaleDecimals(uint256 value, uint8 inputDecimals, uint8 targetDecimals) internal pure returns (uint256);

Parameters

NameTypeDescription
valueuint256The value to convert
inputDecimalsuint8The amount of decimals the input value has
targetDecimalsuint8The amount of decimals to convert to

Returns

NameTypeDescription
<none>uint256The converted value

scaleDecimals

Converts a number with inputDecimals, to a number with given amount of decimals

function scaleDecimals(int256 value, uint8 inputDecimals, uint8 targetDecimals) internal pure returns (int256);

Parameters

NameTypeDescription
valueint256The value to convert
inputDecimalsuint8The amount of decimals the input value has
targetDecimalsuint8The amount of decimals to convert to

Returns

NameTypeDescription
<none>int256The converted value

fromTokenDecimals

Converts a number with decimals, to a UD60x18 type

function fromTokenDecimals(uint256 value, uint8 decimals) internal pure returns (UD60x18);

Parameters

NameTypeDescription
valueuint256The value to convert
decimalsuint8The amount of decimals the value has

Returns

NameTypeDescription
<none>UD60x18The number as a UD60x18

toTokenDecimals

Converts a UD60x18 number with decimals, to it's uint256 type scaled down.

function toTokenDecimals(UD60x18 value, uint8 decimals) internal pure returns (uint256);

Parameters

NameTypeDescription
valueUD60x18The value to convert
decimalsuint8The amount of decimals the value has

Returns

NameTypeDescription
<none>uint256The number as a scaled down uint256

truncate

Truncates a UD60x18 number down to the correct precision.

function truncate(UD60x18 value, uint8 decimals) internal pure returns (UD60x18);

Parameters

NameTypeDescription
valueUD60x18The value to convert
decimalsuint8The amount of decimals the value has

Returns

NameTypeDescription
<none>UD60x18The truncated UD60x18 number

log10Floor

Performs a naive log10 calculation on input returning the floor of the result

function log10Floor(uint256 input) internal pure returns (uint256 count);

computeCLevel

Calculates the C-level given a utilisation value and time since last trade value (duration). (https://www.desmos.com/calculator/0uzv50t7jy)

function computeCLevel(
    UD60x18 utilisation,
    UD60x18 duration,
    UD60x18 alpha,
    UD60x18 minCLevel,
    UD60x18 maxCLevel,
    UD60x18 decayRate
) internal pure returns (UD60x18);

Parameters

NameTypeDescription
utilisationUD60x18The utilisation after some collateral is utilised
durationUD60x18The time since last trade (hours)
alphaUD60x18(needs to be filled in)
minCLevelUD60x18The minimum C-level
maxCLevelUD60x18The maximum C-level
decayRateUD60x18The decay rate of the C-level back down to minimum level (decay/hour)

Returns

NameTypeDescription
<none>UD60x18The C-level corresponding to the post-utilisation value.

computeCLevelGeoMean

Calculates the geo-mean C-level given a utilisation before and after collateral is utilised.

function computeCLevelGeoMean(
    UD60x18 utilisationBefore,
    UD60x18 utilisationAfter,
    UD60x18 duration,
    UD60x18 alpha,
    UD60x18 minCLevel,
    UD60x18 maxCLevel,
    UD60x18 decayRate
) internal pure returns (UD60x18 cLevel);

Parameters

NameTypeDescription
utilisationBeforeUD60x18The utilisation before some collateral is utilised.
utilisationAfterUD60x18The utilisation after some collateral is utilised
durationUD60x18The time since last trade (hours)
alphaUD60x18(needs to be filled in)
minCLevelUD60x18The minimum C-level
maxCLevelUD60x18The maximum C-level
decayRateUD60x18The decay rate of the C-level back down to minimum level (decay/hour)

Returns

NameTypeDescription
cLevelUD60x18The C-level corresponding to the geo-mean of the utilisation value before and after collateral is utilised.

Errors

OptionMath__NonPositiveVol

error OptionMath__NonPositiveVol();

OptionMath__OutOfBoundsPrice

error OptionMath__OutOfBoundsPrice(UD60x18 min, UD60x18 max, UD60x18 price);

OptionMath__Underflow

error OptionMath__Underflow();

OptionMath__UtilisationOutOfBounds

error OptionMath__UtilisationOutOfBounds();

Structs

BlackScholesPriceVarsInternal

struct BlackScholesPriceVarsInternal {
    int256 discountFactor;
    int256 timeScaledVol;
    int256 timeScaledVar;
    int256 timeScaledRiskFreeRate;
}