-
Notifications
You must be signed in to change notification settings - Fork 958
/
SendTransaction.tsx
46 lines (39 loc) · 1.6 KB
/
SendTransaction.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Button } from '@material-ui/core';
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { Keypair, SystemProgram, Transaction, TransactionSignature } from '@solana/web3.js';
import React, { FC, useCallback } from 'react';
import { useNotify } from './notify';
const SendTransaction: FC = () => {
const { connection } = useConnection();
const { publicKey, sendTransaction } = useWallet();
const notify = useNotify();
const onClick = useCallback(async () => {
if (!publicKey) {
notify('error', 'Wallet not connected!');
return;
}
let signature: TransactionSignature = '';
try {
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: Keypair.generate().publicKey,
lamports: 1,
})
);
signature = await sendTransaction(transaction, connection);
notify('info', 'Transaction sent:', signature);
await connection.confirmTransaction(signature, 'processed');
notify('success', 'Transaction successful!', signature);
} catch (error: any) {
notify('error', `Transaction failed! ${error?.message}`, signature);
return;
}
}, [publicKey, notify, connection, sendTransaction]);
return (
<Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey}>
Send Transaction (devnet)
</Button>
);
};
export default SendTransaction;