Skip to content

Commit

Permalink
clear data type names with unlikely collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepighetti committed Feb 11, 2019
1 parent 8b139a1 commit 1e0e5df
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
6 changes: 3 additions & 3 deletions lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BinanceHttp {
/// Acceptable intervals are
/// 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
Future<List<Candlestick>> candlesticks(
Future<List<Kline>> candlesticks(
String symbol,
String interval, {
DateTime startTime,
Expand All @@ -86,8 +86,8 @@ class BinanceHttp {

final response = await _public('/v1/klines', params);

return List<Candlestick>.from(
response.map((c) => Candlestick.fromList(c)),
return List<Kline>.from(
response.map((c) => Kline.fromList(c)),
);
}

Expand Down
31 changes: 16 additions & 15 deletions lib/http_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ class ExchangeInfoResponse {
final String timezone;
final DateTime serverTime;

final List<EISymbol> symbols;
final List<ExchangeInfoSymbol> symbols;

ExchangeInfoResponse.fromMap(Map m)
: this.timezone = m['timezone'],
this.serverTime = DateTime.fromMillisecondsSinceEpoch(m['serverTime']),
this.symbols =
m['symbols'].map<EISymbol>((s) => EISymbol.fromMap(s)).toList();
this.symbols = m['symbols']
.map<ExchangeInfoSymbol>((s) => ExchangeInfoSymbol.fromMap(s))
.toList();
}

/// A class that represents the Symbols returned by /v1/exchangeInfo
class EISymbol {
class ExchangeInfoSymbol {
final String symbol;

/// PRE_TRADING, TRADING, POST_TRADING, END_OF_DAY, HALT, AUCTION_MATCH, BREAK
Expand All @@ -30,7 +31,7 @@ class EISymbol {
final bool icebergAllowed;
// List<Filter> filters;

EISymbol.fromMap(Map m)
ExchangeInfoSymbol.fromMap(Map m)
: this.symbol = m['symbol'],
this.status = m['status'],
this.baseAsset = m['baseAsset'],
Expand All @@ -45,23 +46,23 @@ class EISymbol {
class BookDepth {
final int lastUpdateId;
final List<BDPoint> bids;
final List<BDPoint> asks;
final List<BookDepthPoint> bids;
final List<BookDepthPoint> asks;

BookDepth.fromMap(Map m)
: this.lastUpdateId = m["lastUpdateId"],
this.bids =
List<BDPoint>.from(m["bids"].map((b) => BDPoint.fromList(b))),
this.asks =
List<BDPoint>.from(m["asks"].map((b) => BDPoint.fromList(b)));
this.bids = List<BookDepthPoint>.from(
m["bids"].map((b) => BookDepthPoint.fromList(b))),
this.asks = List<BookDepthPoint>.from(
m["asks"].map((b) => BookDepthPoint.fromList(b)));
}

/// A class that represents the bids/asks data provided by /v1/depth
class BDPoint {
class BookDepthPoint {
final num price;
final num qty;

BDPoint.fromList(List values)
BookDepthPoint.fromList(List values)
: this.price = num.parse(values.first),
this.qty = num.parse(values[1]);
}
Expand Down Expand Up @@ -111,7 +112,7 @@ class AggregatedTrade implements RecentTrade {

/// A class that represents a candlestick provided by /v1/klines
class Candlestick {
class Kline {
final DateTime openTime;
final double open;
final double high;
Expand All @@ -125,7 +126,7 @@ class Candlestick {
final double takerBase;
final double takerQuote;

Candlestick.fromList(List c)
Kline.fromList(List c)
: this.openTime = DateTime.fromMillisecondsSinceEpoch(c.first),
this.open = double.parse(c[1]),
this.high = double.parse(c[2]),
Expand Down
33 changes: 17 additions & 16 deletions lib/ws.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,49 @@ class BinanceWebsocket {
List<Map> _toList(json) => List<Map>.from(convert.jsonDecode(json));

/// Reports aggregated trade events from <symbol>@aggTrade
Stream<WSAggTrade> aggTrade(String symbol) {
Stream<WebsocketAggregatedTrade> aggTrade(String symbol) {
final channel = _public("${symbol.toLowerCase()}@aggTrade");

return channel.stream
.map<Map>(_toMap)
.map<WSAggTrade>((e) => WSAggTrade.fromMap(e));
return channel.stream.map<Map>(_toMap).map<WebsocketAggregatedTrade>(
(e) => WebsocketAggregatedTrade.fromMap(e));
}

/// Reports 24hr miniTicker events every second from <symbol>@miniTicker
Stream<WSMiniTicker> miniTicker(String symbol) {
Stream<WebsocketMiniTicker> miniTicker(String symbol) {
final channel = _public("${symbol.toLowerCase()}@miniTicker");

return channel.stream
.map<Map>(_toMap)
.map<WSMiniTicker>((e) => WSMiniTicker.fromMap(e));
.map<WebsocketMiniTicker>((e) => WebsocketMiniTicker.fromMap(e));
}

/// Reports 24hr miniTicker events every second for every trading pair
/// that changed in the last second
Stream<List<WSMiniTicker>> allMiniTickers() {
Stream<List<WebsocketMiniTicker>> allMiniTickers() {
final channel = _public("!miniTicker@arr");

return channel.stream.map<List<Map>>(_toList).map<List<WSMiniTicker>>(
(ev) => ev.map((m) => WSMiniTicker.fromMap(m)).toList());
return channel.stream
.map<List<Map>>(_toList)
.map<List<WebsocketMiniTicker>>(
(ev) => ev.map((m) => WebsocketMiniTicker.fromMap(m)).toList());
}

/// Reports 24hr ticker events every second from <symbol>@ticker
Stream<WSTicker> ticker(String symbol) {
Stream<WebsocketTicker> ticker(String symbol) {
final channel = _public("${symbol.toLowerCase()}@ticker");

return channel.stream
.map<Map>(_toMap)
.map<WSTicker>((e) => WSTicker.fromMap(e));
.map<WebsocketTicker>((e) => WebsocketTicker.fromMap(e));
}

/// Reports 24hr miniTicker events every second for every trading pair
/// that changed in the last second
Stream<List<WSTicker>> allTickers() {
Stream<List<WebsocketTicker>> allTickers() {
final channel = _public("!ticker@arr");

return channel.stream.map<List<Map>>(_toList).map<List<WSTicker>>(
(ev) => ev.map((m) => WSTicker.fromMap(m)).toList());
return channel.stream.map<List<Map>>(_toList).map<List<WebsocketTicker>>(
(ev) => ev.map((m) => WebsocketTicker.fromMap(m)).toList());
}

/// Reports book depth
Expand All @@ -74,11 +75,11 @@ class BinanceWebsocket {
/// Difference book depth
///
/// This can be used to update an existing book with incremental changes
Stream<WSDiffBookDepth> diffBookDepth(String symbol) {
Stream<DiffBookDepth> diffBookDepth(String symbol) {
final channel = _public("${symbol.toLowerCase()}@depth");

return channel.stream
.map<Map>(_toMap)
.map<WSDiffBookDepth>((m) => WSDiffBookDepth.fromMap(m));
.map<DiffBookDepth>((m) => DiffBookDepth.fromMap(m));
}
}
30 changes: 16 additions & 14 deletions lib/ws_classes.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'http_classes.dart' show AggregatedTrade, BookDepth, BDPoint;
import 'http_classes.dart' show AggregatedTrade, BookDepth, BookDepthPoint;
export 'http_classes.dart' show BookDepth;

abstract class WSBase {
abstract class WebsocketBase {
String get eventType;
DateTime get eventTime;
String get symbol;
}

/// Represents data provided by <symbol>@aggTrade
class WSAggTrade implements AggregatedTrade, WSBase {
class WebsocketAggregatedTrade implements AggregatedTrade, WebsocketBase {
final String eventType;
final DateTime eventTime;
final String symbol;
Expand All @@ -24,7 +24,7 @@ class WSAggTrade implements AggregatedTrade, WSBase {
final bool isBuyerMaker;
final bool isBestMatch;

WSAggTrade.fromMap(Map m)
WebsocketAggregatedTrade.fromMap(Map m)
: this.eventType = m['e'],
this.eventTime = DateTime.fromMillisecondsSinceEpoch(m['E']),
this.symbol = m['s'],
Expand All @@ -39,7 +39,7 @@ class WSAggTrade implements AggregatedTrade, WSBase {
}

/// Represents data provided by <symbol>@miniTicker and !miniTicker@arr
class WSMiniTicker implements WSBase {
class WebsocketMiniTicker implements WebsocketBase {
final String eventType;
final DateTime eventTime;
final String symbol;
Expand All @@ -51,7 +51,7 @@ class WSMiniTicker implements WSBase {
final double volume;
final double quoteVolume;

WSMiniTicker.fromMap(Map m)
WebsocketMiniTicker.fromMap(Map m)
: this.eventType = m['e'],
this.eventTime = DateTime.fromMillisecondsSinceEpoch(m['E']),
this.symbol = m['s'],
Expand All @@ -64,7 +64,7 @@ class WSMiniTicker implements WSBase {
}

/// Represents data provided by <symbol>@ticker and !ticker@arr
class WSTicker implements WSMiniTicker {
class WebsocketTicker implements WebsocketMiniTicker {
final String eventType;
final DateTime eventTime;
final String symbol;
Expand Down Expand Up @@ -95,7 +95,7 @@ class WSTicker implements WSMiniTicker {

double get close => lastPrice; // extra from WSMiniTicker

WSTicker.fromMap(Map m)
WebsocketTicker.fromMap(Map m)
: this.eventType = m['e'],
this.eventTime = DateTime.fromMillisecondsSinceEpoch(m['E']),
this.symbol = m['s'],
Expand All @@ -121,23 +121,25 @@ class WSTicker implements WSMiniTicker {
this.tradesCount = m['n'];
}

class WSDiffBookDepth implements BookDepth, WSBase {
class DiffBookDepth implements BookDepth, WebsocketBase {
final String eventType;
final DateTime eventTime;
final String symbol;

final int firstUpdateId;
final int lastUpdateId;

final List<BDPoint> bids;
final List<BDPoint> asks;
final List<BookDepthPoint> bids;
final List<BookDepthPoint> asks;

WSDiffBookDepth.fromMap(Map m)
DiffBookDepth.fromMap(Map m)
: this.eventType = m['e'],
this.eventTime = DateTime.fromMillisecondsSinceEpoch(m['E']),
this.symbol = m['s'],
this.firstUpdateId = m["U"],
this.lastUpdateId = m["u"],
this.bids = List<BDPoint>.from(m["b"].map((b) => BDPoint.fromList(b))),
this.asks = List<BDPoint>.from(m["a"].map((b) => BDPoint.fromList(b)));
this.bids = List<BookDepthPoint>.from(
m["b"].map((b) => BookDepthPoint.fromList(b))),
this.asks = List<BookDepthPoint>.from(
m["a"].map((b) => BookDepthPoint.fromList(b)));
}

0 comments on commit 1e0e5df

Please sign in to comment.