From 181c8307bb68e2f2f7d1cdfe216730782f06cd20 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 17 Jul 2024 14:29:46 +0300 Subject: [PATCH 1/5] Add progress states --- .../sync/transaction.tax.report/helpers/index.js | 2 ++ .../helpers/progress.states.js | 12 ++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 workers/loc.api/sync/transaction.tax.report/helpers/progress.states.js diff --git a/workers/loc.api/sync/transaction.tax.report/helpers/index.js b/workers/loc.api/sync/transaction.tax.report/helpers/index.js index dc9b8137..680903d1 100644 --- a/workers/loc.api/sync/transaction.tax.report/helpers/index.js +++ b/workers/loc.api/sync/transaction.tax.report/helpers/index.js @@ -3,6 +3,7 @@ const TRX_TAX_TYPES = require('./trx.tax.types') const TRX_TAX_STRATEGIES = require('./trx.tax.strategies') const PRIORITY_CURRENCY_LIST = require('./priority.currency.list') +const PROGRESS_STATES = require('./progress.states') const remapTrades = require('./remap-trades') const remapMovements = require('./remap-movements') const lookUpTrades = require('./look-up-trades') @@ -16,6 +17,7 @@ module.exports = { TRX_TAX_TYPES, TRX_TAX_STRATEGIES, PRIORITY_CURRENCY_LIST, + PROGRESS_STATES, remapTrades, remapMovements, lookUpTrades, diff --git a/workers/loc.api/sync/transaction.tax.report/helpers/progress.states.js b/workers/loc.api/sync/transaction.tax.report/helpers/progress.states.js new file mode 100644 index 00000000..654a39b3 --- /dev/null +++ b/workers/loc.api/sync/transaction.tax.report/helpers/progress.states.js @@ -0,0 +1,12 @@ +'use strict' + +const PROGRESS_STATES = { + GENERATION_STARTED: 'GENERATION_STARTED', + OBTAINING_CURRENCY_PRICES: 'OBTAINING_CURRENCY_PRICES', + TRANSACTION_HISTORY_GENERATION: 'TRANSACTION_HISTORY_GENERATION', + GENERATION_COMPLETED: 'GENERATION_COMPLETED', + + GENERATION_INTERRUPTED: 'GENERATION_INTERRUPTED' +} + +module.exports = PROGRESS_STATES From f57ecbfefc61498d4eddfc5305e8d976f17814a8 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 17 Jul 2024 14:30:27 +0300 Subject: [PATCH 2/5] Add ability to emit emitTrxTaxReportGenerationProgressToOne event --- workers/loc.api/ws-transport/ws.event.emitter.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/workers/loc.api/ws-transport/ws.event.emitter.js b/workers/loc.api/ws-transport/ws.event.emitter.js index 4d0f2350..ff4ab821 100644 --- a/workers/loc.api/ws-transport/ws.event.emitter.js +++ b/workers/loc.api/ws-transport/ws.event.emitter.js @@ -153,6 +153,21 @@ class WSEventEmitter extends AbstractWSEventEmitter { }, 'emitTrxTaxReportGenerationInBackgroundToOne') } + emitTrxTaxReportGenerationProgressToOne ( + handler = () => {}, + auth = {} + ) { + return this.emit(async (user, ...args) => { + if (this.isNotTargetUser(auth, user)) { + return { isNotEmitted: true } + } + + return typeof handler === 'function' + ? await handler(user, ...args) + : handler + }, 'emitTrxTaxReportGenerationProgressToOne') + } + async emitRedirectingRequestsStatusToApi ( handler = () => {} ) { From 138b636fe57b9d434f47a2fee4f4e39670baa7cc Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Wed, 17 Jul 2024 14:40:12 +0300 Subject: [PATCH 3/5] Emit base progress states for trx tax report generation --- .../sync/transaction.tax.report/index.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/workers/loc.api/sync/transaction.tax.report/index.js b/workers/loc.api/sync/transaction.tax.report/index.js index caff388a..162f91f0 100644 --- a/workers/loc.api/sync/transaction.tax.report/index.js +++ b/workers/loc.api/sync/transaction.tax.report/index.js @@ -10,6 +10,7 @@ const { PubTradeFindForTrxTaxError } = require('../../errors') const { TRX_TAX_STRATEGIES, + PROGRESS_STATES, remapTrades, remapMovements, lookUpTrades, @@ -96,6 +97,10 @@ class TransactionTaxReport { user, name: INTERRUPTER_NAMES.TRX_TAX_REPORT_INTERRUPTER }) + await this.#emitProgress( + user, + { process: 0, state: PROGRESS_STATES.GENERATION_STARTED } + ) const isFIFO = strategy === TRX_TAX_STRATEGIES.FIFO const isLIFO = strategy === TRX_TAX_STRATEGIES.LIFO @@ -114,6 +119,10 @@ class TransactionTaxReport { trxsForCurrPeriod.length === 0 ) { interrupter.emitInterrupted() + await this.#emitProgress( + user, + { process: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } + ) return [] } @@ -165,9 +174,19 @@ class TransactionTaxReport { interrupter.emitInterrupted() if (interrupter.hasInterrupted()) { + await this.#emitProgress( + user, + { process: null, state: PROGRESS_STATES.GENERATION_INTERRUPTED } + ) + return [] } + await this.#emitProgress( + user, + { process: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } + ) + return saleTradesWithRealizedProfit } @@ -481,6 +500,24 @@ class TransactionTaxReport { } } } + + async #emitProgress (user, params) { + const { + progress = null, + state = null + } = params ?? {} + + await this.wsEventEmitterFactory() + .emitTrxTaxReportGenerationProgressToOne( + { + progress: Number.isFinite(progress) + ? Math.floor(progress) + : progress, + state + }, + user + ) + } } decorateInjectable(TransactionTaxReport, depsTypes) From 5879f9f06be4bab7c6ea90e5879ebcc01d935dc5 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 18 Jul 2024 13:21:37 +0300 Subject: [PATCH 4/5] Add ability to calc total trx amount --- .../helpers/get-trx-map-by-ccy.js | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/workers/loc.api/sync/transaction.tax.report/helpers/get-trx-map-by-ccy.js b/workers/loc.api/sync/transaction.tax.report/helpers/get-trx-map-by-ccy.js index 45a0eee9..f2a2fe68 100644 --- a/workers/loc.api/sync/transaction.tax.report/helpers/get-trx-map-by-ccy.js +++ b/workers/loc.api/sync/transaction.tax.report/helpers/get-trx-map-by-ccy.js @@ -15,18 +15,32 @@ const setCcyCalculator = (map, symb, trxPriceCalculator) => { map.get(symb).push(trxPriceCalculator) } +const calcTotalTrxAmount = (trxMapArray) => { + return trxMapArray.reduce((accum, [s, calcs]) => { + return accum + calcs.length + }, 0) +} + const placeTriangulationCcyAtStart = (map) => { if (!map.has(TrxPriceCalculator.CRYPTO_CCY_FOR_TRIANGULATION)) { - return map + return { + trxMapByCcy: map, + totalTrxAmount: calcTotalTrxAmount([...map]) + } } const triangulationCcyCalculators = map.get(TrxPriceCalculator.CRYPTO_CCY_FOR_TRIANGULATION) map.delete(TrxPriceCalculator.CRYPTO_CCY_FOR_TRIANGULATION) - return new Map([ + const trxMapArray = [ [TrxPriceCalculator.CRYPTO_CCY_FOR_TRIANGULATION, triangulationCcyCalculators], ...map - ]) + ] + + return { + trxMapByCcy: new Map(trxMapArray), + totalTrxAmount: calcTotalTrxAmount(trxMapArray) + } } module.exports = (trxs) => { From d227ecec1d2d3f0caf1a16b1044c530477a2b911 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Thu, 18 Jul 2024 13:22:21 +0300 Subject: [PATCH 5/5] Emit progress for tax report based on trxs --- .../sync/transaction.tax.report/index.js | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/workers/loc.api/sync/transaction.tax.report/index.js b/workers/loc.api/sync/transaction.tax.report/index.js index 162f91f0..0c5435d6 100644 --- a/workers/loc.api/sync/transaction.tax.report/index.js +++ b/workers/loc.api/sync/transaction.tax.report/index.js @@ -99,7 +99,7 @@ class TransactionTaxReport { }) await this.#emitProgress( user, - { process: 0, state: PROGRESS_STATES.GENERATION_STARTED } + { progress: 0, state: PROGRESS_STATES.GENERATION_STARTED } ) const isFIFO = strategy === TRX_TAX_STRATEGIES.FIFO @@ -121,7 +121,7 @@ class TransactionTaxReport { interrupter.emitInterrupted() await this.#emitProgress( user, - { process: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } + { progress: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } ) return [] @@ -160,7 +160,10 @@ class TransactionTaxReport { !Number.isFinite(trx?.lastSymbPriceUsd) )) ) - await this.#convertCurrencies(trxsForConvToUsd, { interrupter }) + await this.#convertCurrencies( + trxsForConvToUsd, + { interrupter, user } + ) const { saleTradesWithRealizedProfit } = await lookUpTrades( trxsForCurrPeriod, @@ -176,7 +179,7 @@ class TransactionTaxReport { if (interrupter.hasInterrupted()) { await this.#emitProgress( user, - { process: null, state: PROGRESS_STATES.GENERATION_INTERRUPTED } + { progress: null, state: PROGRESS_STATES.GENERATION_INTERRUPTED } ) return [] @@ -184,7 +187,7 @@ class TransactionTaxReport { await this.#emitProgress( user, - { process: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } + { progress: 100, state: PROGRESS_STATES.GENERATION_COMPLETED } ) return saleTradesWithRealizedProfit @@ -250,8 +253,13 @@ class TransactionTaxReport { } async #convertCurrencies (trxs, opts) { - const { interrupter } = opts - const trxMapByCcy = getTrxMapByCcy(trxs) + const { interrupter, user } = opts + const { + trxMapByCcy, + totalTrxAmount + } = getTrxMapByCcy(trxs) + let count = 0 + let progress = 0 for (const [symbol, trxPriceCalculators] of trxMapByCcy.entries()) { if (interrupter.hasInterrupted()) { @@ -265,6 +273,8 @@ class TransactionTaxReport { let pubTradeEnd = pubTrades[pubTrades.length - 1]?.mts for (const trxPriceCalculator of trxPriceCalculatorIterator) { + count += 1 + if (interrupter.hasInterrupted()) { return } @@ -356,10 +366,28 @@ class TransactionTaxReport { const pubTrade = findPublicTrade(pubTrades, trx.mtsCreate) trxPriceCalculator.calcPrice(pubTrade?.price) + const _progress = (count / totalTrxAmount) * 100 + + if ( + _progress <= 0 || + _progress >= 100 + ) { + continue + } + + progress = _progress + await this.#emitProgress( + user, + { progress, state: PROGRESS_STATES.OBTAINING_CURRENCY_PRICES } + ) } } await this.#updateExactUsdValueInColls(trxs) + await this.#emitProgress( + user, + { progress, state: PROGRESS_STATES.TRANSACTION_HISTORY_GENERATION } + ) } async #getTrades ({