mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-11 16:32:57 +00:00
e823114623
* Result types: * (Web)CallResult types are replaced by HttpResult, WebSocketResult and QueryResult with the same logic * Updated result types to record type * Result creation can be done with (Http/WebSocket/Query)Result.Ok(..) and .Fail(..) * Removed implicit result type conversion to bool, `if (result)` no longer works, instead use `if (result.Success)` * Replaced CallResult.SuccessResult with CallResult.Ok() * Fixed result object nullability hinting, for example Data might be null if Success isn't checked for true * Parameters & serialization: * Added support for `enabled` and `disabled` strings to bool converter * Removed ParameterCollection type, has been replaced by Parameters type * Removed ArraySerialization, OrderParameters and ParameterOrderComparer properties from RestApiClient, moved to ParameterSerializationsSettings * Updated RestRequestConfiguration in AuthenticationProvider.ProcessRequest to contain the full RequestDefinition instead of copied fields * Clients: * Updated Api client constructor logging parameter from ILogger to ILoggerFactory? * Added Api client constructor exchange name parameter * Added ToString overrides on base API types * Added Exchange property on BaseApiClient * Added ApiCredentials property on IRestApiClient and ISocketApiClient interfaces * Updated ILogger source from client name to topic specific client name * Removed logging from client creation * Fixed BaseRestClient SetApiCredentials not marked as virtual * Rest: * Added BaseAddress to RequestDefinition object * Updated RestApiClient AuthenticationProvider logic from private to protected and virtual * Removed RestApiClient.SendAsync baseAddress parameter removed * Removed RestApiClient.SendAsync without type parameter * WebSocket: * Updated MessageRouting definition into CreateForEvent for subscriptions and CreateForQuery for queries * Improved Query type safety with CeateForQuery which allows second parameter for specifying the result type * Renamed MessageRouter.CreateWithoutHandler to CreateVoid * Updated SocketApiClient.GetSocketConnection to check connection uri instead of Tag for finding compatible connections * Removed unused UnhandledMessageExpected property SocketApiClient * Fixed issue in SocketApiClient.GetSocketConnection causing requests to always wait the full max 10 seconds when there was a reconnecting socket * Shared APIs: * Updated Option definitions to always require the exchange name as first parameter * Added missing dedicated option types * Added Discover method on ISharedClient interface, returning info on supported capabilities and operations * Added SharedRequest GetParamValue helper method accepting multiple parameter names * Added ResetStaticExchangeParameters method on ExchangeParameters * Added Status property to SharedWithdrawal model * Added TradingModes property to SharedBalance model * Updated ExchangeSymbolCache to support multiple environments and additional key separation * Updated Shared ExchangeParameters parameter names to be case insensitive * Updated code comments * Replaced ExchangeResult with ExchangeCallResult type * Removed AsExchangeResult/ExchangeWebResult * Removed TradingMode from the response model, only maintained on models where it makes sense * Removed IListenKey support, listen keys now rely on internal management with TokenManager * Rate limiting: * Fixed websocket connection attempts counting towards rate limit even when server could not be reached * Removed host from rate limit methods, now part of the already provided RequestDefinition * Added amount parameter to RateLimit Reset method to allow partially resetting the limit * Added TokenManager implementation for automatic listenkey/token management * Added UserClientProvider base class * Added async streaming on UserDataTracker items with StreamUpdatesAsync * Added cancellation token support to UserDataTracker starting * Added Unit type for non-result types * Added ServerError constructor taking ErrorType and message to make it easier to create * Added SupportedEnvironments property to PlatformInfo * Updated SymbolOrderBook DoResyncAsync to return CallResult instead of CallResult<bool> which was redundant * Various small performance improvements
170 lines
7.6 KiB
Markdown
170 lines
7.6 KiB
Markdown
---
|
|
name: cryptoexchange-net
|
|
description: Use CryptoExchange.Net abstractions when generating C#/.NET code that needs to work across MULTIPLE cryptocurrency exchanges (Binance + Bybit + OKX + Kraken + Coinbase + ...) — for arbitrage, best-execution routing, multi-exchange portfolio dashboards, exchange-agnostic trading bots, or comparison tools. Triggers on requests mentioning multi-exchange, cross-exchange, arbitrage, exchange-agnostic, or unified crypto API in C#. Also triggers when the user is implementing a new exchange library following the CryptoExchange.Net pattern.
|
|
---
|
|
|
|
# CryptoExchange.Net Skill
|
|
|
|
## When to use
|
|
|
|
CryptoExchange.Net is the **base library** powering 28+ exchange-specific libraries (Binance.Net, Bybit.Net, OKX.Net, Kraken.Net, Coinbase.Net, etc.). You don't install it directly — you install the exchange libraries, which depend on it.
|
|
|
|
**Three usage modes:**
|
|
|
|
1. **You target ONE exchange** → use that exchange's library directly (e.g., Binance.Net), see its CLAUDE.md.
|
|
2. **You target MULTIPLE exchanges** → install each library you need + use `CryptoExchange.Net.SharedApis` interfaces — write code once, runs against any exchange. **This is the main use case for this skill.**
|
|
3. **You want ALL exchanges in one package** → install `CryptoClients.Net`, get `ExchangeRestClient` and `ExchangeSocketClient` with everything bundled.
|
|
|
|
## Installation
|
|
|
|
For a multi-exchange project:
|
|
|
|
```bash
|
|
dotnet add package Binance.Net
|
|
dotnet add package JK.OKX.Net
|
|
dotnet add package Bybit.Net
|
|
# ... etc
|
|
```
|
|
|
|
Or the bundle:
|
|
|
|
```bash
|
|
dotnet add package CryptoClients.Net
|
|
```
|
|
|
|
## Core Pattern: Shared Interfaces
|
|
|
|
Every exchange library exposes `.SharedClient` properties on its API surfaces. These implement the same interfaces from `CryptoExchange.Net.SharedApis`.
|
|
|
|
```csharp
|
|
using Binance.Net.Clients;
|
|
using OKX.Net.Clients;
|
|
using Bybit.Net.Clients;
|
|
using CryptoExchange.Net.SharedApis;
|
|
|
|
// All three implement ISpotTickerRestClient
|
|
ISpotTickerRestClient binance = new BinanceRestClient().SpotApi.SharedClient;
|
|
ISpotTickerRestClient okx = new OKXRestClient().UnifiedApi.SharedClient;
|
|
ISpotTickerRestClient bybit = new BybitRestClient().V5Api.SharedClient;
|
|
|
|
// Single agnostic call — works against any of them
|
|
var symbol = new SharedSymbol(TradingMode.Spot, "BTC", "USDT");
|
|
var ticker = await binance.GetSpotTickerAsync(new GetTickerRequest(symbol));
|
|
// ticker.Data.LastPrice, ticker.Data.HighPrice, etc. — same model regardless of exchange
|
|
```
|
|
|
|
## Core Pattern: SharedSymbol
|
|
|
|
Different exchanges format symbols differently — Binance uses `BTCUSDT`, OKX uses `BTC-USDT`, others may have other formats. `SharedSymbol` normalizes this:
|
|
|
|
```csharp
|
|
var btcusdt = new SharedSymbol(TradingMode.Spot, "BTC", "USDT");
|
|
// Each exchange library translates SharedSymbol → its native format internally.
|
|
|
|
// For futures:
|
|
var btcusdtPerp = new SharedSymbol(TradingMode.PerpetualLinear, "BTC", "USDT");
|
|
```
|
|
|
|
For exchanges that use exotic asset names, see the AssetAliases configuration.
|
|
|
|
## Available Shared Interfaces
|
|
|
|
**REST:**
|
|
|
|
- Market data: `ISpotTickerRestClient`, `IBookTickerRestClient`, `ISpotSymbolRestClient`, `IFuturesSymbolRestClient`, `IOrderBookRestClient`, `IRecentTradeRestClient`, `IKlineRestClient`
|
|
- Orders: `ISpotOrderRestClient`, `IFuturesOrderRestClient`, `ISpotOrderClientIdRestClient`, `IFuturesOrderClientIdRestClient`, `ISpotTriggerOrderRestClient`, `IFuturesTriggerOrderRestClient`, `IFuturesTpSlRestClient`
|
|
- Account: `IBalanceRestClient`, `IPositionRestClient`, `IFeeRestClient`, `ITransferRestClient`, `IDepositRestClient`, `IWithdrawalRestClient`
|
|
|
|
**WebSocket:**
|
|
|
|
- `ITickerSocketClient`, `IBookTickerSocketClient`
|
|
- `IOrderBookSocketClient`, `ITradeSocketClient`, `IKlineSocketClient`
|
|
- `IUserTradeSocketClient`, `ISpotOrderSocketClient`, `IFuturesOrderSocketClient`, `IPositionSocketClient`, `IBalanceSocketClient`
|
|
|
|
Each exchange documents which interfaces it implements (some exchanges don't support every operation).
|
|
|
|
## Core Pattern: Result Handling
|
|
|
|
Same as exchange-specific libraries: REST calls return `HttpResult<T>` and websocket subscription calls return `WebSocketResult<UpdateSubscription>`, both with `.Success`, `.Data`, and `.Error`. Always check `.Success` first.
|
|
|
|
```csharp
|
|
var result = await sharedClient.GetSpotTickerAsync(new GetTickerRequest(symbol));
|
|
if (!result.Success)
|
|
{
|
|
Console.WriteLine($"[{sharedClient.Exchange}] Error: {result.Error}");
|
|
return;
|
|
}
|
|
Console.WriteLine($"[{sharedClient.Exchange}] {result.Data.LastPrice}");
|
|
```
|
|
|
|
`.Exchange` property on every shared client tells you which exchange you're talking to — useful for logging.
|
|
|
|
## Core Pattern: Multi-Exchange Aggregation
|
|
|
|
```csharp
|
|
var clients = new ISpotTickerRestClient[]
|
|
{
|
|
new BinanceRestClient().SpotApi.SharedClient,
|
|
new OKXRestClient().UnifiedApi.SharedClient,
|
|
new BybitRestClient().V5Api.SharedClient,
|
|
};
|
|
|
|
var symbol = new SharedSymbol(TradingMode.Spot, "BTC", "USDT");
|
|
|
|
// Fetch concurrently from all exchanges
|
|
var tasks = clients.Select(c => c.GetSpotTickerAsync(new GetTickerRequest(symbol))).ToArray();
|
|
var results = await Task.WhenAll(tasks);
|
|
|
|
for (int i = 0; i < clients.Length; i++)
|
|
{
|
|
if (results[i].Success)
|
|
Console.WriteLine($"{clients[i].Exchange}: {results[i].Data!.LastPrice}");
|
|
}
|
|
```
|
|
|
|
## Per-Exchange Setup
|
|
|
|
Each exchange library has its own credentials class and options. See each library's CLAUDE.md for specifics. The pattern is consistent: `XxxRestClient(options => { options.ApiCredentials = new XxxCredentials(...); })`.
|
|
|
|
## Dependency Injection
|
|
|
|
Each exchange library has its own `services.AddXxx(...)` extension. They all share the same option-builder pattern. Register only the ones you use:
|
|
|
|
```csharp
|
|
services.AddBinance(restOpts => { /*...*/ }, socketOpts => { /*...*/ });
|
|
services.AddOKX(restOpts => { /*...*/ }, socketOpts => { /*...*/ });
|
|
// Inject IBinanceRestClient, IOKXRestClient, etc.
|
|
```
|
|
|
|
For one-package access: `services.AddCryptoClients(...)` from `CryptoClients.Net`.
|
|
|
|
## Common Pitfalls — AVOID
|
|
|
|
- **Do NOT install `CryptoExchange.Net` and try to call exchange APIs directly** — it's a base abstraction; you need an exchange library.
|
|
- **Do NOT try to use one exchange's models with another's client** — use the SharedApis types (`SharedSymbol`, `SharedSpotTicker`, `SharedSpotOrder`, etc.) for cross-exchange code.
|
|
- **Do NOT block on async operations** — use `await` throughout. `Task.WhenAll` for parallelism across exchanges.
|
|
- **Do NOT assume every exchange supports every operation** — check exchange docs or the library's implementation. Operations may return errors like "not supported on this exchange".
|
|
- **Do NOT instantiate clients per-request** — reuse via DI.
|
|
- **Do NOT iterate exchanges sequentially when concurrency is fine** — use `Task.WhenAll` for ~Nx speedup.
|
|
|
|
## Implementing a New Exchange Library
|
|
|
|
If you're building a NEW exchange wrapper following the CryptoExchange.Net pattern (rare but valuable):
|
|
|
|
- Inherit from `RestApiClient` and `SocketApiClient` base classes
|
|
- Define your own `XxxCredentials` extending `ApiCredentials` (or use `ApiCredentials` directly)
|
|
- Implement `AuthenticationProvider` for the exchange's signing scheme
|
|
- Implement the relevant `Shared*` interfaces on your API client classes for cross-exchange support
|
|
- Follow the same `XxxRestOptions` / `XxxSocketOptions` pattern
|
|
|
|
See existing libraries (Binance.Net, Bybit.Net) as reference implementations.
|
|
|
|
## Reference
|
|
|
|
- Source: https://github.com/JKorf/CryptoExchange.Net
|
|
- Documentation: https://cryptoexchange.jkorf.dev/
|
|
- SharedApis docs: https://cryptoexchange.jkorf.dev/CryptoExchange.Net/idocs_shared.html
|
|
- Bundle (all exchanges): https://github.com/JKorf/CryptoClients.Net
|
|
- Demo app: https://github.com/JKorf/CryptoManager.Net
|
|
- Discord: https://discord.gg/MSpeEtSY8t
|