Binary options

Cash-or-nothing binary options. The call pays 1 if spot > strike at expiry, otherwise 0; the put is symmetric. Greek functions return both call and put values in a single call.

Contract: BinaryOptions.sol

Functions

FunctionGasDescription
call1,913Cash-or-nothing call: e^(−r·τ) · Φ(d₂)
put1,918Cash-or-nothing put
delta1,717First derivative w.r.t. spot — returns (Δcall, Δput)
gamma1,859Second derivative w.r.t. spot — returns (Γcall, Γput)
theta3,161Time decay, per day — returns (Θcall, Θput)
vega1,805Sensitivity per 1% vol — returns (νcall, νput)

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.
  • Unit payout. All results assume a payout of 1. Multiply the result externally for an arbitrary payout Q.
  • All functions are internal pure.

Quick example

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

uint256 binCall = BinaryOptions.call(spot, strike, timeToExp, vol, rate);
uint256 binPut  = BinaryOptions.put (spot, strike, timeToExp, vol, rate);

// All binary Greeks return (call, put) tuples.
(int128 dC, int128 dP) = BinaryOptions.delta(spot, strike, timeToExp, vol, rate);

Important notes

  • All four Greeks return tuples. Unlike vanilla options (where gamma and vega are equal for call and put under put-call parity), binary call and put have different second-order sensitivities — so all of delta, gamma, theta, and vega return (call, put).
  • Unit payout — scale externally. To price a digital with payout Q, compute the unit-payout price and multiply by Q on the call site.
  • theta is per day. The result is the price change for a one-day decrease in time to expiration.
  • vega is per 1% vol. The result is the price change for a 1-percentage-point change in volatility.
  • When to use binary vs. vanilla. Use binaries when the payout is discrete (prediction markets, depeg coverage, threshold hedges). For continuous payoff structures, reach for the Black-Scholes module.

Every function reverts on out-of-bounds inputs with a named error — see the per-function pages for limits and error specifics.

Testing

Hardhat correctness layer. 109 tests across 6 function groups (binary call, put, delta, gamma, theta, vega). Validated against a JavaScript reference derived from the closed-form cash-or-nothing pricing equations over 5×5×3×3 strike/time/vol/rate matrices. Limits-and-near-limits sweeps probe all four parameter boundaries; failure tests cover every documented revert path.

Foundry property-fuzz layer. 13 mathematical properties × 32,000 random runs each = 416,000 random executions per CI run.

CategoryCountWhat they check
Monotonicity4binary call ↑ in spot, put ↓ in spot, call ↓ in strike, put ↑ in strike
Identities3binary put-call parity (BC + BP = e−rT), δcall + δput = 0, θcall + θput = r·e−rT/365
Output bounds4BC ∈ [0, 1], BP ∈ [0, 1], δcall ≥ 0, δput ≤ 0
Symmetries2γcall = −γput, νcall = −νput (unique to binary — BC+BP is constant in spot and vol)

Sources: test/hardhat/BinaryOptions.test.mjs · test/foundry/BinaryOptions.t.sol