Expand description
§PerpCity Rust SDK
A Rust SDK for the PerpCity perpetual futures protocol on Base L2.
§Module overview
| Module | Purpose |
|---|---|
constants | Protocol constants mirrored from on-chain Constants.sol |
contracts | ABI bindings via Alloy sol! — structs, events, errors, functions |
convert | Conversions between client f64 values and on-chain representations |
errors | SDK-wide error types using thiserror |
events | Event decoding: raw logs → typed MarketEvent values |
feed | Live market event feed over WebSocket |
hft | HFT infrastructure: nonce, gas, pipeline, state cache, latency, positions |
math | Pure math: tick ↔ price, liquidity estimation, position calculations |
transport | Multi-endpoint RPC transport with health-aware routing |
types | Client-facing types with human-readable f64 fields |
§Quick start
use perpcity_sdk::{
PerpClient, HftTransport, TransportConfig, Urgency,
Deployments, OpenTakerParams, OpenMakerParams,
PerpCityError, Result,
};
use alloy::signers::local::PrivateKeySigner;Modules§
- IBeacon
- Beacon interface — emits
IndexUpdatedwhen the oracle index changes. Each perp has its own beacon (fromPerpConfig.beacon). - IERC20
- Minimal ERC20 interface for USDC interactions.
- IFees
- Fee module — returns trading fees for a perp.
- IMargin
Ratios - Margin ratio module — returns min/max/liquidation margin ratios.
- IMulticall3
- Multicall3 interface for batching contract reads.
- Perp
Manager - The PerpManager contract interface. Contains all structs, events, errors, and function signatures from IPerpManager.sol and PerpManager.sol.
- client
- High-level client for the PerpCity perpetual futures protocol.
- constants
- Protocol constants from
perpcity-contracts/src/libraries/Constants.sol. - contracts
- On-chain contract bindings generated via Alloy’s
sol!macro. - convert
- Conversions between client-facing types (
f64, human-readable) and on-chain types (6-decimal integers,U256,sqrtPriceX96). - errors
- Error types for the PerpCity SDK.
- events
- Event decoding for PerpManager and Beacon contracts.
- feed
- Live market event feed over WebSocket.
- hft
- High-frequency trading infrastructure.
- math
- Pure math functions for the PerpCity protocol.
- transport
- Multi-endpoint RPC transport with health-aware routing.
- types
- Client-facing types for the PerpCity SDK.
Structs§
- Adjust
Margin Params - Client-facing parameters for adjusting a position’s margin.
- Adjust
Margin Result - Result of adjusting a position’s margin.
- Adjust
Notional Params - Client-facing parameters for adjusting a position’s notional exposure.
- Adjust
Notional Result - Result of adjusting a position’s notional size.
- Bounds
- Leverage and margin constraints for a perpetual market.
- Close
Params - Client-facing parameters for closing a position.
- Close
Result - Result of closing a position.
- Deployments
- Deployed contract addresses for a PerpCity instance.
- Fees
- Fee percentages for a perpetual market, expressed as fractions of 1.
- GasLimits
- Pre-empirically derived gas limits for PerpCity operations.
- HftTransport
- Multi-endpoint RPC transport with health-aware routing.
- Live
Details - Real-time position metrics, typically from a
quoteClosePositioncall. - Market
Feed - A filtered stream of decoded
MarketEvents for a single perp. - Open
Interest - Taker open interest for a perp market, in USDC.
- Open
Maker Params - Client-facing parameters for opening a maker (LP) position.
- Open
Maker Quote - Result of simulating a maker position open via
quoteOpenMakerPosition. - Open
Result - Result of opening a position (taker or maker).
- Open
Taker Params - Client-facing parameters for opening a taker (long/short) position.
- Open
Taker Quote - Result of simulating a taker position open via
quoteOpenTakerPosition. - Perp
Client - High-level client for the PerpCity protocol.
- Perp
Data - Metadata about a perpetual market.
- Perp
Snapshot - Live market data from a multicall snapshot.
- PoolKey
- Identifies a Uniswap V4 pool (and PerpCity perp) by its constituent parameters.
- Swap
Config - Swap configuration for quoting through the Uniswap V4 pool.
- Swap
Quote - Result of a swap simulation via
quoteSwap. - Transport
Config - Complete transport configuration.
Enums§
- Market
Event - A decoded market event with human-readable f64 values.
- Perp
City Error - Central error type for the PerpCity SDK.
- Urgency
- Transaction urgency level, controlling EIP-1559 fee scaling.
Functions§
- align_
tick_ down - Round a tick down to the nearest multiple of the protocol tick spacing.
- align_
tick_ up - Round a tick up to the nearest multiple of the protocol tick spacing.
- decode_
log - Decode a raw Alloy
Loginto aMarketEvent, if recognized. - get_
sqrt_ ratio_ at_ tick - Compute the
sqrtPriceX96for a given tick using the Uniswap V4 bit-shift lookup table. - price_
to_ tick - Convert a human-readable price to the nearest tick.
- tick_
to_ price - Convert a tick to a human-readable price.
Type Aliases§
- Result
- Convenience alias used throughout the SDK.