React provides the
useDynamicContext
hook with Solana wallet methods to send legacy transactions using the SystemProgram transfer instruction.React
Copy
Ask AI
import React, { useState } from 'react';
import type { ISolana } from '@dynamic-labs/solana-core';
import { isSolanaWallet } from '@dynamic-labs/solana-core';
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import type { Connection } from '@solana/web3.js';
import { PublicKey, SystemProgram, Transaction } from '@solana/web3.js';
function LegacyTransaction() {
const [address, setAddress] = useState('');
const [amount, setAmount] = useState('');
const { primaryWallet } = useDynamicContext();
const sendTransaction = async () => {
if(!primaryWallet || !isSolanaWallet(primaryWallet)) {
return;
}
const connection: Connection = await primaryWallet.getConnection();
const cluster = connection.rpcEndpoint.includes('devnet') ? 'devnet' : 'mainnet';
const fromKey = new PublicKey(primaryWallet.address);
const toKey = new PublicKey(address);
const amountInLamports = Number(amount) * 1000000000;
const transferTransaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: fromKey,
lamports: amountInLamports,
toPubkey: toKey,
}),
);
const blockhash = await connection.getLatestBlockhash();
transferTransaction.recentBlockhash = blockhash.blockhash;
transferTransaction.feePayer = fromKey;
const signer: ISolana = await primaryWallet.getSigner();
await signer
.signAndSendTransaction(transferTransaction)
.then((value: { signature: string }) => {
console.log(
`Transaction successful: https://solscan.io/tx/${value.signature}?cluster=${cluster}`,
);
})
.catch((error: Error) => {
console.error(error);
});
}
return (
<div>
<h1>Legacy Transaction</h1>
<input type="text" placeholder="Address" value={address} onChange={(e) => setAddress(e.target.value)} />
<input type="text" placeholder="Amount" value={amount} onChange={(e) => setAmount(e.target.value)} />
<button type="button" onClick={() => sendTransaction()}>Send Transaction</button>
</div>
)
}
export default LegacyTransaction;
</Tab>
<Tab title="React Native">
React Native supports Solana via the Solana extension with `getConnection` and `getSigner`.
```ts React Native
import { dynamicClient } from '<path to client file>'
import { PublicKey, SystemProgram, Transaction } from '@solana/web3.js'
const wallet = dynamicClient.wallets.primary
if (!wallet) {
// user is not connected yet
} else {
const connection = dynamicClient.solana.getConnection()
const signer = dynamicClient.solana.getSigner({ wallet })
const fromKey = new PublicKey(wallet.address)
const toKey = new PublicKey('<destinationAddress>')
const amountInLamports = 1_000_000_000 // 1 SOL
const tx = new Transaction().add(
SystemProgram.transfer({ fromPubkey: fromKey, toPubkey: toKey, lamports: amountInLamports })
)
const { blockhash } = await connection.getLatestBlockhash()
tx.recentBlockhash = blockhash
tx.feePayer = fromKey
const { signature } = await signer.signAndSendTransaction(tx)
console.log('Successful transaction signature:', signature)
}