Improve response naming

This commit is contained in:
Matthias 2023-10-21 17:18:22 +02:00
parent 41f464e4ca
commit 0d0fc2a4d8
2 changed files with 20 additions and 15 deletions

View File

@ -1,11 +1,15 @@
/**
* Split a trade pair into stake and quote currency
* @param pair pair to split, either <stake>/<quote> or <stake>/<quote>:<settle>
* @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 ?? '' };
}

View File

@ -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' });
});
});