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 reductionx = 2^k · m,m ∈ [1, 2)via afoldover constant bit levels (64 … 1), then2·atanh(u)with 7 unrolled odd terms.fp-exp—x = n·ln2 + y,y ∈ [0, ln2), 10-term unrolled Taylor (Horner form),2^nbypow; negatives via1/e^(−x); clamped ate^66against overflow.fp-sqrt— exact, viasqrti(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-call≈ 338k runtime units ≈ 0.007% of a block budget — asserted incontracts/tests/costs.test.ts. Pricing a full Black–Scholes option on-chain is cheap.