|
1 | 1 | const _ = require('lodash'); |
2 | 2 |
|
| 3 | +const { TOKEN_TYPE } = require('../constants'); |
3 | 4 | const elasticsearch = require('../util/elasticsearch'); |
| 5 | +const getCdnTokenImageUrl = require('./get-cdn-token-image-url'); |
| 6 | +const getTokenPrices = require('./get-token-prices'); |
4 | 7 | const Token = require('../model/token'); |
5 | 8 |
|
6 | 9 | const nullifyValueIfZero = value => (value === 0 ? null : value); |
@@ -79,19 +82,38 @@ const getTokensWithStatsForDates = async (dateFrom, dateTo, options) => { |
79 | 82 | const tokenAddresses = tokenStats.map(x => x.key); |
80 | 83 | const tokenCount = res.body.aggregations.tokenCount.value; |
81 | 84 |
|
82 | | - const tokens = await Token.find({ |
83 | | - address: { $in: tokenAddresses }, |
84 | | - }).lean(); |
| 85 | + const [tokens, prices] = await Promise.all([ |
| 86 | + Token.find({ |
| 87 | + address: { $in: tokenAddresses }, |
| 88 | + }).lean(), |
| 89 | + getTokenPrices(tokenAddresses, { from: dateFrom, to: dateTo }), |
| 90 | + ]); |
85 | 91 |
|
86 | 92 | return { |
87 | 93 | tokens: tokenStats.map(stats => { |
88 | 94 | const token = tokens.find(t => t.address === stats.key); |
| 95 | + const price = prices.find(t => t.tokenAddress === stats.key); |
89 | 96 |
|
90 | 97 | return { |
91 | | - ..._.pick(token, ['address', 'imageUrl', 'name', 'symbol', 'type']), |
92 | | - lastTrade: _.get(token, 'price.lastTrade', null), |
| 98 | + ..._.pick(token, ['address', 'name', 'symbol', 'type']), |
| 99 | + imageUrl: _.isString(token.imageUrl) |
| 100 | + ? getCdnTokenImageUrl(token.imageUrl) |
| 101 | + : undefined, |
| 102 | + lastTrade: _.has(price, 'fillId') |
| 103 | + ? { |
| 104 | + date: price.date, |
| 105 | + id: price.fillId, |
| 106 | + } |
| 107 | + : null, |
93 | 108 | price: { |
94 | | - last: _.get(token, 'price.lastPrice', null), |
| 109 | + change: |
| 110 | + token.type === TOKEN_TYPE.ERC20 |
| 111 | + ? _.get(price, 'priceChange', null) |
| 112 | + : null, |
| 113 | + last: |
| 114 | + token.type === TOKEN_TYPE.ERC20 |
| 115 | + ? _.get(price, 'priceUSD', null) |
| 116 | + : null, |
95 | 117 | }, |
96 | 118 | stats: { |
97 | 119 | fillCount: stats.fillCount.value, |
|
0 commit comments