- React
- React Native
- JavaScript
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;
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
}