Black-Scholes pricing for European options, the full Greek set, and an iterative implied-volatility solver.
Contract: Options.sol
| Function | Gas | Description |
|---|---|---|
| callOptionPrice | 2,876 | European call price (Black-Scholes) |
| putOptionPrice | 2,887 | European put price (Black-Scholes) |
| delta | 1,797 | First derivative w.r.t. spot — returns (Δcall, Δput) |
| gamma | 1,499 | Second derivative w.r.t. spot (Γcall = Γput under put-call parity) |
| theta | 3,441 | Time decay, per day — returns (Θcall, Θput) |
| vega | 1,439 | Sensitivity per 1% vol (νcall = νput under put-call parity) |
| impliedVolatility | ~13,100 | IV solver via Newton-Raphson |
npm install defimath-lib
spot, strike — uint128, 18-decimal fixed-point (1e18 = 1.0).timeToExp — uint32, seconds to expiration.volatility — uint64, annualized vol as 18-decimal fixed-point (e.g. 50% → 5e17).rate — uint64, annualized risk-free rate as 18-decimal fixed-point.internal pure.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);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.| Constant | Value |
|---|---|
| MIN_SPOT | 1e-6 (smallest allowed spot price) |
| MAX_SPOT | 1e15 (largest allowed spot price) |
| MAX_STSP_RATIO | 5× (strike must be within [spot/5, spot·5]) |
| MAX_EXPIRATION | 2 years (63,072,000 seconds) |
| MAX_RATE | 400% annual (4e18) |
| Error | Trigger |
|---|---|
| SpotLowerBoundError | spot ≤ MIN_SPOT |
| SpotUpperBoundError | spot ≥ MAX_SPOT |
| StrikeLowerBoundError | strike · 5 < spot |
| StrikeUpperBoundError | spot · 5 < strike |
| TimeToExpiryUpperBoundError | timeToExp ≥ MAX_EXPIRATION |
| TimeToExpiryLowerBoundError | impliedVolatility when timeToExp == 0 |
| RateUpperBoundError | rate ≥ MAX_RATE |
| PriceOutOfBoundsError | impliedVolatility when market price is outside the no-arbitrage band |
| NoConvergenceError | impliedVolatility when Newton-Raphson fails to converge |