Solana Interactions
Send a Versioned Solana Transaction
// for external wallets only, use IEmbeddedWalletSolanaSigner for Solana embedded wallets
import { ISolana } from '@dynamic-labs/solana';
const { primaryWallet } = useDynamicContext();
import { Connection } from "@solana/web3.js";
const connection = await (
primaryWallet as any
).connector.getPublicClient<Connection | undefined>()
if (!connection) return;
const fromKey = new PublicKey(primaryWallet.address);
const toKey = new PublicKey(address);
const amountInLamports = Number(amount) * 1000000000;
const instructions = [
SystemProgram.transfer({
fromPubkey: fromKey,
lamports: amountInLamports,
toPubkey: toKey,
}),
];
const blockhash = await connection.getLatestBlockhash();
// create v0 compatible message
const messageV0 = new TransactionMessage({
instructions,
payerKey: fromKey,
recentBlockhash: blockhash.blockhash,
}).compileToV0Message();
const transferTransaction = new VersionedTransaction(messageV0);
const signer = await (primaryWallet as any).connector.getSigner<ISolana>()
await signer
.signAndSendTransaction(transferTransaction)
.then((res: any) => {
console.log(
`Transaction successful: https://solscan.io/tx/${res.signature}?cluster=devnet`,
);
})
.catch((reason: any) => {
console.error(reason);
});
Was this page helpful?