Skip to content

Commit

Permalink
chore: simplify calculateBalanceChange func
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
  • Loading branch information
panteleymonchuk committed Jan 30, 2024
1 parent a581c26 commit e37e3a2
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions api/src/routes/stardust/transactionhistory/download/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,24 @@ export const getTransactionHistoryRecords = (
};

export const calculateBalanceChange = (outputs: OutputWithDetails[]) => {
// eslint-disable-next-line unicorn/no-array-reduce
return outputs.reduce((acc, output) => {
let totalAmount = 0;

for (const output of outputs) {
const outputFromDetails = output?.details?.output as CommonOutput;

if (!outputFromDetails?.amount) {
// Perform the calculation only if outputFromDetails and amount are defined
if (outputFromDetails?.amount) {
let amount = Number(outputFromDetails.amount);
if (output.isSpent) {
amount *= -1;
}
totalAmount += amount;
} else {
console.warn("Output details not found for:", output);
return acc;
}
}

let amount = Number(outputFromDetails.amount);
if (output.isSpent) {
amount *= -1;
}
return acc + amount;
}, 0);
return totalAmount;
};

/**
Expand Down

0 comments on commit e37e3a2

Please sign in to comment.