Skip to content

Commit

Permalink
improvement: automatically fix incorrect symbols casing in mappers wh…
Browse files Browse the repository at this point in the history
…en provided by user
  • Loading branch information
thaaddeus committed Dec 28, 2021
1 parent ec4745d commit bfd0cc9
Show file tree
Hide file tree
Showing 27 changed files with 231 additions and 42 deletions.
14 changes: 14 additions & 0 deletions src/handy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,18 @@ if (httpsProxyAgent !== undefined) {
}
}

export function upperCaseSymbols(symbols?: string[]) {
if (symbols !== undefined) {
return symbols.map((s) => s.toUpperCase())
}
return
}

export function lowerCaseSymbols(symbols?: string[]) {
if (symbols !== undefined) {
return symbols.map((s) => s.toLowerCase())
}
return
}

export const httpClient = got.extend(gotDefaultOptions)
7 changes: 6 additions & 1 deletion src/mappers/ascendex.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { upperCaseSymbols } from '../handy'
import { BookChange, BookTicker, DerivativeTicker, Trade } from '../types'
import { Mapper, PendingTickerInfoHelper } from './mapper'
import { Trade, BookChange, DerivativeTicker, BookTicker } from '../types'

export class AscendexTradesMapper implements Mapper<'ascendex', Trade> {
canHandle(message: AscendexTrade) {
return message.m === 'trades'
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)
return [
{
channel: 'trades',
Expand Down Expand Up @@ -38,6 +40,7 @@ export class AscendexBookChangeMapper implements Mapper<'ascendex', BookChange>
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)
return [
{
channel: 'depth-realtime',
Expand Down Expand Up @@ -78,6 +81,7 @@ export class AscendexDerivativeTickerMapper implements Mapper<'ascendex', Deriva
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)
return [
{
channel: 'futures-pricing-data',
Expand Down Expand Up @@ -120,6 +124,7 @@ export class AscendexBookTickerMapper implements Mapper<'ascendex', BookTicker>
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)
return [
{
channel: 'bbo',
Expand Down
11 changes: 2 additions & 9 deletions src/mappers/binance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { debug } from '../debug'
import { CircularBuffer } from '../handy'
import { BookChange, DerivativeTicker, Exchange, FilterForExchange, Liquidation, BookTicker, Trade } from '../types'
import { CircularBuffer, lowerCaseSymbols } from '../handy'
import { BookChange, BookTicker, DerivativeTicker, Exchange, FilterForExchange, Liquidation, Trade } from '../types'
import { Mapper, PendingTickerInfoHelper } from './mapper'

// https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md
Expand Down Expand Up @@ -447,13 +447,6 @@ export class BinanceBookTickerMapper implements Mapper<'binance-futures' | 'bina
}
}

function lowerCaseSymbols(symbols?: string[]) {
if (symbols !== undefined) {
return symbols.map((s) => s.toLowerCase())
}
return
}

type BinanceResponse<T> = {
stream: string
data: T
Expand Down
7 changes: 6 additions & 1 deletion src/mappers/binancedex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BookChange, Trade, BookTicker } from '../types'
import { upperCaseSymbols } from '../handy'
import { BookChange, BookTicker, Trade } from '../types'
import { Mapper } from './mapper'

// https://docs.binance.org/api-reference/dex-api/ws-streams.html
Expand All @@ -9,6 +10,8 @@ export const binanceDexTradesMapper: Mapper<'binance-dex', Trade> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'trades',
Expand Down Expand Up @@ -46,6 +49,7 @@ export const binanceDexBookChangeMapper: Mapper<'binance-dex', BookChange> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)
return [
{
channel: 'depthSnapshot',
Expand Down Expand Up @@ -96,6 +100,7 @@ export const binanceDexBookTickerMapper: Mapper<'binance-dex', BookTicker> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)
return [
{
channel: 'ticker',
Expand Down
8 changes: 7 additions & 1 deletion src/mappers/binanceoptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { asNumberIfValid } from '../handy'
import { asNumberIfValid, upperCaseSymbols } from '../handy'
import { BookChange, OptionSummary, Trade } from '../types'
import { Mapper } from './mapper'

Expand All @@ -15,6 +15,8 @@ export class BinanceOptionsTradesMapper implements Mapper<'binance-options', Tra
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'TRADE',
Expand Down Expand Up @@ -68,6 +70,8 @@ export class BinanceOptionsBookChangeMapper implements Mapper<'binance-options',
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'DEPTH100',
Expand Down Expand Up @@ -109,6 +113,8 @@ export class BinanceOptionSummaryMapper implements Mapper<'binance-options', Opt
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

const indexes =
symbols !== undefined
? symbols.map((s) => {
Expand Down
11 changes: 10 additions & 1 deletion src/mappers/bitfinex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BookChange, DerivativeTicker, Exchange, FilterForExchange, Liquidation, Trade, BookTicker } from '../types'
import { upperCaseSymbols } from '../handy'
import { BookChange, BookTicker, DerivativeTicker, Exchange, FilterForExchange, Liquidation, Trade } from '../types'
import { Mapper, PendingTickerInfoHelper } from './mapper'

// https://docs.bitfinex.com/v2/docs/ws-general
Expand Down Expand Up @@ -33,6 +34,8 @@ export class BitfinexTradesMapper implements Mapper<'bitfinex' | 'bitfinex-deriv
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'trades',
Expand Down Expand Up @@ -107,6 +110,8 @@ export class BitfinexBookChangeMapper implements Mapper<'bitfinex' | 'bitfinex-d
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'book',
Expand Down Expand Up @@ -187,6 +192,8 @@ export class BitfinexDerivativeTickerMapper implements Mapper<'bitfinex-derivati
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'status',
Expand Down Expand Up @@ -345,6 +352,8 @@ export class BitfinexBookTickerMapper implements Mapper<'bitfinex' | 'bitfinex-d
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'ticker',
Expand Down
10 changes: 8 additions & 2 deletions src/mappers/bitflyer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseμs } from '../handy'
import { BookChange, Trade, BookTicker } from '../types'
import { parseμs, upperCaseSymbols } from '../handy'
import { BookChange, BookTicker, Trade } from '../types'
import { Mapper } from './mapper'

export const bitflyerTradesMapper: Mapper<'bitflyer', Trade> = {
Expand All @@ -8,6 +8,8 @@ export const bitflyerTradesMapper: Mapper<'bitflyer', Trade> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'lightning_executions',
Expand Down Expand Up @@ -52,6 +54,8 @@ export class BitflyerBookChangeMapper implements Mapper<'bitflyer', BookChange>
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'lightning_board_snapshot',
Expand Down Expand Up @@ -97,6 +101,8 @@ export const bitflyerBookTickerMapper: Mapper<'bitflyer', BookTicker> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'lightning_ticker',
Expand Down
14 changes: 12 additions & 2 deletions src/mappers/bitmex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { asNumberIfValid } from '../handy'
import { BookChange, BookPriceLevel, DerivativeTicker, FilterForExchange, Liquidation, Trade, BookTicker } from '../types'
import { asNumberIfValid, upperCaseSymbols } from '../handy'
import { BookChange, BookPriceLevel, BookTicker, DerivativeTicker, FilterForExchange, Liquidation, Trade } from '../types'
import { Mapper, PendingTickerInfoHelper } from './mapper'

// https://www.bitmex.com/app/wsAPI
Expand All @@ -10,6 +10,8 @@ export const bitmexTradesMapper: Mapper<'bitmex', Trade> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'trade',
Expand Down Expand Up @@ -45,6 +47,8 @@ export class BitmexBookChangeMapper implements Mapper<'bitmex', BookChange> {
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'orderBookL2',
Expand Down Expand Up @@ -150,6 +154,8 @@ export class BitmexDerivativeTickerMapper implements Mapper<'bitmex', Derivative
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'instrument',
Expand Down Expand Up @@ -198,6 +204,8 @@ export const bitmexLiquidationsMapper: Mapper<'bitmex', Liquidation> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'liquidation',
Expand Down Expand Up @@ -231,6 +239,8 @@ export const bitmexBookTickerMapper: Mapper<'bitmex', BookTicker> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'quote',
Expand Down
8 changes: 1 addition & 7 deletions src/mappers/bitstamp.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { lowerCaseSymbols } from '../handy'
import { BookChange, Trade } from '../types'
import { Mapper } from './mapper'

Expand Down Expand Up @@ -168,13 +169,6 @@ export class BitstampBookChangeMapper implements Mapper<'bitstamp', BookChange>
}
}

function lowerCaseSymbols(symbols?: string[]) {
if (symbols !== undefined) {
return symbols.map((s) => s.toLowerCase())
}
return
}

type BitstampTrade = {
event: 'trade'
channel: string
Expand Down
9 changes: 9 additions & 0 deletions src/mappers/bybit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { upperCaseSymbols } from '../handy'
import { BookChange, DerivativeTicker, Exchange, Liquidation, Trade } from '../types'
import { Mapper, PendingTickerInfoHelper } from './mapper'

Expand All @@ -14,6 +15,8 @@ export class BybitTradesMapper implements Mapper<'bybit', Trade> {
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'trade',
Expand Down Expand Up @@ -57,6 +60,8 @@ export class BybitBookChangeMapper implements Mapper<'bybit', BookChange> {
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

if (this._canUseBook200Channel) {
return [
{
Expand Down Expand Up @@ -117,6 +122,8 @@ export class BybitDerivativeTickerMapper implements Mapper<'bybit', DerivativeTi
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'instrument_info',
Expand Down Expand Up @@ -190,6 +197,8 @@ export class BybitLiquidationsMapper implements Mapper<'bybit', Liquidation> {
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'liquidation',
Expand Down
8 changes: 7 additions & 1 deletion src/mappers/coinbase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseμs, upperCaseSymbols } from '../handy'
import { BookChange, BookPriceLevel, BookTicker, Trade } from '../types'
import { Mapper } from './mapper'
import { parseμs } from '../handy'

// https://docs.pro.coinbase.com/#websocket-feed

Expand All @@ -10,6 +10,8 @@ export const coinbaseTradesMapper: Mapper<'coinbase', Trade> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'match',
Expand Down Expand Up @@ -69,6 +71,8 @@ export class CoinbaseBookChangMapper implements Mapper<'coinbase', BookChange> {
}

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'snapshot',
Expand Down Expand Up @@ -128,6 +132,8 @@ export const coinbaseBookTickerMapper: Mapper<'coinbase', BookTicker> = {
},

getFilters(symbols?: string[]) {
symbols = upperCaseSymbols(symbols)

return [
{
channel: 'ticker',
Expand Down
Loading

0 comments on commit bfd0cc9

Please sign in to comment.