- React
- React Native
- JavaScript
- Flutter
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;
React Native
Copy
Ask AI
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)
}
Copy
Ask AI
import { getWalletAccounts, getActiveNetworkData } from '@dynamic-labs-sdk/client';
import { getSolanaConnection, isSolanaWalletAccount, signAndSendTransaction } from '@dynamic-labs-sdk/solana';
import { LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction } from '@solana/web3.js';
/**
* Sends a legacy SOL transfer using the connected Solana wallet account.
* - Uses only Dynamic JavaScript SDK helpers and @solana/web3.js
*/
async function sendLegacySolanaTransaction({ toAddress, amountSol }) {
// 1) Choose a connected Solana wallet account
const walletAccounts = getWalletAccounts();
const walletAccount = walletAccounts.find(isSolanaWalletAccount);
if (!walletAccount) {
throw new Error('No connected Solana wallet account found.');
}
// 2) Create a Connection from the active network for this wallet
const { networkData } = await getActiveNetworkData({ walletAccount });
const connection = getSolanaConnection({ networkData });
// 3) Build a legacy transfer transaction
const fromAddress = walletAccount.accountAddress;
const fromPubkey = new PublicKey(fromAddress);
const toPubkey = new PublicKey(toAddress);
const lamports = Math.round(Number(amountSol) * LAMPORTS_PER_SOL);
const tx = new Transaction().add(
SystemProgram.transfer({
fromPubkey,
toPubkey,
lamports,
})
);
const { blockhash } = await connection.getLatestBlockhash();
tx.recentBlockhash = blockhash;
tx.feePayer = fromPubkey;
// 4) Sign and send the transaction
const { signature } = await signAndSendTransaction({ walletAccount, transaction: tx });
console.log('Successful transaction signature:', signature);
}
// Example:
// await sendLegacySolanaTransaction({
// toAddress: '<DESTINATION_PUBLIC_KEY>',
// amountSol: 0.01,
// });
Flutter doesn’t support sending the legacy TX to Solana yet.
Simulate a Transaction
Before sending a transaction, you can simulate it to preview the effects and validate that it will succeed. Transaction simulation shows all asset transfers involved in the transaction.Transaction simulation uses the currently selected primary wallet. Make sure the user has a wallet connected before attempting to simulate a transaction.
- React Native
React Native
Copy
Ask AI
import { dynamicClient } from '<path to client file>'
import { PublicKey, SystemProgram, Transaction, LAMPORTS_PER_SOL } from '@solana/web3.js'
const simulateSolanaLegacyTransaction = async (to: string, amountSol: number) => {
const connection = dynamicClient.solana.getConnection()
const fromKey = new PublicKey(dynamicClient.wallets.primary.address)
const toKey = new PublicKey(to)
const amountInLamports = Math.round(amountSol * LAMPORTS_PER_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 simulationResult = await dynamicClient.solana.simulateSVMTransaction({
transaction: tx,
type: 'SignTransaction',
})
console.log('Simulation result:', simulationResult)
return simulationResult
}