1
1
import React , { useState } from 'react' ;
2
2
import { BrowserLink , ConnectButton , Connector , useAccount } from '@ant-design/web3' ;
3
3
import { useConnection , useWallet } from '@ant-design/web3-solana' ;
4
- import { PublicKey , Transaction , TransactionInstruction } from '@solana/web3.js' ;
5
- import { Button , Card , ConfigProvider , Flex , Form , Input , message , Space } from 'antd' ;
4
+ import {
5
+ LAMPORTS_PER_SOL ,
6
+ PublicKey ,
7
+ SystemProgram ,
8
+ Transaction ,
9
+ TransactionInstruction ,
10
+ } from '@solana/web3.js' ;
11
+ import { Button , Card , ConfigProvider , Flex , Form , Input , InputNumber , message , Space } from 'antd' ;
12
+
13
+ type FormModel = {
14
+ memo : string ;
15
+ amount : number ;
16
+ } ;
6
17
7
18
const MEMO_PROGRAM_ID = new PublicKey ( 'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr' ) ;
8
19
@@ -14,30 +25,37 @@ export default function MemoTx() {
14
25
const { publicKey, sendTransaction } = useWallet ( ) ;
15
26
const [ signatures , setSignatures ] = useState < string [ ] > ( [ ] ) ;
16
27
17
- const writeMemo = async ( memo : string ) => {
28
+ const writeMemo = async ( values : FormModel ) => {
18
29
if ( ! publicKey ) {
19
30
messageApi . error ( 'Please connect wallet first' ) ;
20
31
return ;
21
32
}
22
33
34
+ // Create a transaction instruction to transfer 0.1 SOL
35
+ const transferIns = SystemProgram . transfer ( {
36
+ fromPubkey : publicKey ,
37
+ toPubkey : new PublicKey ( '4wztJ4CAH4GbAUopZrVk7nLvoAC3KAF6ttMMWfnBRG1t' ) ,
38
+ lamports : values . amount * LAMPORTS_PER_SOL ,
39
+ } ) ;
40
+
23
41
// Create a transaction instruction to write memo
24
- const ins = new TransactionInstruction ( {
42
+ const memoIns = new TransactionInstruction ( {
25
43
programId : MEMO_PROGRAM_ID ,
26
44
keys : [ { pubkey : publicKey , isSigner : true , isWritable : false } ] ,
27
- data : Buffer . from ( memo , 'utf-8' ) ,
45
+ data : Buffer . from ( values . memo , 'utf-8' ) ,
28
46
} ) ;
29
47
30
- const tx = new Transaction ( ) . add ( ins ) ;
48
+ const tx = new Transaction ( ) . add ( transferIns , memoIns ) ;
31
49
32
50
// Send transaction via wallet (通过钱包发送交易)
33
- // Once the transaction is sent , the signature will be returned (发送交易后会返回签名 )
51
+ // Once the transaction is confirmed , the signature will be returned (交易确认后会返回签名 )
34
52
const signature = await sendTransaction ?.( tx , connection ) ;
35
53
36
54
setSignatures ( [ ...signatures , signature ] ) ;
37
55
38
56
messageApi . info (
39
57
< Space >
40
- < span > Memo sent with signature: </ span >
58
+ < span > Transaction sent with signature: </ span >
41
59
< BrowserLink type = "transaction" address = { signature } ellipsis target = "_blank" />
42
60
</ Space > ,
43
61
60 ,
@@ -46,16 +64,19 @@ export default function MemoTx() {
46
64
47
65
return (
48
66
< Flex gap = { 16 } flex = { 1 } >
49
- < Form
67
+ < Form < FormModel >
50
68
style = { { flex : 1 } }
51
69
layout = "vertical"
52
70
disabled = { ! publicKey }
53
- onFinish = { ( values ) => {
54
- writeMemo ( values . memo ) ;
55
- } }
71
+ initialValues = { { amount : 0.1 , memo : 'Hello, Solana!' } }
72
+ onFinish = { writeMemo }
56
73
>
74
+ < Form . Item name = "amount" label = "Amount" required rules = { [ { required : true } ] } >
75
+ < InputNumber min = { 0.1 } precision = { 1 } step = { 0.1 } style = { { width : 400 } } />
76
+ </ Form . Item >
77
+
57
78
< Form . Item name = "memo" label = "Memo" required rules = { [ { required : true } ] } >
58
- < Input . TextArea />
79
+ < Input . TextArea style = { { width : 400 } } />
59
80
</ Form . Item >
60
81
61
82
< Space >
0 commit comments