|
| 1 | +const _ = require('lodash'); |
| 2 | + |
| 3 | +const { JOB, QUEUE } = require('../../constants'); |
| 4 | +const { publishJob } = require('../../queues'); |
| 5 | +const elasticsearch = require('../../util/elasticsearch'); |
| 6 | +const getAddressMetadata = require('../../addresses/get-address-metadata'); |
| 7 | +const getTransactionByHash = require('../../transactions/get-transaction-by-hash'); |
| 8 | + |
| 9 | +const indexFillTraders = async (job, { logger }) => { |
| 10 | + const delayJobProcessing = async () => { |
| 11 | + await publishJob(QUEUE.INDEXING, JOB.INDEX_FILL_TRADERS, job.data, { |
| 12 | + delay: 30000, |
| 13 | + }); |
| 14 | + }; |
| 15 | + |
| 16 | + const { |
| 17 | + fillDate, |
| 18 | + fillId, |
| 19 | + fillValue, |
| 20 | + maker, |
| 21 | + taker, |
| 22 | + tradeCount, |
| 23 | + transactionHash, |
| 24 | + } = job.data; |
| 25 | + |
| 26 | + const takerMetadata = await getAddressMetadata(taker); |
| 27 | + |
| 28 | + if (takerMetadata === null || takerMetadata.isContract === undefined) { |
| 29 | + logger.warn(`taker address type is unknown: ${taker}`); |
| 30 | + await delayJobProcessing(); |
| 31 | + |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + let transaction; |
| 36 | + |
| 37 | + if (takerMetadata.isContract) { |
| 38 | + transaction = await getTransactionByHash(transactionHash); |
| 39 | + |
| 40 | + if (transaction === null) { |
| 41 | + logger.warn(`transaction has not been fetched: ${transactionHash}`); |
| 42 | + await delayJobProcessing(); |
| 43 | + |
| 44 | + return; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const tradeValue = |
| 49 | + fillValue === undefined ? undefined : fillValue * tradeCount; |
| 50 | + |
| 51 | + const requestBody = [ |
| 52 | + JSON.stringify({ |
| 53 | + index: { |
| 54 | + _id: `${fillId}_maker`, |
| 55 | + }, |
| 56 | + }), |
| 57 | + JSON.stringify({ |
| 58 | + address: maker, |
| 59 | + fillId, |
| 60 | + date: fillDate, |
| 61 | + makerFillCount: 1, |
| 62 | + makerFillValue: fillValue, |
| 63 | + makerTradeCount: tradeCount, |
| 64 | + makerTradeValue: tradeValue, |
| 65 | + totalFillCount: 1, |
| 66 | + totalFillValue: fillValue, |
| 67 | + totalTradeCount: tradeCount, |
| 68 | + totalTradeValue: tradeValue, |
| 69 | + updatedAt: new Date().toISOString(), |
| 70 | + }), |
| 71 | + JSON.stringify({ |
| 72 | + index: { |
| 73 | + _id: `${fillId}_taker`, |
| 74 | + }, |
| 75 | + }), |
| 76 | + JSON.stringify({ |
| 77 | + address: takerMetadata.isContract ? transaction.from : taker, |
| 78 | + fillId, |
| 79 | + date: fillDate, |
| 80 | + takerFillCount: 1, |
| 81 | + takerFillValue: fillValue, |
| 82 | + takerTradeCount: tradeCount, |
| 83 | + takerTradeValue: tradeValue, |
| 84 | + totalFillCount: 1, |
| 85 | + totalFillValue: fillValue, |
| 86 | + totalTradeCount: tradeCount, |
| 87 | + totalTradeValue: tradeValue, |
| 88 | + updatedAt: new Date().toISOString(), |
| 89 | + }), |
| 90 | + ].join('\n'); |
| 91 | + |
| 92 | + const result = await elasticsearch |
| 93 | + .getClient() |
| 94 | + .bulk({ body: `${requestBody}\n`, index: 'trader_fills' }); |
| 95 | + |
| 96 | + if (result.body.errors === true) { |
| 97 | + const errorMessage = _.get( |
| 98 | + result, |
| 99 | + 'body.items[0].index.error.reason', |
| 100 | + `Failed to index trader_fills`, |
| 101 | + ); |
| 102 | + |
| 103 | + throw new Error(errorMessage); |
| 104 | + } |
| 105 | + |
| 106 | + logger.info(`indexed fill traders: ${fillId}`); |
| 107 | +}; |
| 108 | + |
| 109 | +module.exports = { |
| 110 | + fn: indexFillTraders, |
| 111 | + jobName: JOB.INDEX_FILL_TRADERS, |
| 112 | + queueName: QUEUE.INDEXING, |
| 113 | +}; |
0 commit comments