PerpCity Zig SDK
High-performance Zig SDK for HFT bots and latency-sensitive trading on PerpCity perpetual futures.
The PerpCity Zig SDK is a high-performance, low-level SDK for the PerpCity perpetual futures protocol. Built for HFT bots and latency-sensitive trading systems on Base L2.
Why Zig?
- Zero runtime overhead -- No GC, no hidden allocations, no async coloring
- Deterministic memory layout -- Cache-friendly structs with predictable performance
- Comptime ABI encoding -- Function selectors computed at compile time, zero runtime cost
- Direct hardware control -- Atomic nonce management, lock-free data structures
Architecture
| Layer | Description | Network Required |
|---|---|---|
| Pure Math | Price/tick conversions, position calculations, liquidity math | No |
| HFT Infrastructure | Nonce management, multi-RPC failover, gas caching, latency tracking, tx pipeline | No |
| Contract Interaction | PerpCityContext, trading functions, USDC approval | Yes |
Quick Start
const std = @import("std");
const sdk = @import("perpcity_sdk");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const private_key: [32]u8 = undefined; // TODO: your private key bytes
const perp_manager_addr: [20]u8 = undefined; // TODO: PerpManager contract address
const usdc_addr: [20]u8 = undefined; // TODO: USDC contract address
const perp_id: [32]u8 = undefined; // TODO: perp market ID
var ctx = sdk.context.PerpCityContext.init(
allocator,
"https://base-rpc-url.com",
private_key,
.{
.perp_manager = perp_manager_addr,
.usdc = usdc_addr,
},
);
ctx.fixPointers();
defer ctx.deinit();
try ctx.setupForTrading();
const position = try sdk.perp_manager.openTakerPosition(&ctx, perp_id, .{
.is_long = true,
.margin = 1000.0,
.leverage = 10.0,
.unspecified_amount_limit = 0,
});
const live = try position.liveDetails();
std.debug.print("PnL: {d:.2}\n", .{live.pnl});
}Guides
Resources
- GitHub: perpcity-zig-sdk repository
- eth.zig: The underlying Ethereum library for Zig
- TypeScript SDK: For a higher-level alternative, see the PerpCity TypeScript SDK
- Python SDK: For Python, see the PerpCity Python SDK