Options

Black-Scholes pricing for European options, the full Greek set, and an iterative implied-volatility solver.

Contract: Options.sol

Functions

FunctionGasDescription
callOptionPrice2,876European call price (Black-Scholes)
putOptionPrice2,887European put price (Black-Scholes)
delta1,797First derivative w.r.t. spot — returns (Δcall, Δput)
gamma1,499Second derivative w.r.t. spot (Γcall = Γput under put-call parity)
theta3,441Time decay, per day — returns (Θcall, Θput)
vega1,439Sensitivity per 1% vol (νcall = νput under put-call parity)
impliedVolatility~13,100IV solver via Newton-Raphson

npm install defimath-lib

Conventions

  • spot, strikeuint128, 18-decimal fixed-point (1e18 = 1.0).
  • timeToExpuint32, seconds to expiration.
  • volatilityuint64, annualized vol as 18-decimal fixed-point (e.g. 50% → 5e17).
  • rateuint64, annualized risk-free rate as 18-decimal fixed-point.
  • All functions are internal pure.

Quick example

solidity
import "defimath-lib/contracts/derivatives/Options.sol";

uint256 callPx = DeFiMathOptions.callOptionPrice(spot, strike, timeToExp, vol, rate);
uint256 putPx  = DeFiMathOptions.putOptionPrice (spot, strike, timeToExp, vol, rate);

// delta and theta return (call, put) tuples.
(int128 dC, int128 dP) = DeFiMathOptions.delta(spot, strike, timeToExp, vol, rate);

// gamma and vega return a single value (equal for call and put under put-call parity).
uint256 g = DeFiMathOptions.gamma(spot, strike, timeToExp, vol, rate);

Important notes

  • delta and theta return tuples; gamma and vega return scalars. For delta / theta, a single normal-CDF evaluation is amortized across both call and put — ~halves gas vs. two separate calls. gamma and vega are identical for call and put under put-call parity, so they return a single value.
  • theta is per day. The result is the price change for a one-day decrease in time to expiration, not per-second.
  • vega is per 1% vol change. The result is the price change for a 1-percentage-point change in volatility (Δσ = 0.01).
  • impliedVolatility requires market price within no-arb band. The passed market price must lie within [max(S − K·e−r·T, 0), S] for calls (analogous for puts), otherwise the solver reverts. Typical convergence is 4–6 Newton-Raphson iterations.

Limits & errors

ConstantValue
MIN_SPOT1e-6 (smallest allowed spot price)
MAX_SPOT1e15 (largest allowed spot price)
MAX_STSP_RATIO5× (strike must be within [spot/5, spot·5])
MAX_EXPIRATION2 years (63,072,000 seconds)
MAX_RATE400% annual (4e18)
ErrorTrigger
SpotLowerBoundErrorspot ≤ MIN_SPOT
SpotUpperBoundErrorspot ≥ MAX_SPOT
StrikeLowerBoundErrorstrike · 5 < spot
StrikeUpperBoundErrorspot · 5 < strike
TimeToExpiryUpperBoundErrortimeToExp ≥ MAX_EXPIRATION
TimeToExpiryLowerBoundErrorimpliedVolatility when timeToExp == 0
RateUpperBoundErrorrate ≥ MAX_RATE
PriceOutOfBoundsErrorimpliedVolatility when market price is outside the no-arbitrage band
NoConvergenceErrorimpliedVolatility when Newton-Raphson fails to converge