-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathsignTransaction.tsx
More file actions
55 lines (48 loc) · 1.72 KB
/
signTransaction.tsx
File metadata and controls
55 lines (48 loc) · 1.72 KB
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
47
48
49
50
51
52
53
54
55
import { FormEvent } from "react";
import { useSolanaWallet, useSignTransaction } from "@web3auth/modal/react/solana";
import { LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction } from "@solana/web3.js";
export function SignTransaction() {
const { data: signedTransaction, error, loading: isPending, signTransaction,
} = useSignTransaction();
const { accounts, connection } = useSolanaWallet();
async function submit(e: FormEvent<HTMLFormElement>) {
e.preventDefault()
const formData = new FormData(e.target as HTMLFormElement)
const to = formData.get('address') as string
const value = formData.get('value') as string
const block = await connection!.getLatestBlockhash();
const TransactionInstruction = SystemProgram.transfer({
fromPubkey: new PublicKey(accounts![0]),
toPubkey: new PublicKey(to),
lamports: Number(value) * LAMPORTS_PER_SOL,
});
const transaction = new Transaction({
blockhash: block.blockhash,
lastValidBlockHeight: block.lastValidBlockHeight,
feePayer: new PublicKey(accounts![0]),
}).add(TransactionInstruction);
signTransaction(transaction);
}
return (
<div>
<h2>Sign Transaction</h2>
<form onSubmit={submit}>
<input name="address" placeholder="Address" required />
<input
name="value"
placeholder="Amount (SOL)"
type="number"
step="0.01"
required
/>
<button disabled={isPending} type="submit" >
{isPending ? 'Signing...' : 'Sign'}
</button>
</form>
{signedTransaction && <div>Signed Transaction: {signedTransaction}</div>}
{error && (
<div>Error: {error.message}</div>
)}
</div>
)
}