Skip to main content

The on-chain Black–Scholes engine

contracts/contracts/bs-math.clar prices every option on-chain. All values are 8-decimal fixed point (ONE = u100000000), signed int throughout with 128-bit headroom. Clarity has no floating point and no loops, so each transcendental is range-reduced and unrolled.

Building blocks

  • fp-ln — binary range reduction x = 2^k · m, m ∈ [1, 2) via a fold over constant bit levels (64 … 1), then 2·atanh(u) with 7 unrolled odd terms.
  • fp-expx = n·ln2 + y, y ∈ [0, ln2), 10-term unrolled Taylor (Horner form), 2^n by pow; negatives via 1/e^(−x); clamped at e^66 against overflow.
  • fp-sqrt — exact, via sqrti(x · ONE).
  • fp-normcdf — Abramowitz–Stegun 7.1.26 with the prototype's exact truncated coefficients, so the TypeScript mirror and Clarity agree term-for-term.
  • bs-call-price — guards its domain (ERR-DOMAIN u1001) and returns (ok price).

The pricing call

(bs-call-price (spot int) (strike int) (iv int) (t int) (r int))

spot, strike are 1e8 USD; iv, t (year fraction), and r are 1e8 fixed point. It returns the call premium in 1e8 USD.

Reference vector

The canonical test vector (asserted on devnet, testnet, the TS mirror, and the API):

bs-call-price(
spot = 10421000000000, ;; $104,210
strike = 11463100000000, ;; +10% OTM
iv = 55000000, ;; 0.55
t = 1917808, ;; 1 week as a year-fraction
r = 4000000 ;; 0.04
) = 42848661381 ;; ≈ $428.49 / contract

This 42848661381 value is verified on-chain by the deploy script immediately after publishing — see Deployment.

Parity & cost gates

  • Parity: ≤ 0.05% vs the TypeScript mirror (packages/shared/src/bs.ts) on the reference vectors, and ≤ 0.1% across a 216-point grid (moneyness × vol × tenor × rate).
  • Execution cost: buy-call338k runtime units ≈ 0.007% of a block budget — asserted in contracts/tests/costs.test.ts. Pricing a full Black–Scholes option on-chain is cheap.