Skip to content

Commit

Permalink
refactor: improve transaction operation categorization and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
nelitow committed Feb 6, 2025
1 parent e2b9258 commit b1a9ab8
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions packages/app/src/systems/Transaction/utils/simplifyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ function categorizeOperations(
const otherRoot: SimplifiedOperation[] = [];
const intermediate: SimplifiedOperation[] = [];

// First pass: separate operations
for (const op of operations) {
// Check if operation has assets or is a contract call with amount
const hasAssets =
Expand All @@ -237,21 +238,23 @@ function categorizeOperations(
}

const depth = op.metadata?.depth || 0;
const isTransfer = op.type === TxCategory.SEND;
const isTransfer =
op.type === TxCategory.SEND || op.type === TxCategory.RECEIVE;
const isFromCurrentAccount =
currentAccount &&
op.from.address.toLowerCase() === currentAccount.toLowerCase();
const isToCurrentAccount =
currentAccount &&
op.to.address.toLowerCase() === currentAccount.toLowerCase();

if (isTransfer) {
main.push(op);
continue;
}

if (depth === 0) {
if (isFromCurrentAccount || isToCurrentAccount) {
if (isTransfer) {
if (isFromCurrentAccount || isToCurrentAccount) {
main.push(op);
} else {
otherRoot.push(op);
}
} else if (isFromCurrentAccount || isToCurrentAccount) {
main.push(op);
} else {
otherRoot.push(op);
Expand All @@ -262,6 +265,20 @@ function categorizeOperations(
intermediate.push(op);
}

// Sort main operations: from user first, then to user
main.sort((a, b) => {
const aFromUser =
currentAccount &&
a.from.address.toLowerCase() === currentAccount.toLowerCase();
const bFromUser =
currentAccount &&
b.from.address.toLowerCase() === currentAccount.toLowerCase();

if (aFromUser && !bFromUser) return -1;
if (!aFromUser && bFromUser) return 1;
return 0;
});

// set all main operations to depth 0
for (const op of main) {
op.metadata.depth = 0;
Expand Down

0 comments on commit b1a9ab8

Please sign in to comment.