Perp CityDocs

PerpCity Rust SDK

Async Rust SDK for PerpCity perpetual futures, built on Tokio and Alloy.

The PerpCity Rust SDK is an async Rust library for the PerpCity perpetual futures protocol on Arbitrum, built on Tokio and Alloy. Strobe's Legion agent framework uses it as a git dependency.

Versions and current markets

The crates.io release is 0.3.0 (September 2026), and these pages document it. It predates contracts v0.2.2, so on markets created since October 2026:

  • its liquidation helpers send the two-argument call, which those markets reject;
  • it decodes only the earlier close events, so a taker or maker close that succeeds on-chain still returns EventNotFound.

Opening and adjusting positions and reading market state work on all markets. Later releases (up to v0.12.0, which liquidates both builds) are tagged on GitHub but not published to crates.io, and their API differs from these pages. See Ecosystem for how the SDKs compare.

Features

  • Nonce management -- local nonce allocation on an AtomicU64, no RPC round-trip per transaction
  • Read/write endpoint splitting -- route reads and writes to different RPC endpoints, with circuit-breaker failover
  • Multicall3 batching -- fetch balances or a market snapshot in one RPC call
  • Pre-flight simulation -- sends are simulated before broadcast, and reverts decode into typed errors
  • Local swap simulation -- quote taker trades against a block-pinned copy of the pool
  • Maker equity previews -- batched, block-pinned settle previews for open maker positions. get_maker_equities prices at the AMM spot price, not the mark the contracts use, so do not rely on it for liquidation decisions
  • Human-readable API -- f64 prices and margins; the SDK converts to on-chain 6-decimal units

Architecture

LayerDescriptionNetwork Required
Pure MathPrice/tick conversions, position calculations, liquidity math, swap simulation, maker settle mathNo
HFT InfrastructureNonce management, multi-RPC failover, fee/gas caching, latency tracking, tx pipelineNo
Contract InteractionPerpClient (one per market), trading functions, liquidations, Multicall3 batchingYes

Each market is its own Perp contract, identified by its address -- a PerpClient is constructed per market and its methods need no market parameter.

Quick Start

use alloy::primitives::U256;
use alloy::signers::local::PrivateKeySigner;
use perpcity_sdk::prelude::*;

#[tokio::main]
async fn main() -> perpcity_sdk::Result<()> {
    let signer: PrivateKeySigner = "0xYOUR_PRIVATE_KEY".parse()?;

    let transport = HftTransport::new(
        TransportConfig::builder()
            .shared_endpoint("https://arb-mainnet.g.alchemy.com/v2/KEY")
            .read_endpoint("https://arbitrum-one-rpc.publicnode.com")
            .build()?,
    )?;

    let deployments = Deployments {
        perp: "0xYOUR_PERP".parse()?,
        usdc: ARBITRUM_USDC,
        pool_manager: ARBITRUM_POOL_MANAGER,
    };

    let client = PerpClient::new_arbitrum(transport, signer, deployments)?;
    client.sync_nonce().await?;
    client.refresh_gas().await?;
    client.ensure_approval(U256::MAX).await?;

    // Fetch config + live market data in 2 multicalls
    let (config, snapshot) = client.get_perp_snapshot().await?;
    println!("Mark: {:.2}, Index: {:.2}", snapshot.mark_price, snapshot.index_price);

    // Open a long: 100 USDC margin, +1 perp of exposure
    let result = client.open_taker(&OpenTakerParams {
        margin: 100.0,
        perp_delta: 1.0,     // positive = long, negative = short
        amt1_limit: 0,       // slippage limit, 0 = no limit
    }, Urgency::Normal).await?;

    println!("Position opened: {}", result.pos_id);

    // Close it (pass the position's current perp delta)
    let closed = client
        .close_taker(result.pos_id, result.perp_delta, Urgency::Normal)
        .await?;
    println!("Closed: perp {:+.6}, usd {:+.2}", closed.perp_delta, closed.usd_delta);

    Ok(())
}

Guides

Resources

On this page