export const sendSolFromImportedWallet = async ({
walletAddress,
toAddress,
amount,
}: {
walletAddress: string;
toAddress: string;
amount: number; // Amount in SOL
}) => {
const svmClient = await authenticatedSvmClient();
const connection = new Connection('https://api.mainnet-beta.solana.com', 'confirmed');
// Check balance first
const publicKey = new PublicKey(walletAddress);
const balance = await connection.getBalance(publicKey);
const amountLamports = LAMPORTS_PER_SOL * amount;
if (balance < amountLamports) {
throw new Error('Insufficient balance');
}
// Create transaction
const fromPubkey = new PublicKey(walletAddress);
const toPubkey = new PublicKey(toAddress);
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey,
toPubkey,
lamports: amountLamports,
})
);
// Sign transaction
const signedTransaction = await svmClient.signTransaction({
senderAddress: walletAddress,
transaction,
});
// Send transaction
const txHash = await connection.sendRawTransaction(signedTransaction.serialize());
await connection.confirmTransaction(txHash);
return txHash;
};
// Usage
const txHash = await sendSolFromImportedWallet({
walletAddress: 'YourImportedSolanaWalletAddress',
toAddress: 'RecipientAddress',
amount: 0.001,
});
console.log('SOL sent:', txHash);