With access to the user account and the connection authorized, you can send a transaction using sendTransaction()
.
const account = await fuel.currentAccount();
if (!account) {
throw new Error("Current account not authorized for this connection!");
}
const wallet = await fuel.getWallet(account);
// The amount of coins to transfer.
const amount = bn(1);
// Create a transaction request using wallet helper
const transactionRequest = await wallet.createTransfer(destination, amount);
// Broadcast the transaction to the network
const transactionId = await fuel.sendTransaction(account, transactionRequest);
console.log("Transaction ID", transactionId);
In a React app, once the connection is established, you can use the useSendTransaction()
to send a transaction.
const { wallet } = useWallet();
const { sendTransaction, data, isPending, error } = useSendTransaction();
async function handleSendTransaction(destination: string) {
if (!wallet) {
throw new Error("Current wallet is not authorized for this connection!");
}
// The amount of coins to transfer.
const amount = bn(1);
// Create a transaction request using wallet helper
const transactionRequest = await wallet.createTransfer(destination, amount);
// Broadcast the transaction to the network
sendTransaction({
address: wallet.address, // The address to sign the transaction (a connected wallet)
transaction: transactionRequest, // The transaction to send
});
}