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@alpha

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());

Polyfils

Your React Native project might require polyfills for certain global objects. Install the necessary polyfills and set them up as follows

Installation

npm install text-encoding react-native-get-random-values buffer

Include the following code in your project’s entry point to set up the polyfills:

import { Buffer } from 'buffer';
import 'react-native-get-random-values';

global.TextEncoder = require('text-encoding').TextEncoder;
global.Buffer = Buffer;

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();
    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} />;
};