babel_exchange 1.3.0 copy "babel_exchange: ^1.3.0" to clipboard
babel_exchange: ^1.3.0 copied to clipboard

A unified multi-exchange API client for Binance, MEXC, and Kraken. Supports REST, WebSocket, and provides a common interface across all exchanges.

example/example.dart

import 'package:babel_exchange/babel_exchange.dart';

void main() async {
  // Create MEXC exchange (public API - no auth needed for market data)
  final exchange = BabelExchange.create(Exchange.mexc);

  print('=== Babel Exchange Demo ===\n');

  // Test connectivity
  await exchange.ping();
  print('Connected to ${exchange.name}');

  // Get server time
  final serverTime = await exchange.getServerTime();
  print('Server time: $serverTime\n');

  // Get ticker
  final ticker = await exchange.getTicker('BTCUSDT');
  print('BTC/USDT Ticker:');
  print('  Last Price: \$${ticker.lastPrice.toStringAsFixed(2)}');
  print('  24h Change: ${ticker.priceChangePercent.toStringAsFixed(2)}%');
  print('  24h Volume: ${ticker.volume.toStringAsFixed(2)} BTC\n');

  // Get order book
  final orderBook = await exchange.getOrderBook('BTCUSDT', limit: 5);
  print('Order Book (top 5):');
  print('  Best Bid: ${orderBook.bids.first.price} @ ${orderBook.bids.first.quantity}');
  print('  Best Ask: ${orderBook.asks.first.price} @ ${orderBook.asks.first.quantity}');
  print('  Spread: ${orderBook.spreadBps.toStringAsFixed(2)} bps\n');

  // Get klines
  final klines = await exchange.getKlines(
    symbol: 'BTCUSDT',
    interval: KlineInterval.h1,
    limit: 5,
  );
  print('Last 5 hourly candles:');
  for (final candle in klines) {
    print('  ${candle.openTime}: O=${candle.open} H=${candle.high} L=${candle.low} C=${candle.close}');
  }
  print('');

  // Calculate fees
  final fees = BabelExchange.getDefaultFees(Exchange.mexc);
  final notional = 1000.0; // $1000 trade
  print('Fee calculation for \$1000 trade on MEXC:');
  print('  Maker fee: \$${fees.calculateFee(notional, isMaker: true).toStringAsFixed(4)}');
  print('  Taker fee: \$${fees.calculateFee(notional, isMaker: false).toStringAsFixed(4)}\n');

  // Authenticated example (uncomment with real keys)
  /*
  final authExchange = BabelExchange.create(
    Exchange.mexc,
    apiKey: 'your-api-key',
    secretKey: 'your-secret-key',
  );

  // Get account info
  final account = await authExchange.getAccountInfo();
  print('Account balances:');
  for (final balance in account.balances.where((b) => b.free > 0)) {
    print('  ${balance.asset}: ${balance.free} (locked: ${balance.locked})');
  }

  // Place a test order
  await authExchange.testOrder(
    symbol: 'BTCUSDT',
    side: OrderSide.buy,
    type: OrderType.limit,
    quantity: 0.001,
    price: 50000,
  );
  print('Test order validated successfully');

  // Place a real order
  final order = await authExchange.placeLimitOrder(
    symbol: 'BTCUSDT',
    side: OrderSide.buy,
    quantity: 0.001,
    price: 50000,
  );
  print('Order placed: ${order.orderId}');

  authExchange.dispose();
  */

  // WebSocket example (commented to avoid long-running demo)
  /*
  print('Subscribing to trades...');
  final subscription = exchange.subscribeToTrades('BTCUSDT').listen((trade) {
    print('Trade: ${trade.price} x ${trade.quantity} (${trade.isBuyerMaker ? "SELL" : "BUY"})');
  });

  // Listen for 10 seconds
  await Future.delayed(Duration(seconds: 10));
  await subscription.cancel();
  */

  exchange.dispose();
  print('Demo completed!');
}
0
likes
150
points
34
downloads

Documentation

API reference

Publisher

verified publishermaycloud.uk

Weekly Downloads

A unified multi-exchange API client for Binance, MEXC, and Kraken. Supports REST, WebSocket, and provides a common interface across all exchanges.

Repository (GitHub)

Topics

#cryptocurrency #trading #api-client #binance #kraken

License

MIT (license)

Dependencies

crypto, http, web_socket_channel

More

Packages that depend on babel_exchange