"use client";
import { useThirdwebSmartAccount } from "@getpara/react-sdk";
import { useMutation } from "@tanstack/react-query";
import { sepolia } from "viem/chains";
import { parseEther } from "viem";
const THIRDWEB_CLIENT_ID = process.env.NEXT_PUBLIC_THIRDWEB_CLIENT_ID!;
export function ThirdwebDemo() {
const { smartAccount, isLoading, error } = useThirdwebSmartAccount({
clientId: THIRDWEB_CLIENT_ID,
chain: sepolia,
sponsorGas: true, // enable gas sponsorship (default)
mode: "4337", // or "7702"
});
const {
mutate: sendTransaction,
isPending: isSending,
error: sendError,
} = useMutation({
mutationFn: async () => {
if (!smartAccount) throw new Error("Smart account not ready");
return smartAccount.sendTransaction({
to: "0xRecipient",
value: parseEther("0.01"),
});
},
onSuccess: (receipt) => {
console.log("Transaction hash:", receipt.transactionHash);
},
});
const {
mutate: sendBatch,
isPending: isBatching,
error: batchError,
} = useMutation({
mutationFn: async () => {
if (!smartAccount) throw new Error("Smart account not ready");
return smartAccount.sendBatchTransaction([
{ to: "0xRecipientA", value: parseEther("0.01") },
{ to: "0xRecipientB", data: "0xencodedCallData" },
]);
},
onSuccess: (receipt) => {
console.log("Batch tx hash:", receipt.transactionHash);
},
});
if (isLoading) return <p>Setting up smart account...</p>;
if (error) return <p>Error: {error.message}</p>;
if (!smartAccount) return null;
return (
<div>
<h2>thirdweb Smart Account</h2>
<p>Address: {smartAccount.smartAccountAddress}</p>
<p>Mode: {smartAccount.mode}</p>
<button onClick={() => sendTransaction()} disabled={isSending}>
{isSending ? "Sending..." : "Send Transaction"}
</button>
{sendError && <p style={{ color: "red" }}>{sendError.message}</p>}
<button onClick={() => sendBatch()} disabled={isBatching}>
{isBatching ? "Batching..." : "Send Batch"}
</button>
{batchError && <p style={{ color: "red" }}>{batchError.message}</p>}
</div>
);
}