Skip to content

Commit

Permalink
Add readonly property modifier everywhere the property isn't modified. (
Browse files Browse the repository at this point in the history
coinbase#192)

* Add readonly property modifier everywhere the property isn't modified.

* BookBuilder: don't set default property values when clear() resets them.

This is more of a do not repeat yourself best practice to not set the
intial property values twice.

* OrderbookDiff: removed unused property.
  • Loading branch information
blair authored and fb55 committed Apr 14, 2018
1 parent fe89dcb commit e6c3e6e
Show file tree
Hide file tree
Showing 30 changed files with 85 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/FXService/FXProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export interface FXProviderConfig {
}

export abstract class FXProvider {
private logger: Logger;
private _pending: { [pair: string]: Promise<FXObject> } = {};
private readonly logger: Logger;
private readonly _pending: { [pair: string]: Promise<FXObject> } = {};

constructor(config: FXProviderConfig) {
this.logger = config.logger;
Expand Down
4 changes: 2 additions & 2 deletions src/FXService/calculators/FailoverCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export interface LastRequestInfo {
* If the pair is unavailable, or some other error occurs, the calculator returns null for that pair
*/
export default class FailoverCalculator extends FXRateCalculator {
logger: Logger;
calculators: FXRateCalculator[];
readonly logger: Logger;
readonly calculators: FXRateCalculator[];
private lastCalculatorUsed: FXRateCalculator = null;

constructor(config: FailoverCalculatorConfig) {
Expand Down
10 changes: 5 additions & 5 deletions src/FXService/calculators/RobustCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export interface QueryStatus {
* To be reliable, one should supply at least 5 rate sources for this method.
*/
export class RobustCalculator extends FXRateCalculator {
deltaThreshold: number;
priceThreshold: number;
logger: Logger;
sources: FXProvider[];
minNumberOfReliableSources: number;
readonly deltaThreshold: number;
readonly priceThreshold: number;
readonly logger: Logger;
readonly sources: FXProvider[];
readonly minNumberOfReliableSources: number;
private report: RobustCalculatorReport;

constructor(config: RobustCalculatorConfig) {
Expand Down
4 changes: 2 additions & 2 deletions src/FXService/calculators/SimpleRateCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { ConsoleLoggerFactory, Logger } from '../../utils/Logger';
* If the pair is unavailable, or some other error occurs, the calculator returns null for that pair
*/
export default class SimpleRateCalculator extends FXRateCalculator {
logger: Logger;
provider: FXProvider;
readonly logger: Logger;
readonly provider: FXProvider;

constructor(provider: FXProvider, logger?: Logger) {
super();
Expand Down
2 changes: 1 addition & 1 deletion src/FXService/providers/CoinMarketCapProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interface ResultCache {
}

export default class CoinMarketCapProvider extends FXProvider {
private lastUpdate: { [id: string]: ResultCache };
private readonly lastUpdate: { [id: string]: ResultCache };
private initializing: Promise<void>;

constructor(config: FXProviderConfig) {
Expand Down
2 changes: 1 addition & 1 deletion src/FXService/providers/CryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface CryptoProviderConfig extends FXProviderConfig {
}

export class CryptoProvider extends FXProvider {
private exchange: PublicExchangeAPI;
private readonly exchange: PublicExchangeAPI;
private products: string[];

constructor(config: CryptoProviderConfig) {
Expand Down
2 changes: 1 addition & 1 deletion src/FXService/providers/FailoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface FailoverProviderConfig extends FXProviderConfig {
* son on.
*/
export class FailoverProvider extends FXProvider {
private providers: FXProvider[];
private readonly providers: FXProvider[];

constructor(config: FailoverProviderConfig) {
super(config);
Expand Down
4 changes: 2 additions & 2 deletions src/FXService/providers/OpenExchangeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export interface OpenExchangeConfig extends FXProviderConfig {
}

export default class OpenExchangeProvider extends FXProvider {
private apiKey: string;
private readonly apiKey: string;
private pending: Promise<Response> = null;
private cacheDuration: number;
private readonly cacheDuration: number;
private cacheTimer: Timer = null;
private base: string = null;

Expand Down
8 changes: 4 additions & 4 deletions src/core/ExchangeRateFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ const ARRAY_FIELDS = ['bids', 'asks'];
* can watch for FXRateMayBeInvalid events and respond accordingly
*/
export class ExchangeRateFilter extends AbstractMessageTransform {
private pairIndex: string;
private pair: CurrencyPair;
private fxService: FXService;
private precision: number;
private readonly pairIndex: string;
private readonly pair: CurrencyPair;
private readonly fxService: FXService;
private readonly precision: number;

constructor(config: ExchangeRateFilterConfig) {
super(config);
Expand Down
2 changes: 1 addition & 1 deletion src/core/HFTFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class HFTFilter extends Duplex {
private messagesById: { [id: string]: BaseOrderMessage };
private skippedMessages: number = 0;
private tradesSkipped: number = 0;
private targetQueueLength: number;
private readonly targetQueueLength: number;

constructor(config: HFTFilterConfig) {
super({ readableObjectMode: true, writableObjectMode: true });
Expand Down
8 changes: 4 additions & 4 deletions src/core/LiveOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export class LiveOrderbook extends Duplex implements Orderbook {
public readonly baseCurrency: string;
public readonly quoteCurrency: string;
protected snapshotReceived: boolean;
protected strictMode: boolean;
protected readonly strictMode: boolean;
protected lastBookUpdate: Date;
protected _book: BookBuilder;
protected liveTicker: Ticker;
protected readonly _book: BookBuilder;
protected readonly liveTicker: Ticker;
protected _sourceSequence: number;
private logger: Logger;
private readonly logger: Logger;

constructor(config: LiveBookConfig) {
super({ objectMode: true, highWaterMark: 1024 });
Expand Down
8 changes: 4 additions & 4 deletions src/core/MessageQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export interface MessageQueueConfig {
* Emitted if a message is about to be sent out, out of sequence and waiting would violate the `targetQueueLength` constraint
*/
export class MessageQueue extends Duplex {
private logger: Logger;
private readonly logger: Logger;
private readonly messages: RBTree<SequencedMessage>;
private targetQueueLength: number;
private readonly targetQueueLength: number;
private lastSequence: number;
private productId: string;
private waitForSnapshot: boolean;
private readonly productId: string;
private readonly waitForSnapshot: boolean;

constructor(options: MessageQueueConfig) {
super({ readableObjectMode: true, writableObjectMode: true });
Expand Down
2 changes: 1 addition & 1 deletion src/core/ProductFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface ProductFilterConfig extends MessageTransformConfig {
* Filters out any messages that don't have the configured product_id
*/
export class ProductFilter extends AbstractMessageTransform {
public productId: string;
public readonly productId: string;

constructor(config: ProductFilterConfig) {
super(config);
Expand Down
2 changes: 1 addition & 1 deletion src/core/ProductSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { StreamCopier } from '../lib/StreamCopier';
import { ExchangeFeed } from '../exchanges/ExchangeFeed';

export class ProductSplitter extends StreamCopier {
private products: string[];
private readonly products: string[];

constructor(feed: ExchangeFeed, productIds: string[]) {
const numProducts = productIds.length;
Expand Down
14 changes: 7 additions & 7 deletions src/core/Trader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ export interface TraderConfig {
* Trader.cancel-order-failed - A Cancel request returned with an error status
*/
export class Trader extends Writable {
private _productId: string;
private logger: Logger;
private readonly _productId: string;
private readonly logger: Logger;
// Used to keep track of all non-market orders this trader has placed.
private myBook: BookBuilder;
private api: AuthenticatedExchangeAPI;
private readonly myBook: BookBuilder;
private readonly api: AuthenticatedExchangeAPI;
private _fitOrders: boolean = true;
private sizePrecision: number;
private pricePrecision: number;
private unfilledMarketOrders: Set<string>;
private readonly sizePrecision: number;
private readonly pricePrecision: number;
private readonly unfilledMarketOrders: Set<string>;

constructor(config: TraderConfig) {
super({ objectMode: true });
Expand Down
6 changes: 3 additions & 3 deletions src/exchanges/ExchangeFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export const hooks = {
};

export abstract class ExchangeFeed extends Readable {
protected auth: ExchangeAuthConfig;
protected readonly auth: ExchangeAuthConfig;
protected url: string;
protected _isConnecting: boolean;
// keys in this list will be sanitised in log messages
protected sensitiveKeys: string[];
protected readonly sensitiveKeys: string[];
private lastHeartBeat: number = -1;
private connectionChecker: Timer = null;
private socket: WebSocket;
private _logger: Logger;
private readonly _logger: Logger;

constructor(config: ExchangeFeedConfig) {
super({ objectMode: true, highWaterMark: 1024 });
Expand Down
6 changes: 3 additions & 3 deletions src/exchanges/bitfinex/BitfinexExchangeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export class BitfinexExchangeAPI implements PublicExchangeAPI, AuthenticatedExch
};
}

owner: string;
private auth: ExchangeAuthConfig;
private logger: Logger;
readonly owner: string;
private readonly auth: ExchangeAuthConfig;
private readonly logger: Logger;

constructor(config: BitfinexConfig) {
this.owner = 'Bitfinex';
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/bitfinex/BitfinexFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class BitfinexFeed extends ExchangeFeed {
private subscriptions: BitfinexChannels;
private paused: boolean;
private pinger: NodeJS.Timer;
private standardMessages: boolean;
private snapshotDepth: number;
private readonly standardMessages: boolean;
private readonly snapshotDepth: number;

constructor(config: BitfinexFeedConfig) {
super(config);
Expand Down
2 changes: 1 addition & 1 deletion src/exchanges/bittrex/BittrexFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Bittrex = require('node-bittrex-api');
export class BittrexFeed extends ExchangeFeed {
private client: any;
private connection: any;
private counters: { [product: string]: MessageCounter };
private readonly counters: { [product: string]: MessageCounter };

constructor(config: ExchangeFeedConfig) {
super(config);
Expand Down
6 changes: 3 additions & 3 deletions src/exchanges/ccxt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ export default class CCXTExchangeWrapper implements PublicExchangeAPI, Authentic
}

readonly owner: string;
private instance: ccxt.Exchange;
private options: any;
private logger: Logger;
private readonly instance: ccxt.Exchange;
private readonly options: any;
private readonly logger: Logger;

constructor(owner: string, opts: any, ccxtInstance: ccxt.Exchange, logger: Logger) {
this.owner = owner;
Expand Down
16 changes: 8 additions & 8 deletions src/exchanges/gdax/GDAXExchangeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ interface OrderPage {
}

export class GDAXExchangeAPI implements PublicExchangeAPI, AuthenticatedExchangeAPI, ExchangeTransferAPI {
owner: string;
quoteCurrency: string;
baseCurrency: string;
readonly owner: string;
readonly quoteCurrency: string;
readonly baseCurrency: string;
private coinbaseAccounts: CoinbaseAccount[];
private _apiURL: string;
private publicClient: PublicClient;
private authClient: AuthenticatedClient;
private auth: GDAXAuthConfig;
private logger: Logger;
private readonly _apiURL: string;
private readonly publicClient: PublicClient;
private readonly authClient: AuthenticatedClient;
private readonly auth: GDAXAuthConfig;
private readonly logger: Logger;

constructor(options: GDAXConfig) {
this.owner = 'GDAX';
Expand Down
8 changes: 4 additions & 4 deletions src/exchanges/gdax/GDAXFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export interface GDAXFeedConfig extends ExchangeFeedConfig {
* Error messages from the Websocket feed are passed down the stream and also emitted as 'feed-error' events.
*/
export class GDAXFeed extends ExchangeFeed {
private products: Set<string>;
private gdaxAPI: GDAXExchangeAPI;
private internalSequence: { [index: string]: number } = {};
private channels: string[];
private readonly products: Set<string>;
private readonly gdaxAPI: GDAXExchangeAPI;
private readonly internalSequence: { [index: string]: number } = {};
private readonly channels: string[];

constructor(config: GDAXFeedConfig) {
super(config);
Expand Down
4 changes: 2 additions & 2 deletions src/exchanges/gemini/GeminiMarketFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import * as GI from './GeminiInterfaces';
export class GeminiMarketFeed extends ExchangeFeed {
readonly owner: string;
readonly feedUrl: string;
private productId: string;
private ccxtProductId: string;
private readonly productId: string;
private readonly ccxtProductId: string;

constructor(config: GI.GeminiMarketFeedConfig) {
super(config);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/AbstractMessageTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface MessageTransformConfig {
* will implement.
*/
export abstract class AbstractMessageTransform extends stream.Transform {
protected logger: Logger;
protected readonly logger: Logger;

constructor(config: MessageTransformConfig) {
super({ readableObjectMode: true, writableObjectMode: true });
Expand Down
18 changes: 9 additions & 9 deletions src/lib/BookBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface OrderPool { [id: string]: Level3Order; }
export class AggregatedLevel implements PriceLevel {
totalSize: BigJS;
totalValue: BigJS;
price: BigJS;
readonly price: BigJS;
private _numOrders: number;

constructor(price: BigJS) {
Expand Down Expand Up @@ -98,7 +98,7 @@ export class AggregatedLevel implements PriceLevel {
}

export class AggregatedLevelWithOrders extends AggregatedLevel implements PriceLevelWithOrders {
private _orders: Level3Order[];
private readonly _orders: Level3Order[];

constructor(price: BigJS) {
super(price);
Expand Down Expand Up @@ -150,15 +150,15 @@ export class AggregatedLevelWithOrders extends AggregatedLevel implements PriceL
* Call #state to get a hierarchical object representation of the orderbook
*/
export class BookBuilder extends EventEmitter implements Orderbook {
public sequence: number = -1;
public sequence: number;
protected readonly bids: RBTree<AggregatedLevelWithOrders> = PriceTreeFactory<AggregatedLevelWithOrders>();
protected readonly asks: RBTree<AggregatedLevelWithOrders> = PriceTreeFactory<AggregatedLevelWithOrders>();
protected _bidsTotal: BigJS = ZERO;
protected _bidsValueTotal: BigJS = ZERO;
protected _asksTotal: BigJS = ZERO;
protected _asksValueTotal: BigJS = ZERO;
private _orderPool: OrderPool = {};
private logger: Logger;
protected _bidsTotal: BigJS;
protected _bidsValueTotal: BigJS;
protected _asksTotal: BigJS;
protected _asksValueTotal: BigJS;
private _orderPool: OrderPool;
private readonly logger: Logger;

constructor(logger: Logger) {
super();
Expand Down
8 changes: 3 additions & 5 deletions src/lib/OrderbookDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ export class OrderbookDiff {
return book.state();
}

productId: string;
commands: StreamMessage[] = [];
initial: BookBuilder;
final: BookBuilder;
readonly productId: string;
readonly initial: BookBuilder;
readonly final: BookBuilder;

constructor(productId: string, initial: BookBuilder, final: BookBuilder) {
this.initial = initial;
Expand Down Expand Up @@ -174,7 +173,6 @@ export class OrderbookDiff {
commands.push(order);
});
});
this.commands = commands;
return commands;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/OrderbookUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class OrderbookUtils {
return result;
}

private book: OrderbookState;
private readonly book: OrderbookState;

private precalc: OrderbookCache;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/StaticCommandSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { StreamMessage } from '../core/Messages';
* Simple stream that pipes through a supplied set of stream messages when the `send` method is called.
*/
export class StaticCommandSet extends Readable {
messages: StreamMessage[];
private autoClose: boolean;
readonly messages: StreamMessage[];
private readonly autoClose: boolean;

constructor(messages: StreamMessage[], autoClose: boolean = true) {
super({ objectMode: true });
Expand Down
Loading

0 comments on commit e6c3e6e

Please sign in to comment.