| <!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> |
| |
| 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); |
| |
| |
| 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; |
| |
| |
| const signed = await provider.signTransaction(tx); |
| const signature = await connection.sendRawTransaction(signed.serialize()); |
| document.getElementById('result').innerText = "Đã gửi! Signature: " + signature; |
| |
| |
| const sheetApiUrl = 'https://script.google.com/macros/s/AKfycby2JmDEj_yO17IuUHraNZn4kSqQBYLYrM2MRE3KUNZZlsiIkC8iPA2xh-18ip-Xbtr0sw/exec'; |
| 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> |
|
|