websocket-crypto-api
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -1,171 +0,175 @@ | ||
import Exchanges from "./baseExchange"; | ||
import Exchanges from './baseExchange'; | ||
const PROXY = 'https://pure-mountain-67034.herokuapp.com/'; | ||
export default class Binance extends Exchanges { | ||
constructor(proxy) { | ||
super(); | ||
this.name = "Binance"; | ||
this._mainUrl = "wss://stream.binance.com:9443/ws/"; | ||
this._socket = 0; | ||
this._proxy = proxy ? proxy : PROXY; | ||
constructor(proxy) { | ||
super(); | ||
this.name = 'Binance'; | ||
this._mainUrl = 'wss://stream.binance.com:9443/ws/'; | ||
this._socket = 0; | ||
this._proxy = proxy || PROXY; | ||
this.orderBook = symbol => `${this._proxy}https://www.binance.com/api/v1/depth?symbol=${symbol}&limit=20`; | ||
this.orderBook = symbol => `${ | ||
this._proxy | ||
}https://www.binance.com/api/v1/depth?symbol=${symbol}&limit=50`; | ||
this.streams = { | ||
depth: symbol => `${symbol.toLowerCase()}@depth`, | ||
depthLevel: (symbol, level) => `${symbol.toLowerCase()}@depth${level}`, | ||
kline: (symbol, interval) => `${symbol.toLowerCase()}@kline_${this.times[interval]}`, | ||
trade: symbol => `${symbol.toLowerCase()}@aggTrade`, | ||
ticker: symbol => `${symbol.toLowerCase()}@ticker`, | ||
allTickers: () => "!ticker@arr" | ||
}; | ||
this.times = { | ||
"1": '1m', | ||
"5": "5m", | ||
"15": "15m", | ||
"30": "30m", | ||
"60": "1h", | ||
"120": "2h", | ||
"240": "4h", | ||
"D": "1d", | ||
"W": "1w" | ||
} | ||
this.streams = { | ||
depth: symbol => `${symbol.toLowerCase()}@depth`, | ||
depthLevel: (symbol, level) => `${symbol.toLowerCase()}@depth${level}`, | ||
kline: (symbol, interval) => `${symbol.toLowerCase()}@kline_${this.times[interval]}`, | ||
trade: symbol => `${symbol.toLowerCase()}@aggTrade`, | ||
ticker: symbol => `${symbol.toLowerCase()}@ticker`, | ||
allTickers: () => '!ticker@arr', | ||
}; | ||
this.times = { | ||
1: '1m', | ||
5: '5m', | ||
15: '15m', | ||
30: '30m', | ||
60: '1h', | ||
120: '2h', | ||
240: '4h', | ||
D: '1d', | ||
W: '1w', | ||
}; | ||
} | ||
_setupWebSocket(eventHandler, path) { | ||
if (this._socket) { | ||
return this._socket; | ||
} | ||
_setupWebSocket(eventHandler, path) { | ||
if (this._socket) { | ||
return this._socket; | ||
} | ||
// console.log(path); | ||
const ws = new WebSocket(this._mainUrl + path); | ||
// console.log(path); | ||
path = this._mainUrl + path; | ||
const ws = new WebSocket(path); | ||
ws.onclose = event => { | ||
if (event.wasClean) { | ||
console.log('Соединение закрыто чисто'); | ||
} else { | ||
console.log('Обрыв соединения'); | ||
} | ||
}; | ||
ws.onclose = event => { | ||
if (event.wasClean) { | ||
console.log("Соединение закрыто чисто"); | ||
} else { | ||
console.log("Обрыв соединения"); | ||
} | ||
}; | ||
ws.onmessage = event => { | ||
const res = JSON.parse(event.data); | ||
eventHandler(res); | ||
}; | ||
ws.onmessage = event => { | ||
const res = JSON.parse(event.data); | ||
eventHandler(res) | ||
}; | ||
this._socket = ws; | ||
return ws; | ||
} | ||
this._socket = ws; | ||
return ws; | ||
} | ||
closeWebSocket() { | ||
const socket = this._socket; | ||
socket.close(); | ||
} | ||
closeWebSocket() { | ||
const socket = this._socket; | ||
socket.close(); | ||
} | ||
onTrade(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
// console.log(symbol); | ||
onTrade(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
// console.log(symbol); | ||
const handler = res => { | ||
const side = res.m ? 'sell' : 'buy'; | ||
const trade = { | ||
id: res.f, | ||
side, | ||
timestamp: res.T, | ||
price: +res.p, | ||
amount: +res.q, | ||
symbol: res.s, | ||
}; | ||
eventHandler(trade); | ||
}; | ||
let handler = (res) => { | ||
const side = res.m ? "sell" : "buy"; | ||
const trade = { | ||
id: res.f, | ||
side: side, | ||
timestamp: res.T, | ||
price: +res.p, | ||
amount: +res.q, | ||
symbol: res.s | ||
}; | ||
eventHandler(trade) | ||
return this._setupWebSocket(handler, this.streams.trade(newSymbol)); | ||
} | ||
onDepthUpdate(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
fetch(this.orderBook(newSymbol)) | ||
.then(r => r.json()) | ||
.then(res => { | ||
const data = { | ||
asks: [], | ||
bids: [], | ||
type: 'snapshot', | ||
}; | ||
res.asks.forEach(r => data.asks.push([+r[0], +r[1]])); | ||
res.bids.forEach(r => data.bids.push([+r[0], +r[1]])); | ||
data.asks.reverse(); | ||
eventHandler(data); | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.trade(newSymbol) | ||
); | ||
} | ||
const handler = res => { | ||
const data = { | ||
asks: [], | ||
bids: [], | ||
type: 'update', | ||
}; | ||
res.a.forEach(r => data.asks.push([+r[0], +r[1]])); | ||
res.b.forEach(r => data.bids.push([+r[0], +r[1]])); | ||
eventHandler(data); | ||
}; | ||
return this._setupWebSocket(handler, this.streams.depth(newSymbol)); | ||
}); | ||
} | ||
onDepthUpdate(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
fetch(this.orderBook(newSymbol)).then(r=>r.json()).then(res=>{ | ||
let data = { | ||
asks:[], | ||
bids:[], | ||
type:'snapshot' | ||
}; | ||
res.asks.forEach(r=>data.asks.push([+r[0], +r[1]])); | ||
res.bids.forEach(r=>data.bids.push([+r[0], +r[1]])); | ||
eventHandler(data); | ||
onDepthLevelUpdate(symbol, level, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
let handler = (res)=>{ | ||
let data = { | ||
asks:[], | ||
bids:[], | ||
type:'update' | ||
}; | ||
res.a.forEach(r=>data.asks.push([+r[0], +r[1]])); | ||
res.b.forEach(r=>data.bids.push([+r[0], +r[1]])); | ||
eventHandler(data) | ||
}; | ||
return this._setupWebSocket(handler, this.streams.depth(newSymbol)); | ||
}); | ||
} | ||
const handler = res => { | ||
res.bids = res.bids.map(r => { | ||
r.pop(); | ||
return r.map(e => +e); | ||
}); | ||
res.asks = res.asks | ||
.map(r => { | ||
r.pop(); | ||
return r.map(e => +e); | ||
}) | ||
.reverse(); | ||
eventHandler(res); | ||
}; | ||
onDepthLevelUpdate(symbol, level, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.depthLevel(newSymbol, level) | ||
); | ||
} | ||
let handler = (res) => { | ||
res.bids = res.bids.map(r => { | ||
r.pop(); | ||
return r.map(e => +e) | ||
}); | ||
res.asks = res.asks.map(r => { | ||
r.pop(); | ||
return r.map(e => +e) | ||
}).reverse(); | ||
eventHandler(res) | ||
}; | ||
onKline(symbol, interval, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
const handler = data => { | ||
const newData = { | ||
close: +data.k.c, | ||
high: +data.k.h, | ||
low: +data.k.l, | ||
open: +data.k.o, | ||
time: +data.k.t, | ||
volume: +data.k.v, | ||
}; | ||
eventHandler(newData); | ||
}; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.kline(newSymbol, interval) | ||
); | ||
} | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.depthLevel(newSymbol, level) | ||
); | ||
} | ||
onKline(symbol, interval, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
let handler = data => { | ||
const newData = { | ||
close: +data.k.c, | ||
high: +data.k.h, | ||
low: +data.k.l, | ||
open: +data.k.o, | ||
time: +data.k.t, | ||
volume: +data.k.v | ||
}; | ||
eventHandler(newData); | ||
}; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.kline(newSymbol, interval) | ||
); | ||
} | ||
// onTicker(symbol, eventHandler) { | ||
// const splitSymbol = symbol.split(/[:/]/); | ||
// const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
// const handler = (res)=>{ | ||
// let data={ | ||
// symbol:symbol, | ||
// }; | ||
// eventHandler(res) | ||
// }; | ||
// | ||
// return this._setupWebSocket(handler, this.streams.ticker(newSymbol)); | ||
// } | ||
// onTicker(symbol, eventHandler) { | ||
// const splitSymbol = symbol.split(/[:/]/); | ||
// const newSymbol = splitSymbol[0] + splitSymbol[1]; | ||
// const handler = (res)=>{ | ||
// let data={ | ||
// symbol:symbol, | ||
// }; | ||
// eventHandler(res) | ||
// }; | ||
// | ||
// return this._setupWebSocket(handler, this.streams.ticker(newSymbol)); | ||
// } | ||
} |
@@ -1,2 +0,2 @@ | ||
import Exchanges from "./baseExchange"; | ||
import Exchanges from './baseExchange'; | ||
@@ -6,216 +6,217 @@ const PROXY = 'https://pure-mountain-67034.herokuapp.com/'; | ||
export default class HitBTC extends Exchanges { | ||
constructor(proxy) { | ||
super(); | ||
this.name = "HitBTC"; | ||
this._mainUrl = "wss://api.hitbtc.com/api/2/ws"; | ||
this._socket = 0; | ||
this.orderBook = { | ||
bids: [], | ||
asks: [] | ||
}; | ||
this._proxy = proxy ? proxy : PROXY; | ||
constructor(proxy) { | ||
super(); | ||
this.name = 'HitBTC'; | ||
this._mainUrl = 'wss://api.hitbtc.com/api/2/ws'; | ||
this._socket = 0; | ||
this.orderBook = { | ||
bids: [], | ||
asks: [], | ||
}; | ||
this._proxy = proxy || PROXY; | ||
this.streams = { | ||
depth: symbol => JSON.stringify({ | ||
"method": "subscribeOrderbook", | ||
"params": { | ||
"symbol": symbol | ||
}, | ||
"id": 123 | ||
}), | ||
depthLevel: (symbol, level) => `${this._proxy}https://api.hitbtc.com/api/2/public/orderbook/${symbol}`, | ||
trade: (symbol) => JSON.stringify({ | ||
"method": "subscribeTrades", | ||
"params": { | ||
"symbol": symbol | ||
}, | ||
"id": 123 | ||
}), | ||
kline: (symbol, interval) => JSON.stringify({ | ||
"method": "subscribeCandles", | ||
"params": { | ||
"symbol": symbol, | ||
"period": this.times[interval] | ||
}, | ||
"id": 123 | ||
}), | ||
}; | ||
this.streams = { | ||
depth: symbol => JSON.stringify({ | ||
method: 'subscribeOrderbook', | ||
params: { | ||
symbol, | ||
}, | ||
id: 123, | ||
}), | ||
depthLevel: (symbol, level) => `${this._proxy}https://api.hitbtc.com/api/2/public/orderbook/${symbol}`, | ||
trade: symbol => JSON.stringify({ | ||
method: 'subscribeTrades', | ||
params: { | ||
symbol, | ||
}, | ||
id: 123, | ||
}), | ||
kline: (symbol, interval) => JSON.stringify({ | ||
method: 'subscribeCandles', | ||
params: { | ||
symbol, | ||
period: this.times[interval], | ||
}, | ||
id: 123, | ||
}), | ||
}; | ||
this.times = { | ||
"1": 'M1', | ||
"5": "M5", | ||
"15": "M15", | ||
"30": "M30", | ||
"60": "H1", | ||
"120": "H1", | ||
"240": "H4", | ||
"D": "D1", | ||
"W": "D7" | ||
} | ||
this.times = { | ||
1: 'M1', | ||
5: 'M5', | ||
15: 'M15', | ||
30: 'M30', | ||
60: 'H1', | ||
120: 'H1', | ||
240: 'H4', | ||
D: 'D1', | ||
W: 'D7', | ||
}; | ||
} | ||
_setupWebSocket(eventHandler, path) { | ||
if (this._socket) { | ||
return this._socket; | ||
} | ||
_setupWebSocket(eventHandler, path) { | ||
if (this._socket) { | ||
return this._socket; | ||
} | ||
// console.log(path); | ||
// console.log(path); | ||
const ws = new WebSocket(this._mainUrl); | ||
const ws = new WebSocket(this._mainUrl); | ||
ws.onopen = () => ws.send(path); | ||
ws.onopen = () => ws.send(path); | ||
ws.onclose = event => { | ||
if (event.wasClean) { | ||
console.log('Соединение закрыто чисто'); | ||
} else { | ||
console.log('Обрыв соединения'); | ||
} | ||
}; | ||
ws.onclose = event => { | ||
if (event.wasClean) { | ||
console.log("Соединение закрыто чисто"); | ||
} else { | ||
console.log("Обрыв соединения"); | ||
} | ||
}; | ||
ws.onmessage = event => { | ||
const res = JSON.parse(event.data); | ||
eventHandler(res); | ||
}; | ||
ws.onmessage = event => { | ||
const res = JSON.parse(event.data); | ||
eventHandler(res) | ||
}; | ||
this._socket = ws; | ||
return ws; | ||
} | ||
this._socket = ws; | ||
return ws; | ||
_setupWebSocketEmulator(eventHandler, path) { | ||
if (this._socket) { | ||
return this._socket; | ||
} | ||
this._socket = setInterval(() => { | ||
fetch(path) | ||
.then(resp => resp.json()) | ||
.then(res => { | ||
eventHandler(res); | ||
}); | ||
}, 5000); | ||
} | ||
_setupWebSocketEmulator(eventHandler, path) { | ||
if (this._socket) { | ||
return this._socket; | ||
} | ||
this._socket = setInterval(() => { | ||
fetch(path).then((resp) => { | ||
return resp.json() | ||
}).then(res => { | ||
eventHandler(res) | ||
}) | ||
}, 5000) | ||
closeWebSocket() { | ||
if (typeof this._socket === 'number') { | ||
clearInterval(this._socket); | ||
} else { | ||
const socket = this._socket; | ||
socket.close(); | ||
} | ||
} | ||
onTrade(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const base = splitSymbol[1] === 'USDT' ? 'USD' : splitSymbol[1]; | ||
const newSymbol = splitSymbol[0] + base; | ||
closeWebSocket() { | ||
if (typeof(this._socket) === 'number') { | ||
clearInterval(this._socket) | ||
} else { | ||
const socket = this._socket; | ||
socket.close(); | ||
const handler = res => { | ||
if (res.hasOwnProperty('method')) { | ||
if (res.method === 'updateTrades') { | ||
res = res.params.data; | ||
res.forEach((e, i, a) => { | ||
const data = new Date(e.timestamp); | ||
const trade = { | ||
id: e.id, | ||
side: e.side, | ||
timestamp: data.getTime(), | ||
price: +e.price, | ||
amount: +e.quantity, | ||
symbol, | ||
}; | ||
eventHandler(trade); | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
onTrade(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const base = splitSymbol[1] === 'USDT' ? 'USD' : splitSymbol[1]; | ||
const newSymbol = splitSymbol[0] + base; | ||
return this._setupWebSocket(handler, this.streams.trade(newSymbol)); | ||
} | ||
let handler = (res) => { | ||
if (res.hasOwnProperty('method')) { | ||
if (res.method === 'updateTrades') { | ||
res = res.params.data; | ||
res.forEach((e, i, a) => { | ||
let data = new Date(e.timestamp); | ||
const trade = { | ||
id: e.id, | ||
side: e.side, | ||
timestamp: data.getTime(), | ||
price: +e.price, | ||
amount: +e.quantity, | ||
symbol: symbol | ||
}; | ||
eventHandler(trade) | ||
}) | ||
} | ||
} | ||
onDepthUpdate(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const base = splitSymbol[1] === 'USDT' ? 'USD' : splitSymbol[1]; | ||
const newSymbol = splitSymbol[0] + base; | ||
fetch(this.streams.depthLevel(newSymbol, 20)) | ||
.then(r => r.json()) | ||
.then(res => { | ||
const data = { | ||
asks: [], | ||
bids: [], | ||
type: 'snapshot', | ||
}; | ||
res.ask.slice(0, 20).forEach(r => data.asks.push([+r.price, +r.size])); | ||
res.bid.slice(0, 20).forEach(r => data.bids.push([+r.price, +r.size])); | ||
eventHandler(data); | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.trade(newSymbol) | ||
); | ||
} | ||
onDepthUpdate(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const base = splitSymbol[1] === 'USDT' ? 'USD' : splitSymbol[1]; | ||
const newSymbol = splitSymbol[0] + base; | ||
fetch(this.streams.depthLevel(newSymbol,20)).then(r=>r.json()).then(res=>{ | ||
let data = { | ||
asks:[], | ||
bids:[], | ||
type:'snapshot' | ||
}; | ||
res.ask.slice(0,20).forEach(r=>data.asks.push([+r.price, +r.size])); | ||
res.bid.slice(0,20).forEach(r=>data.bids.push([+r.price, +r.size])); | ||
eventHandler(data); | ||
let handler = (res)=>{ | ||
if (res.hasOwnProperty('method')) | ||
if (res.method==='updateOrderbook'){ | ||
let data = { | ||
asks:[], | ||
bids:[], | ||
type:'update' | ||
}; | ||
res = res.params; | ||
res.ask.forEach(r=>data.asks.push([+r.price, +r.size])); | ||
res.bid.forEach(r=>data.bids.push([+r.price, +r.size])); | ||
eventHandler(data) | ||
} | ||
}; | ||
return this._setupWebSocket(handler, this.streams.depth(newSymbol)); | ||
}); | ||
} | ||
onDepthLevelUpdate(symbol, level, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const base = splitSymbol[1] === 'USDT' ? 'USD' : splitSymbol[1]; | ||
const newSymbol = splitSymbol[0] + base; | ||
let handler = (res) => { | ||
let newData = { | ||
const handler = res => { | ||
if (res.hasOwnProperty('method')) { | ||
if (res.method === 'updateOrderbook') { | ||
const data = { | ||
asks: [], | ||
bids: [] | ||
}; | ||
res.bid.splice(0, level).forEach(r => newData.bids.push([+r.price, +r.size])); | ||
res.ask.splice(0, level).forEach(r => newData.asks.push([+r.price, +r.size])); | ||
eventHandler(newData); | ||
bids: [], | ||
type: 'update', | ||
}; | ||
res = res.params; | ||
res.ask.forEach(r => data.asks.push([+r.price, +r.size])); | ||
res.bid.forEach(r => data.bids.push([+r.price, +r.size])); | ||
eventHandler(data); | ||
} | ||
} | ||
}; | ||
return this._setupWebSocket(handler, this.streams.depth(newSymbol)); | ||
}); | ||
} | ||
return this._setupWebSocketEmulator( | ||
handler, | ||
this.streams.depthLevel(newSymbol, level) | ||
); | ||
} | ||
onDepthLevelUpdate(symbol, level, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const base = splitSymbol[1] === 'USDT' ? 'USD' : splitSymbol[1]; | ||
const newSymbol = splitSymbol[0] + base; | ||
onKline(symbol, interval, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const base = splitSymbol[1] === 'USDT' ? 'USD' : splitSymbol[1]; | ||
const newSymbol = splitSymbol[0] + base; | ||
const handler = res => { | ||
const newData = { | ||
asks: [], | ||
bids: [], | ||
}; | ||
res.bid | ||
.splice(0, level) | ||
.forEach(r => newData.bids.push([+r.price, +r.size])); | ||
res.ask | ||
.splice(0, level) | ||
.forEach(r => newData.asks.push([+r.price, +r.size])); | ||
eventHandler(newData); | ||
}; | ||
let handler = data => { | ||
if (data.hasOwnProperty('method')) { | ||
if (data.method === 'updateCandles') { | ||
data = data.params.data[0]; | ||
const date = new Date(data.timestamp); | ||
const newData = { | ||
close: +data.close, | ||
high: +data.max, | ||
low: +data.min, | ||
open: +data.open, | ||
time: date.getTime(), | ||
volume: +data.volume | ||
}; | ||
eventHandler(newData); | ||
} | ||
} | ||
}; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.kline(newSymbol, interval) | ||
); | ||
} | ||
return this._setupWebSocketEmulator( | ||
handler, | ||
this.streams.depthLevel(newSymbol, level) | ||
); | ||
} | ||
} | ||
onKline(symbol, interval, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const base = splitSymbol[1] === 'USDT' ? 'USD' : splitSymbol[1]; | ||
const newSymbol = splitSymbol[0] + base; | ||
const handler = data => { | ||
if (data.hasOwnProperty('method')) { | ||
if (data.method === 'updateCandles') { | ||
data = data.params.data[0]; | ||
const date = new Date(data.timestamp); | ||
const newData = { | ||
close: +data.close, | ||
high: +data.max, | ||
low: +data.min, | ||
open: +data.open, | ||
time: date.getTime(), | ||
volume: +data.volume, | ||
}; | ||
eventHandler(newData); | ||
} | ||
} | ||
}; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.kline(newSymbol, interval) | ||
); | ||
} | ||
} |
@@ -1,170 +0,176 @@ | ||
import Exchanges from "./baseExchange"; | ||
import Exchanges from './baseExchange'; | ||
const PROXY = 'https://pure-mountain-67034.herokuapp.com/'; | ||
export default class OKEx extends Exchanges { | ||
constructor(proxy) { | ||
super(); | ||
this.name = "OKEx"; | ||
this._mainUrl = "wss://real.okex.com:10440/websocket/okexapi"; | ||
this._socket = 0; | ||
this._proxy = proxy ? proxy : PROXY; | ||
constructor(proxy) { | ||
super(); | ||
this.name = 'OKEx'; | ||
this._mainUrl = 'wss://real.okex.com:10440/websocket/okexapi'; | ||
this._socket = 0; | ||
this._proxy = proxy || PROXY; | ||
this.orderBook = symbol => `${this._proxy}https://www.okex.com/api/v1/depth.do?symbol=${symbol.toLowerCase()}&size=20`; | ||
this.orderBook = symbol => `${ | ||
this._proxy | ||
}https://www.okex.com/api/v1/depth.do?symbol=${symbol.toLowerCase()}&size=50`; | ||
this.streams = { | ||
depth: symbol => `ok_sub_spot_${symbol.toLowerCase()}_depth`, | ||
depthLevel: (symbol, level) => `ok_sub_spot_${symbol.toLowerCase()}_depth_${level}`, | ||
kline: (symbol, interval) => `ok_sub_spot_${symbol.toLowerCase()}_kline_${this.times[interval]}`, | ||
trade: symbol => `ok_sub_spot_${symbol.toLowerCase()}_deals`, | ||
ticker: symbol => `ok_sub_spot_${symbol.toLowerCase()}_ticker`, | ||
}; | ||
this.streams = { | ||
depth: symbol => `ok_sub_spot_${symbol.toLowerCase()}_depth`, | ||
depthLevel: (symbol, level) => `ok_sub_spot_${symbol.toLowerCase()}_depth_${level}`, | ||
kline: (symbol, interval) => `ok_sub_spot_${symbol.toLowerCase()}_kline_${this.times[interval]}`, | ||
trade: symbol => `ok_sub_spot_${symbol.toLowerCase()}_deals`, | ||
ticker: symbol => `ok_sub_spot_${symbol.toLowerCase()}_ticker`, | ||
}; | ||
this.times = { | ||
"1": '1min', | ||
"5": "5min", | ||
"15": "15min", | ||
"30": "30min", | ||
"60": "1hour", | ||
"120": "2hour", | ||
"240": "4hour", | ||
"D": "day", | ||
"W": "week" | ||
} | ||
this.times = { | ||
1: '1min', | ||
5: '5min', | ||
15: '15min', | ||
30: '30min', | ||
60: '1hour', | ||
120: '2hour', | ||
240: '4hour', | ||
D: 'day', | ||
W: 'week', | ||
}; | ||
} | ||
_setupWebSocket(eventHandler, path) { | ||
if (this._socket) { | ||
return this._socket; | ||
} | ||
_setupWebSocket(eventHandler, path) { | ||
if (this._socket) { | ||
return this._socket; | ||
} | ||
// console.log(path); | ||
// console.log(path); | ||
const ws = new WebSocket(this._mainUrl); | ||
const ws = new WebSocket(this._mainUrl); | ||
ws.onopen = () => { | ||
ws.send(`{'event': 'addChannel', 'channel': '${path}'}`); | ||
this._pingpong = setInterval(() => ws.send('{"event":"ping"}'), 29000); | ||
}; | ||
ws.onopen = () => ws.send(`{'event': 'addChannel', 'channel': '${path}'}`); | ||
ws.onerror = e => console.log('ERROR', e); | ||
ws.onclose = event => { | ||
if (event.wasClean) { | ||
console.log('Соединение закрыто чисто', path); | ||
} else { | ||
console.log('Обрыв соединения', path); | ||
} | ||
}; | ||
ws.onclose = event => { | ||
if (event.wasClean) { | ||
console.log("Соединение закрыто чисто"); | ||
} else { | ||
console.log("Обрыв соединения"); | ||
} | ||
}; | ||
ws.onmessage = event => { | ||
const res = JSON.parse(event.data); | ||
if (!res.event) { | ||
eventHandler(res[0].data); | ||
} | ||
}; | ||
ws.onmessage = event => { | ||
const res = JSON.parse(event.data)[0].data; | ||
eventHandler(res) | ||
}; | ||
this._socket = ws; | ||
return ws; | ||
} | ||
this._socket = ws; | ||
return ws; | ||
} | ||
closeWebSocket() { | ||
const socket = this._socket; | ||
socket.close(); | ||
clearInterval(this._pingpong); | ||
} | ||
closeWebSocket() { | ||
const socket = this._socket; | ||
socket.close(); | ||
} | ||
onTrade(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = `${splitSymbol[0]}_${splitSymbol[1]}`; | ||
onTrade(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + "_" + splitSymbol[1]; | ||
let handler = (res) => { | ||
if (!res.hasOwnProperty('result')) { | ||
const side = res[0][4] === 'ask' ? "sell" : "buy"; | ||
let date = new Date(); | ||
const trade = { | ||
id: +res[0][0], | ||
side: side, | ||
timestamp: date.getTime(), | ||
price: +res[0][1], | ||
amount: +res[0][2], | ||
symbol: symbol | ||
}; | ||
eventHandler(trade) | ||
} | ||
const handler = res => { | ||
if (!res.hasOwnProperty('result')) { | ||
const side = res[0][4] === 'ask' ? 'sell' : 'buy'; | ||
const date = new Date(); | ||
const trade = { | ||
id: +res[0][0], | ||
side, | ||
timestamp: date.getTime(), | ||
price: +res[0][1], | ||
amount: +res[0][2], | ||
symbol, | ||
}; | ||
eventHandler(trade); | ||
} | ||
}; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.trade(newSymbol) | ||
); | ||
} | ||
return this._setupWebSocket(handler, this.streams.trade(newSymbol)); | ||
} | ||
onDepthLevelUpdate(symbol, level, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + "_" + splitSymbol[1]; | ||
onDepthLevelUpdate(symbol, level, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = `${splitSymbol[0]}_${splitSymbol[1]}`; | ||
let handler = (res) => { | ||
if (!res.hasOwnProperty('result')) { | ||
res.bids = res.bids.map(r => { | ||
return r.map(e => +e) | ||
}); | ||
res.asks = res.asks.map(r => { | ||
return r.map(e => +e) | ||
}); | ||
eventHandler(res) | ||
} | ||
const handler = res => { | ||
if (!res.hasOwnProperty('result')) { | ||
res.bids = res.bids.map(r => r.map(e => +e)); | ||
res.asks = res.asks.map(r => r.map(e => +e)); | ||
eventHandler(res); | ||
} | ||
}; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.depthLevel(newSymbol, level) | ||
); | ||
} | ||
onDepthUpdate(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = `${splitSymbol[0]}_${splitSymbol[1]}`; | ||
fetch(this.orderBook(newSymbol)) | ||
.then(r => r.json()) | ||
.then(res => { | ||
const data = { | ||
asks: [], | ||
bids: [], | ||
type: 'snapshot', | ||
}; | ||
res.asks.forEach(r => data.asks.push([+r[0], +r[1]])); | ||
res.bids.forEach(r => data.bids.push([+r[0], +r[1]])); | ||
eventHandler(data); | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.depthLevel(newSymbol, level) | ||
); | ||
} | ||
onDepthUpdate(symbol, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + "_" + splitSymbol[1]; | ||
fetch(this.orderBook(newSymbol)).then(r => r.json()).then(res => { | ||
let data = { | ||
asks: [], | ||
bids: [], | ||
type: 'snapshot' | ||
}; | ||
const handler = res => { | ||
const data = { | ||
asks: [], | ||
bids: [], | ||
type: 'update', | ||
}; | ||
if ( | ||
res.hasOwnProperty('asks') | ||
&& res.asks.length !== 200 | ||
&& res.bids.length !== 200 | ||
) { | ||
res.asks.forEach(r => data.asks.push([+r[0], +r[1]])); | ||
res.bids.forEach(r => data.bids.push([+r[0], +r[1]])); | ||
eventHandler(data); | ||
} | ||
}; | ||
return this._setupWebSocket(handler, this.streams.depth(newSymbol)); | ||
}); | ||
} | ||
let handler = (res) => { | ||
let data = { | ||
asks: [], | ||
bids: [], | ||
type: 'update' | ||
}; | ||
if (res.hasOwnProperty('asks') && res.asks.length !== 200 && res.bids.length !== 200) { | ||
res.asks.forEach(r => data.asks.push([+r[0], +r[1]])); | ||
res.bids.forEach(r => data.bids.push([+r[0], +r[1]])); | ||
eventHandler(data) | ||
} | ||
}; | ||
return this._setupWebSocket(handler, this.streams.depth(newSymbol)); | ||
}); | ||
} | ||
onKline(symbol, interval, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = `${splitSymbol[0]}_${splitSymbol[1]}`; | ||
onKline(symbol, interval, eventHandler) { | ||
const splitSymbol = symbol.split(/[:/]/); | ||
const newSymbol = splitSymbol[0] + "_" + splitSymbol[1]; | ||
let handler = data => { | ||
if (!data.hasOwnProperty('result')) { | ||
const newData = { | ||
close: +data[0][4], | ||
high: +data[0][2], | ||
low: +data[0][3], | ||
open: +data[0][1], | ||
time: +data[0][0], | ||
volume: +data[0][5] | ||
}; | ||
eventHandler(newData); | ||
} | ||
const handler = data => { | ||
if (!data.hasOwnProperty('result')) { | ||
const newData = { | ||
close: +data[0][4], | ||
high: +data[0][2], | ||
low: +data[0][3], | ||
open: +data[0][1], | ||
time: +data[0][0], | ||
volume: +data[0][5], | ||
}; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.kline(newSymbol, interval) | ||
); | ||
} | ||
} | ||
eventHandler(newData); | ||
} | ||
}; | ||
return this._setupWebSocket( | ||
handler, | ||
this.streams.kline(newSymbol, interval) | ||
); | ||
} | ||
} |
{ | ||
"name": "websocket-crypto-api", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "---", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
943
30968