Skip to content

Commit

Permalink
explorer: Show token delta when token account is closed (solana-labs#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored Sep 29, 2022
1 parent 816940c commit 7abc322
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions explorer/src/components/transaction/TokenBalancesCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,51 @@ export function generateTokenBalanceRows(
accounts: ParsedMessageAccount[]
): TokenBalanceRow[] {
let preBalanceMap: { [index: number]: TokenBalance } = {};
let postBalanceMap: { [index: number]: TokenBalance } = {};

preTokenBalances.forEach(
(balance) => (preBalanceMap[balance.accountIndex] = balance)
);
postTokenBalances.forEach(
(balance) => (postBalanceMap[balance.accountIndex] = balance)
);

// Check if any pre token balances do not have corresponding
// post token balances. If not, insert a post balance of zero
// so that the delta is displayed properly
for (let index in preBalanceMap) {
const preBalance = preBalanceMap[index];
if (!postBalanceMap[index]) {
postBalanceMap[index] = {
accountIndex: Number(index),
mint: preBalance.mint,
uiTokenAmount: {
amount: "0",
decimals: preBalance.uiTokenAmount.decimals,
uiAmount: null,
uiAmountString: "0",
},
};
}
}

let rows: TokenBalanceRow[] = [];

postTokenBalances.forEach(({ uiTokenAmount, accountIndex, mint }) => {
for (let index in postBalanceMap) {
const { uiTokenAmount, accountIndex, mint } = postBalanceMap[index];
const preBalance = preBalanceMap[accountIndex];
const account = accounts[accountIndex].pubkey;

if (!uiTokenAmount.uiAmountString) {
// uiAmount deprecation
return;
continue;
}

// case where mint changes
if (preBalance && preBalance.mint !== mint) {
if (!preBalance.uiTokenAmount.uiAmountString) {
// uiAmount deprecation
return;
continue;
}

rows.push({
Expand All @@ -139,15 +163,15 @@ export function generateTokenBalanceRows(
delta: new BigNumber(uiTokenAmount.uiAmountString),
mint: mint,
});
return;
continue;
}

let delta;

if (preBalance) {
if (!preBalance.uiTokenAmount.uiAmountString) {
// uiAmount deprecation
return;
continue;
}

delta = new BigNumber(uiTokenAmount.uiAmountString).minus(
Expand All @@ -164,7 +188,7 @@ export function generateTokenBalanceRows(
delta,
accountIndex,
});
});
}

return rows.sort((a, b) => a.accountIndex - b.accountIndex);
}

0 comments on commit 7abc322

Please sign in to comment.