> ## Documentation Index
> Fetch the complete documentation index at: https://docs.solanamobile.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Building transactions

> A client interacts with the Solana network by submitting a _transaction_ to the cluster. Transactions allow a client to invoke instructions of on-chain [_Programs_](https://docs.solana.com/developing/intro-programs).

export const FooterDisclaimer = () => {
  return <p className="not-prose mt-16 text-center text-xs text-gray-500 dark:text-gray-400">
      Code samples on this page are subject to the{" "}
      <a className="underline underline-offset-2" href="https://www.apache.org/licenses/LICENSE-2.0">
        Apache 2.0 license
      </a>
      .
    </p>;
};

For a full explanation, see the core docs overview of a [*transaction*](https://docs.solana.com/developing/programming-model/transactions).

## Add dependencies

The [@solana/web3.js](https://github.com/solana-labs/solana-web3.js) library provides convenient classes and Solana primitive types to build transactions.

<CodeGroup>
  ```bash yarn theme={null}
  yarn install @solana/web3.js
  ```

  ```bash npm theme={null}
  npm install @solana/web3.js
  ```
</CodeGroup>

### Add polyfills

After installing, ensure you have also added these [polyfills](/react-native/polyfill-guides/web3-js) to your React native app.
These are needed by some parts of `@solana/web3.js` because it is originally written as a web/node library and, as a result, certain expected APIs are missing in a React Native environment.

## Example: SOL Transfer Transaction

In the following example, we create a *transaction* that invokes the [System Program](https://docs.solana.com/developing/runtime-facilities/programs#system-program)'s *transfer* instruction to send SOL to an address.

A *[transaction instruction](https://docs.solana.com/developing/programming-model/transactions#instructions)* is comprised of a program id, a list of accounts, and instruction data specific to the program.

<Tabs>
  <Tab title="Versioned Transactions">
    A [versioned transaction](https://docs.solana.com/developing/versioned-transactions) is a new format for transactions recommended for use by clients.

    As an example, we'll be invoking the *transfer* instruction from the *System Program*. Use the `SystemProgram` factory class
    to conveniently generate the *transfer* instruction.

    ```tsx expandable theme={null}
    import {
      Connection,
      PublicKey,
      VersionedTransaction,
      SystemProgram,
    } from "@solana/web3.js";

    // Create a list of Program instructions to execute.
    const instructions = [
      SystemProgram.transfer({
        fromPubkey: fromPublicKey,
        toPubkey: toPublicKey,
        lamports: 1_000_000,
      }),
    ];

    // Connect to an RPC endpoint and get the latest blockhash, to include in
    // the transaction.
    const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
    const latestBlockhash = await connection.getLatestBlockhash();

    // Create the "message" of a transaction and compile to `V0Message` format.
    const txMessage = new TransactionMessage({
      payerKey: fromPublicKey,
      recentBlockhash: latestBlockhash.blockhash,
      instructions,
    }).compileToV0Message();

    // Construct the Versioned Transaction passing in the message.
    const versionedTransaction = new VersionedTransaction(txMessage);
    ```
  </Tab>

  <Tab title="Legacy Transactions">
    For backwards compatiblity, you can still construct legacy transactions with `@solana/web3.js`.

    ```tsx expandable theme={null}
    import {
      Connection,
      PublicKey,
      Transaction,
      SystemProgram,
    } from "@solana/web3.js";

    const latestBlockhash = await connection.getLatestBlockhash();
    const randomTransferTransaction = new Transaction({
      ...latestBlockhash,
      feePayer: fromPublicKey,
    }).add(
      SystemProgram.transfer({
        fromPubkey: fromPublicKey,
        toPubkey: toPublicKey,
        lamports: 1_000,
      }),
    );
    ```
  </Tab>
</Tabs>

## Send a Transaction

After a transaction is signed by the appropriate accounts, it can be submitted to the Solana network via RPC. See the
next guide, *Using Mobile Walelt Adapter* to learn how to sign transactions.

<Tabs>
  <Tab title="Versioned Transactions">
    ```tsx expandable theme={null}
    import { transact } from "@solana-mobile/mobile-wallet-adapter-protocol-web3js";
    import {
      sendTransaction,
      clusterApiUrl,
      Connection,
      VersionedTransaction,
      confirmTransaction,
    } from "@solana/web3.js";

    const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
    const unsignedTx = new VersionedTransaction(/* ... */);
    const signedTx: VersionedTransaction = await transact((wallet) => {
      /* ...sign `unsignedTx` with Mobile Wallet Adapter... */
    });

    // After sending, a transaction signature is returned.
    const txSignature = await connection.sendTransaction(signedTx);

    // Confirm the transaction was successful.
    const confirmationResult = await connection.confirmTransaction(
      txSignature,
      "confirmed",
    );

    if (confirmationResult.value.err) {
      throw new Error(JSON.stringify(confirmationResult.value.err));
    } else {
      console.log("Transaction successfully submitted!");
    }
    ```
  </Tab>

  <Tab title="Legacy Transactions">
    ```tsx expandable theme={null}
    import { transact } from "@solana-mobile/mobile-wallet-adapter-protocol-web3js";
    import {
      sendTransaction,
      clusterApiUrl,
      Connection,
      Transaction,
      confirmTransaction,
    } from "@solana/web3.js";

    const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
    const signedTx: Transaction = await transact((wallet) => {
      /* ...signing code from above... */
    });

    // After sending, a transaction signature is returned.
    const txSignature = await sendTransaction(signedTx, connection);

    // Confirm the transaction was successful.
    const confirmationResult = await connection.confirmTransaction(
      txSignature,
      "confirmed",
    );

    if (confirmationResult.value.err) {
      throw new Error(JSON.stringify(confirmationResult.value.err));
    } else {
      console.log("Transaction successfully submitted!");
    }
    ```
  </Tab>
</Tabs>

## Next steps

* Read the following *Using Mobile Wallet Adapter* guide to learn how to sign these transactions and submit them to the Solana network.
* See the [Anchor Integration guide](/recipes/solana-development/anchor-integration) to learn how to create and create transactions and invoke instructions from Anchor programs.

<FooterDisclaimer />
