useSendTransaction
Description
Returns
Example
import { useSendTransaction } from "@cometh/connect-react-hooks";
import { useState } from "react";
import { parseEther, Address } from "viem";
export const TransactionSender = () => {
const { sendTransaction, sendTransactionAsync, isLoading, isError, error, isSuccess, data } = useSendTransaction();
const [recipient, setRecipient] = useState<Address>();
const [amount, setAmount] = useState<string>("0");
const handleSendTransaction = () => {
if (recipient) {
sendTransaction({
calls: {
to: recipient,
value: parseEther(amount),
data: "0x",
}
});
}
};
const handleSendBatchTransactions = async () => {
if (recipient) {
try {
const hash = await sendTransactionAsync({
calls: [
{
to: recipient,
value: parseEther(amount),
data: "0x",
},
{
to: recipient,
value: parseEther((Number(amount) * 2).toString()),
data: "0x",
}
]
});
console.log("Batch transactions sent! Hash:", hash);
} catch (error) {
console.error("Error sending batch transactions:", error);
}
}
};
return (
<div>
<input
placeholder="Recipient address"
onChange={(e) => setRecipient(e.target.value as Address)}
/>
<input
type="number"
placeholder="Amount in ETH"
onChange={(e) => setAmount(e.target.value)}
/>
<button onClick={handleSendTransaction} disabled={isLoading}>
Send Transaction
</button>
<button onClick={handleSendBatchTransactions} disabled={isLoading}>
Send Batch Transactions
</button>
{isError && <p>Error: {error.message}</p>}
{isSuccess && <p>Transaction sent! Hash: {data}</p>}
</div>
);
};Last updated