Skip to content

Commit d990ce0

Browse files
Added Token to WS Client and Historical Candles Endpoint
1 parent 9cf54a3 commit d990ce0

11 files changed

Lines changed: 177 additions & 15 deletions

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,21 +495,23 @@ The client handles pings to the Shrimpy server based on the [`API Documentation`
495495
```js
496496
import { ShrimpyWsClient, ISubscriptionRequest, IWebsocketMessage, IErrorMessage } from 'shrimpy-node';
497497

498+
499+
let token = "6ET73KTKDO0..."
498500
let errorHandler = (error: IErrorMessage) => { console.log(error) };
499-
let client = new ShrimpyWsClient(errorHandler);
501+
let client = new ShrimpyWsClient(errorHandler, token);
500502

501503
const subscribeData: ISubscriptionRequest = {
502-
"type": "subscribe",
503-
"pair": "btc-usd",
504-
"exchange": "coinbasepro",
505-
"channel": "trade"
504+
"type": "subscribe",
505+
"pair": "btc-usd",
506+
"exchange": "coinbasepro",
507+
"channel": "trade"
506508
};
507509

508510
const unsubscribeData : ISubscriptionRequest = {
509-
"type": "unsubscribe",
510-
"pair": "btc-usd",
511-
"exchange": "coinbasepro",
512-
"channel": "trade"
511+
"type": "unsubscribe",
512+
"pair": "btc-usd",
513+
"exchange": "coinbasepro",
514+
"channel": "trade"
513515
};
514516

515517
let handler = (msg: IWebsocketMessage) => { console.log(msg); };
@@ -518,4 +520,5 @@ client.connect();
518520
client.subscribe(subscribeData, handler);
519521
client.unsubscribe(unsubscribeData);
520522
client.forceDisconnect();
523+
521524
```

lib/client/shrimpy-api-client.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ICandlestick,
1515
IExchangeAsset,
1616
IExchangeInfo,
17+
IHistoricalCandlestick,
1718
IHistoricalOrderBook,
1819
IHistoricalInstrument,
1920
IHistoricalTrade,
@@ -43,6 +44,7 @@ import {
4344
IExchangeAssetDto,
4445
IExchangeInfoDto,
4546
IGuidIdResultDto,
47+
IHistoricalCandlestickDto,
4648
IHistoricalOrderBookDto,
4749
IHistoricalInstrumentDto,
4850
IHistoricalTradeDto,
@@ -58,6 +60,7 @@ import {
5860
ITradeDto,
5961
ITradingPairDto,
6062
IUserDto,
63+
IWebsocketTokenDto,
6164
} from "../dtos";
6265
import {
6366
AccountBalanceDtoConverter,
@@ -68,6 +71,7 @@ import {
6871
CandlestickDtoConverter,
6972
DateDtoConverter,
7073
DecimalDtoConverter,
74+
HistoricalCandlestickDtoConverter,
7175
HistoricalOrderBooksDtoConverter,
7276
HistoricalInstrumentsDtoConverter,
7377
HistoricalTradesDtoConverter,
@@ -91,6 +95,7 @@ export class ShrimpyApiClient {
9195
private _candlestickDtoConverter = new CandlestickDtoConverter();
9296
private _dateDtoConverter = new DateDtoConverter();
9397
private _decimalDtoConverter = new DecimalDtoConverter();
98+
private _historicalCandlestickDtoConverter = new HistoricalCandlestickDtoConverter();
9499
private _historicalOrderBooksDtoConverter = new HistoricalOrderBooksDtoConverter();
95100
private _historicalInstrumentsDtoConverter = new HistoricalInstrumentsDtoConverter();
96101
private _historicalTradesDtoConverter = new HistoricalTradesDtoConverter();
@@ -695,6 +700,41 @@ export class ShrimpyApiClient {
695700
return this._historicalOrderBooksDtoConverter.convertFromDto(resultDto);
696701
}
697702

703+
public async getHistoricalCandles(
704+
exchange: string,
705+
baseTradingSymbol: string,
706+
quoteTradingSymbol: string,
707+
startTime: Date,
708+
endTime: Date,
709+
limit: number,
710+
interval: '1m' | '5m' | '15m' | '1h' | '6h' | '1d'
711+
): Promise<IHistoricalCandlestick[]> {
712+
const endpoint = `historical/candles`;
713+
const parameters: {
714+
exchange: string,
715+
baseTradingSymbol: string,
716+
quoteTradingSymbol: string,
717+
startTime: string,
718+
endTime: string,
719+
limit: number,
720+
interval: '1m' | '5m' | '15m' | '1h' | '6h' | '1d'
721+
} = {
722+
exchange: exchange,
723+
baseTradingSymbol: baseTradingSymbol,
724+
quoteTradingSymbol: quoteTradingSymbol,
725+
startTime: this._dateDtoConverter.convertToDto(startTime),
726+
endTime: this._dateDtoConverter.convertToDto(endTime),
727+
limit: limit,
728+
interval: interval
729+
};
730+
const resultDto = await this._callEndpoint<IHistoricalCandlestickDto[]>(endpoint, 'GET', parameters, true);
731+
const result = resultDto.map((candlestick) => {
732+
return this._historicalCandlestickDtoConverter.convertFromDto(candlestick);
733+
});
734+
735+
return result;
736+
}
737+
698738
public async getHistoricalInstruments(
699739
exchange?: string,
700740
baseTradingSymbol?: string,
@@ -728,6 +768,14 @@ export class ShrimpyApiClient {
728768
return await this._callEndpoint<IManagementUsage>(endpoint, 'GET', null, true);
729769
}
730770

771+
/* WebSocket */
772+
773+
public async getToken(): Promise<string> {
774+
const endpoint = `ws/token`;
775+
const websocketTokenResult = await this._callEndpoint<IWebsocketTokenDto>(endpoint, 'GET', null, true);
776+
777+
return websocketTokenResult.token;
778+
}
731779

732780
/* private methods */
733781

lib/client/shrimpy-ws-client.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,32 @@ import { ISubscriptionRequest, IWebsocketMessage, IPingMessage, IErrorMessage }
33

44

55
export class ShrimpyWsClient {
6-
private _websocket: WebSocket;
6+
private _baseUrl = 'wss://ws-feed.shrimpy.io';
7+
private _token = "";
8+
9+
private _websocket: WebSocket | undefined = undefined;
10+
711
private _subscriptionCallbacks: {
812
[subscription: string]: (data: IWebsocketMessage) => void
913
} = {};
10-
1114
private _websocketErrorCallback: (error: IErrorMessage) => void;
1215

13-
constructor (errorCallback: (error: IErrorMessage) => void) {
14-
this._websocket = new WebSocket('wss://ws-feed.shrimpy.io/');
16+
constructor (errorCallback: (error: IErrorMessage) => void, token: string = "") {
17+
let url = this._baseUrl;
18+
if (token) {
19+
this._token = token;
20+
url = this._baseUrl + "?token=" + this._token;
21+
}
1522
this._websocketErrorCallback = errorCallback;
23+
this._websocket = new WebSocket(url);
1624
}
1725

1826
public connect() {
1927

28+
if (this._websocket == undefined) {
29+
return;
30+
}
31+
2032
this._websocket.on('open', function open() {
2133
// Open the connection
2234
});
@@ -69,10 +81,25 @@ export class ShrimpyWsClient {
6981
}
7082
}
7183

84+
public reconnect(token: string = "") {
85+
let url = this._baseUrl;
86+
if (token) {
87+
this._token = token;
88+
url = this._baseUrl + "?token=" + this._token;
89+
}
90+
this.forceDisconnect();
91+
this._websocket = new WebSocket(url);
92+
this.connect();
93+
}
94+
7295
public subscribe(
7396
subscriptionRequest: ISubscriptionRequest,
7497
successCallback: (data: IWebsocketMessage) => void,
7598
) {
99+
if (this._websocket == undefined) {
100+
return;
101+
}
102+
76103
if (this._websocket.OPEN == this._websocket.readyState) {
77104
const topic = this._getTopic(subscriptionRequest);
78105
this._subscriptionCallbacks[topic] = successCallback;
@@ -86,12 +113,20 @@ export class ShrimpyWsClient {
86113
const topic = this._getTopic(unsubscriptionRequest);
87114
delete this._subscriptionCallbacks[topic];
88115

116+
if (this._websocket == undefined) {
117+
return;
118+
}
119+
89120
if (this._websocket.OPEN == this._websocket.readyState) {
90121
this._websocket.send(JSON.stringify(unsubscriptionRequest));
91122
}
92123
}
93124

94125
public getReadyState(): number {
126+
if (this._websocket == undefined) {
127+
return WebSocket.CLOSED;
128+
}
129+
95130
return this._websocket.readyState;
96131
}
97132

@@ -117,6 +152,11 @@ export class ShrimpyWsClient {
117152
}
118153

119154
private _pong(parsedData: IPingMessage) {
155+
156+
if (this._websocket == undefined) {
157+
return;
158+
}
159+
120160
const pong = {
121161
'type': 'pong',
122162
'data': parsedData.data
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { IDtoConverter } from "../interfaces";
2+
import { IHistoricalCandlestickDto } from "../dtos";
3+
import { IHistoricalCandlestick } from "../models";
4+
import { DateDtoConverter } from "./date-dto-converter";
5+
import { DecimalDtoConverter } from "./decimal-dto-converter";
6+
7+
export class HistoricalCandlestickDtoConverter implements IDtoConverter<IHistoricalCandlestickDto, IHistoricalCandlestick> {
8+
private _dateDtoConverter: DateDtoConverter = new DateDtoConverter();
9+
private _decimalDtoConverter: DecimalDtoConverter = new DecimalDtoConverter();
10+
11+
public convertFromDto(dto: IHistoricalCandlestickDto): IHistoricalCandlestick {
12+
const result: IHistoricalCandlestick = {
13+
open: this._decimalDtoConverter.convertFromDto(dto.open),
14+
high: this._decimalDtoConverter.convertFromDto(dto.high),
15+
low: this._decimalDtoConverter.convertFromDto(dto.low),
16+
close: this._decimalDtoConverter.convertFromDto(dto.close),
17+
volume: this._decimalDtoConverter.convertFromDto(dto.volume),
18+
quoteVolume: dto.quoteVolume,
19+
btcVolume: dto.btcVolume,
20+
usdVolume: dto.usdVolume,
21+
time: this._dateDtoConverter.convertFromDto(dto.time),
22+
};
23+
return result;
24+
}
25+
26+
public convertToDto(model: IHistoricalCandlestick): IHistoricalCandlestickDto {
27+
const result: IHistoricalCandlestickDto = {
28+
open: this._decimalDtoConverter.convertToDto(model.open),
29+
high: this._decimalDtoConverter.convertToDto(model.high),
30+
low: this._decimalDtoConverter.convertToDto(model.low),
31+
close: this._decimalDtoConverter.convertToDto(model.close),
32+
volume: this._decimalDtoConverter.convertToDto(model.volume),
33+
quoteVolume: model.quoteVolume,
34+
btcVolume: model.btcVolume,
35+
usdVolume: model.usdVolume,
36+
time: this._dateDtoConverter.convertToDto(model.time),
37+
};
38+
return result;
39+
}
40+
}

lib/dto-converters/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export * from './date-dto-converter';
1010
export * from './decimal-dto-converter';
1111
export * from './dynamic-strategy-dto-converter';
1212
export * from './exchange-order-book-dto-converter';
13+
export * from './historical-candlestick-dto-converter';
1314
export * from './historical-orderbooks-dto-converter';
1415
export * from './historical-instruments-dto-converter';
1516
export * from './historical-trades-dto-converter';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface IHistoricalCandlestickDto {
2+
open: string;
3+
high: string;
4+
low: string;
5+
close: string;
6+
volume: string;
7+
quoteVolume: number;
8+
btcVolume: number;
9+
usdVolume: number;
10+
time: string;
11+
}

lib/dtos/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export * from './iexchange-asset-dto';
1616
export * from './iexchange-info-dto';
1717
export * from './iexchange-order-book-dto';
1818
export * from './iguid-id-result-dto';
19+
export * from './ihistorical-candlestick-dto';
1920
export * from './ihistorical-order-book-dto';
2021
export * from './ihistorical-instrument-dto';
2122
export * from './ihistorical-trade-dto';
@@ -34,4 +35,5 @@ export * from './itrade-changes-dto';
3435
export * from './itrade-dto';
3536
export * from './itrade-fill-dto';
3637
export * from './itrading-pair-dto';
37-
export * from './iuser-dto';
38+
export * from './iuser-dto';
39+
export * from './iwebsocket-token-dto';

lib/dtos/iwebsocket-token-dto.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface IWebsocketTokenDto {
2+
token: string;
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Decimal } from 'decimal.js';
2+
3+
export interface IHistoricalCandlestick {
4+
open: Decimal;
5+
high: Decimal;
6+
low: Decimal;
7+
close: Decimal;
8+
volume: Decimal;
9+
quoteVolume: number;
10+
btcVolume: number;
11+
usdVolume: number;
12+
time: Date;
13+
}

lib/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './iexchange-api-error';
1515
export * from './iexchange-asset';
1616
export * from './iexchange-info';
1717
export * from './iexchange-order-book';
18+
export * from './ihistorical-candlestick';
1819
export * from './ihistorical-order-book';
1920
export * from './ihistorical-instrument';
2021
export * from './ihistorical-trade';

0 commit comments

Comments
 (0)