Cairo Black-Scholes Library
Black-Scholes library implemented as a Cairo smart contract.
All inputs, outputs, and internal calculations use 27-digit fixed-point numbers.
Library
This library implements the interface described here. The external functions exposed allow you to retrieve the Black-Scholes call and put option prices and the option greeks, given the expiration time, volatility, spot price, strike price, and interest rate.
Validation
The pytest deploys the smart contract in a local test environment and queries for the call and put option prices with randomly generated test inputs. Each test input consists of (expiration time, volatility, spot price, strike price, interest rate).
The results are compared against the options prices as computed by the py_vollib library. The test validates that the error from the Cairo library and the py_vollib libary is minimal. Option greeks were validated against online tools such as this one.
Test Instructions
- First make sure you can run a basic StarkNet contract unit test (see here).
- Install py_vollib:
pip install py_vollib
. - Clone this repo and run
pytest black_scholes_test.py
.
SHARP
In the sharp/ folder, you can generate a proof of call option price computation for a given input (input.json
) using the Shared Prover (SHARP) and check that the proof gets verified on-chain by running:
cairo-sharp submit --source black_scholes.cairo --program_input input.json
cairo-sharp status {JOB_KEY}
When the status is "PROCESSED", you can check to see if it is verified on chain.
cairo-sharp is_verified {FACT} --node_url https://goerli-light.eth.linkpool.io/