Skip to content

Commit cfcea5a

Browse files
authored
Add publishing docs: README update, CONTRIBUTING, CHANGELOG (#2)
Update README with accurate feature list, installation instructions, environment setup, and links to sibling SDKs. Add CONTRIBUTING.md with dev commands and code style guide. Add CHANGELOG.md with initial 0.1.0 release entry.
1 parent d413093 commit cfcea5a

3 files changed

Lines changed: 193 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [0.1.0] - 2026-02-25
9+
10+
### Added
11+
12+
- **Contract interaction** -- Read/write calls with comptime-computed ABI selectors via eth.zig (zabi)
13+
- **Position management** -- `openTakerPosition`, `openMakerPosition`, `closePosition`, `adjustNotional`, `adjustMargin`
14+
- **HFT nonce manager** -- Lock-free atomic nonce acquisition with `std.atomic.Value(u64)`, zero RPC round-trips per transaction
15+
- **Gas cache** -- Pre-computed gas limits for all contract operations, configurable TTL with urgency-based multipliers (low/normal/high/critical)
16+
- **Transaction pipeline** -- Combines nonce manager + gas cache for fire-and-forget transaction submission with gas bump support
17+
- **Multi-RPC failover** -- Automatic endpoint selection based on health status and latency, 30s cooldown on unhealthy endpoints
18+
- **Connection management** -- Dual HTTP/WebSocket connection support with multi-RPC failover
19+
- **State cache** -- Multi-layer caching with configurable TTLs (slow: 60s for fees/bounds, fast: 2s for prices/funding)
20+
- **Pure math** -- Tick/price conversions, sqrt price math, liquidity calculations, position PnL -- all in pure Zig with no external deps
21+
- **Event streaming** -- Comptime keccak256 topic hashes, event identification from logs, subscription registry
22+
- **Position manager** -- Position tracking with stop-loss, take-profit, and trailing stop triggers
23+
- **Latency observability** -- Rolling window latency tracker with min/max/avg/p50/p95/p99 percentiles
24+
- **Pre-approved USDC** -- `setupForTrading()` approves max allowance once at startup, eliminating per-trade approval transactions
25+
- **View functions** -- `getFundingRate`, `getUtilFee`, `getInsurance`, `getOpenInterest` for market data reads
26+
- **Quote functions** -- ABI definitions for `quoteOpenTakerPosition`, `quoteOpenMakerPosition` simulation
27+
- Unit tests for all HFT infrastructure modules (150+ tests)
28+
- Integration test framework with Anvil support
29+
- CI pipeline with build, test, and format checks

CONTRIBUTING.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Contributing to PerpCity Zig SDK
2+
3+
## Prerequisites
4+
5+
- [Zig 0.15.2](https://ziglang.org/download/)
6+
- [Anvil](https://book.getfoundry.sh/anvil/) (for integration tests)
7+
8+
## Getting Started
9+
10+
1. Clone the repository:
11+
```bash
12+
git clone https://github.com/StrobeLabs/perpcity-zig-sdk.git
13+
cd perpcity-zig-sdk
14+
```
15+
16+
2. Build the project:
17+
```bash
18+
zig build
19+
```
20+
21+
3. Run unit tests:
22+
```bash
23+
zig build test
24+
```
25+
26+
## Development Commands
27+
28+
| Command | Description |
29+
|---------|-------------|
30+
| `zig build` | Build the SDK |
31+
| `zig build test` | Run unit tests (pure math, no network) |
32+
| `zig build integration-test` | Run integration tests (requires Anvil) |
33+
| `zig fmt --check src/ tests/` | Check formatting |
34+
| `zig fmt src/ tests/` | Auto-format code |
35+
36+
## Project Structure
37+
38+
```
39+
src/
40+
root.zig # Full SDK module (with eth.zig dependency)
41+
math_root.zig # Pure math module (no external deps)
42+
context.zig # SDK context with RPC provider and wallet
43+
approve.zig # USDC approval helpers
44+
perp_manager.zig # Core contract interactions
45+
open_position.zig # Position operations
46+
types.zig # Shared type definitions
47+
constants.zig # Protocol constants
48+
conversions.zig # Tick/price conversions
49+
liquidity.zig # Liquidity calculations
50+
position.zig # Position math
51+
perp.zig # Perp math
52+
nonce.zig # Lock-free nonce management
53+
gas.zig # Gas price cache and pre-computed limits
54+
tx_pipeline.zig # Transaction pipeline
55+
state_cache.zig # Multi-layer state cache
56+
multi_rpc.zig # Multi-endpoint failover
57+
connection.zig # Connection management
58+
latency.zig # Latency tracking
59+
events.zig # Event streaming
60+
position_manager.zig # Position tracking with triggers
61+
abi/ # ABI definitions
62+
tests/
63+
unit_tests.zig # Unit test runner
64+
unit/ # Unit test files
65+
integration_tests.zig # Integration test runner
66+
integration/ # Integration test files
67+
```
68+
69+
## Code Style
70+
71+
- Run `zig fmt` before committing. CI enforces formatting.
72+
- All time-dependent methods accept explicit `now_ms: i64` parameters for deterministic testing. Do not use OS-level clock calls in library code.
73+
- Use comptime where possible for ABI encoding and type-level computation.
74+
- Avoid heap allocations on the hot path. Prefer stack-allocated buffers with bounded sizes.
75+
- Keep the pure math layer (`math_root.zig`) free of external dependencies.
76+
77+
## Pull Request Workflow
78+
79+
1. Create a feature branch from `main`
80+
2. Make your changes
81+
3. Run CI checks locally:
82+
```bash
83+
zig build && zig build test && zig fmt --check src/ tests/
84+
```
85+
4. Open a pull request against `main`
86+
5. All CI checks must pass before merge
87+
88+
## Reporting Issues
89+
90+
Open an issue on [GitHub](https://github.com/StrobeLabs/perpcity-zig-sdk/issues) with:
91+
92+
- A description of the issue
93+
- Steps to reproduce
94+
- Expected vs actual behavior
95+
- Zig version (`zig version`)

README.md

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# perpcity-zig-sdk
1+
# PerpCity Zig SDK
22

3-
High-performance, low-level Zig SDK for the PerpCity perpetual futures protocol. Built for HFT bots and latency-sensitive trading systems.
3+
High-performance, low-level Zig SDK for the PerpCity perpetual futures protocol on Base. Built for HFT bots and latency-sensitive trading systems.
44

55
## Why Zig?
66

@@ -11,7 +11,7 @@ This SDK is designed for use cases where nanoseconds matter. Zig gives us:
1111
- **Comptime ABI encoding** -- function selectors computed at compile time, zero runtime cost
1212
- **Direct hardware control** -- atomic nonce management, lock-free data structures
1313

14-
If you're building a trading bot in Python or TypeScript, this is not the SDK for you. This is for systems that need sub-millisecond transaction construction and direct EVM interaction with no abstraction tax.
14+
If you're building a trading bot in Python or TypeScript, use [perpcity-sdk](https://github.com/StrobeLabs/perpcity-sdk) or [perpcity-python-sdk](https://github.com/StrobeLabs/perpcity-python-sdk) instead. This SDK is for systems that need sub-millisecond transaction construction and direct EVM interaction with no abstraction tax.
1515

1616
## Features
1717

@@ -20,15 +20,37 @@ If you're building a trading bot in Python or TypeScript, this is not the SDK fo
2020
- **HFT nonce manager** -- Lock-free atomic nonce acquisition, no RPC round-trip per transaction
2121
- **Gas cache** -- Pre-computed gas limits and fee caching to skip `estimateGas` calls
2222
- **Transaction pipeline** -- Combines nonce manager + gas cache for fire-and-forget submission
23-
- **Multi-RPC failover** -- Automatic failover across multiple RPC endpoints
23+
- **Multi-RPC failover** -- Automatic failover across multiple RPC endpoints with latency tracking
2424
- **State cache** -- Multi-layer caching (mark prices, perp configs) with configurable TTLs
2525
- **Pure math** -- Tick/price conversions, sqrt price math, liquidity calculations -- all in pure Zig with no external deps
2626
- **Event streaming** -- Subscription registry for on-chain event processing
27+
- **Position manager** -- Stop-loss, take-profit, and trailing stop triggers
28+
- **Latency observability** -- Rolling window latency tracking with p50/p95/p99 percentiles
29+
30+
## Installation
31+
32+
Add to your `build.zig.zon`:
33+
34+
```zig
35+
.dependencies = .{
36+
.perpcity_sdk = .{
37+
.url = "git+https://github.com/StrobeLabs/perpcity-zig-sdk.git#<commit>",
38+
},
39+
},
40+
```
41+
42+
Then in `build.zig`:
43+
44+
```zig
45+
const sdk_dep = b.dependency("perpcity_sdk", .{ .target = target, .optimize = optimize });
46+
exe.root_module.addImport("perpcity_sdk", sdk_dep.module("perpcity_sdk"));
47+
```
48+
49+
Requires **Zig 0.15.2**.
2750

2851
## Quick Start
2952

3053
```zig
31-
const eth = @import("eth");
3254
const sdk = @import("perpcity_sdk");
3355
3456
// Initialize context
@@ -41,7 +63,7 @@ var ctx = sdk.context.PerpCityContext.init(
4163
ctx.fixPointers();
4264
defer ctx.deinit();
4365
44-
// Ensure USDC approval
66+
// Approve USDC once at startup (max allowance)
4567
try ctx.setupForTrading();
4668
4769
// Open a 10x long taker position
@@ -53,35 +75,15 @@ const position = try sdk.perp_manager.openTakerPosition(&ctx, perp_id, .{
5375
});
5476
```
5577

56-
## Installation
57-
58-
Add to your `build.zig.zon`:
59-
60-
```zig
61-
.dependencies = .{
62-
.perpcity_sdk = .{
63-
.url = "git+https://github.com/StrobeLabs/perpcity-zig-sdk.git#<commit>",
64-
},
65-
},
66-
```
67-
68-
Then in `build.zig`:
69-
70-
```zig
71-
const sdk_dep = b.dependency("perpcity_sdk", .{ .target = target, .optimize = optimize });
72-
exe.root_module.addImport("perpcity_sdk", sdk_dep.module("perpcity_sdk"));
73-
```
74-
75-
Requires **Zig 0.15.2**.
76-
7778
## Architecture
7879

7980
```
8081
Pure math layer (no dependencies):
8182
types, constants, conversions, liquidity, position, perp
8283
83-
HFT infrastructure:
84-
nonce manager, gas cache, tx pipeline, state cache, multi-rpc
84+
HFT infrastructure (no dependencies):
85+
nonce, gas, tx_pipeline, state_cache, multi_rpc, connection,
86+
latency, events, position_manager
8587
8688
Contract interaction (requires eth.zig):
8789
context, approve, perp_manager, open_position
@@ -90,19 +92,53 @@ ABI definitions:
9092
perp_manager_abi, erc20_abi, fees_abi, margin_ratios_abi, beacon_abi
9193
```
9294

93-
The pure math layer has zero external dependencies and can be used standalone for off-chain calculations (mark price conversions, PnL estimation, liquidation checks).
95+
The pure math and HFT infrastructure layers have zero external dependencies and can be used standalone for off-chain calculations (mark price conversions, PnL estimation, liquidation checks) and trading infrastructure (nonce management, gas caching, latency tracking).
96+
97+
## Development
9498

95-
## Testing
99+
### Build
96100

97101
```bash
98-
# Unit tests (pure math, no network)
102+
zig build
103+
```
104+
105+
### Test
106+
107+
```bash
108+
# Unit tests (pure math + HFT infrastructure, no network)
99109
zig build test
100110

101-
# Integration tests (requires Anvil)
111+
# Integration tests (requires Anvil running locally)
102112
anvil &
103113
zig build integration-test
104114
```
105115

116+
### Lint
117+
118+
```bash
119+
zig fmt --check src/ tests/
120+
```
121+
122+
## Environment Setup
123+
124+
Create a `.env.local` file:
125+
126+
```env
127+
# Required for integration tests
128+
PRIVATE_KEY=your_private_key_here
129+
RPC_URL=https://your-rpc-url.com
130+
131+
# Contract addresses (Base Sepolia)
132+
PERP_MANAGER_ADDRESS=0x...
133+
USDC_ADDRESS=0x...
134+
```
135+
106136
## License
107137

108138
MIT
139+
140+
## Links
141+
142+
- [Perp City Documentation](https://docs.perpcity.io)
143+
- [Strobe Labs](https://strobelabs.io)
144+
- [TypeScript SDK](https://github.com/StrobeLabs/perpcity-sdk)

0 commit comments

Comments
 (0)