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

(improvements) Symbols and pairs handling #641

Merged
merged 10 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/state/symbols/reducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import authTypes from 'state/auth/constants'
import { formatPair, mapPair, mapSymbol } from 'state/symbols/utils'
import {
formatPair, mapPair, mapSymbol, isTestSymbol,
} from 'state/symbols/utils'
import _map from 'lodash/map'
import _join from 'lodash/join'
import _split from 'lodash/split'
import _reduce from 'lodash/reduce'
import _replace from 'lodash/replace'
import _includes from 'lodash/includes'

import SymbolMap from './map'
import types from './constants'
Expand Down Expand Up @@ -37,13 +44,16 @@ export function symbolsReducer(state = initialState, action) {
let { symbol } = currency

if (symbol && id !== symbol) {
if (id.includes('TEST')) {
symbol = `${symbol} (Test)`
}
if (id.includes('F0')) {
symbol = `${symbol} (deriv)`
}
if (symbol === 'USDt') {
symbolMapping.UST = symbol
} else {
symbolMapping[_replace(id, 'TEST', '')] = symbol
symbolMapping[id] = symbol
}

explorersDict[symbol] = explorer
Expand All @@ -59,7 +69,13 @@ export function symbolsReducer(state = initialState, action) {

const preparedCoins = [...new Set(coins)].sort()

const pairMapping = mapSymbols.reduce((acc, symbol) => {
const preparedMapSymbols = _map(mapSymbols, item => {
const [itemId, itemName] = item
if (_includes(itemId, 'TEST')) return [itemId, `${itemName} (Test)`]
return item
})

const pairMapping = _reduce(preparedMapSymbols, (acc, symbol) => {
const [from, to] = symbol
acc[from] = to
return acc
Expand All @@ -72,6 +88,15 @@ export function symbolsReducer(state = initialState, action) {
const formattedPairs = pairs.map(formatPair).map(mapPair).sort()
const formattedInactivePairs = inactiveSymbols.map(formatPair).map(mapPair).sort()


const preparedPairs = _map(formattedPairs, pair => {
const [firstSymbol, secondSymbol] = _split(pair, ':')
if (isTestSymbol(firstSymbol) && isTestSymbol(secondSymbol)) {
return _join([_replace(firstSymbol, ' (Test)', ''), secondSymbol], ':')
}
return pair
})

return {
...state,
coins: preparedCoins,
Expand All @@ -80,7 +105,7 @@ export function symbolsReducer(state = initialState, action) {
inactiveCurrencies: formattedInactiveCurrencies,
inactivePairs: formattedInactivePairs,
isFetched: true,
pairs: formattedPairs,
pairs: preparedPairs,
}
}
case authTypes.LOGOUT:
Expand Down
3 changes: 3 additions & 0 deletions src/state/symbols/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export const formatRawSymbols = (symbols, isFunding) => {
: symbolsArray[0]
}

export const isTestSymbol = (symbol) => _includes(symbol, ' (Test)')

export default {
formatPair,
formatRawSymbols,
Expand All @@ -87,4 +89,5 @@ export default {
isFundingSymbol,
isTradingPair,
removePrefix,
isTestSymbol,
}
15 changes: 14 additions & 1 deletion src/state/symbols/utils/mapping.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import _join from 'lodash/join'
import _split from 'lodash/split'
import _includes from 'lodash/includes'
import _castArray from 'lodash/castArray'

import { isTestSymbol } from 'state/symbols/utils'

import SymbolMap from '../map'

const preparePair = (pair) => {
const [firstSymbol, secondSymbol] = _split(pair, ':')
if (isTestSymbol(secondSymbol)) {
return _join([`${firstSymbol} (Test)`, secondSymbol], ':')
}
return pair
}

// BAB -> BCH
export const mapSymbol = symbol => SymbolMap.symbols[symbol] || symbol

Expand Down Expand Up @@ -51,7 +63,8 @@ export const demapPairs = (pairs, returnString = false) => {
if (SymbolMap.pairsDemap[pair]) {
return SymbolMap.pairsDemap[pair]
}
return demapSymbols(pair.split(':')).join(':')
const preparedPair = preparePair(pair)
return demapSymbols(preparedPair.split(':')).join(':')
})

return returnString
Expand Down