Skip to content

Commit

Permalink
fix duobleted messages
Browse files Browse the repository at this point in the history
  • Loading branch information
joramirezStackit committed Feb 2, 2022
1 parent 47ee6e8 commit f3363d5
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions services/jupiterTransactionsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ class JupiterTransactionsService {
if(!gu.isNonEmptyString(tag)){throw new Error('tag is empty')}
logger.debug(`address= ${JSON.stringify(address)}`);
logger.debug(`tag= ${JSON.stringify(tag)}`);
const unconfirmedTransactionsPromise = this.getUnconfirmedTransactionsByTag(address,tag, firstIndex, lastIndex);
const unconfirmedTransactionsPromise = this.getUnconfirmedTransactionsByTag(address,tag);
const confirmedTransactionsPromise = this.fetchConfirmedBlockChainTransactionsByTag(address,tag,firstIndex,lastIndex);
const [confirmedTransactionsResponse, unconfirmendTransactionsResponse] = await Promise.all([confirmedTransactionsPromise, unconfirmedTransactionsPromise]);
const combinedTransactions = [ ...unconfirmendTransactionsResponse, ...confirmedTransactionsResponse ];

// const combinedTransactions = [ ...unconfirmendTransactionsResponse, ...confirmedTransactionsResponse ];
const combinedTransactions = this.filterByBetweenTimestamp(confirmedTransactionsResponse, unconfirmendTransactionsResponse);
const hasInvalidTransactions = combinedTransactions.every(t => {
const valid = this.validator.validateBaseTransaction(t);
if(!valid.isValid){
Expand All @@ -277,8 +279,10 @@ class JupiterTransactionsService {
if(!hasInvalidTransactions){
throw new mError.MetisError(`Transactions from Jupiter are invalid!`);
}

const sortedCombinedTransactions = this.transactionUtils.sortTransactionsByTimestamp(combinedTransactions, orderBy);
logger.debug(`sortedCombinedTransactions.length= ${sortedCombinedTransactions.length}`);

// @TODO Jupiter needs to be fixed!
return combinedTransactions.filter(transaction => {
return transaction.hasOwnProperty('attachment') &&
Expand All @@ -287,6 +291,28 @@ class JupiterTransactionsService {
});
}

filterByBetweenTimestamp(confirmedTransactions, unconfirmedTransactions){
if (!Array.isArray(confirmedTransactions)){
throw new Error('Confirmed transactions must be an array');
}

if(!Array.isArray(unconfirmedTransactions)){
throw new Error('Un ConfirmedTransactions transactions must be an array');
}

const [lasConfirmedTransaction] = confirmedTransactions.slice(-1);
const unConfirmedTransactionsFilter = unconfirmedTransactions.filter(unconfirmed => {
if (lasConfirmedTransaction){
return unconfirmed.timestamp >= lasConfirmedTransaction.timestamp;
}
return true;
});
const results = [...confirmedTransactions, ...unConfirmedTransactionsFilter];
return results.sort((a, b) =>
new Date(b.timestamp) - new Date(a.timestamp)
);
}

/**
*
* @param {string} address
Expand Down

0 comments on commit f3363d5

Please sign in to comment.