| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import { Button } from '@mui/material';
- import { useConnection, useWallet } from '@solana/wallet-adapter-react';
- import type { TransactionSignature } from '@solana/web3.js';
- import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
- import React, { useCallback, type FC } from 'react';
- import { useNotify } from './useNotify';
- export const SendTransaction: FC = () => {
- const { connection } = useConnection();
- const { publicKey, sendTransaction } = useWallet();
- const notify = useNotify();
- const onClick = useCallback(async () => {
- let signature: TransactionSignature | undefined = undefined;
- try {
- if (!publicKey) throw new Error('Wallet not connected!');
- const {
- context: { slot: minContextSlot },
- value: { blockhash, lastValidBlockHeight },
- } = await connection.getLatestBlockhashAndContext();
- const transaction = new Transaction({
- feePayer: publicKey,
- recentBlockhash: blockhash,
- }).add(
- new TransactionInstruction({
- data: Buffer.from('Hello, from the Solana Wallet Adapter example app!'),
- keys: [],
- programId: new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'),
- })
- );
- signature = await sendTransaction(transaction, connection, { minContextSlot });
- notify('info', 'Transaction sent:', signature);
- await connection.confirmTransaction({ blockhash, lastValidBlockHeight, signature });
- notify('success', 'Transaction successful!', signature);
- } catch (error: any) {
- notify('error', `Transaction failed! ${error?.message}`, signature);
- }
- }, [publicKey, connection, sendTransaction, notify]);
- return (
- <Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey}>
- Send Transaction (devnet)
- </Button>
- );
- };
|