Skip to content

Commit

Permalink
Merge pull request #1772 from RafaelTaranto/fix/exchange-markets-loader
Browse files Browse the repository at this point in the history
LAM-551 fix: build markets requests and caching
  • Loading branch information
RafaelTaranto authored Dec 2, 2024
2 parents a811969 + fe99e5e commit 9f9cda1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
29 changes: 18 additions & 11 deletions lib/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,26 @@ function active (settings, cryptoCode) {
}

function getMarkets () {
const filterExchanges = _.filter(it => it.class === 'exchange')
const filterExchanges = _.filter(it => it.class === 'exchange' && !it.dev && it.code !== 'no-exchange')
const availableExchanges = _.map(it => it.code, filterExchanges(accounts.ACCOUNT_LIST))

return _.reduce(
(acc, value) =>
Promise.all([acc, ccxt.getMarkets(value, ALL_CRYPTOS)])
.then(([a, markets]) => Promise.resolve({
...a,
[value]: markets
})),
Promise.resolve({}),
availableExchanges
)
const fetchMarketForExchange = exchange =>
ccxt.getMarkets(exchange, ALL_CRYPTOS)
.then(markets => ({ exchange, markets }))
.catch(error => ({
exchange,
markets: [],
error: error.message
}))

const transformToObject = _.reduce((acc, { exchange, markets }) => ({
...acc,
[exchange]: markets
}), {})

const promises = _.map(fetchMarketForExchange, availableExchanges)
return Promise.all(promises)
.then(transformToObject)
}

module.exports = {
Expand Down
4 changes: 4 additions & 0 deletions lib/new-admin/admin-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { ApolloServer } = require('apollo-server-express')
require('../environment-helper')
const { asyncLocalStorage, defaultStore } = require('../async-storage')
const logger = require('../logger')
const exchange = require('../exchange')

const { AuthDirective } = require('./graphql/directives')
const { typeDefs, resolvers } = require('./graphql/schema')
Expand Down Expand Up @@ -98,6 +99,9 @@ function run () {

const serverLog = `lamassu-admin-server listening on port ${serverPort}`

// cache markets on startup
exchange.getMarkets().catch(console.error)

const webServer = https.createServer(certOptions, app)
webServer.listen(serverPort, () => logger.info(serverLog))
})
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/exchange/ccxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function calculatePrice (side, amount, orderBook) {
}

function _getMarkets (exchangeName, availableCryptos) {
const prunedCryptos = _.compose(_.uniq, _.map(coinUtils.getEquivalentCode))(availableCryptos)

try {
const exchange = new ccxt[exchangeName]()
const cryptosToQuoteAgainst = ['USDT']
Expand All @@ -65,13 +67,13 @@ function _getMarkets (exchangeName, availableCryptos) {
.then(_.filter(it => (it.type === 'spot' || it.spot)))
.then(res =>
_.reduce((acc, value) => {
if (_.includes(value.base, availableCryptos) && _.includes(value.quote, currencyCodes)) {
if (_.includes(value.base, prunedCryptos) && _.includes(value.quote, currencyCodes)) {
if (value.quote === value.base) return acc

if (_.isNil(acc[value.quote])) {
return { ...acc, [value.quote]: [value.base] }
}

acc[value.quote].push(value.base)
}
return acc
Expand Down
10 changes: 7 additions & 3 deletions new-lamassu-admin/src/pages/Services/schemas/helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ALL_CRYPTOS } from '@lamassu/coins'
import { ALL_CRYPTOS, utils as coinUtils } from '@lamassu/coins'
import * as R from 'ramda'

const WARNING_LEVELS = {
Expand Down Expand Up @@ -29,8 +29,12 @@ const leadingZerosTest = (value, context) => {
}

const buildCurrencyOptions = markets => {
const prunedCoins = R.compose(
R.uniq,
R.map(coinUtils.getEquivalentCode)
)(ALL_CRYPTOS)
return R.map(it => {
const unavailableCryptos = R.difference(ALL_CRYPTOS, markets[it])
const unavailableCryptos = R.difference(prunedCoins, markets[it])
const unavailableCryptosFiltered = R.difference(unavailableCryptos, [it]) // As the markets can have stablecoins to trade against other crypto, filter them out, as there can't be pairs such as USDT/USDT

const unavailableMarketsStr =
Expand All @@ -44,7 +48,7 @@ const buildCurrencyOptions = markets => {
const warningLevel = R.isEmpty(unavailableCryptosFiltered)
? WARNING_LEVELS.CLEAN
: !R.isEmpty(unavailableCryptosFiltered) &&
R.length(unavailableCryptosFiltered) < R.length(ALL_CRYPTOS)
R.length(unavailableCryptosFiltered) < R.length(prunedCoins)
? WARNING_LEVELS.PARTIAL
: WARNING_LEVELS.IMPORTANT

Expand Down

0 comments on commit 9f9cda1

Please sign in to comment.