Skip to content

Commit

Permalink
fix bybit_unified and provide pair util
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Oct 3, 2024
1 parent 39c9beb commit a5d92b1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/exchange/bybit_unified.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ module.exports = class BybitUnified {
order.symbol,
order.status,
order.price,
order.qty,
order.amount,
status === ExchangeOrder.STATUS_REJECTED,
order.clientOrderId ? order.clientOrderId : undefined,
order.side.toLowerCase() === 'buy' ? 'buy' : 'sell', // secure the value,
Expand Down
165 changes: 87 additions & 78 deletions src/utils/instance_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = {
* @param callback
* @returns {Promise<unknown>}
*/
binanceInitUsdT: async callback => {
return new Promise(resolve => {
binanceInitUsdT: async callback =>
new Promise(resolve => {
request('https://api.binance.com/api/v1/exchangeInfo', (_error, _res, body) => {
const pairs = [];

Expand All @@ -28,10 +28,7 @@ module.exports = {

content.symbols
.filter(
p =>
['USDT'].includes(p.quoteAsset) &&
!['USDC', 'PAX', 'USDS', 'TUSD', 'BUSD'].includes(p.baseAsset) &&
p.status.toLowerCase() === 'trading'
p => ['USDT'].includes(p.quoteAsset) && !['USDC', 'PAX', 'USDS', 'TUSD', 'BUSD'].includes(p.baseAsset) && p.status.toLowerCase() === 'trading'
)
.forEach(pair => {
let result = {
Expand All @@ -51,16 +48,15 @@ module.exports = {

resolve(pairs);
});
});
},
}),

/**
* Init helper for Binance to fetch all BUSD pairs
* @param callback
* @returns {Promise<unknown>}
*/
binanceInitBusd: async callback => {
return new Promise(resolve => {
binanceInitBusd: async callback =>
new Promise(resolve => {
request('https://api.binance.com/api/v1/exchangeInfo', (_error, _res, body) => {
const pairs = [];

Expand All @@ -81,10 +77,7 @@ module.exports = {

content.symbols
.filter(
p =>
['BUSD'].includes(p.quoteAsset) &&
!['USDC', 'PAX', 'USDS', 'TUSD', 'USDT'].includes(p.baseAsset) &&
p.status.toLowerCase() === 'trading'
p => ['BUSD'].includes(p.quoteAsset) && !['USDC', 'PAX', 'USDS', 'TUSD', 'USDT'].includes(p.baseAsset) && p.status.toLowerCase() === 'trading'
)
.forEach(pair => {
let result = {
Expand All @@ -104,16 +97,15 @@ module.exports = {

resolve(pairs);
});
});
},
}),

/**
* Init helper for Binance to fetch all BNB pairs
* @param callback
* @returns {Promise<unknown>}
*/
binanceInitBNB: async callback => {
return new Promise(resolve => {
binanceInitBNB: async callback =>
new Promise(resolve => {
request('https://api.binance.com/api/v1/exchangeInfo', (_error, _res, body) => {
const pairs = [];

Expand All @@ -135,9 +127,7 @@ module.exports = {
content.symbols
.filter(
p =>
['BNB'].includes(p.quoteAsset) &&
!['BUSD', 'USDC', 'PAX', 'USDS', 'TUSD', 'USDT'].includes(p.baseAsset) &&
p.status.toLowerCase() === 'trading'
['BNB'].includes(p.quoteAsset) && !['BUSD', 'USDC', 'PAX', 'USDS', 'TUSD', 'USDT'].includes(p.baseAsset) && p.status.toLowerCase() === 'trading'
)
.forEach(pair => {
let result = {
Expand All @@ -157,16 +147,15 @@ module.exports = {

resolve(pairs);
});
});
},
}),

/**
* Init helper for Binance to fetch all BTC pairs
* @param callback
* @returns {Promise<unknown>}
*/
binanceInitBTC: async callback => {
return new Promise(resolve => {
binanceInitBTC: async callback =>
new Promise(resolve => {
request('https://api.binance.com/api/v1/exchangeInfo', (_error, _res, body) => {
const pairs = [];

Expand All @@ -188,9 +177,7 @@ module.exports = {
content.symbols
.filter(
p =>
['BTC'].includes(p.quoteAsset) &&
!['BUSD', 'USDC', 'PAX', 'USDS', 'TUSD', 'USDT'].includes(p.baseAsset) &&
p.status.toLowerCase() === 'trading'
['BTC'].includes(p.quoteAsset) && !['BUSD', 'USDC', 'PAX', 'USDS', 'TUSD', 'USDT'].includes(p.baseAsset) && p.status.toLowerCase() === 'trading'
)
.forEach(pair => {
let result = {
Expand All @@ -210,16 +197,15 @@ module.exports = {

resolve(pairs);
});
});
},
}),

/**
* Init helper for Binance to fetch all ETH pairs
* @param callback
* @returns {Promise<unknown>}
*/
binanceInitETH: async callback => {
return new Promise(resolve => {
binanceInitETH: async callback =>
new Promise(resolve => {
request('https://api.binance.com/api/v1/exchangeInfo', (_error, _res, body) => {
const pairs = [];

Expand All @@ -241,9 +227,7 @@ module.exports = {
content.symbols
.filter(
p =>
['ETH'].includes(p.quoteAsset) &&
!['BUSD', 'USDC', 'PAX', 'USDS', 'TUSD', 'USDT'].includes(p.baseAsset) &&
p.status.toLowerCase() === 'trading'
['ETH'].includes(p.quoteAsset) && !['BUSD', 'USDC', 'PAX', 'USDS', 'TUSD', 'USDT'].includes(p.baseAsset) && p.status.toLowerCase() === 'trading'
)
.forEach(pair => {
let result = {
Expand All @@ -263,8 +247,7 @@ module.exports = {

resolve(pairs);
});
});
},
}),

/**
* Init helper for Binance to fetch all USDT pairs with spot only
Expand Down Expand Up @@ -382,84 +365,79 @@ module.exports = {
* @link https://www.binance.com/de/margin-fee
* @returns {Promise<unknown>}
*/
binancecrossMarginPairsUsdT: () => {
return new Promise(resolve => {
binancecrossMarginPairsUsdT: () =>
new Promise(resolve => {
request('https://www.binance.com/gateway-api/v1/friendly/margin/vip/spec/list-all', (_error, _res, body) => {
const content = JSON.parse(body);
const crossMarginPairsUsdT = content.data.map(i => i.assetName);

resolve(crossMarginPairsUsdT);
});
});
},
}),

/**
* There is API (or not documented) where to filter isolated and cross margin wallet pairs take them from fee page api
*
* @link https://www.binance.com/de/margin-fee
* @returns {Promise<unknown>}
*/
binancecrossMarginPairsBusd: () => {
return new Promise(resolve => {
binancecrossMarginPairsBusd: () =>
new Promise(resolve => {
request('https://www.binance.com/gateway-api/v1/friendly/margin/vip/spec/list-all', (_error, _res, body) => {
const content = JSON.parse(body);
const crossMarginPairsBusd = content.data.map(i => i.assetName);

resolve(crossMarginPairsBusd);
});
});
},
}),

/**
* There is API (or not documented) where to filter isolated and cross margin wallet pairs take them from fee page api
*
* @link https://www.binance.com/de/margin-fee
* @returns {Promise<unknown>}
*/
binancecrossMarginPairsBNB: () => {
return new Promise(resolve => {
binancecrossMarginPairsBNB: () =>
new Promise(resolve => {
request('https://www.binance.com/gateway-api/v1/friendly/margin/vip/spec/list-all', (_error, _res, body) => {
const content = JSON.parse(body);
const crossMarginPairsBNB = content.data.map(i => i.assetName);

resolve(crossMarginPairsBNB);
});
});
},
}),

/**
* There is API (or not documented) where to filter isolated and cross margin wallet pairs take them from fee page api
*
* @link https://www.binance.com/de/margin-fee
* @returns {Promise<unknown>}
*/
binancecrossMarginPairsBTC: () => {
return new Promise(resolve => {
binancecrossMarginPairsBTC: () =>
new Promise(resolve => {
request('https://www.binance.com/gateway-api/v1/friendly/margin/vip/spec/list-all', (_error, _res, body) => {
const content = JSON.parse(body);
const crossMarginPairsBTC = content.data.map(i => i.assetName);

resolve(crossMarginPairsBTC);
});
});
},
}),

/**
* There is API (or not documented) where to filter isolated and cross margin wallet pairs take them from fee page api
*
* @link https://www.binance.com/de/margin-fee
* @returns {Promise<unknown>}
*/
binancecrossMarginPairsETH: () => {
return new Promise(resolve => {
binancecrossMarginPairsETH: () =>
new Promise(resolve => {
request('https://www.binance.com/gateway-api/v1/friendly/margin/vip/spec/list-all', (_error, _res, body) => {
const content = JSON.parse(body);
const crossMarginPairsETH = content.data.map(i => i.assetName);

resolve(crossMarginPairsETH);
});
});
},
}),

/**
* Init helper for Binance to fetch all USDT pairs with margin only
Expand Down Expand Up @@ -558,8 +536,8 @@ module.exports = {
* @param callback
* @returns {Promise<unknown>}
*/
bitmexInit: async callback => {
return new Promise(resolve => {
bitmexInit: async callback =>
new Promise(resolve => {
request('https://www.bitmex.com/api/v1/instrument/active', (_error, _res, body) => {
const pairs = [];

Expand All @@ -585,16 +563,15 @@ module.exports = {

resolve(pairs);
});
});
},
}),

/**
* Init helper for Binance futures USDT & BUSD exchange to fetch active contracts
* @param callback
* @returns {Promise<unknown>}
*/
binanceFuturesInit: async callback => {
return new Promise(resolve => {
binanceFuturesInit: async callback =>
new Promise(resolve => {
request('https://fapi.binance.com/fapi/v1/exchangeInfo', (_error, _res, body) => {
const pairs = [];

Expand All @@ -620,16 +597,15 @@ module.exports = {

resolve(pairs);
});
});
},
}),

/**
* Init helper for Binance futures COIN exchange to fetch active contracts
* @param callback
* @returns {Promise<unknown>}
*/
binanceFuturesCoin: async callback => {
return new Promise(resolve => {
binanceFuturesCoin: async callback =>
new Promise(resolve => {
request('https://dapi.binance.com/dapi/v1/exchangeInfo', (_error, _res, body) => {
const pairs = [];

Expand All @@ -655,12 +631,47 @@ module.exports = {

resolve(pairs);
});
});
},
}),

/**
* @param callback
* @param limit
* @returns {Promise<unknown>}
*/
bybitLinearCoin: async (callback, limit = 100) =>
new Promise(resolve => {
request('https://api.bybit.com/v5/market/tickers?category=linear', (_error, _res, body) => {
const pairs = [];

// BitFinex
bitfinexUsdMarginInit: async callback => {
return new Promise(resolve => {
const content = JSON.parse(body);

const arr = (content?.result?.list || [])
.filter(p => p.symbol.toUpperCase().endsWith('USDT'))
.sort((a, b) => b.turnover24h - a.turnover24h);

arr.slice(0, limit).forEach(pair => {
let result = {
symbol: `${pair.symbol.substring(0, pair.symbol.length - 4)}/USDT:USDT`,
periods: ['15m', '1h'],
exchange: 'bybit_unified'
};

if (callback) {
result = callback(result, pair);
}

if (result) {
pairs.push(result);
}
});

resolve(pairs);
});
}),

// BitFinex
bitfinexUsdMarginInit: async callback =>
new Promise(resolve => {
request('https://api.bitfinex.com/v1/symbols_details', (_error, _res, body) => {
const pairs = [];

Expand All @@ -686,12 +697,11 @@ module.exports = {

resolve(pairs);
});
});
},
}),

// Bybit
bybitInit: async callback => {
return new Promise(resolve => {
bybitInit: async callback =>
new Promise(resolve => {
request('https://api.bybit.com/v2/public/symbols', (_error, _res, body) => {
const pairs = [];

Expand Down Expand Up @@ -735,6 +745,5 @@ module.exports = {

resolve(pairs);
});
});
}
})
};

0 comments on commit a5d92b1

Please sign in to comment.