Sending SOL is the most common transaction on the Solana blockchain. This recipe walks you through creating and sending SOL transfer transactions using @solana/web3.js with Privy wallets.
Before following this recipe, make sure you have configured Privy for Solana in your app.

Overview

This recipe demonstrates how to:
  • Create a SOL transfer transaction using @solana/web3.js
  • Sign and send the transaction using Privy wallets

Prerequisites

Install the required dependencies:
bash npm install @solana/web3.js

1. Create the SOL transfer transaction

Create a SOL transfer transaction using your preferred language:
import {Connection, PublicKey, SystemProgram, Transaction, LAMPORTS_PER_SOL} from '@solana/web3.js';

const createSOLTransferTransaction = async (
  fromAddress: string,
  toAddress: string,
  amount: number // Amount in SOL
) => {
  // Set up connection to Solana network
  const connection = new Connection('https://api.devnet.solana.com', 'confirmed');

  // Create public key objects
  const fromPubkey = new PublicKey(fromAddress);
  const toPubkey = new PublicKey(toAddress);

  // Convert SOL to lamports (1 SOL = 1,000,000,000 lamports)
  const lamports = amount * LAMPORTS_PER_SOL;

  // Create transfer instruction
  const transferInstruction = SystemProgram.transfer({
    fromPubkey,
    toPubkey,
    lamports
  });

  // Create transaction and add instruction
  const transaction = new Transaction().add(transferInstruction);

  // Get recent blockhash
  const {blockhash} = await connection.getLatestBlockhash();
  transaction.recentBlockhash = blockhash;
  transaction.feePayer = fromPubkey;

  return {transaction, connection};
};

2. Send the transaction

You can send the transaction using Privy’s different SDKs. Below are examples for React, React Native, NodeJS, and Python:
import {useSendTransaction, useSolanaWallets} from '@privy-io/react-auth/solana';

const {wallets} = useSolanaWallets();
const {sendTransaction} = useSendTransaction();

const {transaction, connection} = await createSOLTransferTransaction(
  wallets[0].address, // fromAddress
  'recipient-wallet-address', // toAddress
  0.01 // amount in SOL
);

// Assuming you have a transaction created from the previous step
const signature = await sendTransaction({
  transaction, // from createSOLTransferTransaction
  address: wallets[0].address,
  connection // from createSOLTransferTransaction
});
You’ve successfully sent SOL!

Next steps

Now that you can send SOL, you might want to explore: