@@ -3,7 +3,6 @@ import { ScrollShadow } from "@/components/ui/ScrollShadow/ScrollShadow";
33import { Spinner } from "@/components/ui/Spinner/Spinner" ;
44import { Alert , AlertTitle } from "@/components/ui/alert" ;
55import { Button } from "@/components/ui/button" ;
6- import { getThirdwebClient } from "@/constants/thirdweb.server" ;
76import { cn } from "@/lib/utils" ;
87import type { Account as TWAccount } from "@3rdweb-sdk/react/hooks/useApi" ;
98import { useMutation } from "@tanstack/react-query" ;
@@ -17,8 +16,7 @@ import {
1716import { useEffect , useRef , useState } from "react" ;
1817import { toast } from "sonner" ;
1918import type { ThirdwebClient } from "thirdweb" ;
20- import { sendTransaction } from "thirdweb" ;
21- import { useActiveAccount } from "thirdweb/react" ;
19+ import { useSendTransaction } from "thirdweb/react" ;
2220import type { Account } from "thirdweb/wallets" ;
2321import { TransactionButton } from "../../../../components/buttons/TransactionButton" ;
2422import { MarkdownRenderer } from "../../../../components/contract-components/published-contract/markdown-renderer" ;
@@ -170,9 +168,10 @@ export function Chats(props: {
170168 { message . text }
171169 </ span >
172170 ) : message . type === "send_transaction" ? (
173- < SendTransactionButton
171+ < ExecuteTransaction
174172 txData = { message . data }
175173 twAccount = { props . twAccount }
174+ client = { props . client }
176175 />
177176 ) : (
178177 < span className = "leading-loose" > { message . text } </ span >
@@ -203,6 +202,29 @@ export function Chats(props: {
203202 ) ;
204203}
205204
205+ function ExecuteTransaction ( props : {
206+ txData : SendTransactionOption | null ;
207+ twAccount : TWAccount ;
208+ client : ThirdwebClient ;
209+ } ) {
210+ if ( ! props . txData ) {
211+ return (
212+ < Alert variant = "destructive" >
213+ < AlertCircleIcon className = "size-5" />
214+ < AlertTitle > Failed to parse transaction data</ AlertTitle >
215+ </ Alert >
216+ ) ;
217+ }
218+
219+ return (
220+ < SendTransactionButton
221+ txData = { props . txData }
222+ twAccount = { props . twAccount }
223+ client = { props . client }
224+ />
225+ ) ;
226+ }
227+
206228function MessageActions ( props : {
207229 authToken : string ;
208230 requestId : string ;
@@ -297,51 +319,28 @@ function MessageActions(props: {
297319}
298320
299321function SendTransactionButton ( props : {
300- txData : SendTransactionOption | null ;
322+ txData : SendTransactionOption ;
301323 twAccount : TWAccount ;
324+ client : ThirdwebClient ;
302325} ) {
303- const account = useActiveAccount ( ) ;
304- const chain = useV5DashboardChain ( props . txData ?. chainId ) ;
305-
306- const sendTxMutation = useMutation ( {
307- mutationFn : ( ) => {
308- if ( ! account ) {
309- throw new Error ( "No active account" ) ;
310- }
311-
312- if ( ! props . txData || ! chain ) {
313- throw new Error ( "Invalid transaction" ) ;
314- }
315-
316- return sendTransaction ( {
317- account,
318- transaction : {
319- ...props . txData ,
320- nonce : Number ( props . txData . nonce ) ,
321- to : props . txData . to || undefined , // Get rid of the potential null value
322- chain,
323- client : getThirdwebClient ( ) ,
324- } ,
325- } ) ;
326- } ,
327- } ) ;
328-
329- if ( ! props . txData ) {
330- return (
331- < Alert variant = "destructive" >
332- < AlertCircleIcon className = "size-5" />
333- < AlertTitle > Failed to parse transaction data</ AlertTitle >
334- </ Alert >
335- ) ;
336- }
326+ const { txData } = props ;
327+ const sendTransaction = useSendTransaction ( ) ;
328+ const chain = useV5DashboardChain ( txData . chainId ) ;
337329
338330 return (
339331 < TransactionButton
340- isPending = { sendTxMutation . isPending }
332+ isPending = { sendTransaction . isPending }
341333 transactionCount = { 1 }
342- txChainID = { props . txData . chainId }
334+ txChainID = { txData . chainId }
343335 onClick = { ( ) => {
344- const promise = sendTxMutation . mutateAsync ( ) ;
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+ chain : chain ,
341+ client : props . client ,
342+ } ) ;
343+
345344 toast . promise ( promise , {
346345 success : "Transaction sent successfully" ,
347346 error : "Failed to send transaction" ,
0 commit comments