Skip to content

Commit

Permalink
Merge branch 'master' into tighten-gdax-feed-messages-types
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Apr 10, 2018
2 parents 536cca8 + e721151 commit 40e4d7f
Show file tree
Hide file tree
Showing 49 changed files with 326 additions and 268 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ node_modules/
!types/
docs/apiref

src/
/src/
npm-shrinkwrap.json
yarn.lock
tsconfig.json
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@
"@types/commander": "2.3.31",
"@types/crypto-js": "3.1.32",
"@types/nock": "8.2.0",
"@types/node": "6.0.51",
"@types/node": "7.0.58",
"@types/query-string": "3.0.30",
"@types/simple-mock": "0.0.27",
"@types/superagent": "2.0.35",
"@types/uuid": "2.0.29",
"@types/winston": "2.2.0",
"@types/winston": "2.3.8",
"@types/ws": "0.0.37",
"bignumber.js": "4.0.2",
"bintrees": "1.0.1",
Expand All @@ -72,11 +71,10 @@
"nock": "9.0.2",
"nyc": "11.1.0",
"source-map-support": "0.4.16",
"ts-node": "^3.3.0",
"tslint": "5.6.0",
"ts-node": "^5.0.1",
"tslint": "^5.9.1",
"typedoc": "0.8.0",
"typescript": "^2.5.3",
"uuid": "3.0.0"
"typescript": "^2.7.2"
},
"resolutions": {
"deasync": "^0.1.12"
Expand Down
4 changes: 2 additions & 2 deletions src/FXService/FXProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ export abstract class FXProvider {
return this.supportsPair(inversePair).then<FXObject>((inverseOk: boolean) => {
if (inverseOk) {
return this.getPromiseForRate(inversePair).then((inverse: FXObject) => {
const rate = {
const rate: FXObject = {
from: pair.from,
to: pair.to,
rate: ONE.div(inverse.rate),
time: inverse.time
};
return Promise.resolve<FXObject>(rate);
return rate;
});
} else {
return Promise.reject(new EFXRateUnavailable(`Currency pair ${pair.from}-${pair.to} or its inverse is not supported`, this.name));
Expand Down
13 changes: 6 additions & 7 deletions src/FXService/FXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class FXService extends EventEmitter {
/**
* Replaces the [[FXRateCalculator]] instance and returns `this` so that you can chain setter calls.
*/
setCalculator(value: FXRateCalculator) {
setCalculator(value: FXRateCalculator): this {
this._calculator = value;
return this;
}
Expand All @@ -88,7 +88,7 @@ export class FXService extends EventEmitter {
/**
* Sets a logger for the service and returns `this` so that you can chain setter calls.
*/
setLogger(value: Logger) {
setLogger(value: Logger): this {
this._logger = value;
return this;
}
Expand All @@ -103,7 +103,7 @@ export class FXService extends EventEmitter {
/**
* Assigns a new polling interval for calculating exchange rates (in ms). Returns `this` so that you can chain setter calls.
*/
setRefreshInterval(value: number) {
setRefreshInterval(value: number): this {
this._refreshInterval = value;
if (this.timer) {
clearInterval(this.timer);
Expand All @@ -124,7 +124,7 @@ export class FXService extends EventEmitter {
/**
* Replace all current pairs with the array of pairs provided. Returns `this` so that you can chain setter calls.
*/
setActivePairs(pairs: CurrencyPair[]) {
setActivePairs(pairs: CurrencyPair[]): this {
this._currencyPairs = [];
pairs.forEach((pair) => this.addCurrencyPair(pair));
return this;
Expand All @@ -134,7 +134,7 @@ export class FXService extends EventEmitter {
* Adds a single currency pair to the service without removing any that are already being queried. If the pair is already
* in the list, this function has no effect. Returns `this` so that you can chain setter calls.
*/
addCurrencyPair(pair: CurrencyPair) {
addCurrencyPair(pair: CurrencyPair): this {
if (this.indexOf(pair) >= 0) {
return this;
}
Expand Down Expand Up @@ -228,7 +228,6 @@ export class FXService extends EventEmitter {
this._rates[index] = Object.assign({ change: change }, rate);
});
this.errorState = false;
return Promise.resolve();
}, (err: Error) => {
this.log('warn', 'An error occurred fetching latest exchange rates', err.message);
this.errorState = true;
Expand All @@ -239,7 +238,7 @@ export class FXService extends EventEmitter {
// because we emit a message above, client errors will be caught and returned here.
this.log('error', 'A client error has caused an FXUpdate failure', err);
this.errorState = true;
return Promise.resolve(null);
return null;
});
}
}
2 changes: 1 addition & 1 deletion src/FXService/calculators/RobustCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class RobustCalculator extends FXRateCalculator {
rate: rate && Big(rate),
change: null
};
return Promise.resolve(result);
return result;
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/FXService/providers/CoinMarketCapProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ export default class CoinMarketCapProvider extends FXProvider {
REVERSE_MAP[currency.id] = currency.symbol;
SUPPORTED_BASE_CURRENCIES.push(currency.symbol);
});
return Promise.resolve();
});
this.initializing = initCodeMap;
}
return initCodeMap.then(() => {
return Promise.resolve(SUPPORTED_BASE_CURRENCIES.includes(pair.from));
return SUPPORTED_BASE_CURRENCIES.includes(pair.from);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/FXService/providers/OpenExchangeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class OpenExchangeProvider extends FXProvider {
.then((res: Response) => {
const curs: { [cur: string]: string } = res.body;
supportedCurrencies = Object.keys(curs);
return Promise.resolve(isSupported(pair));
return isSupported(pair);
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/FXService/providers/YahooFXProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class YahooFinanceFXProvider extends FXProvider {
SUPPORTED_PAIRS.push(tag.slice(0, tag.length - 2));
}
});
return Promise.resolve(this.isSupportedPair(pair));
return this.isSupportedPair(pair);
}, (err: Error) => Promise.reject(err));
}

Expand All @@ -73,12 +73,12 @@ export default class YahooFinanceFXProvider extends FXProvider {
} catch (err) {
return Promise.reject(error);
}
return Promise.resolve<FXObject>({
const r: FXObject = {
time: new Date(created),
from: pair.from,
to: pair.to,
rate: Big(rate)
});
rate: Big(rate)};
return r;
}, (result: Response) => {
let details = {};
try {
Expand Down
8 changes: 5 additions & 3 deletions src/consoles/gdaxConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import { Ticker } from '../exchanges/PublicExchangeAPI';
import { Balances } from '../exchanges/AuthenticatedExchangeAPI';
import { PlaceOrderMessage } from '../core/Messages';
import { LiveOrder } from '../lib/Orderbook';
import { AuthCallOptions, GDAXAuthConfig, GDAXConfig } from '../exchanges/gdax/GDAXInterfaces';
import { AuthCallOptions,
GDAXAuthConfig,
GDAXConfig } from '../exchanges/gdax/GDAXInterfaces';
import { TransferRequest, TransferResult } from '../exchanges/ExchangeTransferAPI';
import { Big } from '../lib/types';

Expand Down Expand Up @@ -142,11 +144,11 @@ if (program.newMarketOrder && hasAuth()) {
}
const vals = program.newMarketOrder.split(',');
const params: PlaceOrderMessage = {
type: 'placeOrder',
time: new Date(),
clientId: null,
side: vals[0],
size: vals[1],
type: 'market',
productId: program.product,
price: null,
orderType: 'market'
Expand All @@ -165,11 +167,11 @@ if (program.newLimitOrder && hasAuth()) {
}
const [side, size, price] = program.newLimitOrder.split(',');
const params: PlaceOrderMessage = {
type: 'placeOrder',
time: new Date(),
clientId: null,
side: side,
size: size,
type: 'market',
productId: program.product,
price: price,
orderType: 'limit'
Expand Down
3 changes: 2 additions & 1 deletion src/core/ExchangeRateFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* License for the specific language governing permissions and limitations under the License. *
***************************************************************************************************************************/

import { AbstractMessageTransform, MessageTransformConfig } from '../lib/AbstractMessageTransform';
import { AbstractMessageTransform,
MessageTransformConfig } from '../lib/AbstractMessageTransform';
import { StreamMessage } from './Messages';
import { CurrencyPair, FXObject, pairAsString } from '../FXService/FXProvider';
import { FXRates, FXService } from '../FXService/FXService';
Expand Down
7 changes: 6 additions & 1 deletion src/core/HFTFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
* License for the specific language governing permissions and limitations under the License. *
***************************************************************************************************************************/

import { BaseOrderMessage, ChangedOrderMessage, isOrderMessage, isStreamMessage, NewOrderMessage, StreamMessage } from './Messages';
import { BaseOrderMessage,
ChangedOrderMessage,
isOrderMessage,
isStreamMessage,
NewOrderMessage,
StreamMessage } from './Messages';
import { RBTree } from 'bintrees';
import { Duplex } from 'stream';
import { MessageTransformConfig } from '../lib/AbstractMessageTransform';
Expand Down
13 changes: 9 additions & 4 deletions src/core/LiveOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
***************************************************************************************************************************/

import { Big, BigJS, Biglike, ZERO } from '../lib/types';
import { CumulativePriceLevel, Level3Order, Orderbook, OrderbookState } from '../lib/Orderbook';
import { AggregatedLevelFactory, AggregatedLevelWithOrders, BookBuilder, StartPoint } from '../lib/BookBuilder';
import { CumulativePriceLevel,
Level3Order,
Orderbook,
OrderbookState } from '../lib/Orderbook';
import { AggregatedLevelFactory,
AggregatedLevelWithOrders,
BookBuilder,
StartPoint } from '../lib/BookBuilder';
import { Logger } from '../utils/Logger';
import { Ticker } from '../exchanges/PublicExchangeAPI';
import {
Expand Down Expand Up @@ -292,8 +298,7 @@ export class LiveOrderbook extends Duplex implements Orderbook {
// If we're using an order pool, then we only remove orders that we're aware of. GDAX, for example might
// send a done message for a stop order that is cancelled (and was not previously known to us).
// Also filled orders will already have been removed by the time a GDAX done order reaches here
const book: BookBuilder = this._book;
if (!book.hasOrder(msg.orderId)) {
if (!this._book.hasOrder(msg.orderId)) {
return;
}
if (!(this._book.remove(msg.orderId))) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/MessageQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Duplex } from 'stream';
*
* @param logger {Logger} An optional logging interface
*
* @param product {string} A product to filter for. A single feed might be producing messages form multiple products (each with their own
* @param product {string} A product to filter for. A single feed might be producing messages from multiple products (each with their own
* sequence numbers). This selects for the product so that all messages can be emitted in strictly increasing sequence number.
*
* @param targetQueueLength {number} Tries to maintain the queue length at this value. Default is zero, but you can increase to
Expand Down
7 changes: 6 additions & 1 deletion src/core/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function isErrorMessage(msg: any): boolean {
* Any context-rich information can be extracted into the `extra` field, and the original message should be attached to the `origin` field as usual.
*/
export interface UnknownMessage extends StreamMessage {
type: 'unknown';
sequence?: number;
productId?: string;
tag?: string;
Expand Down Expand Up @@ -166,6 +167,7 @@ export interface TickerMessage extends StreamMessage, Ticker {
* in the extra field, which can be handled by the target trade engine.
*/
export interface PlaceOrderMessage extends StreamMessage {
type: 'placeOrder';
productId: string;
clientId?: string;
side: string;
Expand All @@ -178,14 +180,15 @@ export interface PlaceOrderMessage extends StreamMessage {
}

export interface CancelOrderRequestMessage extends StreamMessage {
type: string; // cancelOrder
type: 'cancelOrder';
orderId: string;
}

/**
* Emitted from a feed when one of my orders has been matched. (An authenticated feed is required)
*/
export interface TradeExecutedMessage extends StreamMessage {
type: 'tradeExecuted';
productId: string;
orderId: string;
side: string;
Expand All @@ -196,6 +199,7 @@ export interface TradeExecutedMessage extends StreamMessage {
}

export interface TradeFinalizedMessage extends StreamMessage {
type: 'tradeFinalized';
productId: string;
orderId: string;
side: string;
Expand All @@ -205,6 +209,7 @@ export interface TradeFinalizedMessage extends StreamMessage {
}

export interface MyOrderPlacedMessage extends StreamMessage {
type: 'myOrderPlaced';
productId: string;
orderId: string;
side: string;
Expand Down
8 changes: 5 additions & 3 deletions src/core/ProductFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
* License for the specific language governing permissions and limitations under the License. *
***************************************************************************************************************************/

import { AbstractMessageTransform, MessageTransformConfig } from '../lib/AbstractMessageTransform';
import { AbstractMessageTransform,
MessageTransformConfig } from '../lib/AbstractMessageTransform';
import { StreamMessage } from '../core/Messages';

export interface ProductFilterConfig extends MessageTransformConfig {
productId: string;
Expand All @@ -29,8 +31,8 @@ export class ProductFilter extends AbstractMessageTransform {
this.productId = config.productId;
}

transformMessage(msg: any): any {
if (!msg || !msg.productId || msg.productId !== this.productId) {
transformMessage(msg: StreamMessage): StreamMessage {
if (!msg || !(msg as any).productId || (msg as any).productId !== this.productId) {
return null;
}
return msg;
Expand Down
Loading

0 comments on commit 40e4d7f

Please sign in to comment.