The @dynamic-labs/solana-extension integrates your application with the @solana/web3.js library, providing a Connection and Signer to interact with the Solana blockchain.

Installation

To install the Solana extension, run the following command:
npm install @dynamic-labs/solana-extension

Setup

Incorporate the Solana extension into your client using the extend method to add getSigner and getConnection methods:
import { createClient } from '@dynamic-labs/client'
import { SolanaExtension } from '@dynamic-labs/solana-extension'

/**
 * Creates and extends the client with Solana blockchain functionality.
 */
export const dynamicClient = createClient({
  environmentId: 'YOUR-ENVIRONMENT-ID',
}).extend(SolanaExtension())
Also please refer to the Polyfills section for information on how to set up necessary polyfills.

Usage

After setup, you can interact with the Solana blockchain. Below is an example of a component that sends 1 SOL to a specified wallet address:
import { Button } from 'react-native'
import { FC } from 'react'
import { dynamicClient } from '<path to client file>'

import {
  LAMPORTS_PER_SOL,
  PublicKey,
  SystemProgram,
  TransactionMessage,
  VersionedTransaction,
} from '@solana/web3.js'

interface Send1SolButtonProps {
  destinationAddress: string
}

/**
 * Renders a button that sends 1 SOL to a given address.
 */
const Send1SolButton: FC<Send1SolButtonProps> = ({ destinationAddress }) => {
  const send = async () => {
    const wallet = dynamicClient.wallets.primary

    if (!wallet) return

    const connection = dynamicClient.solana.getConnection({
      commitment: 'singleGossip',
    })
    const signer = dynamicClient.solana.getSigner({ wallet })

    const { blockhash } = await connection.getLatestBlockhash()
    const amountInLamports = 1 * LAMPORTS_PER_SOL
    const fromKey = new PublicKey(wallet.address)
    const toKey = new PublicKey(destinationAddress)

    const instructions = [
      SystemProgram.transfer({
        fromPubkey: fromKey,
        lamports: amountInLamports,
        toPubkey: toKey,
      }),
    ]

    const messageV0 = new TransactionMessage({
      instructions,
      payerKey: fromKey,
      recentBlockhash: blockhash,
    }).compileToV0Message()

    const transaction = new VersionedTransaction(messageV0)
    const { signature } = await signer.signAndSendTransaction(transaction)

    console.log('Successful transaction signature:', signature)
  }

  return <Button title="Send" onPress={send} />
}