# strobelabs/perpcity-zig-sdk architecture

> Architecture diagram of the GitHub repository strobelabs/perpcity-zig-sdk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/strobelabs/perpcity-zig-sdk
- Repository: https://github.com/strobelabs/perpcity-zig-sdk

## Overview

PerpCity Zig SDK is a low-level SDK for building and submitting perpetual-futures transactions on Arbitrum. The documented quick-start flow initializes a context, approves trading, derives a position size, and opens a taker position. Other documented capabilities include pure-Zig calculations, transaction infrastructure, RPC failover, cached state, event streaming, and position triggers. Relationships below rely on the README; source excerpts were unavailable, so internal call paths not documented there are omitted.

Main entry points: [`src/context.zig`](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/context.zig) (Trading context), [`src/perp_contract.zig`](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/perp_contract.zig) (Perp contracts), [`src/tx_pipeline.zig`](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/tx_pipeline.zig) (Transaction pipeline), [`src/multi_rpc.zig`](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/multi_rpc.zig) (RPC failover)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_math["Pure math"]
  node_sizing["Position sizing<br/>[sizing.zig]"]
  node_conversions["Price conversions<br/>[conversions.zig]"]
  node_funding["Funding math<br/>[funding.zig]"]
  node_liquidity["Liquidity math<br/>[liquidity.zig]"]
  node_position["Position math<br/>[position.zig]"]
end

subgraph group_infra["Trading infrastructure"]
  node_nonce["Nonce manager<br/>[nonce.zig]"]
  node_gas["Gas cache<br/>[gas.zig]"]
  node_pipeline["Transaction pipeline<br/>[tx_pipeline.zig]"]
  node_multi_rpc["RPC failover<br/>[multi_rpc.zig]"]
  node_state_cache[("State cache<br/>[state_cache.zig]")]
end

subgraph group_contracts["Contract interaction"]
  node_context["Trading context<br/>[context.zig]"]
  node_chain["Chain client<br/>[chain_client.zig]"]
  node_perp_contract["Perp contracts<br/>[perp_contract.zig]"]
  node_factory["Perp factory<br/>[perp_factory.zig]"]
  node_approve["Token approval<br/>[approve.zig]"]
  node_abis["Contract ABIs"]
end

subgraph group_monitor["Monitoring and triggers"]
  node_events["Event streaming<br/>[events.zig]"]
  node_watcher["Event watcher<br/>[watcher.zig]"]
  node_position_manager["Position triggers"]
  node_latency["Latency tracking<br/>[latency.zig]"]
end

node_trader(("SDK user"))
node_eth["eth.zig"]
node_rpc["RPC endpoints"]

node_trader -->|"initializes"| node_context
node_context -->|"owns transport"| node_rpc
node_context -->|"sets up trading"| node_approve
node_trader -->|"derives size"| node_sizing
node_sizing -->|"provides delta"| node_perp_contract
node_trader -->|"opens position"| node_perp_contract
node_chain -->|"uses"| node_eth
node_perp_contract -->|"uses"| node_eth
node_factory -->|"uses"| node_eth
node_approve -->|"uses"| node_eth
node_pipeline -->|"combines"| node_nonce
node_pipeline -->|"combines"| node_gas
node_multi_rpc -->|"fails over"| node_rpc
node_multi_rpc -->|"tracks latency"| node_latency

click node_context "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/context.zig"
click node_chain "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/chain_client.zig"
click node_perp_contract "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/perp_contract.zig"
click node_factory "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/perp_factory.zig"
click node_approve "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/approve.zig"
click node_abis "https://github.com/strobelabs/perpcity-zig-sdk/tree/main/src/abi"
click node_sizing "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/sizing.zig"
click node_conversions "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/conversions.zig"
click node_funding "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/funding.zig"
click node_liquidity "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/liquidity.zig"
click node_position "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/position.zig"
click node_nonce "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/nonce.zig"
click node_gas "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/gas.zig"
click node_pipeline "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/tx_pipeline.zig"
click node_multi_rpc "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/multi_rpc.zig"
click node_state_cache "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/state_cache.zig"
click node_events "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/events.zig"
click node_watcher "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/watcher.zig"
click node_position_manager "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/position_manager.zig"
click node_latency "https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/latency.zig"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sizing,node_conversions,node_funding,node_liquidity,node_position,node_trader toneBlue
class node_nonce,node_gas,node_pipeline,node_multi_rpc,node_state_cache toneAmber
class node_context,node_chain,node_perp_contract,node_factory,node_approve,node_abis toneMint
class node_events,node_watcher,node_position_manager,node_latency toneRose
class node_eth,node_rpc toneIndigo
```

## Components

### Pure math

- [Position sizing](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/sizing.zig): `src/sizing.zig`
- [Price conversions](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/conversions.zig): `src/conversions.zig`
- [Funding math](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/funding.zig): `src/funding.zig`
- [Liquidity math](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/liquidity.zig): `src/liquidity.zig`
- [Position math](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/position.zig): `src/position.zig`

### Trading infrastructure

- [Nonce manager](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/nonce.zig): `src/nonce.zig`
- [Gas cache](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/gas.zig): `src/gas.zig`
- [Transaction pipeline](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/tx_pipeline.zig): `src/tx_pipeline.zig`
- [RPC failover](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/multi_rpc.zig): `src/multi_rpc.zig`
- [State cache](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/state_cache.zig): `src/state_cache.zig`

### Contract interaction

- [Trading context](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/context.zig): `src/context.zig`
- [Chain client](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/chain_client.zig): `src/chain_client.zig`
- [Perp contracts](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/perp_contract.zig): `src/perp_contract.zig`
- [Perp factory](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/perp_factory.zig): `src/perp_factory.zig`
- [Token approval](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/approve.zig): `src/approve.zig`
- [Contract ABIs](https://github.com/strobelabs/perpcity-zig-sdk/tree/main/src/abi): `src/abi`

### Monitoring and triggers

- [Event streaming](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/events.zig): `src/events.zig`
- [Event watcher](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/watcher.zig): `src/watcher.zig`
- [Position triggers](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/position_manager.zig): `src/position_manager.zig`
- [Latency tracking](https://github.com/strobelabs/perpcity-zig-sdk/blob/main/src/latency.zig): `src/latency.zig`

### Other components

- **SDK user**
- **eth.zig**
- **RPC endpoints**

## Connections

- SDK user → Trading context: initializes (evidence: `README.md`)
- Trading context → RPC endpoints: owns transport (evidence: `README.md`)
- Trading context → Token approval: sets up trading (evidence: `README.md`)
- SDK user → Position sizing: derives size (evidence: `README.md`)
- Position sizing → Perp contracts: provides delta (evidence: `README.md`)
- SDK user → Perp contracts: opens position (evidence: `README.md`)
- Chain client → eth.zig: uses (evidence: `README.md`)
- Perp contracts → eth.zig: uses (evidence: `README.md`)
- Perp factory → eth.zig: uses (evidence: `README.md`)
- Token approval → eth.zig: uses (evidence: `README.md`)
- Transaction pipeline → Nonce manager: combines (evidence: `README.md`)
- Transaction pipeline → Gas cache: combines (evidence: `README.md`)
- RPC failover → RPC endpoints: fails over (evidence: `README.md`)
- RPC failover → Latency tracking: tracks latency (evidence: `README.md`)
