@sha3/crypto
v0.8.0
Published
Node.js TypeScript library that normalizes real-time crypto feeds across Binance, Coinbase, Kraken, OKX, and Chainlink.
Downloads
344
Readme
@sha3/crypto
Node.js TypeScript library that normalizes real-time crypto feeds across Binance, Coinbase, Kraken, OKX, and Chainlink.
It exposes a single backend-oriented API for:
- live unified events (
price,orderbook,trade,status), - resilient multi-provider connections,
- latest snapshots,
- historical range queries,
- nearest-price lookup by timestamp.
TL;DR
npm i @sha3/cryptoimport { CryptoFeedClient } from "@sha3/crypto";
const client = CryptoFeedClient.create({ symbols: ["btc"], providers: ["binance", "chainlink"] });
const subscription = client.subscribe((event) => {
if (event.type === "price") {
console.log(event.provider, event.symbol, event.price);
}
});
await client.connect();
const now = Date.now();
const prices = client.getPriceHistory({ symbol: "btc", fromTs: now - 60_000, toTs: now });
console.log(prices.length);
subscription.unsubscribe();
await client.disconnect();Why This Exists
Provider payloads, symbols, and message semantics differ by exchange. This library isolates that complexity and provides one deterministic integration contract for application services and LLM-driven tooling.
Installation
npm i @sha3/cryptoCompatibility
- Node.js
>=20 - ESM runtime (
"type": "module") - TypeScript consumer support expected (package publishes
.d.ts) - Outbound websocket network access required
Integration Guide (External Projects)
- Install
@sha3/crypto. - Import from package root only.
- Create one
CryptoFeedClientper service boundary. - Subscribe to feed events and route/persist as needed.
- Query latest/historical data through client methods.
import { CryptoFeedClient } from "@sha3/crypto";
const client = CryptoFeedClient.create({
symbols: ["btc", "eth"],
providers: ["binance", "coinbase", "kraken", "okx", "chainlink"]
});
await client.connect();Do not import internal modules like src/* from consuming projects.
Public API Reference
Class
CryptoFeedClientstatic create(options?: ClientOptions): CryptoFeedClientasync connect(): Promise<void>async disconnect(): Promise<void>subscribe(listener: FeedEventListener): SubscriptiongetLatestPrice(symbol, provider?)getLatestOrderBook(symbol, provider?)getLatestTrade(symbol, provider?)getPriceClosestTo(symbol, targetTs, provider?)getPriceHistory(query)getOrderBookHistory(query)getTradeHistory(query)
Exported Types
ClientOptionsHistoryQueryRetentionOptionsFeedEventPricePointOrderBookSnapshotTradePointCryptoProviderIdCryptoSymbolSubscription
Exported Errors
NoProvidersConnectedErrorProviderConnectionErrorProviderParseErrorInvalidHistoryQueryError
Behavior Expectations
connect()attempts all selected providers in parallel.connect()resolves if at least one provider connects.- If all fail,
connect()throwsNoProvidersConnectedError. - Range queries are inclusive (
fromTs <= ts <= toTs). - Aggregated history (without
provider) is sorted by timestamp asc, then provider id.
Configuration Reference (src/config.ts)
Runtime defaults are centralized in src/config.ts as a single default object (CONFIG).
CONFIG.clientDefaults.symbols- default symbol list when
ClientOptions.symbolsis omitted.
- default symbol list when
CONFIG.clientDefaults.providers- default provider list when
ClientOptions.providersis omitted.
- default provider list when
CONFIG.clientDefaults.retention.windowMs- in-memory retention window (ms).
CONFIG.clientDefaults.retention.maxSamplesPerStream- max retained
price/orderbookpoints per stream.
- max retained
CONFIG.clientDefaults.retention.maxTradesPerStream- max retained
tradepoints per stream.
- max retained
CONFIG.clientDefaults.orderBookLevels- depth used by provider adapters.
CONFIG.providerConnection.reconnectBaseDelayMs- initial reconnect delay.
CONFIG.providerConnection.reconnectMaxDelayMs- max reconnect delay cap.
CONFIG.providerConnection.reconnectJitterRatio- jitter factor for reconnect backoff.
CONFIG.providerConnection.connectTimeoutMs- connect timeout per provider.
CONFIG.providerUrls.*- websocket endpoints by provider.
CONFIG.chainlink.topic- Chainlink subscription topic.
Testing
Run deterministic checks:
npm run checkRun live integration tests against real providers:
npm run test:liveLive tests can skip provider-specific checks when endpoints are temporarily rate-limited or unavailable.
Troubleshooting
No providers connected
- Verify websocket egress from your environment.
- Inspect
statusevents to identify the failing provider.
Missing historical points
- Increase
ClientOptions.retention. - Ensure process uptime is long enough to accumulate data.
ESM import errors
- Ensure consumer project supports ESM imports on Node.js 20+.
AI Usage
When using assistants in this repo:
- Treat
AGENTS.mdas blocking contract. - Keep class-first architecture and constructor injection.
- Keep single-return policy and control-flow braces.
- Keep
src/config.tsas a single default object and import it asimport CONFIG from ".../config.ts". - Update tests for behavior changes.
- Run
npm run checkbefore finalizing.
Development
npm install
npm run check
npm run test:live
npm run build