@dynamic-labs/solana-extension
@solana/web3.js
Connection
Signer
npm install @dynamic-labs/solana-extension@alpha
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())
npm install text-encoding react-native-get-random-values buffer
import { Buffer } from 'buffer' import 'react-native-get-random-values' global.TextEncoder = require('text-encoding').TextEncoder global.Buffer = Buffer
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} /> }
Was this page helpful?