Skip to content

Commit

Permalink
ErrorMessage extends StreamMessage (fixes coinbase#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
kostola committed Sep 22, 2017
1 parent 7f3293b commit a99fd39
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/core/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ export function isStreamMessage(msg: any): boolean {
return !!msg.type;
}

export interface ErrorMessage {
export interface ErrorMessage extends StreamMessage {
message: string;
details?: any;
}

export function isErrorMessage(msg: any): boolean {
return !!msg.message && typeof msg.message === 'string';
return isStreamMessage(msg) && !!msg.message && typeof msg.message === 'string';
}

export interface UnknownMessage extends StreamMessage {
Expand All @@ -43,6 +44,10 @@ export interface UnknownMessage extends StreamMessage {
message: any;
}

export function isUnknownMessage(msg: any): boolean {
return isStreamMessage(msg) && !!msg.message && typeof msg.message !== 'string';
}

/**
* Root definition for messages that stem from a websocket feed
*/
Expand Down
12 changes: 11 additions & 1 deletion src/exchanges/gdax/GDAXFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { AuthHeaders, GDAXAuthConfig, GDAXExchangeAPI } from './GDAXExchangeAPI'
import { Big } from '../../lib/types';
import {
ChangedOrderMessage,
ErrorMessage,
LevelMessage,
MyOrderPlacedMessage,
NewOrderMessage,
Expand All @@ -32,6 +33,7 @@ import { OrderPool } from '../../lib/BookBuilder';
import {
GDAXChangeMessage, GDAXChannel,
GDAXDoneMessage,
GDAXErrorMessage,
GDAXL2UpdateMessage,
GDAXMatchMessage,
GDAXMessage,
Expand Down Expand Up @@ -374,12 +376,20 @@ export class GDAXFeed extends ExchangeFeed {
price: change.price,
newSize: change.new_size
} as ChangedOrderMessage;
case 'error':
const error: GDAXErrorMessage = feedMessage as GDAXErrorMessage;
return {
type: 'error',
time: new Date(),
message: error.message,
details: { reason: error.reason }
} as ErrorMessage;
default:
return {
type: 'unknown',
time: new Date(),
sequence: (feedMessage as any).sequence,
productId: feedMessage.product_id,
productId: (feedMessage as any).product_id,
message: feedMessage
} as UnknownMessage;
}
Expand Down
27 changes: 18 additions & 9 deletions src/exchanges/gdax/GDAXMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

export interface GDAXMessage {
type: string;
product_id: string;
user_id?: string; // Authenticated messages only
}

Expand All @@ -27,12 +26,22 @@ export interface GDAXChannel {
product_ids: string[];
}

export interface GDAXSubscriptionsMessage {
export interface GDAXErrorMessage extends GDAXMessage {
type: string;
message: string;
reason: string;
}

export interface GDAXProductMessage extends GDAXMessage {
product_id: string;
}

export interface GDAXSubscriptionsMessage extends GDAXMessage {
type: string;
channels: GDAXChannel[];
}

export interface GDAXOpenMessage extends GDAXMessage {
export interface GDAXOpenMessage extends GDAXProductMessage {
sequence: number;
time: string;
order_id: string;
Expand All @@ -41,7 +50,7 @@ export interface GDAXOpenMessage extends GDAXMessage {
side: string;
}

export interface GDAXDoneMessage extends GDAXMessage {
export interface GDAXDoneMessage extends GDAXProductMessage {
sequence: number;
time: string;
price: string;
Expand All @@ -51,7 +60,7 @@ export interface GDAXDoneMessage extends GDAXMessage {
remaining_size: string;
}

export interface GDAXMatchMessage extends GDAXMessage {
export interface GDAXMatchMessage extends GDAXProductMessage {
sequence: number;
time: string;
trade_id: string;
Expand All @@ -62,7 +71,7 @@ export interface GDAXMatchMessage extends GDAXMessage {
side: string;
}

export interface GDAXChangeMessage extends GDAXMessage {
export interface GDAXChangeMessage extends GDAXProductMessage {
sequence: number;
time: string;
order_id: string;
Expand All @@ -74,11 +83,11 @@ export interface GDAXChangeMessage extends GDAXMessage {
side: string;
}

export interface GDAXL2UpdateMessage extends GDAXMessage {
export interface GDAXL2UpdateMessage extends GDAXProductMessage {
changes: string[][]; // [ [ side, price, newSize ] ]
}

export interface GDAXTickerMessage extends GDAXMessage {
export interface GDAXTickerMessage extends GDAXProductMessage {
trade_id: number;
sequence: number;
time: string;
Expand All @@ -89,7 +98,7 @@ export interface GDAXTickerMessage extends GDAXMessage {
last_size: string;
}

export interface GDAXSnapshotMessage extends GDAXMessage {
export interface GDAXSnapshotMessage extends GDAXProductMessage {
bids: string[][]; // [ [price, size] ]
asks: string[][]; // [ [price, size] ]
}
Expand Down

0 comments on commit a99fd39

Please sign in to comment.