diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/connect/src/http_client.ts | 27 | ||||
-rw-r--r-- | packages/connect/src/types.ts | 43 | ||||
-rw-r--r-- | packages/connect/src/ws_orderbook_channel.ts | 23 |
3 files changed, 49 insertions, 44 deletions
diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 0a85dced5..3a9e11a7d 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -11,6 +11,8 @@ import { Client, FeesRequest, FeesResponse, + HttpRequestOptions, + HttpRequestType, OrderbookRequest, OrderbookResponse, OrdersRequest, @@ -24,16 +26,6 @@ BigNumber.config({ EXPONENTIAL_AT: 1000, }); -interface RequestOptions { - params?: object; - payload?: object; -} - -enum RequestType { - Get = 'GET', - Post = 'POST', -} - /** * This class includes all the functionality related to interacting with a set of HTTP endpoints * that implement the standard relayer API v0 @@ -62,7 +54,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const tokenPairs = await this._requestAsync('/token_pairs', RequestType.Get, requestOpts); + const tokenPairs = await this._requestAsync('/token_pairs', HttpRequestType.Get, requestOpts); assert.doesConformToSchema( 'tokenPairs', tokenPairs, schemas.relayerApiTokenPairsResponseSchema); _.each(tokenPairs, (tokenPair: object) => { @@ -87,7 +79,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const orders = await this._requestAsync(`/orders`, RequestType.Get, requestOpts); + const orders = await this._requestAsync(`/orders`, HttpRequestType.Get, requestOpts); assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema); _.each(orders, (order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order)); return orders; @@ -99,7 +91,7 @@ export class HttpClient implements Client { */ public async getOrderAsync(orderHash: string): Promise<SignedOrder> { assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema); - const order = await this._requestAsync(`/order/${orderHash}`, RequestType.Get); + const order = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get); assert.doesConformToSchema('order', order, schemas.signedOrderSchema); typeConverters.convertOrderStringFieldsToBigNumber(order); return order; @@ -114,7 +106,7 @@ export class HttpClient implements Client { const requestOpts = { params: request, }; - const orderBook = await this._requestAsync('/orderbook', RequestType.Get, requestOpts); + const orderBook = await this._requestAsync('/orderbook', HttpRequestType.Get, requestOpts); assert.doesConformToSchema('orderBook', orderBook, schemas.relayerApiOrderBookResponseSchema); typeConverters.convertOrderbookStringFieldsToBigNumber(orderBook); return orderBook; @@ -135,7 +127,7 @@ export class HttpClient implements Client { const requestOpts = { payload: request, }; - const fees = await this._requestAsync('/fees', RequestType.Post, requestOpts); + const fees = await this._requestAsync('/fees', HttpRequestType.Post, requestOpts); assert.doesConformToSchema('fees', fees, schemas.relayerApiFeesResponseSchema); typeConverters.convertStringsFieldsToBigNumbers(fees, ['makerFee', 'takerFee']); return fees; @@ -149,9 +141,10 @@ export class HttpClient implements Client { const requestOpts = { payload: signedOrder, }; - await this._requestAsync('/order', RequestType.Post, requestOpts); + await this._requestAsync('/order', HttpRequestType.Post, requestOpts); } - private async _requestAsync(path: string, requestType: RequestType, requestOptions?: RequestOptions): Promise<any> { + private async _requestAsync(path: string, requestType: HttpRequestType, + requestOptions?: HttpRequestOptions): Promise<any> { const params = _.get(requestOptions, 'params'); const payload = _.get(requestOptions, 'payload'); let query = ''; diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts index 75b6b8020..eb6df3ba9 100644 --- a/packages/connect/src/types.ts +++ b/packages/connect/src/types.ts @@ -15,6 +15,19 @@ export interface OrderbookChannel { close: () => void; } +/* + * baseTokenAddress: The address of token designated as the baseToken in the currency pair calculation of price + * quoteTokenAddress: The address of token designated as the quoteToken in the currency pair calculation of price + * snapshot: If true, a snapshot of the orderbook will be sent before the updates to the orderbook + * limit: Maximum number of bids and asks in orderbook snapshot + */ +export interface OrderbookChannelSubscriptionOpts { + baseTokenAddress: string; + quoteTokenAddress: string; + snapshot: boolean; + limit: number; +} + export interface OrderbookChannelHandler { onSnapshot: (channel: OrderbookChannel, snapshot: OrderbookResponse) => void; onUpdate: (channel: OrderbookChannel, order: SignedOrder) => void; @@ -48,17 +61,15 @@ export interface UnknownOrderbookChannelMessage { payload: undefined; } -/* - * baseTokenAddress: The address of token designated as the baseToken in the currency pair calculation of price - * quoteTokenAddress: The address of token designated as the quoteToken in the currency pair calculation of price - * snapshot: If true, a snapshot of the orderbook will be sent before the updates to the orderbook - * limit: Maximum number of bids and asks in orderbook snapshot - */ -export interface OrderbookChannelSubscriptionOpts { - baseTokenAddress: string; - quoteTokenAddress: string; - snapshot: boolean; - limit: number; +export enum WebsocketConnectionEventType { + Close = 'close', + Error = 'error', + Message = 'message', +} + +export enum WebsocketClientEventType { + Connect = 'connect', + ConnectFailed = 'connectFailed', } export interface TokenPairsRequest { @@ -118,3 +129,13 @@ export interface FeesResponse { makerFee: BigNumber; takerFee: BigNumber; } + +export interface HttpRequestOptions { + params?: object; + payload?: object; +} + +export enum HttpRequestType { + Get = 'GET', + Post = 'POST', +} diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts index d3aadb25e..f5badf624 100644 --- a/packages/connect/src/ws_orderbook_channel.ts +++ b/packages/connect/src/ws_orderbook_channel.ts @@ -9,20 +9,11 @@ import { OrderbookChannelHandler, OrderbookChannelMessageTypes, OrderbookChannelSubscriptionOpts, + WebsocketClientEventType, + WebsocketConnectionEventType, } from './types'; import {orderbookChannelMessageParsers} from './utils/orderbook_channel_message_parsers'; -enum ConnectionEventType { - Close = 'close', - Error = 'error', - Message = 'message', -} - -enum ClientEventType { - Connect = 'connect', - ConnectFailed = 'connectFailed', -} - /** * This class includes all the functionality related to interacting with a websocket endpoint * that implements the standard relayer API v0 @@ -64,13 +55,13 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { if (!_.isUndefined(error)) { handler.onError(this, error); } else if (!_.isUndefined(connection) && connection.connected) { - connection.on(ConnectionEventType.Error, wsError => { + connection.on(WebsocketConnectionEventType.Error, wsError => { handler.onError(this, wsError); }); - connection.on(ConnectionEventType.Close, () => { + connection.on(WebsocketConnectionEventType.Close, () => { handler.onClose(this); }); - connection.on(ConnectionEventType.Message, message => { + connection.on(WebsocketConnectionEventType.Message, message => { this._handleWebSocketMessage(message, handler); }); connection.sendUTF(JSON.stringify(subscribeMessage)); @@ -89,11 +80,11 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { if (!_.isUndefined(this.connectionIfExists) && this.connectionIfExists.connected) { callback(undefined, this.connectionIfExists); } else { - this.client.on(ClientEventType.Connect, connection => { + this.client.on(WebsocketClientEventType.Connect, connection => { this.connectionIfExists = connection; callback(undefined, this.connectionIfExists); }); - this.client.on(ClientEventType.ConnectFailed, error => { + this.client.on(WebsocketClientEventType.ConnectFailed, error => { callback(error, undefined); }); this.client.connect(this.apiEndpointUrl); |