forked from celo-org/celo-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
okcoin.ts
125 lines (115 loc) · 3.96 KB
/
okcoin.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Exchange } from '../utils'
import { BaseExchangeAdapter, ExchangeDataType, Ticker, Trade } from './base'
export class OKCoinAdapter extends BaseExchangeAdapter {
baseApiUrl = 'https://www.okcoin.com/api'
// Cloudflare Inc ECC CA-3
readonly _certFingerprint256 =
'3A:BB:E6:3D:AF:75:6C:50:16:B6:B8:5F:52:01:5F:D8:E8:AC:BE:27:7C:50:87:B1:27:A6:05:63:A8:41:ED:8A'
readonly _exchangeName = Exchange.OKCOIN
// There are no known deviations from the standard mapping
private static readonly tokenSymbolMap = OKCoinAdapter.standardTokenSymbolMap
async fetchTicker(): Promise<Ticker> {
const json = await this.fetchFromApi(
ExchangeDataType.TICKER,
`spot/v3/instruments/${this.pairSymbol}/ticker`
)
return this.parseTicker(json)
}
async fetchTrades(): Promise<Trade[]> {
const tradeJson = await this.fetchFromApi(
ExchangeDataType.TRADE,
`spot/v3/instruments/${this.pairSymbol}/trades`
)
return this.parseTrades(tradeJson)
}
/**
* Parses the json response from the ticker endpoint and returns a Ticker object
*
* @param json response from /spot/v3/instruments/${this.pairSymbol}/ticker
*
* Example response from OKCoin docs: https://www.okcoin.com/docs/en/#spot-some
* {
* "best_ask": "7222.2",
* "best_bid": "7222.1",
* "instrument_id": "BTC-USDT",
* "product_id": "BTC-USDT",
* "last": "7222.2",
* "last_qty": "0.00136237",
* "ask": "7222.2",
* "best_ask_size": "0.09207739",
* "bid": "7222.1",
* "best_bid_size": "3.61314948",
* "open_24h": "7356.8",
* "high_24h": "7367.7",
* "low_24h": "7160",
* "base_volume_24h": "18577.2",
* "timestamp": "2019-12-11T07:48:04.014Z",
* "quote_volume_24h": "134899542.8"
* }
*/
parseTicker(json: any): Ticker {
const ticker = {
...this.priceObjectMetadata,
ask: this.safeBigNumberParse(json.ask)!,
baseVolume: this.safeBigNumberParse(json.base_volume_24h)!,
bid: this.safeBigNumberParse(json.bid)!,
high: this.safeBigNumberParse(json.high_24h),
lastPrice: this.safeBigNumberParse(json.last)!,
low: this.safeBigNumberParse(json.low_24h),
open: this.safeBigNumberParse(json.open_24h),
quoteVolume: this.safeBigNumberParse(json.quote_volume_24h)!,
timestamp: this.safeDateParse(json.timestamp)!,
}
this.verifyTicker(ticker)
return ticker
}
/**
* Parses the response from the trades endpoint and returns an array of standard
* Trade objects.
*
* @param json response from /spot/v3/instruments/${this.pairSymbol}/trades
*
* Example response from OKCoin Docs: https://www.okcoin.com/docs/en/#spot-deal_information
*
* [
* {
* "time":"2019-04-12T02:07:30.523Z",
* "timestamp":"2019-04-12T02:07:30.523Z",
* "trade_id":"1296412902",
* "price":"4913.4",
* "size":"0.00990734",
* "side":"buy"
* }
* ]
*/
parseTrades(json: any): Trade[] {
return json.map((trade: any) => {
const price = this.safeBigNumberParse(trade.price)
const amount = this.safeBigNumberParse(trade.size)
const cost = amount ? price?.times(amount) : undefined
const normalizedTrade = {
...this.priceObjectMetadata,
amount,
cost,
id: trade.trade_id,
price,
side: trade.side,
timestamp: this.safeDateParse(trade.timestamp),
}
this.verifyTrade(normalizedTrade)
return normalizedTrade
})
}
protected generatePairSymbol(): string {
return `${OKCoinAdapter.tokenSymbolMap.get(
this.config.baseCurrency
)}-${OKCoinAdapter.tokenSymbolMap.get(this.config.quoteCurrency)}`
}
/**
* OKCoin doesn't have an endpoint to check this. So, return true, and assume
* that if the API can be reached, the orderbook is live.
*/
async isOrderbookLive(): Promise<boolean> {
return true
}
}