-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from ZIMkaRU/feature/add-transaction-tax-repo…
…rt-v2 [PART-3] Add transaction tax report
- Loading branch information
Showing
5 changed files
with
255 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
'use strict' | ||
|
||
const TRX_TAX_STRATEGIES = require('./trx.tax.strategies') | ||
const remapTrades = require('./remap-trades') | ||
const remapMovements = require('./remap-movements') | ||
|
||
module.exports = { | ||
TRX_TAX_STRATEGIES | ||
TRX_TAX_STRATEGIES, | ||
remapTrades, | ||
remapMovements | ||
} |
69 changes: 69 additions & 0 deletions
69
workers/loc.api/sync/transaction.tax.report/helpers/remap-movements.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict' | ||
|
||
const { | ||
isForexSymb | ||
} = require('../../helpers') | ||
|
||
module.exports = (movements, params) => { | ||
const { | ||
remappedTrxs, | ||
remappedTrxsForConvToUsd | ||
} = params | ||
|
||
for (const movement of movements) { | ||
if ( | ||
!movement?.currency || | ||
isForexSymb(movement.currency) || | ||
!Number.isFinite(movement?.amount) || | ||
movement.amount === 0 || | ||
!Number.isFinite(movement?.mtsUpdated) | ||
) { | ||
continue | ||
} | ||
|
||
const firstSymb = movement.currency | ||
const lastSymb = 'USD' | ||
const symbSeparator = firstSymb.length > 3 | ||
? ':' | ||
: '' | ||
|
||
const remappedMovement = { | ||
_id: movement._id, | ||
// NOTE: it means entries are not taken form trades table | ||
isAdditionalTrxMovements: true, | ||
// NOTE: movements can have sub-account transfer entries from ledgers table | ||
isMovements: !movement.isLedgers, | ||
isLedgers: !!movement.isLedgers, | ||
isTrades: false, | ||
symbol: `t${firstSymb}${symbSeparator}${lastSymb}`, | ||
mtsCreate: movement.mtsUpdated, | ||
firstSymb, | ||
lastSymb, | ||
firstSymbPriceUsd: null, | ||
lastSymbPriceUsd: 1, | ||
execAmount: movement.amount, | ||
// NOTE: execPrice = firstSymbPriceUsd and should be set when converting currencies | ||
execPrice: 0, | ||
// NOTE: exactUsdValue can be null on the first launch, for warm-up it's filling from pub-trades | ||
exactUsdValue: movement.exactUsdValue | ||
} | ||
|
||
remappedTrxs.push(remappedMovement) | ||
|
||
if ( | ||
Number.isFinite(movement.exactUsdValue) && | ||
movement.exactUsdValue > 0 | ||
) { | ||
const price = movement.exactUsdValue / movement.amount | ||
|
||
remappedMovement.firstSymbPriceUsd = price | ||
remappedMovement.execPrice = price | ||
|
||
continue | ||
} | ||
|
||
remappedTrxsForConvToUsd.push(remappedMovement) | ||
} | ||
|
||
return params | ||
} |
57 changes: 57 additions & 0 deletions
57
workers/loc.api/sync/transaction.tax.report/helpers/remap-trades.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict' | ||
|
||
const splitSymbolPairs = require( | ||
'bfx-report/workers/loc.api/helpers/split-symbol-pairs' | ||
) | ||
|
||
module.exports = (trades, params) => { | ||
const { | ||
remappedTrxs, | ||
remappedTrxsForConvToUsd | ||
} = params | ||
|
||
for (const trade of trades) { | ||
if ( | ||
!trade?.symbol || | ||
!Number.isFinite(trade?.execAmount) || | ||
trade.execAmount === 0 || | ||
!Number.isFinite(trade?.execPrice) || | ||
trade.execPrice === 0 || | ||
!Number.isFinite(trade?.mtsCreate) | ||
) { | ||
continue | ||
} | ||
|
||
const [firstSymb, lastSymb] = splitSymbolPairs(trade.symbol) | ||
trade.firstSymb = firstSymb | ||
trade.lastSymb = lastSymb | ||
trade.firstSymbPriceUsd = null | ||
trade.lastSymbPriceUsd = null | ||
trade.isAdditionalTrxMovements = false | ||
trade.isMovements = false | ||
trade.isLedgers = false | ||
trade.isTrades = true | ||
|
||
remappedTrxs.push(trade) | ||
|
||
if (lastSymb === 'USD') { | ||
trade.firstSymbPriceUsd = trade.execPrice | ||
trade.lastSymbPriceUsd = 1 | ||
|
||
continue | ||
} | ||
if ( | ||
Number.isFinite(trade.exactUsdValue) && | ||
trade.exactUsdValue > 0 | ||
) { | ||
trade.firstSymbPriceUsd = trade.exactUsdValue / trade.execAmount | ||
trade.lastSymbPriceUsd = trade.exactUsdValue / trade.execPrice | ||
|
||
continue | ||
} | ||
|
||
remappedTrxsForConvToUsd.push(trade) | ||
} | ||
|
||
return params | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters