Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript) - add typescript runtime test #22276

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions build/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ class Transpiler {
[ /Precise\.stringLe\s/g, 'Precise.string_le' ],
[ /\.padEnd\s/g, '.ljust'],
[ /\.padStart\s/g, '.rjust' ],
[ /ArrayCache<\w+>/g, 'ArrayCache' ],
[ /ArrayCacheByTimestamp<\w+>/g, 'ArrayCacheByTimestamp' ],
[ /ArrayCacheBySymbolById<\w+>/g, 'ArrayCacheBySymbolById' ],
[ /ArrayCacheBySymbolBySide<\w+>/g, 'ArrayCacheBySymbolBySide' ],

// insert common regexes in the middle (critical)
].concat (this.getCommonRegexes ()).concat ([
Expand Down Expand Up @@ -678,6 +682,10 @@ class Transpiler {
[ /Precise\.stringLe\s/g, 'Precise::string_le' ],
[ /(\w+)\.padEnd\s*\(([^,]+),\s*([^)]+)\)/g, 'str_pad($1, $2, $3, STR_PAD_RIGHT)' ],
[ /(\w+)\.padStart\s*\(([^,]+),\s*([^)]+)\)/g, 'str_pad($1, $2, $3, STR_PAD_LEFT)' ],
[ /ArrayCache<\w+>/g, 'ArrayCache' ],
[ /ArrayCacheByTimestamp<\w+>/g, 'ArrayCacheByTimestamp' ],
[ /ArrayCacheBySymbolById<\w+>/g, 'ArrayCacheBySymbolById' ],
[ /ArrayCacheBySymbolBySide<\w+>/g, 'ArrayCacheBySymbolBySide' ],

// insert common regexes in the middle (critical)
].concat (this.getCommonRegexes ()).concat ([
Expand Down Expand Up @@ -1018,6 +1026,7 @@ class Transpiler {
'MarketInterface': /-> MarketInterface:/,
'MarketType': /: MarketType/,
'Num': /: (?:List\[)?Num =/,
'OHLCV': /-> (?:List\[)?OHLCV/,
'Option': /-> Option:/,
'OptionChain': /-> OptionChain:/,
'Order': /-> (?:List\[)?Order\]?:/,
Expand Down
14 changes: 7 additions & 7 deletions ts/src/base/Exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,15 @@ export default class Exchange {

balance = {}
orderbooks: Dictionary<Ob> = {}
tickers: Dictionary<Ticker> = {}
tickers: Dictionary<Ticker> = {}
bidsasks: Dictionary<Ticker> = {}
fundingRates: Dictionary<FundingRate> = {}
bidsasks: Dictionary<Ticker> = {}
orders: ArrayCache = undefined
triggerOrders: ArrayCache = undefined
trades: Dictionary<ArrayCache>
orders: ArrayCache<Order> = undefined
triggerOrders: ArrayCache<Order> = undefined
trades: Dictionary<ArrayCache<Trade>>
transactions = {}
ohlcvs: Dictionary<Dictionary<ArrayCacheByTimestamp>>
myTrades: ArrayCache;
ohlcvs: Dictionary<Dictionary<ArrayCacheByTimestamp<OHLCV>>>
myTrades: ArrayCache<Trade>;
positions: any;
urls: {
logo?: string;
Expand Down
32 changes: 16 additions & 16 deletions ts/src/base/ws/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable max-classes-per-file */
// @ts-nocheck

interface CustomArray extends Array {
interface CustomArray<T> extends Array<T> {
hashmap: object;
}

class BaseCache extends Array {
class BaseCache<T> extends Array<T> {

constructor (maxSize = undefined) {
constructor (maxSize: number | undefined = undefined) {
super ()
Object.defineProperty (this, 'maxSize', {
__proto__: null, // make it invisible
Expand All @@ -21,11 +21,11 @@ class BaseCache extends Array {
}
}

class ArrayCache extends BaseCache implements CustomArray {
class ArrayCache<T> extends BaseCache<T> implements CustomArray<T> {

hashmap: object = {};

constructor (maxSize = undefined) {
constructor (maxSize: number | undefined = undefined) {
super (maxSize);
Object.defineProperty (this, 'nestedNewUpdatesBySymbol', {
__proto__: null, // make it invisible
Expand Down Expand Up @@ -83,7 +83,7 @@ class ArrayCache extends BaseCache implements CustomArray {
}
}

append (item) {
append (item: T) {
// maxSize may be 0 when initialized by a .filter() copy-construction
if (this.maxSize && (this.length === this.maxSize)) {
this.shift ()
Expand All @@ -104,9 +104,9 @@ class ArrayCache extends BaseCache implements CustomArray {
}
}

class ArrayCacheByTimestamp extends BaseCache {
class ArrayCacheByTimestamp<T> extends BaseCache<T> {

constructor (maxSize = undefined) {
constructor (maxSize: number | undefined = undefined) {
super (maxSize)
Object.defineProperty (this, 'hashmap', {
__proto__: null, // make it invisible
Expand Down Expand Up @@ -138,7 +138,7 @@ class ArrayCacheByTimestamp extends BaseCache {
return Math.min (this.newUpdates, limit)
}

append (item) {
append (item: T) {
if (item[0] in this.hashmap) {
const reference = this.hashmap[item[0]]
if (reference !== item) {
Expand All @@ -164,9 +164,9 @@ class ArrayCacheByTimestamp extends BaseCache {
}
}

class ArrayCacheBySymbolById extends ArrayCache {
class ArrayCacheBySymbolById<T> extends ArrayCache<T> {

constructor (maxSize = undefined) {
constructor (maxSize: number | undefined = undefined) {
super (maxSize)
this.nestedNewUpdatesBySymbol = true
// Object.defineProperty (this, 'hashmap', {
Expand All @@ -176,7 +176,7 @@ class ArrayCacheBySymbolById extends ArrayCache {
// })
}

append (item) {
append (item: T) {
const byId = this.hashmap[item.symbol] = this.hashmap[item.symbol] || {}
if (item.id in byId) {
const reference = byId[item.id]
Expand Down Expand Up @@ -219,10 +219,10 @@ class ArrayCacheBySymbolById extends ArrayCache {
}
}

class ArrayCacheBySymbolBySide extends ArrayCache {
class ArrayCacheBySymbolBySide<T> extends ArrayCache<T> {

constructor () {
super ()
constructor (maxSize: number | undefined = undefined) {
super (maxSize)
this.nestedNewUpdatesBySymbol = true
Object.defineProperty (this, 'hashmap', {
__proto__: null, // make it invisible
Expand All @@ -231,7 +231,7 @@ class ArrayCacheBySymbolBySide extends ArrayCache {
})
}

append (item) {
append (item: T) {
const bySide = this.hashmap[item.symbol] = this.hashmap[item.symbol] || {}
if (item.side in bySide) {
const reference = bySide[item.side]
Expand Down
8 changes: 4 additions & 4 deletions ts/src/pro/alpaca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default class alpaca extends alpacaRest {
let stored = this.safeValue (this.ohlcvs, symbol);
if (stored === undefined) {
const limit = this.safeInteger (this.options, 'OHLCVLimit', 1000);
stored = new ArrayCacheByTimestamp (limit);
stored = new ArrayCacheByTimestamp<OHLCV> (limit);
this.ohlcvs[symbol] = stored;
}
const parsed = this.parseOHLCV (message);
Expand Down Expand Up @@ -312,7 +312,7 @@ export default class alpaca extends alpacaRest {
let stored = this.safeValue (this.trades, symbol);
if (stored === undefined) {
const limit = this.safeInteger (this.options, 'tradesLimit', 1000);
stored = new ArrayCache (limit);
stored = new ArrayCache<Trade> (limit);
this.trades[symbol] = stored;
}
const parsed = this.parseTrade (message);
Expand Down Expand Up @@ -442,7 +442,7 @@ export default class alpaca extends alpacaRest {
const rawOrder = this.safeValue (data, 'order', {});
if (this.orders === undefined) {
const limit = this.safeInteger (this.options, 'ordersLimit', 1000);
this.orders = new ArrayCacheBySymbolById (limit);
this.orders = new ArrayCacheBySymbolById<Order> (limit);
}
const orders = this.orders;
const order = this.parseOrder (rawOrder);
Expand Down Expand Up @@ -508,7 +508,7 @@ export default class alpaca extends alpacaRest {
let myTrades = this.myTrades;
if (myTrades === undefined) {
const limit = this.safeInteger (this.options, 'tradesLimit', 1000);
myTrades = new ArrayCacheBySymbolById (limit);
myTrades = new ArrayCacheBySymbolById<Trade> (limit);
}
const trade = this.parseMyTrade (rawOrder);
myTrades.append (trade);
Expand Down
6 changes: 3 additions & 3 deletions ts/src/pro/ascendex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default class ascendex extends ascendexRest {
let stored = this.safeValue (this.ohlcvs[symbol], timeframe);
if (stored === undefined) {
const limit = this.safeInteger (this.options, 'OHLCVLimit', 1000);
stored = new ArrayCacheByTimestamp (limit);
stored = new ArrayCacheByTimestamp<OHLCV> (limit);
this.ohlcvs[symbol][timeframe] = stored;
}
stored.append (parsed);
Expand Down Expand Up @@ -197,7 +197,7 @@ export default class ascendex extends ascendexRest {
let tradesArray = this.safeValue (this.trades, symbol);
if (tradesArray === undefined) {
const limit = this.safeInteger (this.options, 'tradesLimit', 1000);
tradesArray = new ArrayCache (limit);
tradesArray = new ArrayCache<Trade> (limit);
}
for (let i = 0; i < trades.length; i++) {
tradesArray.append (trades[i]);
Expand Down Expand Up @@ -580,7 +580,7 @@ export default class ascendex extends ascendexRest {
const order = this.parseWsOrder (data);
if (this.orders === undefined) {
const limit = this.safeInteger (this.options, 'ordersLimit', 1000);
this.orders = new ArrayCacheBySymbolById (limit);
this.orders = new ArrayCacheBySymbolById<Order> (limit);
}
const orders = this.orders;
orders.append (order);
Expand Down
12 changes: 6 additions & 6 deletions ts/src/pro/binance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ export default class binance extends binanceRest {
let stored = this.safeValue (this.ohlcvs[symbol], timeframe);
if (stored === undefined) {
const limit = this.safeInteger (this.options, 'OHLCVLimit', 1000);
stored = new ArrayCacheByTimestamp (limit);
stored = new ArrayCacheByTimestamp<OHLCV> (limit);
this.ohlcvs[symbol][timeframe] = stored;
}
stored.append (parsed);
Expand Down Expand Up @@ -2918,7 +2918,7 @@ export default class binance extends binanceRest {
this.spawn (this.loadPositionsSnapshot, client, messageHash, type, isPortfolioMargin);
}
} else {
this.positions[type] = new ArrayCacheBySymbolBySide ();
this.positions[type] = new ArrayCacheBySymbolBySide<Position> ();
}
}

Expand All @@ -2930,7 +2930,7 @@ export default class binance extends binanceRest {
params['portfolioMargin'] = true;
}
const positions = await this.fetchPositions (undefined, params);
this.positions[type] = new ArrayCacheBySymbolBySide ();
this.positions[type] = new ArrayCacheBySymbolBySide<Position> ();
const cache = this.positions[type];
for (let i = 0; i < positions.length; i++) {
const position = positions[i];
Expand Down Expand Up @@ -2981,7 +2981,7 @@ export default class binance extends binanceRest {
this.positions = {};
}
if (!(accountType in this.positions)) {
this.positions[accountType] = new ArrayCacheBySymbolBySide ();
this.positions[accountType] = new ArrayCacheBySymbolBySide<Position> ();
}
const cache = this.positions[accountType];
const data = this.safeDict (message, 'a', {});
Expand Down Expand Up @@ -3330,7 +3330,7 @@ export default class binance extends binanceRest {
}
if (this.myTrades === undefined) {
const limit = this.safeInteger (this.options, 'tradesLimit', 1000);
this.myTrades = new ArrayCacheBySymbolById (limit);
this.myTrades = new ArrayCacheBySymbolById<Trade> (limit);
}
const myTrades = this.myTrades;
myTrades.append (trade);
Expand All @@ -3347,7 +3347,7 @@ export default class binance extends binanceRest {
if (symbol !== undefined) {
if (this.orders === undefined) {
const limit = this.safeInteger (this.options, 'ordersLimit', 1000);
this.orders = new ArrayCacheBySymbolById (limit);
this.orders = new ArrayCacheBySymbolById<Order> (limit);
}
const cachedOrders = this.orders;
const orders = this.safeValue (cachedOrders.hashmap, symbol, {});
Expand Down
8 changes: 4 additions & 4 deletions ts/src/pro/bingx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export default class bingx extends bingxRest {
let stored = this.safeValue (this.trades, symbol);
if (stored === undefined) {
const limit = this.safeInteger (this.options, 'tradesLimit', 1000);
stored = new ArrayCache (limit);
stored = new ArrayCache<Trade> (limit);
this.trades[symbol] = stored;
}
for (let j = 0; j < trades.length; j++) {
Expand Down Expand Up @@ -541,7 +541,7 @@ export default class bingx extends bingxRest {
let stored = this.safeValue (this.ohlcvs[symbol], timeframeId);
if (stored === undefined) {
const limit = this.safeInteger (this.options, 'OHLCVLimit', 1000);
stored = new ArrayCacheByTimestamp (limit);
stored = new ArrayCacheByTimestamp<OHLCV> (limit);
this.ohlcvs[symbol][timeframeId] = stored;
}
for (let i = 0; i < candles.length; i++) {
Expand Down Expand Up @@ -933,7 +933,7 @@ export default class bingx extends bingxRest {
const data = this.safeValue2 (message, 'data', 'o', {});
if (this.orders === undefined) {
const limit = this.safeInteger (this.options, 'ordersLimit', 1000);
this.orders = new ArrayCacheBySymbolById (limit);
this.orders = new ArrayCacheBySymbolById<Order> (limit);
}
const stored = this.orders;
const parsedOrder = this.parseOrder (data);
Expand Down Expand Up @@ -1008,7 +1008,7 @@ export default class bingx extends bingxRest {
let cachedTrades = this.myTrades;
if (cachedTrades === undefined) {
const limit = this.safeInteger (this.options, 'tradesLimit', 1000);
cachedTrades = new ArrayCacheBySymbolById (limit);
cachedTrades = new ArrayCacheBySymbolById<Trade> (limit);
this.myTrades = cachedTrades;
}
const parsed = this.parseTrade (result);
Expand Down
4 changes: 2 additions & 2 deletions ts/src/pro/bitfinex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class bitfinex extends bitfinexRest {
const data = this.safeValue (message, 1);
let stored = this.safeValue (this.trades, symbol);
if (stored === undefined) {
stored = new ArrayCache (tradesLimit);
stored = new ArrayCache<Trade> (tradesLimit);
this.trades[symbol] = stored;
}
if (Array.isArray (data)) {
Expand Down Expand Up @@ -616,7 +616,7 @@ export default class bitfinex extends bitfinexRest {
});
if (this.orders === undefined) {
const limit = this.safeInteger (this.options, 'ordersLimit', 1000);
this.orders = new ArrayCacheBySymbolById (limit);
this.orders = new ArrayCacheBySymbolById<Order> (limit);
}
const orders = this.orders;
orders.append (parsed);
Expand Down
8 changes: 4 additions & 4 deletions ts/src/pro/bitfinex2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class bitfinex2 extends bitfinex2Rest {
let stored = this.safeValue (this.ohlcvs[symbol], timeframe);
if (stored === undefined) {
const limit = this.safeInteger (this.options, 'OHLCVLimit', 1000);
stored = new ArrayCacheByTimestamp (limit);
stored = new ArrayCacheByTimestamp<OHLCV> (limit);
this.ohlcvs[symbol][timeframe] = stored;
}
const ohlcvsLength = ohlcvs.length;
Expand Down Expand Up @@ -275,7 +275,7 @@ export default class bitfinex2 extends bitfinex2Rest {
const messageHash = name + ':' + market['id'];
if (this.myTrades === undefined) {
const limit = this.safeInteger (this.options, 'tradesLimit', 1000);
this.myTrades = new ArrayCacheBySymbolById (limit);
this.myTrades = new ArrayCacheBySymbolById<Trade> (limit);
}
const tradesArray = this.myTrades;
tradesArray.append (trade);
Expand Down Expand Up @@ -325,7 +325,7 @@ export default class bitfinex2 extends bitfinex2Rest {
const symbol = market['symbol'];
let stored = this.safeValue (this.trades, symbol);
if (stored === undefined) {
stored = new ArrayCache (tradesLimit);
stored = new ArrayCache<Trade> (tradesLimit);
this.trades[symbol] = stored;
}
const messageLength = message.length;
Expand Down Expand Up @@ -968,7 +968,7 @@ export default class bitfinex2 extends bitfinex2Rest {
const messageType = this.safeString (message, 1);
if (this.orders === undefined) {
const limit = this.safeInteger (this.options, 'ordersLimit', 1000);
this.orders = new ArrayCacheBySymbolById (limit);
this.orders = new ArrayCacheBySymbolById<Order> (limit);
}
const orders = this.orders;
const symbolIds = {};
Expand Down