diff --git a/src/shared/formatters/pairFormat.ts b/src/shared/formatters/pairFormat.ts index a4e33a7d..30a1d5a9 100644 --- a/src/shared/formatters/pairFormat.ts +++ b/src/shared/formatters/pairFormat.ts @@ -1,11 +1,15 @@ /** * Split a trade pair into stake and quote currency * @param pair pair to split, either / or /: - * @returns {stakeCurrency, quoteCurrency} + * @returns {baseCurrency, quoteCurrency} */ -export function splitTradePair(pair: string): { stakeCurrency: string; quoteCurrency: string } { - if (!pair) return { stakeCurrency: '', quoteCurrency: '' }; - const [stakeCurrency, quoteCurrency] = pair.split('/'); - const quoteCurrencySplit = quoteCurrency.split(':'); - return { stakeCurrency, quoteCurrency: quoteCurrencySplit[0] || quoteCurrency }; +export function splitTradePair(pair: string): { baseCurrency: string; quoteCurrency: string } { + if (!pair) return { baseCurrency: '', quoteCurrency: '' }; + const [baseCurrency, quoteCurrency] = pair.split('/'); + if (quoteCurrency !== undefined) { + const quoteCurrencySplit = quoteCurrency.split(':'); + return { baseCurrency: baseCurrency, quoteCurrency: quoteCurrencySplit[0] || quoteCurrency }; + } + // In case only one asset is passed in (e.g. USDT) we invert the values, assuming this is the quote currency. + return { baseCurrency: quoteCurrency ?? '', quoteCurrency: baseCurrency ?? '' }; } diff --git a/tests/unit/formatPair.spec.ts b/tests/unit/formatPair.spec.ts index 2488832b..d4068a39 100644 --- a/tests/unit/formatPair.spec.ts +++ b/tests/unit/formatPair.spec.ts @@ -3,27 +3,28 @@ import { splitTradePair } from '@/shared/formatters'; describe('splitTradePair', () => { it('Extracts stake and quote currencies from spot pairs', () => { - expect(splitTradePair('BTC/USDT')).toEqual({ stakeCurrency: 'BTC', quoteCurrency: 'USDT' }); - expect(splitTradePair('USDT/BTC')).toEqual({ stakeCurrency: 'USDT', quoteCurrency: 'BTC' }); - expect(splitTradePair('USDT/USDT')).toEqual({ stakeCurrency: 'USDT', quoteCurrency: 'USDT' }); - expect(splitTradePair('USDT/USDT')).toEqual({ stakeCurrency: 'USDT', quoteCurrency: 'USDT' }); + expect(splitTradePair('BTC/USDT')).toEqual({ baseCurrency: 'BTC', quoteCurrency: 'USDT' }); + expect(splitTradePair('USDT/BTC')).toEqual({ baseCurrency: 'USDT', quoteCurrency: 'BTC' }); + expect(splitTradePair('USDT/USDT')).toEqual({ baseCurrency: 'USDT', quoteCurrency: 'USDT' }); + expect(splitTradePair('USDT/USDT')).toEqual({ baseCurrency: 'USDT', quoteCurrency: 'USDT' }); }); it('Extracts stake and quote currencies from spot pairs', () => { expect(splitTradePair('BTC/USDT:USDT')).toEqual({ - stakeCurrency: 'BTC', + baseCurrency: 'BTC', quoteCurrency: 'USDT', }); - expect(splitTradePair('USDT/BTC:BTC')).toEqual({ stakeCurrency: 'USDT', quoteCurrency: 'BTC' }); + expect(splitTradePair('USDT/BTC:BTC')).toEqual({ baseCurrency: 'USDT', quoteCurrency: 'BTC' }); expect(splitTradePair('USDT/USDT:USDT')).toEqual({ - stakeCurrency: 'USDT', + baseCurrency: 'USDT', quoteCurrency: 'USDT', }); expect(splitTradePair('USDT/USDT:USDT')).toEqual({ - stakeCurrency: 'USDT', + baseCurrency: 'USDT', quoteCurrency: 'USDT', }); }); it('Does not fail on empty input', () => { - expect(splitTradePair('')).toEqual({ stakeCurrency: '', quoteCurrency: '' }); + expect(splitTradePair('')).toEqual({ baseCurrency: '', quoteCurrency: '' }); + expect(splitTradePair('USDT')).toEqual({ baseCurrency: '', quoteCurrency: 'USDT' }); }); });