Skip to content

Commit b2a52d9

Browse files
committed
[TOOL-2807] Nebula: Fix incorrect prepared transaction object, Fix sign_transaction action not shown properly on page reload (#5809)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on enhancing the `Chat` component's handling of transaction messages, introducing a new transaction data structure, and modifying related components to support this change. ### Detailed summary - Updated `role` in `types.ts` to include `"action"`. - Replaced `SendTransactionOption` with `NebulaTxData` in `chat.ts`. - Refactored message handling in `ChatPageContent.tsx` to support transaction messages. - Created `NebulaTxData` type with properties for transaction details. - Adjusted `Chats.tsx` to utilize `NebulaTxData` in various functions. - Modified `SendTransactionButton` to prepare and send transactions using the new data structure. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent fa26eef commit b2a52d9

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed

apps/dashboard/src/app/nebula-app/(app)/api/chat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NEXT_PUBLIC_NEBULA_URL } from "@/constants/env";
22
// TODO - copy the source of this library to dashboard
33
import { stream } from "fetch-event-stream";
4-
import type { SendTransactionOption } from "thirdweb/dist/types/wallets/interfaces/wallet";
4+
import type { NebulaTxData } from "../components/Chats";
55
import type { ExecuteConfig } from "./types";
66

77
export type ContextFilters = {
@@ -140,7 +140,7 @@ type ChatStreamedResponse =
140140
| {
141141
event: "action";
142142
type: "sign_transaction" & (string & {});
143-
data: SendTransactionOption;
143+
data: NebulaTxData;
144144
};
145145

146146
type ChatStreamedEvent =

apps/dashboard/src/app/nebula-app/(app)/api/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type SessionInfo = {
3535
created_at: string;
3636
deleted_at: string | null;
3737
history: Array<{
38-
role: "user" | "assistant"; // role: action is coming up
38+
role: "user" | "assistant" | "action";
3939
content: string;
4040
timestamp: number;
4141
}> | null;

apps/dashboard/src/app/nebula-app/(app)/components/ChatPageContent.tsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,44 @@ export function ChatPageContent(props: {
2626
const [userHasSubmittedMessage, setUserHasSubmittedMessage] = useState(false);
2727
const [messages, setMessages] = useState<Array<ChatMessage>>(() => {
2828
if (props.session?.history) {
29-
return props.session.history.map((message) => ({
30-
text: message.content,
31-
type: message.role,
32-
request_id: undefined,
33-
}));
29+
const _messages: ChatMessage[] = [];
30+
31+
for (const message of props.session.history) {
32+
if (message.role === "action") {
33+
try {
34+
const content = JSON.parse(message.content) as {
35+
session_id: string;
36+
request_id: string;
37+
data: string;
38+
type: "sign_transaction" | (string & {});
39+
};
40+
41+
if (content.type === "sign_transaction") {
42+
const txData = JSON.parse(content.data);
43+
if (
44+
typeof txData === "object" &&
45+
txData !== null &&
46+
txData.chainId
47+
) {
48+
_messages.push({
49+
type: "send_transaction",
50+
data: txData,
51+
});
52+
}
53+
}
54+
} catch {
55+
// ignore
56+
}
57+
} else {
58+
_messages.push({
59+
text: message.content,
60+
type: message.role,
61+
request_id: undefined,
62+
});
63+
}
64+
}
65+
66+
return _messages;
3467
}
3568
return [];
3669
});

apps/dashboard/src/app/nebula-app/(app)/components/Chats.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ import {
1515
} from "lucide-react";
1616
import { useEffect, useRef, useState } from "react";
1717
import { toast } from "sonner";
18-
import type { ThirdwebClient } from "thirdweb";
18+
import { type ThirdwebClient, prepareTransaction } from "thirdweb";
1919
import { useSendTransaction } from "thirdweb/react";
20-
import type { Account } from "thirdweb/wallets";
2120
import { TransactionButton } from "../../../../components/buttons/TransactionButton";
2221
import { MarkdownRenderer } from "../../../../components/contract-components/published-contract/markdown-renderer";
2322
import { useV5DashboardChain } from "../../../../lib/v5-adapter";
2423
import { submitFeedback } from "../api/feedback";
2524
import { NebulaIcon } from "../icons/NebulaIcon";
2625

27-
type SendTransactionOption = Parameters<Account["sendTransaction"]>[0];
26+
export type NebulaTxData = {
27+
chainId: number;
28+
data: `0x${string}`;
29+
to: string;
30+
value: string;
31+
};
2832

2933
export type ChatMessage =
3034
| {
@@ -39,7 +43,7 @@ export type ChatMessage =
3943
}
4044
| {
4145
type: "send_transaction";
42-
data: SendTransactionOption | null;
46+
data: NebulaTxData | null;
4347
};
4448

4549
export function Chats(props: {
@@ -203,7 +207,7 @@ export function Chats(props: {
203207
}
204208

205209
function ExecuteTransaction(props: {
206-
txData: SendTransactionOption | null;
210+
txData: NebulaTxData | null;
207211
twAccount: TWAccount;
208212
client: ThirdwebClient;
209213
}) {
@@ -319,7 +323,7 @@ function MessageActions(props: {
319323
}
320324

321325
function SendTransactionButton(props: {
322-
txData: SendTransactionOption;
326+
txData: NebulaTxData;
323327
twAccount: TWAccount;
324328
client: ThirdwebClient;
325329
}) {
@@ -333,14 +337,16 @@ function SendTransactionButton(props: {
333337
transactionCount={1}
334338
txChainID={txData.chainId}
335339
onClick={() => {
336-
const promise = sendTransaction.mutateAsync({
337-
...props.txData,
338-
nonce: Number(txData.nonce),
339-
to: txData.to || undefined, // Get rid of the potential null value
340+
const tx = prepareTransaction({
340341
chain: chain,
341342
client: props.client,
343+
data: txData.data,
344+
to: txData.to,
345+
value: BigInt(txData.value),
342346
});
343347

348+
const promise = sendTransaction.mutateAsync(tx);
349+
344350
toast.promise(promise, {
345351
success: "Transaction sent successfully",
346352
error: "Failed to send transaction",

0 commit comments

Comments
 (0)