Skip to content

Commit

Permalink
Merge pull request #260 from ZIMkaRU/bugfix/fix-symbol-pair-splitting
Browse files Browse the repository at this point in the history
Fix symbol pair splitting
  • Loading branch information
prdn authored Apr 28, 2022
2 parents 21f2b42 + 2d528d6 commit 21dfc87
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 7 deletions.
4 changes: 3 additions & 1 deletion workers/loc.api/di/app.deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const {
getREST,
grcBfxReq,
prepareResponse,
prepareApiResponse
prepareApiResponse,
FOREX_SYMBS
} = require('../helpers')
const HasGrcService = require('../has.grc.service')
const processor = require('../queue/processor')
Expand Down Expand Up @@ -86,6 +87,7 @@ module.exports = ({
[TYPES.GetREST]
)
)
bind(TYPES.FOREX_SYMBS).toConstantValue(FOREX_SYMBS)
bind(TYPES.Link).toConstantValue(link)
bind(TYPES.HasGrcService)
.to(HasGrcService)
Expand Down
1 change: 1 addition & 0 deletions workers/loc.api/di/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = {
CONF: Symbol.for('CONF'),
FOREX_SYMBS: Symbol.for('FOREX_SYMBS'),
RootPath: Symbol.for('RootPath'),
Container: Symbol.for('Container'),
LoggerFactory: Symbol.for('LoggerFactory'),
Expand Down
52 changes: 52 additions & 0 deletions workers/loc.api/helpers/__test__/split-symbol-pairs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ describe('splitSymbolPairs helper', () => {
assert.strictEqual(res[1], 'USD')
})

it('BTCEOS pair', function () {
this.timeout(1000)

const res = splitSymbolPairs('BTCEOS')

assert.isArray(res)
assert.lengthOf(res, 2)
assert.strictEqual(res[0], 'BTC')
assert.strictEqual(res[1], 'EOS')
})

it('tXAUT:USD pair', function () {
this.timeout(1000)

Expand Down Expand Up @@ -103,4 +114,45 @@ describe('splitSymbolPairs helper', () => {
assert.strictEqual(res[0], 'EUR')
assert.strictEqual(res[1], 'USD')
})

it('MATIC coin, without separator', function () {
this.timeout(1000)

const res = splitSymbolPairs('MATIC')

assert.isArray(res)
assert.lengthOf(res, 1)
assert.strictEqual(res[0], 'MATIC')
})

it('tMATIC:USD pair, without separator', function () {
this.timeout(1000)

const res = splitSymbolPairs('tMATIC:USD')

assert.isArray(res)
assert.lengthOf(res, 2)
assert.strictEqual(res[0], 'MATIC')
assert.strictEqual(res[1], 'USD')
})

it('MATICM coin, without separator', function () {
this.timeout(1000)

const res = splitSymbolPairs('MATICM')

assert.isArray(res)
assert.lengthOf(res, 1)
assert.strictEqual(res[0], 'MATICM')
})

it('MATICMF0 coin, without separator', function () {
this.timeout(1000)

const res = splitSymbolPairs('MATICMF0')

assert.isArray(res)
assert.lengthOf(res, 1)
assert.strictEqual(res[0], 'MATICMF0')
})
})
3 changes: 3 additions & 0 deletions workers/loc.api/helpers/forex.symbs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = ['EUR', 'JPY', 'GBP', 'USD']
4 changes: 3 additions & 1 deletion workers/loc.api/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const FILTER_MODELS_NAMES = require('./filter.models.names')
const FILTER_CONDITIONS = require('./filter.conditions')
const getDataFromApi = require('./get-data-from-api')
const splitSymbolPairs = require('./split-symbol-pairs')
const FOREX_SYMBS = require('./forex.symbs')

module.exports = {
getREST,
Expand Down Expand Up @@ -91,5 +92,6 @@ module.exports = {
FILTER_CONDITIONS,
getDataFromApi,
parsePositionsAuditId,
splitSymbolPairs
splitSymbolPairs,
FOREX_SYMBS
}
52 changes: 47 additions & 5 deletions workers/loc.api/helpers/split-symbol-pairs.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
'use strict'

module.exports = (symbol) => {
const str = (
symbol[0] === 't' ||
symbol[0] === 'f'
const FOREX_SYMBS = require('./forex.symbs')

const detectingSymbs = [
'BTC',
...FOREX_SYMBS
]

/* It allows to cover pairs with str length 6 and without `t` and `f` prefixes
* for currency converter of bfx-reports-framework used to triangulation:
* `BTCUSD`, `BTCEOS` etc
*/
const isNotSymbContained = (
currSymb,
symbs = detectingSymbs
) => {
return (
Array.isArray(symbs) &&
symbs.every((symb) => (
!currSymb.startsWith(symb) &&
!currSymb.endsWith(symb)
))
)
}

const _startsWithPrefix = (str) => (
str.startsWith('t') ||
str.startsWith('f')
)

const _containsSeparator = (str) => (
/.+[:].+/.test(str)
)

module.exports = (symbol) => {
const hasPrefix = _startsWithPrefix(symbol)
const hasSeparator = _containsSeparator(symbol)

if (
!hasPrefix &&
!hasSeparator &&
symbol.length > 5 &&
isNotSymbContained(symbol)
) {
return [symbol]
}

const str = hasPrefix
? symbol.slice(1)
: symbol

if (
str.length > 5 &&
/.+[:].+/.test(str)
hasSeparator
) {
return str.split(':')
}
Expand Down

0 comments on commit 21dfc87

Please sign in to comment.