Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Gateways

Gateways implement the ExchangeGateway trait and handle order submission to exchanges.

ExchangeGateway trait

#![allow(unused)]
fn main() {
#[async_trait]
pub trait ExchangeGateway: Send + Sync {
    async fn connect(&mut self) -> GatewayResult<()>;
    async fn submit_order(&self, order: &Order) -> GatewayResult<OrderId>;
    async fn cancel_order(&self, id: &OrderId, symbol: &Symbol) -> GatewayResult<()>;
    fn exchange(&self) -> Exchange;
}
}

Supported gateways

GatewayExchangeNotes
BinanceGatewayBinance SpotHMAC-SHA256 signing
CoinbaseGatewayCoinbase Advanced TradeHMAC-SHA256 signing
BybitGatewayBybit v5HMAC-SHA256 signing
KrakenGatewayKraken v0HMAC-SHA512 with nonce
OkxGatewayOKX v5HMAC-SHA256, base64
PolymarketGatewayPolymarket CLOBHMAC-SHA256; fills via WebSocket user channel
KalshiGatewayKalshi v2RSA-SHA256 private key signing
PaperGateway—Local simulated matching

PaperGateway

PaperGateway is a local matching engine that simulates order fills against live BookUpdate events on the bus. It is the recommended way to develop and test strategies.

  • Default simulated RTT: 5 ms
  • No API keys required
  • Enabled with --paper CLI flag

Configurable latency:

#![allow(unused)]
fn main() {
PaperGateway::with_latency(Duration::from_millis(10))
}

Adding a new gateway

  1. Create crates/gateway/src/myexchange.rs
  2. Implement ExchangeGateway
  3. Add the variant to Exchange enum in mercury-core
  4. Register in crates/cli/src/main.rs (gateway match and feed_manager match)