> ## Documentation Index
> Fetch the complete documentation index at: https://ouroborai.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy Contracts

> Deploy Stylus contracts to Arbitrum Sepolia

ouroborai includes four Stylus (Rust) contracts compiled to WASM for
10-100x gas savings on computation-heavy operations. This guide covers
deploying them to Arbitrum Sepolia for testing.

## Prerequisites

* **Rust** toolchain with `wasm32-unknown-unknown` target
* **cargo-stylus** CLI (`cargo install cargo-stylus`)
* **cast** from Foundry for contract interaction
* A funded Arbitrum Sepolia wallet (ETH for gas)

<Note>
  Get Sepolia ETH from the [Arbitrum Sepolia faucet](https://faucet.arbitrum.io)
  or bridge from Ethereum Sepolia via the [Arbitrum bridge](https://bridge.arbitrum.io).
</Note>

## Contracts Overview

| Contract              | Purpose                                           | Tests |
| --------------------- | ------------------------------------------------- | ----- |
| `agent-registry`      | On-chain agent registry with capabilities bitmask | 7     |
| `liquidation-monitor` | Multi-protocol lending health scanner             | 43    |
| `route-optimizer`     | Multi-DEX route comparison engine                 | 38    |
| `timeboost-vault`     | Bid funding and express lane resale payments      | 27    |

## Deployment

<Steps>
  <Step title="Run tests locally">
    Before deploying, confirm all 115 Rust tests pass:

    ```bash theme={"theme":{"light":"dracula","dark":"dracula"}}
    cargo test --features stylus-test \
      --manifest-path contracts/Cargo.toml
    ```

    All four contracts share a workspace `Cargo.toml` and use the
    `stylus-test` feature flag for test utilities.
  </Step>

  <Step title="Validate with cargo-stylus">
    Check that the contract compiles to valid Stylus WASM. Run this for each
    contract:

    <CodeGroup>
      ```bash agent-registry theme={"theme":{"light":"dracula","dark":"dracula"}}
      cargo stylus check \
        --manifest-path contracts/agent-registry/Cargo.toml \
        --endpoint https://sepolia-rollup.arbitrum.io/rpc
      ```

      ```bash liquidation-monitor theme={"theme":{"light":"dracula","dark":"dracula"}}
      cargo stylus check \
        --manifest-path contracts/liquidation-monitor/Cargo.toml \
        --endpoint https://sepolia-rollup.arbitrum.io/rpc
      ```

      ```bash route-optimizer theme={"theme":{"light":"dracula","dark":"dracula"}}
      cargo stylus check \
        --manifest-path contracts/route-optimizer/Cargo.toml \
        --endpoint https://sepolia-rollup.arbitrum.io/rpc
      ```

      ```bash timeboost-vault theme={"theme":{"light":"dracula","dark":"dracula"}}
      cargo stylus check \
        --manifest-path contracts/timeboost-vault/Cargo.toml \
        --endpoint https://sepolia-rollup.arbitrum.io/rpc
      ```
    </CodeGroup>

    Each check validates the WASM binary size, ABI compatibility, and
    entrypoint structure against the Stylus runtime.
  </Step>

  <Step title="Deploy each contract">
    Deploy using `cargo stylus deploy`. You need a private key with Sepolia
    ETH:

    <CodeGroup>
      ```bash agent-registry theme={"theme":{"light":"dracula","dark":"dracula"}}
      cargo stylus deploy \
        --manifest-path contracts/agent-registry/Cargo.toml \
        --endpoint https://sepolia-rollup.arbitrum.io/rpc \
        --private-key 0xYOUR_PRIVATE_KEY
      ```

      ```bash liquidation-monitor theme={"theme":{"light":"dracula","dark":"dracula"}}
      cargo stylus deploy \
        --manifest-path contracts/liquidation-monitor/Cargo.toml \
        --endpoint https://sepolia-rollup.arbitrum.io/rpc \
        --private-key 0xYOUR_PRIVATE_KEY
      ```

      ```bash route-optimizer theme={"theme":{"light":"dracula","dark":"dracula"}}
      cargo stylus deploy \
        --manifest-path contracts/route-optimizer/Cargo.toml \
        --endpoint https://sepolia-rollup.arbitrum.io/rpc \
        --private-key 0xYOUR_PRIVATE_KEY
      ```

      ```bash timeboost-vault theme={"theme":{"light":"dracula","dark":"dracula"}}
      cargo stylus deploy \
        --manifest-path contracts/timeboost-vault/Cargo.toml \
        --endpoint https://sepolia-rollup.arbitrum.io/rpc \
        --private-key 0xYOUR_PRIVATE_KEY
      ```
    </CodeGroup>

    Each command outputs the deployed contract address. Save these addresses
    for the initialization step.
  </Step>

  <Step title="Initialize deployed contracts">
    After deployment, each contract must be initialized. Use `cast send` from
    Foundry:

    <CodeGroup>
      ```bash agent-registry theme={"theme":{"light":"dracula","dark":"dracula"}}
      # AgentRegistry needs governance set
      cast send AGENT_REGISTRY_ADDRESS \
        "set_governance(address)" YOUR_GOV_ADDRESS \
        --rpc-url https://sepolia-rollup.arbitrum.io/rpc \
        --private-key 0xYOUR_PRIVATE_KEY
      ```

      ```bash liquidation-monitor theme={"theme":{"light":"dracula","dark":"dracula"}}
      # LiquidationMonitor needs risk threshold (1.1e18 = health factor 1.1)
      cast send LIQUIDATION_MONITOR_ADDRESS \
        "initialize(uint256)" 1100000000000000000 \
        --rpc-url https://sepolia-rollup.arbitrum.io/rpc \
        --private-key 0xYOUR_PRIVATE_KEY
      ```

      ```bash route-optimizer theme={"theme":{"light":"dracula","dark":"dracula"}}
      # RouteOptimizer auto-initializes with WETH and USDC routing tokens
      cast send ROUTE_OPTIMIZER_ADDRESS \
        "initialize()" \
        --rpc-url https://sepolia-rollup.arbitrum.io/rpc \
        --private-key 0xYOUR_PRIVATE_KEY
      ```

      ```bash timeboost-vault theme={"theme":{"light":"dracula","dark":"dracula"}}
      # TimeBoostVault needs USDC address, bid agent, and resale price
      cast send TIMEBOOST_VAULT_ADDRESS \
        "initialize(address,address,uint256)" \
        USDC_ADDRESS BID_AGENT_ADDRESS 5000000 \
        --rpc-url https://sepolia-rollup.arbitrum.io/rpc \
        --private-key 0xYOUR_PRIVATE_KEY
      ```
    </CodeGroup>

    <Warning>
      Each contract can only be initialized once. Calling `initialize` a second
      time reverts with "already initialized".
    </Warning>
  </Step>

  <Step title="Register protocols (LiquidationMonitor and RouteOptimizer)">
    After initialization, register the protocols and DEXes each contract should
    monitor:

    ```bash theme={"theme":{"light":"dracula","dark":"dracula"}}
    # Add Aave V3 to the liquidation monitor (type 0 = Aave V3)
    cast send LIQUIDATION_MONITOR_ADDRESS \
      "add_lending_protocol(address,uint64)" \
      AAVE_V3_POOL_ADDRESS 0 \
      --rpc-url https://sepolia-rollup.arbitrum.io/rpc \
      --private-key 0xYOUR_PRIVATE_KEY

    # Add Uniswap V3 Quoter to route optimizer (type 0 = UniV3)
    cast send ROUTE_OPTIMIZER_ADDRESS \
      "add_dex(address,uint64)" \
      UNISWAP_V3_QUOTER_ADDRESS 0 \
      --rpc-url https://sepolia-rollup.arbitrum.io/rpc \
      --private-key 0xYOUR_PRIVATE_KEY
    ```
  </Step>

  <Step title="Verify with cast call">
    Confirm the contracts are functioning:

    ```bash theme={"theme":{"light":"dracula","dark":"dracula"}}
    # Check total registered agents
    cast call AGENT_REGISTRY_ADDRESS \
      "total_agents()" \
      --rpc-url https://sepolia-rollup.arbitrum.io/rpc

    # Check lending protocol count
    cast call LIQUIDATION_MONITOR_ADDRESS \
      "lending_protocol_count()" \
      --rpc-url https://sepolia-rollup.arbitrum.io/rpc

    # Check DEX count
    cast call ROUTE_OPTIMIZER_ADDRESS \
      "dex_count()" \
      --rpc-url https://sepolia-rollup.arbitrum.io/rpc
    ```
  </Step>
</Steps>

## Deploying to Mainnet

The process is identical for Arbitrum One mainnet. Replace the RPC endpoint:

```
--endpoint https://arb1.arbitrum.io/rpc
```

After mainnet deployment, update the contract addresses in
`packages/sdk/src/chain.ts` to wire them into the TypeScript adapters.

<Tip>
  Store deployed contract addresses in a shared configuration file or
  environment variables so all services reference the same deployment.
</Tip>
