Skip to content

Commit cb41384

Browse files
Thoralf-Mmarc2332
andauthored
feat(explorer): display balance changes in tx list (#8662)
# Description of change Display balance changes in the transaction list on an address page. The balance change is for the address for which the page is loaded and only IOTA is considered <img width="1927" height="1188" alt="image" src="https://github.com/user-attachments/assets/bdc5ce45-eefc-4dbb-9c7b-66e3ae1c24f6" /> ## How the change has been tested `pnpm explorer dev` and then manually checked addresses on http://localhost:3000/?network=https%3A%2F%2Findexer.mainnet.iota.cafe In the CI deployment https://rebased-explorer-git-dev-tools-explorer-address-tx-58ac61-iota1.vercel.app/address/0x8008587485b6d62225fca4663ca9a226f2ed0e122f7e290ecf575d195f8abff6?network=mainnet --------- Co-authored-by: Marc Espin <[email protected]>
1 parent 9378c63 commit cb41384

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

apps/explorer/src/components/transaction-blocks-for-address/TransactionBlocksForAddress.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function TransactionBlocksForAddress({
110110
} as TransactionFilter);
111111

112112
const currentPage = currentPageState[filterValue];
113-
const tableColumns = generateTransactionsTableColumns();
113+
const tableColumns = generateTransactionsTableColumns(address);
114114

115115
return (
116116
<Panel>

apps/explorer/src/components/transactions/TransactionsForAddress.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function TransactionsForAddressTable({
7272
);
7373
}
7474

75-
const tableColumns = generateTransactionsTableColumns();
75+
const tableColumns = generateTransactionsTableColumns(address);
7676
const hasTxns = data?.length > 0;
7777

7878
if (!hasTxns) {
@@ -122,6 +122,7 @@ export function TransactionsForAddress({ address }: TransactionsForAddressProps)
122122
options: {
123123
showEffects: true,
124124
showInput: true,
125+
showBalanceChanges: true,
125126
},
126127
cursor,
127128
limit,

apps/explorer/src/hooks/useGetTransactionBlocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function useGetTransactionBlocks(
3333
options: {
3434
showEffects: true,
3535
showInput: true,
36+
showBalanceChanges: true,
3637
},
3738
}),
3839
initialPageParam: null,

apps/explorer/src/lib/ui/utils/generateTransactionsTableColumns.tsx

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ import type { IotaTransactionBlockKind, IotaTransactionBlockResponse } from '@io
88
import { TableCellBase, TableCellText } from '@iota/apps-ui-kit';
99
import type { ColumnDef } from '@tanstack/react-table';
1010
import { AddressLink, TransactionLink } from '../../../components/ui';
11-
import { CoinFormat, formatBalance, formatDigest, NANOS_PER_IOTA } from '@iota/iota-sdk/utils';
11+
import {
12+
CoinFormat,
13+
formatBalance,
14+
formatDigest,
15+
IOTA_TYPE_ARG,
16+
NANOS_PER_IOTA,
17+
} from '@iota/iota-sdk/utils';
1218
import { getElapsedTime } from '~/pages/epochs/utils';
1319

1420
/**
1521
* Generate table columns renderers for the transactions data.
1622
*/
17-
export function generateTransactionsTableColumns(): ColumnDef<IotaTransactionBlockResponse>[] {
18-
return [
23+
export function generateTransactionsTableColumns(
24+
address?: string,
25+
): ColumnDef<IotaTransactionBlockResponse>[] {
26+
const columns: ColumnDef<IotaTransactionBlockResponse>[] = [
1927
{
2028
header: 'Digest',
2129
accessorKey: 'digest',
@@ -65,6 +73,53 @@ export function generateTransactionsTableColumns(): ColumnDef<IotaTransactionBlo
6573
);
6674
},
6775
},
76+
];
77+
78+
if (address) {
79+
columns.push({
80+
header: 'Balance Change',
81+
accessorKey: 'balanceChanges',
82+
cell: ({ getValue }) => {
83+
const balanceChanges = getValue<IotaTransactionBlockResponse['balanceChanges']>();
84+
if (!balanceChanges) {
85+
return (
86+
<TableCellBase>
87+
<TableCellText>--</TableCellText>
88+
</TableCellBase>
89+
);
90+
}
91+
const balanceChange = balanceChanges.find(
92+
(change) =>
93+
change.owner &&
94+
typeof change.owner === 'object' &&
95+
'AddressOwner' in change.owner &&
96+
change.owner.AddressOwner === address &&
97+
change.coinType === IOTA_TYPE_ARG,
98+
);
99+
if (!balanceChange) {
100+
return (
101+
<TableCellBase>
102+
<TableCellText>--</TableCellText>
103+
</TableCellBase>
104+
);
105+
}
106+
const amount = balanceChange.amount;
107+
const formatted = formatBalance(
108+
Math.abs(Number(amount)) / Number(NANOS_PER_IOTA),
109+
0,
110+
CoinFormat.Rounded,
111+
);
112+
const sign = Number(amount) >= 0 ? '+' : '-';
113+
return (
114+
<TableCellBase>
115+
<TableCellText supportingLabel="IOTA">{sign + formatted}</TableCellText>
116+
</TableCellBase>
117+
);
118+
},
119+
});
120+
}
121+
122+
columns.push(
68123
{
69124
header: 'Gas',
70125
accessorKey: 'effects',
@@ -102,5 +157,7 @@ export function generateTransactionsTableColumns(): ColumnDef<IotaTransactionBlo
102157
);
103158
},
104159
},
105-
];
160+
);
161+
162+
return columns;
106163
}

0 commit comments

Comments
 (0)