Black-Scholes pricing for European options, the full Greek set, and an iterative implied-volatility solver.
Contract: Options.sol
| Function | Gas | Description |
|---|---|---|
| callOptionPrice | 2,729 | European call price (Black-Scholes) |
| putOptionPrice | 2,739 | European put price (Black-Scholes) |
| delta | 1,724 | 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,293 | Time decay, per day — returns (Θcall, Θput) |
| vega | 1,439 | Sensitivity per 1% vol (νcall = νput under put-call parity) |
| impliedVolatility | ~12,370 | 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.