Skip to content

Commit

Permalink
Merge pull request #394 from ZIMkaRU/feature/add-perc-progress-of-trx…
Browse files Browse the repository at this point in the history
…-tax-report

Add perc progress of trx tax report
  • Loading branch information
ezewer authored Jul 22, 2024
2 parents 2245e0f + d227ece commit be7f0ec
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 2 additions & 0 deletions workers/loc.api/sync/transaction.tax.report/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -16,6 +17,7 @@ module.exports = {
TRX_TAX_TYPES,
TRX_TAX_STRATEGIES,
PRIORITY_CURRENCY_LIST,
PROGRESS_STATES,
remapTrades,
remapMovements,
lookUpTrades,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
71 changes: 68 additions & 3 deletions workers/loc.api/sync/transaction.tax.report/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { PubTradeFindForTrxTaxError } = require('../../errors')

const {
TRX_TAX_STRATEGIES,
PROGRESS_STATES,
remapTrades,
remapMovements,
lookUpTrades,
Expand Down Expand Up @@ -96,6 +97,10 @@ class TransactionTaxReport {
user,
name: INTERRUPTER_NAMES.TRX_TAX_REPORT_INTERRUPTER
})
await this.#emitProgress(
user,
{ progress: 0, state: PROGRESS_STATES.GENERATION_STARTED }
)

const isFIFO = strategy === TRX_TAX_STRATEGIES.FIFO
const isLIFO = strategy === TRX_TAX_STRATEGIES.LIFO
Expand All @@ -114,6 +119,10 @@ class TransactionTaxReport {
trxsForCurrPeriod.length === 0
) {
interrupter.emitInterrupted()
await this.#emitProgress(
user,
{ progress: 100, state: PROGRESS_STATES.GENERATION_COMPLETED }
)

return []
}
Expand Down Expand Up @@ -151,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,
Expand All @@ -165,9 +177,19 @@ class TransactionTaxReport {
interrupter.emitInterrupted()

if (interrupter.hasInterrupted()) {
await this.#emitProgress(
user,
{ progress: null, state: PROGRESS_STATES.GENERATION_INTERRUPTED }
)

return []
}

await this.#emitProgress(
user,
{ progress: 100, state: PROGRESS_STATES.GENERATION_COMPLETED }
)

return saleTradesWithRealizedProfit
}

Expand Down Expand Up @@ -231,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()) {
Expand All @@ -246,6 +273,8 @@ class TransactionTaxReport {
let pubTradeEnd = pubTrades[pubTrades.length - 1]?.mts

for (const trxPriceCalculator of trxPriceCalculatorIterator) {
count += 1

if (interrupter.hasInterrupted()) {
return
}
Expand Down Expand Up @@ -337,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 ({
Expand Down Expand Up @@ -481,6 +528,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)
Expand Down
15 changes: 15 additions & 0 deletions workers/loc.api/ws-transport/ws.event.emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {}
) {
Expand Down

0 comments on commit be7f0ec

Please sign in to comment.