Docs Icon ChevronRight For Developers Icon ChevronRight Sending a Transaction

Sending a Transaction

With access to the user account and the connection authorized, you can send a transaction using sendTransaction().

SendTransaction.tsx Icon Link
1const account = await fuel.currentAccount();
2if (!account) {
3  throw new Error("Current account not authorized for this connection!");
4}
5
6const wallet = await fuel.getWallet(account);
7
8// The amount of coins to transfer.
9const amount = bn(1);
10
11// Create a transaction request using wallet helper
12const transactionRequest = await wallet.createTransfer(destination, amount);
13
14// Broadcast the transaction to the network
15const transactionId = await fuel.sendTransaction(account, transactionRequest);
16
17console.log("Transaction ID", transactionId);

With React

In a React app, once the connection is established, you can use the useSendTransaction() to send a transaction.

SendTransactionHook.tsx Icon Link
1const { wallet } = useWallet();
2const { sendTransaction, data, isPending, error } = useSendTransaction();
3
4async function handleSendTransaction(destination: string) {
5  if (!wallet) {
6    throw new Error("Current wallet is not authorized for this connection!");
7  }
8
9  // The amount of coins to transfer.
10  const amount = bn(1);
11
12  // Create a transaction request using wallet helper
13  const transactionRequest = await wallet.createTransfer(destination, amount);
14
15  // Broadcast the transaction to the network
16  sendTransaction({
17    address: wallet.address, // The address to sign the transaction (a connected wallet)
18    transaction: transactionRequest, // The transaction to send
19  });
20}

See the source file Icon Link