Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pub-trade price lookup for trx tax report #390

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions workers/loc.api/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ class CurrencyPairSeparationError extends BaseError {
}

class PubTradeFindForTrxTaxError extends BaseError {
constructor (message = 'ERR_NO_PUBLIC_TRADES_FOR_TRX_TAX') {
super(message)
constructor (data, message = 'ERR_NO_PUBLIC_TRADES_FOR_TRX_TAX') {
super({ data, message })
}
}

Expand Down
64 changes: 60 additions & 4 deletions workers/loc.api/sync/transaction.tax.report/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const depsTypes = (TYPES) => [
TYPES.GetDataFromApi,
TYPES.WSEventEmitterFactory,
TYPES.Logger,
TYPES.InterrupterFactory
TYPES.InterrupterFactory,
TYPES.CurrencyConverter
]
class TransactionTaxReport {
constructor (
Expand All @@ -46,7 +47,8 @@ class TransactionTaxReport {
getDataFromApi,
wsEventEmitterFactory,
logger,
interrupterFactory
interrupterFactory,
currencyConverter
) {
this.dao = dao
this.authenticator = authenticator
Expand All @@ -59,6 +61,7 @@ class TransactionTaxReport {
this.wsEventEmitterFactory = wsEventEmitterFactory
this.logger = logger
this.interrupterFactory = interrupterFactory
this.currencyConverter = currencyConverter

this.tradesModel = this.syncSchema.getModelsMap()
.get(this.ALLOWED_COLLS.TRADES)
Expand Down Expand Up @@ -234,6 +237,9 @@ class TransactionTaxReport {
}

const trxPriceCalculatorIterator = getBackIterable(trxPriceCalculators)
const symbSeparator = symbol.length > 3
? ':'
: ''

let pubTrades = []
let pubTradeStart = pubTrades[0]?.mts
Expand All @@ -254,10 +260,55 @@ class TransactionTaxReport {
const start = trx.mtsCreate - 1

pubTrades = await this.#getPublicTrades(
{ symbol: `t${symbol}USD`, start },
{ symbol: `t${symbol}${symbSeparator}USD`, start },
opts
)

if (
!Array.isArray(pubTrades) ||
pubTrades.length === 0
) {
const ccySynonymous = await this.currencyConverter
.getCurrenciesSynonymous()
const synonymous = ccySynonymous.get(symbol)

if (!synonymous) {
throw new PubTradeFindForTrxTaxError({
symbol,
pubTradeStart,
pubTradeEnd,
requiredMts: trx.mtsCreate
})
}

for (const [symbol, conversion] of synonymous) {
const symbSeparator = symbol.length > 3
? ':'
: ''
const res = await this.#getPublicTrades(
{ symbol: `t${symbol}${symbSeparator}USD`, start },
opts
)

if (
!Array.isArray(res) ||
res.length === 0
) {
continue
}

pubTrades = res.map((item) => {
if (Number.isFinite(item?.price)) {
item.price = item.price * conversion
}

return item
})

break
}
}

pubTradeStart = start ?? pubTrades[0]?.mts
pubTradeEnd = pubTrades[pubTrades.length - 1]?.mts
}
Expand All @@ -270,7 +321,12 @@ class TransactionTaxReport {
pubTradeStart > trx.mtsCreate ||
pubTradeEnd < trx.mtsCreate
) {
throw new PubTradeFindForTrxTaxError()
throw new PubTradeFindForTrxTaxError({
symbol,
pubTradeStart,
pubTradeEnd,
requiredMts: trx.mtsCreate
})
}

const pubTrade = findPublicTrade(pubTrades, trx.mtsCreate)
Expand Down