import { upperCaseSymbols } from '../handy' ; import { BookChange , BookTicker , Trade } from '../types' ; import { Mapper } from './mapper' ; export const binanceDexTradesMapper = { canHandle ( message ) { return message . stream === 'trades' ; } , getFilters ( symbols ? ) { symbols = upperCaseSymbols ( symbols ) ; return [ { channel : 'trades' , symbols } ] ; } , * map ( binanceDexTradeResponse , localTimestamp ) { for ( const binanceDexTrade of binanceDexTradeResponse . data ) { yield { type : 'trade' , symbol : binanceDexTrade . s , exchange : 'binance-dex' , id : binanceDexTrade . t , price : Number ( binanceDexTrade . p ) , amount : Number ( binanceDexTrade . q ) , side : binanceDexTrade . tt === 2 ? 'sell' : 'buy' , timestamp : new Date ( Math . floor ( binanceDexTrade . T / 1000000 ) ) , localTimestamp : localTimestamp } ; } } } ; const mapBookLevel = ( level ) => { const price = Number ( level [ 0 ] ) ; const amount = Number ( level [ 1 ] ) ; return { price , amount } ; } ; export const binanceDexBookChangeMapper = { canHandle ( message ) { return message . stream === 'marketDiff' || message . stream === 'depthSnapshot' ; } , getFilters ( symbols ? ) { symbols = upperCaseSymbols ( symbols ) ; return [ { channel : 'depthSnapshot' , symbols } , { channel : 'marketDiff' , symbols } ] ; } , * map ( message , localTimestamp ) { if ( 'symbol' in message . data ) { yield { type : 'book_change' , symbol : message . data . symbol , exchange : 'binance-dex' , isSnapshot : true , bids : message . data . bids . map ( mapBookLevel ) , asks : message . data . asks . map ( mapBookLevel ) , timestamp : localTimestamp , localTimestamp } ; } else { yield { type : 'book_change' , symbol : message . data . s , exchange : 'binance-dex' , isSnapshot : false , bids : message . data . b . map ( mapBookLevel ) , asks : message . data . a . map ( mapBookLevel ) , timestamp : localTimestamp , localTimestamp } ; } } } ; export const binanceDexBookTickerMapper = { canHandle ( message ) { return message . stream === 'ticker' ; } , getFilters ( symbols ? ) { symbols = upperCaseSymbols ( symbols ) ; return [ { channel : 'ticker' , symbols } ] ; } , * map ( binanceDexTradeResponse , localTimestamp ) { const binanceDexTicker = binanceDexTradeResponse . data ; const ticker = { type : 'book_ticker' , symbol : binanceDexTicker . s , exchange : 'binance-dex' , askAmount : binanceDexTicker . A !== undefined ? Number ( binanceDexTicker . A ) : undefined , askPrice : binanceDexTicker . a !== undefined ? Number ( binanceDexTicker . a ) : undefined , bidPrice : binanceDexTicker . b !== undefined ? Number ( binanceDexTicker . b ) : undefined , bidAmount : binanceDexTicker . B !== undefined ? Number ( binanceDexTicker . B ) : undefined , timestamp : binanceDexTicker . E !== undefined ? new Date ( binanceDexTicker . E * 1000 ) : localTimestamp , localTimestamp : localTimestamp } ; yield ticker ; } } ; type BinanceDexResponse < T > = { stream : string ; data : T ; } ; type BinanceDexTradeData = { s : string ; t : string ; p : string ; q : string ; T : number ; tt : number ; } [ ] ; type BinanceDexBookLevel = [ string , string ] ; type BinanceDexDepthSnapshotData = { symbol : string ; bids : BinanceDexBookLevel [ ] ; asks : BinanceDexBookLevel [ ] ; } ; type BinanceDexMarketDiffData = { E : number ; s : string ; b : BinanceDexBookLevel [ ] ; a : BinanceDexBookLevel [ ] ; } ; type BinanceDexTickerData = { e : '24hrTicker' ; E : 123456789 ; s : 'ABC_0DX-BNB' ; b : '0.0024' ; B : '10' ; a : '0.0026' ; A : '100' ; } ;