Skip to content

Commit 440bc4e

Browse files
authored
refactor(exchange): parseLeverageTiers accepts either a dictionary or an array (ccxt#22099)
1 parent 473789a commit 440bc4e

File tree

3 files changed

+29
-90
lines changed

3 files changed

+29
-90
lines changed

ts/src/base/Exchange.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,18 +3820,37 @@ export default class Exchange {
38203820
return this.filterBySinceLimit (sorted, since, limit, 0) as any;
38213821
}
38223822

3823-
parseLeverageTiers (response: object[], symbols: string[] = undefined, marketIdKey = undefined) {
3823+
parseLeverageTiers (response: any, symbols: string[] = undefined, marketIdKey = undefined) {
38243824
// marketIdKey should only be undefined when response is a dictionary
38253825
symbols = this.marketSymbols (symbols);
38263826
const tiers = {};
3827-
for (let i = 0; i < response.length; i++) {
3828-
const item = response[i];
3829-
const id = this.safeString (item, marketIdKey);
3830-
const market = this.safeMarket (id, undefined, undefined, 'swap');
3831-
const symbol = market['symbol'];
3832-
const contract = this.safeBool (market, 'contract', false);
3833-
if (contract && ((symbols === undefined) || this.inArray (symbol, symbols))) {
3834-
tiers[symbol] = this.parseMarketLeverageTiers (item, market);
3827+
let symbolsLength = 0;
3828+
if (symbols !== undefined) {
3829+
symbolsLength = symbols.length;
3830+
}
3831+
const noSymbols = (symbols === undefined) || (symbolsLength === 0);
3832+
if (Array.isArray (response)) {
3833+
for (let i = 0; i < response.length; i++) {
3834+
const item = response[i];
3835+
const id = this.safeString (item, marketIdKey);
3836+
const market = this.safeMarket (id, undefined, undefined, 'swap');
3837+
const symbol = market['symbol'];
3838+
const contract = this.safeBool (market, 'contract', false);
3839+
if (contract && (noSymbols || this.inArray (symbol, symbols))) {
3840+
tiers[symbol] = this.parseMarketLeverageTiers (item, market);
3841+
}
3842+
}
3843+
} else {
3844+
const keys = Object.keys (response);
3845+
for (let i = 0; i < keys.length; i++) {
3846+
const marketId = keys[i];
3847+
const item = response[marketId];
3848+
const market = this.safeMarket (marketId, undefined, undefined, 'swap');
3849+
const symbol = market['symbol'];
3850+
const contract = this.safeBool (market, 'contract', false);
3851+
if (contract && (noSymbols || this.inArray (symbol, symbols))) {
3852+
tiers[symbol] = this.parseMarketLeverageTiers (item, market);
3853+
}
38353854
}
38363855
}
38373856
return tiers;

ts/src/coinex.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4103,37 +4103,6 @@ export default class coinex extends Exchange {
41034103
return this.parseLeverageTiers (data, symbols, undefined);
41044104
}
41054105

4106-
parseLeverageTiers (response, symbols: Strings = undefined, marketIdKey = undefined) {
4107-
//
4108-
// {
4109-
// "BTCUSD": [
4110-
// ["500001", "100", "0.005"],
4111-
// ["1000001", "50", "0.01"],
4112-
// ["2000001", "30", "0.015"],
4113-
// ["5000001", "20", "0.02"],
4114-
// ["10000001", "15", "0.025"],
4115-
// ["20000001", "10", "0.03"]
4116-
// ],
4117-
// ...
4118-
// }
4119-
//
4120-
const tiers = {};
4121-
const marketIds = Object.keys (response);
4122-
for (let i = 0; i < marketIds.length; i++) {
4123-
const marketId = marketIds[i];
4124-
const market = this.safeMarket (marketId, undefined, undefined, 'spot');
4125-
const symbol = this.safeString (market, 'symbol');
4126-
let symbolsLength = 0;
4127-
if (symbols !== undefined) {
4128-
symbolsLength = symbols.length;
4129-
}
4130-
if (symbol !== undefined && (symbolsLength === 0 || this.inArray (symbols, symbol))) {
4131-
tiers[symbol] = this.parseMarketLeverageTiers (response[marketId], market);
4132-
}
4133-
}
4134-
return tiers;
4135-
}
4136-
41374106
parseMarketLeverageTiers (item, market: Market = undefined) {
41384107
const tiers = [];
41394108
let minNotional = 0;

ts/src/digifinex.ts

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3783,56 +3783,7 @@ export default class digifinex extends Exchange {
37833783
//
37843784
const data = this.safeValue (response, 'data', []);
37853785
symbols = this.marketSymbols (symbols);
3786-
return this.parseLeverageTiers (data, symbols, 'symbol');
3787-
}
3788-
3789-
parseLeverageTiers (response, symbols: Strings = undefined, marketIdKey = undefined) {
3790-
//
3791-
// [
3792-
// {
3793-
// "instrument_id": "BTCUSDTPERP",
3794-
// "type": "REAL",
3795-
// "contract_type": "PERPETUAL",
3796-
// "base_currency": "BTC",
3797-
// "quote_currency": "USDT",
3798-
// "clear_currency": "USDT",
3799-
// "contract_value": "0.001",
3800-
// "contract_value_currency": "BTC",
3801-
// "is_inverse": false,
3802-
// "is_trading": true,
3803-
// "status": "ONLINE",
3804-
// "price_precision": 1,
3805-
// "tick_size": "0.1",
3806-
// "min_order_amount": 1,
3807-
// "open_max_limits": [
3808-
// {
3809-
// "leverage": "50",
3810-
// "max_limit": "1000000"
3811-
// }
3812-
// ]
3813-
// },
3814-
// ]
3815-
//
3816-
const tiers = {};
3817-
const result = {};
3818-
for (let i = 0; i < response.length; i++) {
3819-
const entry = response[i];
3820-
const marketId = this.safeString (entry, 'instrument_id');
3821-
const market = this.safeMarket (marketId);
3822-
const symbol = this.safeSymbol (marketId, market);
3823-
let symbolsLength = 0;
3824-
tiers[symbol] = this.parseMarketLeverageTiers (response[i], market);
3825-
if (symbols !== undefined) {
3826-
symbolsLength = symbols.length;
3827-
if (this.inArray (symbol, symbols)) {
3828-
result[symbol] = this.parseMarketLeverageTiers (response[i], market);
3829-
}
3830-
}
3831-
if (symbol !== undefined && (symbolsLength === 0 || this.inArray (symbols, symbol))) {
3832-
result[symbol] = this.parseMarketLeverageTiers (response[i], market);
3833-
}
3834-
}
3835-
return result;
3786+
return this.parseLeverageTiers (data, symbols, 'instrument_id');
38363787
}
38373788

38383789
async fetchMarketLeverageTiers (symbol: string, params = {}) {

0 commit comments

Comments
 (0)