-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathAccountsCard.tsx
More file actions
179 lines (161 loc) · 7.62 KB
/
Copy pathAccountsCard.tsx
File metadata and controls
179 lines (161 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { Address } from '@components/common/Address';
import { BalanceDelta } from '@components/common/BalanceDelta';
import { ErrorCard } from '@components/common/ErrorCard';
import { SolBalance } from '@components/common/SolBalance';
import { RawDataField } from '@components/shared/RawDataField';
import { Badge } from '@components/shared/ui/badge';
import { Button } from '@components/shared/ui/button';
import { Popover, PopoverContent, PopoverTrigger } from '@components/shared/ui/popover';
import { cn } from '@components/shared/utils';
import { useAccountsInfo } from '@entities/account';
import { useCluster } from '@providers/cluster';
import { useTransactionDetails } from '@providers/transactions';
import { SignatureProps } from '@utils/index';
import { BigNumber } from 'bignumber.js';
import React, { useMemo } from 'react';
import { Code } from 'react-feather';
import { useBreakpoint } from '@/app/shared/lib/use-breakpoint';
import { CollapsibleSection } from './CollapsibleSection';
const ACCOUNT_KEY_SIZE_BYTES = 32;
export function getTransactionAccountKeysSizeBytes(accountKeys: Array<{ source?: string }>) {
return accountKeys.filter(account => account.source !== 'lookupTable').length * ACCOUNT_KEY_SIZE_BYTES;
}
export function AccountsCard({ signature }: SignatureProps) {
const details = useTransactionDetails(signature);
const { url } = useCluster();
const { isLg } = useBreakpoint();
const transactionWithMeta = details?.data?.transactionWithMeta;
const message = transactionWithMeta?.transaction.message;
const meta = transactionWithMeta?.meta;
const pubkeys = useMemo(() => message?.accountKeys.map(a => a.pubkey) ?? [], [message?.accountKeys]);
const { accounts, error, loading } = useAccountsInfo(pubkeys, url);
const totalAccountKeysSize = useMemo(
() => getTransactionAccountKeysSizeBytes(message?.accountKeys ?? []),
[message],
);
if (!transactionWithMeta) {
return null;
}
if (!meta || !message) {
return <ErrorCard text="Transaction metadata is missing" />;
}
if (error) {
return <ErrorCard text="Failed to fetch accounts info" />;
}
const accountRows = message.accountKeys.map((account, index) => {
const pre = meta.preBalances[index];
const post = meta.postBalances[index];
const pubkey = account.pubkey;
const key = pubkey.toBase58();
const delta = new BigNumber(post).minus(new BigNumber(pre));
const accountInfo = accounts.get(key);
const hasBadges =
index === 0 ||
account.signer ||
account.writable ||
message.instructions.some(ix => ix.programId.equals(pubkey)) ||
account.source === 'lookupTable';
const badges = (
<>
{index === 0 && (
<Badge ui="dashkit" variant="success" className="me-1">
Fee Payer
</Badge>
)}
{account.signer && (
<Badge ui="dashkit" variant="info" className="me-1">
Signer
</Badge>
)}
{account.writable && (
<Badge ui="dashkit" variant="danger" className="me-1">
Writable
</Badge>
)}
{message.instructions.find(ix => ix.programId.equals(pubkey)) && (
<Badge ui="dashkit" variant="warning" className="me-1">
Program
</Badge>
)}
{account.source === 'lookupTable' && (
<Badge ui="dashkit" variant="gray" className="me-1">
Address Table Lookup
</Badge>
)}
</>
);
const dataCell = loading ? (
<span className="text-xs text-outer-space-300">Loading…</span>
) : accountInfo && accountInfo.size > 0 ? (
<Popover>
<PopoverTrigger asChild>
<Button variant="ghost" className="h-auto !px-1 !py-0 text-xs">
<Code size={11} />
<span>{accountInfo.size.toLocaleString('en-US')}</span>
</Button>
</PopoverTrigger>
<PopoverContent className="mx-4 w-auto !rounded-lg border-none p-0" align="end">
<RawDataField data={accountInfo.data} filename={key} />
</PopoverContent>
</Popover>
) : null;
return (
<div
key={key}
className={cn(
'min-h-9 px-3 py-2.5 md:px-4',
'grid items-start gap-x-0 gap-y-0.5 whitespace-nowrap text-sm md:gap-y-0 lg:gap-x-5',
'grid-cols-[minmax(auto,1.75rem)_minmax(100px,auto)_1fr] sm:grid-cols-[minmax(auto,1.75rem)_1fr_auto] lg:grid-cols-[minmax(auto,1.25rem)_1fr_minmax(auto,170px)_minmax(auto,180px)]',
"[grid-template-areas:'number_address_delta'_'number_address_balance'_'number_address_size'] lg:[grid-template-areas:'number_address_delta_balance']",
'border-1 border-b border-white/10 [border-bottom-style:solid] last:border-b-0',
)}
>
<div className="mr-2 text-outer-space-300 [grid-area:number] lg:mr-0">{index + 1}</div>
<div className="[grid-area:address]">
<div className="flex items-center justify-between gap-1 lg:justify-normal">
<Address pubkey={pubkey} link fetchTokenLabelInfo />
</div>
{hasBadges && (
<span className="mt-1 inline-flex flex-wrap gap-1">
{badges}
{isLg && dataCell}
</span>
)}
</div>
<div className="justify-self-end [grid-area:delta]">
<BalanceDelta delta={delta} isSol />
</div>
<div className="justify-self-end [grid-area:balance]">
<SolBalance lamports={post} />
</div>
{!isLg && <div className="justify-self-end [grid-area:size]">{dataCell}</div>}
</div>
);
});
return (
<CollapsibleSection id="accounts" title="Accounts & SOL balance">
<div
className={cn(
'hidden px-3 py-1.5 md:px-4 lg:grid',
'grid-cols-[minmax(auto,1.25rem)_1fr_minmax(auto,170px)_minmax(auto,180px)] gap-5 text-xs uppercase text-outer-space-300',
'border-1 border-b border-white/10 [border-bottom-style:solid]',
)}
>
<div>#</div>
<div>Address</div>
<div className="text-right">Change (SOL)</div>
<div className="text-right">Post Balance (SOL)</div>
</div>
{accountRows}
{!loading && totalAccountKeysSize > 0 && (
<div className="ml-7 flex items-baseline gap-2 px-3 py-2 text-sm text-outer-space-300 md:px-4 lg:ml-10">
<div className="flex flex-col">
<span className="text-sm uppercase leading-none">Total Account Keys Size:</span>
<span className="text-[10px] leading-none">excludes address lookup tables</span>
</div>
<span className="text-white">{totalAccountKeysSize.toLocaleString('en-US')} bytes</span>
</div>
)}
</CollapsibleSection>
);
}