-
Notifications
You must be signed in to change notification settings - Fork 7
/
binance.ts
91 lines (82 loc) · 3.02 KB
/
binance.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { BaseExchangeAdapter, ExchangeAdapter, ExchangeDataType, Ticker } from './base'
import { Exchange } from '../utils'
export class BinanceAdapter extends BaseExchangeAdapter implements ExchangeAdapter {
baseApiUrl = 'https://data-api.binance.vision/api/v3'
readonly _exchangeName: Exchange = Exchange.BINANCE
// GeoTrust RSA CA 2018 - validity not after: 06/11/2027, 09:23:45 GMT-3
_certFingerprint256 =
'BF:8A:69:02:7B:CC:8D:2D:42:A6:E6:D2:5B:DD:48:73:F6:A3:4B:8F:90:ED:F0:7E:86:C5:D6:91:6D:A0:B9:33'
private static readonly tokenSymbolMap = BinanceAdapter.standardTokenSymbolMap
protected generatePairSymbol(): string {
return `${BinanceAdapter.tokenSymbolMap.get(
this.config.baseCurrency
)}${BinanceAdapter.tokenSymbolMap.get(this.config.quoteCurrency)}`
}
async fetchTicker(): Promise<Ticker> {
const tickerJson = await this.fetchFromApi(
ExchangeDataType.TICKER,
`ticker/24hr?symbol=${this.pairSymbol}`
)
return this.parseTicker(tickerJson)
}
/**
*
* @param json parsed response from Binance's ticker endpoint
*
* {
* "symbol": "CELOBTC",
* "priceChange": "0.00000023",
* "priceChangePercent": "0.281",
* "weightedAvgPrice": "0.00008154",
* "prevClosePrice": "0.00008173",
* "lastPrice": "0.00008219",
* "lastQty": "7.10000000",
* "bidPrice": "0.00008213",
* "bidQty": "9.90000000",
* "askPrice": "0.00008243",
* "askQty": "100.00000000",
* "openPrice": "0.00008196",
* "highPrice": "0.00008386",
* "lowPrice": "0.00007948",
* "volume": "155146.90000000",
* "quoteVolume": "12.65048684",
* "openTime": 1614597075604,
* "closeTime": 1614683475604,
* "firstId": 849549, // First tradeId
* "lastId": 854852, // Last tradeId
* "count": 5304 // Trade count
* }
*/
parseTicker(json: any): Ticker {
const ticker = {
...this.priceObjectMetadata,
ask: this.safeBigNumberParse(json.askPrice)!,
baseVolume: this.safeBigNumberParse(json.volume)!,
bid: this.safeBigNumberParse(json.bidPrice)!,
high: this.safeBigNumberParse(json.highPrice),
lastPrice: this.safeBigNumberParse(json.lastPrice)!,
low: this.safeBigNumberParse(json.lowPrice),
open: this.safeBigNumberParse(json.openPrice),
quoteVolume: this.safeBigNumberParse(json.quoteVolume)!,
timestamp: this.safeBigNumberParse(json.closeTime)?.toNumber()!,
}
this.verifyTicker(ticker)
return ticker
}
async isOrderbookLive(): Promise<boolean> {
const res = await this.fetchFromApi(ExchangeDataType.ORDERBOOK_STATUS, 'exchangeInfo')
const marketInfo = (res?.symbols as {
status: string
symbol: string
isSpotTradingAllowed: boolean
orderTypes: string[]
}[])?.find((info) => info?.symbol === this.pairSymbol)
return (
!!marketInfo &&
marketInfo.status === 'TRADING' &&
marketInfo.isSpotTradingAllowed &&
marketInfo.orderTypes.includes('LIMIT') &&
marketInfo.orderTypes.includes('MARKET')
)
}
}