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 three production-ready Stylus example contracts demonstrating precision arithmetic for different DeFi use cases.

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

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.