Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Example Contracts

Keystone includes five Stylus example contracts demonstrating precision arithmetic for different DeFi use cases.

Deployed Contracts (Arbitrum One)

ContractAddressArbiscan
stylus-lending0x4dff9348275ac3c24e2d3abf54af61d3ebee1585View
stylus-amm0x9615cc2f65d8bbe4cdc80343db75a6ec32da93cdView
stylus-vault0xdaf8f1a5f8025210f07665d4ccf2d2c0622a41faView

stylus-lending

Location: examples/stylus-lending/

Lending protocol calculations using Keystone for deterministic risk assessment.

Functions

FunctionDescription
calculate_health_factor(Collateral × Threshold) / Debt
calculate_liquidation_priceDebt / (Collateral × Threshold)
calculate_max_borrowMaximum borrowable given collateral
is_liquidatableCheck if health factor < 1
calculate_liquidation_amountsDebt coverage with liquidation bonus

Example Usage

#![allow(unused)]
fn main() {
// Health factor calculation
// Collateral: $10,000, Debt: $5,000, Threshold: 80%
// HF = (10000 * 0.8) / 5000 = 1.6

let hf = contract.calculate_health_factor(
    U256::from(10000) * U256::from(10).pow(U256::from(18)), // collateral
    U256::from(5000) * U256::from(10).pow(U256::from(18)),  // debt
)?;
// Returns 1.6e18
}

Target Protocols

  • Aave-style lending markets
  • Radiant, Lodestar on Arbitrum
  • Any protocol using health factor for liquidation

stylus-amm

Location: examples/stylus-amm/

Constant product AMM (x*y=k) calculations for decentralized exchanges.

Functions

FunctionDescription
calculate_swap_outputOutput amount for given input
calculate_price_impactPrice impact as percentage
calculate_swap_inputRequired input for desired output
calculate_spot_priceCurrent pool price
calculate_liquidity_mintLP tokens for deposit
calculate_liquidity_burnAssets returned for LP redemption

Example Usage

#![allow(unused)]
fn main() {
// Swap calculation with 0.3% fee
// Pool: 1000 ETH / 2,000,000 USDC
// Swap 10 ETH for USDC

contract.set_fee(U256::from(30)); // 30 bps = 0.3%

let output = contract.calculate_swap_output(
    U256::from(1000) * SCALE,     // reserve_in (ETH)
    U256::from(2000000) * SCALE,  // reserve_out (USDC)
    U256::from(10) * SCALE,       // amount_in
)?;
// Returns ~19,841 USDC (with fee and slippage)
}

Target Protocols

  • Uniswap V2-style AMMs
  • Camelot, Trader Joe on Arbitrum
  • Any constant product DEX

stylus-vault

Location: examples/stylus-vault/

ERC4626-compatible vault calculations for yield aggregators.

Functions

FunctionDescription
calculate_shares_for_depositShares to mint for deposit
calculate_assets_for_redeemAssets returned for share redemption
calculate_share_priceCurrent price per share
calculate_compound_yieldCompounded return over periods
calculate_apy_from_aprConvert APR to APY
calculate_performance_feeFee on gains
calculate_management_feeTime-based fee
calculate_net_asset_valueNAV per share

Example Usage

#![allow(unused)]
fn main() {
// Calculate shares for $1000 deposit
// Vault has $100,000 total assets, 95,000 shares outstanding

let shares = contract.calculate_shares_for_deposit(
    U256::from(1000) * SCALE,    // deposit amount
    U256::from(100000) * SCALE,  // total assets
    U256::from(95000) * SCALE,   // total supply
)?;
// Returns 950 shares (share price = 100000/95000 ≈ 1.0526)
}
#![allow(unused)]
fn main() {
// Calculate APY from 5% APR with daily compounding

let apy_bps = contract.calculate_apy_from_apr(
    U256::from(500) * SCALE,  // 500 bps = 5% APR
    U256::from(365),          // daily compounding
)?;
// Returns ~512.67 bps = 5.1267% APY
}

Target Protocols

  • ERC4626 vaults
  • Yearn-style yield aggregators
  • Pendle, Jones DAO on Arbitrum

stylus-options

Location: examples/stylus-options/

On-chain Black-Scholes options pricing with 128-bit decimal precision.

Functions

FunctionDescription
price_callEuropean call option price (Black-Scholes)
price_putEuropean put option price
call_option_greeksDelta, Gamma, Theta, Vega, Rho for calls
put_option_greeksGreeks for puts
calculate_ivImplied volatility from market price
put_call_parity_checkVerify C - P = S - Ke^(-rT)

Target Protocols

  • Dopex (Arbitrum options)
  • Rysk Finance (options AMM)
  • Any protocol requiring on-chain exp(), ln(), sqrt() for BSM pricing

stylus-oracle

Location: examples/stylus-oracle/

RedStone oracle integration for price-aware DeFi calculations.

Functions

FunctionDescription
calculate_health_factor_with_pricesHealth factor using live oracle prices
calculate_liquidation_price_with_oracleOracle-based liquidation trigger
calculate_max_borrow_with_pricesMax borrow with real-time pricing
is_liquidatable_with_pricesLiquidation status with live data
calculate_twapTime-weighted average price
calculate_price_deviationAnomaly detection vs median

Target Protocols

  • Any DeFi protocol using RedStone pull-based oracles on Arbitrum
  • Cross-VM interoperability between WASM and EVM contracts

Building Examples

# Build all examples
cd examples/stylus-lending && cargo build --release
cd ../stylus-amm && cargo build --release
cd ../stylus-vault && cargo build --release

Deploying Examples

cd examples/stylus-lending
cargo stylus deploy \
  --private-key-path=../../key.txt \
  --endpoint=https://sepolia-rollup.arbitrum.io/rpc

Replace with your private key path and desired network.