CN6PVEIDCNCQD6GRZ3XB66VIFXLL7DW5IVWTAO6WTKG7D4OYZIJAC
import { Transaction, SystemProgram, PublicKey } from '@solana/web3.js';
import { Connection, Keypair, PublicKey, sendAndConfirmTransaction, SystemProgram, Transaction, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
import { Buffer } from 'buffer';
window.Buffer = Buffer;
function createTransaction(amountInLamports: number, sender: PublicKey, recipient: PublicKey, memoText: string): Transaction {
const transaction = new Transaction();
transaction.add(
SystemProgram.transfer({
fromPubkey: sender,
toPubkey: recipient,
lamports: amountInLamports,
})
);
// const userPublicKey = new PublicKey(wallet.address);
// // let tx = new Transaction();
// // const connection = new Connection('http://rpc.solscan.com');
// // console.log(connection)
// // const { blockhash } = await connection.getLatestBlockhash();
// // tx.recentBlockhash = blockhash;
// // tx.feePayer = userPublicKey;
// // // Add Memo Instruction
// // tx.add(
// // new TransactionInstruction({
// // keys: [{ pubkey: userPublicKey, isSigner: true, isWritable: true }],
// // data: Buffer.from(combinedString, "utf-8"),
// // programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),
// // })
// // );
// // Determine the amount to transfer
// const tokenPrice = await fetchTokenPrice();
// const amountInSol = Math.ceil((1 / tokenPrice) * 1000) / 1000;
// const amountInLamports = amountInSol * LAMPORTS_PER_SOL;
// // Add Transfer Instruction
// const transaction = new Transaction().add( SystemProgram.transfer({
// fromPubkey: userPublicKey,toPubkey: new PublicKey("7nbet512GtFRGiPrrS7Ji1BXjdfAeFYQWG7tNqBatvW6"),
// lamports: amountInLamports}),);
const memoInstruction = {
keys: [],
programId: new PublicKey(MEMO_PROGRAM_ID),
data: memoData,
};
transaction.add(memoInstruction);
// // Sign and Send Transaction using Glow
// const { signature } = await window.glow.signAndSendTransaction({
// transactionBase64: transaction,
// network: "mainnet",
// waitForConfirmation: true
// });
async function getLatestBlockhash() {
try {
const response = await fetch('https://solana-blockdata-philiasocial.replit.app/getLatestBlockhash');
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
const blockhash = await response.text(); // assuming the blockhash is returned as plain text
return blockhash;
} catch (error) {
console.error('Failed to fetch blockhash:', error);
throw error; // re-throw the error so it can be handled by the calling code
}
}
console.log(wallet)
// if (!wallet) {
// console.error('No account is connected');
// return;
// }
console.log(blockhash)
// Determine the amount to transfer
const tokenPrice = await fetchTokenPrice();
const amountInSol = 1 / tokenPrice;
const amountInLamports = Math.floor(amountInSol * LAMPORTS_PER_SOL);
console.log(amountInLamports)
// Add Transfer Instruction
// Add Transfer Instruction
const transferInstruction = SystemProgram.transfer({
fromPubkey: userPublicKey,
toPubkey: new PublicKey("7nbet512GtFRGiPrrS7Ji1BXjdfAeFYQWG7tNqBatvW6"),
lamports: amountInLamports,
});
try {
const tokenPrice = await fetchTokenPrice();
const amountInSol = Math.ceil((1 / tokenPrice) * 1000) / 1000;
console.log(combinedString)
// Add Memo Instruction
const memoInstruction = new TransactionInstruction({
keys: [{ pubkey: userPublicKey, isSigner: true, isWritable: true }],
data: Buffer.from(combinedString, "utf-8"),
programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),
});
console.log(amountInSol)
const amountInLamports = amountInSol * LAMPORTS_PER_SOL;
// Combine the transfer and memo instructions into an array
const instructions = [
transferInstruction,
memoInstruction,
];
// create v0 compatible message
const messageV0 = new TransactionMessage({
payerKey: userPublicKey,
recentBlockhash: blockhash,
instructions,
}).compileToV0Message();
const transaction = createTransaction(amountInLamports, sender, recipient, combinedString);
const transactionBytes = new Uint8Array(transaction.serialize());
const transactionBase64 = btoa(new TextDecoder().decode(transactionBytes));
const serializedTransactionV0 = new VersionedTransaction(messageV0).serialize();
// Convert the serialized transaction to a base64 string
const transactionBase64 = Buffer.from(serializedTransactionV0).toString('base64');
await window.glow.signAndSendTransaction({
transactionBase64,
network: "mainnet",
waitForConfirmation: true,
// Sign and Send Transaction using Glow
const {
signature
} = await window.glow.signAndSendTransaction({
transactionBase64: transactionBase64,
network: "mainnet", // Note: Changed from "mainnet" to "devnet" as per your example
waitForConfirmation: true
} catch (error) {
console.error('Failed to send transaction:', error);
}
}
// Return the transaction signature
console.log(signature);
if(signature) {
await sendWords(signature, wallet.address);
// showModal();
} else {
console.error('Transaction hash is not available');
}