/
WriteSummary.tsx
158 lines (151 loc) · 6.84 KB
/
WriteSummary.tsx
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
import { decodeEventLog, AbiEventSignatureNotFoundError } from "viem";
import { twMerge } from "tailwind-merge";
import { isDefined } from "@latticexyz/common/utils";
import { PendingIcon } from "../icons/PendingIcon";
import { usePromise } from "@latticexyz/react";
import { truncateHex } from "../truncateHex";
import { serialize } from "../serialize";
import { getTransaction } from "./getTransaction";
import { getTransactionReceipt } from "./getTransactionReceipt";
import { getTransactionResult } from "./getTransactionResult";
import { ErrorTrace } from "../ErrorTrace";
import { ContractWrite, hexToTableId } from "@latticexyz/common";
import { useDevToolsContext } from "../DevToolsContext";
import { hexKeyTupleToEntity } from "@latticexyz/store-sync/recs";
type Props = {
write: ContractWrite;
};
// TODO: show block number or relative timestamp (e.g. 3s ago)
export function WriteSummary({ write }: Props) {
const { publicClient, worldAbi } = useDevToolsContext();
const blockExplorer = publicClient.chain.blockExplorers?.default.url;
const hash = usePromise(write.result);
const transactionPromise = getTransaction(publicClient, write);
const transactionReceiptPromise = getTransactionReceipt(publicClient, write);
const transactionResultPromise = getTransactionResult(publicClient, worldAbi, write);
const transaction = usePromise(transactionPromise);
const transactionReceipt = usePromise(transactionReceiptPromise);
const transactionResult = usePromise(transactionResultPromise);
const isPending = hash.status === "pending" || transactionReceipt.status === "pending";
const isRevert =
hash.status === "rejected" ||
(transactionReceipt.status === "fulfilled" && transactionReceipt.value.status === "reverted");
// TODO: move all this into their getTransaction functions
const returnData = transactionResult.status === "fulfilled" ? transactionResult.value.result : null;
const events =
worldAbi && transactionReceipt.status === "fulfilled"
? transactionReceipt.value.logs
.map((log) => {
try {
return decodeEventLog({ abi: worldAbi, ...log });
} catch (error) {
// viem throws if there's no ABI for event, which can happen for events defined outside of MUD (e.g. custom worlds)
// TODO: show these logs anyway with a note that they couldn't be parsed
if (error instanceof AbiEventSignatureNotFoundError) {
return;
}
throw error;
}
})
.filter(isDefined)
: null;
return (
<details
onToggle={(event) => {
if (event.currentTarget.open) {
console.log("transaction", transaction);
console.log("transaction receipt", transactionReceipt);
console.log("transaction result", transactionResult);
}
}}
>
<summary
className={twMerge(
"px-2 py-1 rounded flex items-center gap-2 border-2 border-transparent border-dashed cursor-pointer",
isPending ? "border-white/20 cursor-default" : isRevert ? "bg-red-800" : "bg-slate-700"
)}
>
<div className="flex-1 font-mono text-white whitespace-nowrap overflow-hidden text-ellipsis">
{write.request.functionName}({write.request.args?.map((value) => serialize(value)).join(", ")})
</div>
{transactionReceipt.status === "fulfilled" ? (
<a
href={
blockExplorer ? `${blockExplorer}/block/${transactionReceipt.value.blockNumber.toString()}` : undefined
}
target="_blank"
className={twMerge(
"flex-none font-mono text-xs text-white/40",
blockExplorer ? "hover:text-white/60 hover:underline" : null
)}
title={transactionReceipt.value.blockNumber.toString()}
>
block {transactionReceipt.value.blockNumber.toString()}
</a>
) : null}
{hash.status === "fulfilled" ? (
<a
href={blockExplorer ? `${blockExplorer}/tx/${hash.value}` : undefined}
target="_blank"
className={twMerge(
"flex-none font-mono text-xs text-white/40",
blockExplorer ? "hover:text-white/60 hover:underline" : null
)}
title={hash.value}
>
tx {truncateHex(hash.value)}
</a>
) : null}
<div className="flex-none inline-flex w-4 h-4 justify-center items-center font-bold">
{isPending ? <PendingIcon /> : isRevert ? <>⚠</> : <>✓</>}
</div>
</summary>
<div className="p-2 space-y-1">
<div className="font-bold text-white/40 uppercase text-xs">Result</div>
{transactionResult.status === "fulfilled" ? (
<div className="font-mono">{serialize(returnData)}</div>
) : transactionResult.status === "rejected" ? (
<ErrorTrace error={transactionResult.reason} />
) : (
<PendingIcon />
)}
</div>
{events?.length ? (
<div className="p-2 space-y-1">
<div className="font-bold text-white/40 uppercase text-xs">Store events</div>
<table className="w-full table-fixed">
<thead className="bg-slate-800 text-amber-200/80 text-left">
<tr>
<th className="font-semibold uppercase text-xs w-3/12">table</th>
<th className="font-semibold uppercase text-xs w-[1em]"></th>
<th className="font-semibold uppercase text-xs w-3/12">key</th>
<th className="font-semibold uppercase text-xs">value</th>
</tr>
</thead>
<tbody className="font-mono text-xs">
{events.map(({ eventName, args }, i) => {
const table = hexToTableId((args as any).tableId);
return (
<tr key={i}>
<td className="whitespace-nowrap overflow-hidden text-ellipsis">
{table.namespace}:{table.name}
</td>
<td className="whitespace-nowrap">
{eventName === "StoreSetRecord" ? <span className="text-green-500 font-bold">=</span> : null}
{eventName === "StoreSetField" ? <span className="text-green-500 font-bold">+</span> : null}
{eventName === "StoreDeleteRecord" ? <span className="text-red-500 font-bold">-</span> : null}
</td>
<td className="whitespace-nowrap overflow-hidden text-ellipsis">
{hexKeyTupleToEntity((args as any).keyTuple)}
</td>
<td className="whitespace-nowrap overflow-hidden text-ellipsis">{(args as any).data}</td>
</tr>
);
})}
</tbody>
</table>
</div>
) : null}
</details>
);
}