Skip to content

Instantly share code, notes, and snippets.

@thearyanag
Created October 23, 2024 16:47
Show Gist options
  • Select an option

  • Save thearyanag/e2783dfea99a984c48cdbc8c4d9ec644 to your computer and use it in GitHub Desktop.

Select an option

Save thearyanag/e2783dfea99a984c48cdbc8c4d9ec644 to your computer and use it in GitHub Desktop.
Mint Fomo Key using Script
const {
Connection,
Transaction,
sendAndConfirmTransaction,
Keypair,
ComputeBudgetProgram,
} = require("@solana/web3.js");
const bs58 = require("bs58");
const axios = require("axios");
// Configuration
const HeliusURL = "https://mainnet.helius-rpc.com/?api-key=<YOUR_API_KEY>";
const connection = new Connection(HeliusURL);
const fromKeypair = Keypair.fromSecretKey(Uint8Array.from("[Your secret key]")); // Replace with your private key
// Function to fetch transaction from sendfomo API
async function getFomoTransaction(account) {
try {
const response = await axios.post('https://sendfomo.com/api/actions/mint', {
account: account
});
if (!response.data || !response.data.transaction) {
throw new Error('Invalid response format from sendfomo API');
}
console.log('Successfully fetched transaction from sendfomo');
return response.data.transaction;
} catch (error) {
console.error('Error fetching transaction from sendfomo:', error.message);
throw error;
}
}
// Function to get priority fee estimate
async function getPriorityFeeEstimate(priorityLevel, transaction) {
try {
const response = await fetch(HeliusURL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
id: "1",
method: "getPriorityFeeEstimate",
params: [
{
transaction: bs58.encode(transaction.serialize()),
options: { priorityLevel: priorityLevel },
},
],
}),
});
if (!response.ok) {
throw new Error(`Helius API responded with status: ${response.status}`);
}
const data = await response.json();
if (data.error) {
throw new Error(`Helius API error: ${data.error.message}`);
}
console.log(
"Priority fee estimate for",
priorityLevel,
":",
data.result.priorityFeeEstimate
);
return data.result;
} catch (error) {
console.error('Error getting priority fee estimate:', error);
throw error;
}
}
// Main function to send transaction
async function sendTransactionWithPriorityFee(account, priorityLevel) {
try {
// Fetch transaction from sendfomo
const base64Tx = await getFomoTransaction(account);
// Decode base64 transaction
const decodedTx = Buffer.from(base64Tx, 'base64');
// Create transaction from decoded data
const transaction = Transaction.from(decodedTx);
// Add priority fee if specified
if (priorityLevel !== "NONE") {
const feeEstimate = await getPriorityFeeEstimate(priorityLevel, transaction);
const computePriceIx = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: feeEstimate.priorityFeeEstimate,
});
transaction.add(computePriceIx);
}
// Update blockhash
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
// Sign transaction
transaction.sign(fromKeypair);
// Send and confirm transaction with timeout
const txid = await sendAndConfirmTransaction(
connection,
transaction,
[fromKeypair],
{
commitment: 'confirmed',
maxRetries: 5
}
);
console.log(`Transaction sent successfully with signature ${txid}`);
// Check if there's a next action to perform
try {
const successResponse = await axios.post('https://sendfomo.com/api/actions/success');
console.log('Success action completed:', successResponse.data);
} catch (error) {
console.warn('Warning: Failed to complete success action:', error.message);
}
return txid;
} catch (error) {
console.error('Error in sendTransactionWithPriorityFee:', error);
throw error;
}
}
// Example usage with error handling
async function main() {
try {
const account = "4vDHQPxjLsCWcAUCaWP8wqLFvv35FTCQZFf31nHCCCkF";
const priorityLevel = "High"; // Choose between "Min", "Low", "Medium", "High", "VeryHigh", "UnsafeMax"
console.log('Starting transaction process...');
const txid = await sendTransactionWithPriorityFee(account, priorityLevel);
console.log('Transaction completed successfully:', txid);
} catch (error) {
console.error('Fatal error in main execution:', error);
process.exit(1);
}
}
// Run the script
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment