File size: 3,175 Bytes
92f19e8
6815bf3
92f19e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6815bf3
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html>
<head>
    <title>Giao dịch Solana + Lưu Google Sheet</title>
</head>
<body>
    <h2>Gửi giao dịch Solana + Memo</h2>
    <form id="sendForm">
        Địa chỉ nhận SOL:<br>
        <input type="text" id="to" size="45" required><br>
        Số lượng SOL:<br>
        <input type="number" id="amount" step="0.0001" required><br>
        Nội dung Memo:<br>
        <input type="text" id="memo" size="45"><br>
        <button type="submit">Gửi giao dịch</button>
    </form>
    <div id="result"></div>

    <script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js"></script>
    <script>
    // Kết nối Phantom
    const provider = window.solana;

    document.getElementById('sendForm').onsubmit = async function(e) {
        e.preventDefault();
        const to = document.getElementById('to').value.trim();
        const amount = parseFloat(document.getElementById('amount').value);
        const memo = document.getElementById('memo').value.trim();

        if (!provider || !provider.isPhantom) {
            alert('Cần cài Phantom wallet!');
            return;
        }
        await provider.connect();

        const connection = new solanaWeb3.Connection('https://api.devnet.solana.com');
        const fromPubkey = provider.publicKey;
        const toPubkey = new solanaWeb3.PublicKey(to);

        // Tạo instructions: transfer + memo
        const tx = new solanaWeb3.Transaction().add(
            solanaWeb3.SystemProgram.transfer({
                fromPubkey,
                toPubkey,
                lamports: amount * solanaWeb3.LAMPORTS_PER_SOL
            })
        );
        if (memo) {
            tx.add(
                new solanaWeb3.TransactionInstruction({
                    keys: [],
                    programId: new solanaWeb3.PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'),
                    data: Buffer.from(memo)
                })
            );
        }
        tx.feePayer = fromPubkey;
        tx.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;

        // Ký và gửi giao dịch
        const signed = await provider.signTransaction(tx);
        const signature = await connection.sendRawTransaction(signed.serialize());
        document.getElementById('result').innerText = "Đã gửi! Signature: " + signature;

        // Gửi dữ liệu lên Google Sheet qua Apps Script API
        const sheetApiUrl = 'https://script.google.com/macros/s/AKfycby2JmDEj_yO17IuUHraNZn4kSqQBYLYrM2MRE3KUNZZlsiIkC8iPA2xh-18ip-Xbtr0sw/exec'; // thay bằng URL Apps Script bạn vừa deploy
        const payload = {
            signature: signature,
            from: fromPubkey.toString(),
            to: to,
            amount: amount,
            memo: memo,
            time: new Date().toISOString()
        };
        fetch(sheetApiUrl, {
            method: 'POST',
            body: JSON.stringify(payload),
            headers: {'Content-Type': 'application/json'}
        });

        alert("Giao dịch đã gửi! Kiểm tra trạng thái tại Solscan.io (Devnet) và Google Sheet!");
    }
    </script>
</body>
</html>