UNPKG

pumpdotfun-sdk-sp

Version:

A simple SDK for interacting with pumpdotfun

91 lines (87 loc) 3.23 kB
'use strict'; var web3_js = require('@solana/web3.js'); const DEFAULT_COMMITMENT = "finalized"; const DEFAULT_FINALITY = "finalized"; const calculateWithSlippageBuy = (amount, basisPoints) => { return amount + (amount * basisPoints) / 10000n; }; const calculateWithSlippageSell = (amount, basisPoints) => { return amount - (amount * basisPoints) / 10000n; }; async function sendTx(connection, tx, payer, signers, priorityFees, commitment = DEFAULT_COMMITMENT, finality = DEFAULT_FINALITY) { let newTx = new web3_js.Transaction(); if (priorityFees) { const modifyComputeUnits = web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: priorityFees.unitLimit, }); const addPriorityFee = web3_js.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: priorityFees.unitPrice, }); newTx.add(modifyComputeUnits); newTx.add(addPriorityFee); } newTx.add(tx); let versionedTx = await buildVersionedTx(connection, payer, newTx, commitment); versionedTx.sign(signers); try { const sig = await connection.sendTransaction(versionedTx, { skipPreflight: false, }); console.log("sig:", `https://solscan.io/tx/${sig}`); let txResult = await getTxDetails(connection, sig, commitment, finality); if (!txResult) { return { success: false, error: "Transaction failed", }; } return { success: true, signature: sig, results: txResult, }; } catch (e) { if (e instanceof web3_js.SendTransactionError) { let ste = e; console.log("SendTransactionError" + await ste.getLogs(connection)); } else { console.error(e); } return { error: e, success: false, }; } } const buildVersionedTx = async (connection, payer, tx, commitment = DEFAULT_COMMITMENT) => { const blockHash = (await connection.getLatestBlockhash(commitment)) .blockhash; let messageV0 = new web3_js.TransactionMessage({ payerKey: payer, recentBlockhash: blockHash, instructions: tx.instructions, }).compileToV0Message(); return new web3_js.VersionedTransaction(messageV0); }; const getTxDetails = async (connection, sig, commitment = DEFAULT_COMMITMENT, finality = DEFAULT_FINALITY) => { const latestBlockHash = await connection.getLatestBlockhash(); await connection.confirmTransaction({ blockhash: latestBlockHash.blockhash, lastValidBlockHeight: latestBlockHash.lastValidBlockHeight, signature: sig, }, commitment); return connection.getTransaction(sig, { maxSupportedTransactionVersion: 0, commitment: finality, }); }; exports.DEFAULT_COMMITMENT = DEFAULT_COMMITMENT; exports.DEFAULT_FINALITY = DEFAULT_FINALITY; exports.buildVersionedTx = buildVersionedTx; exports.calculateWithSlippageBuy = calculateWithSlippageBuy; exports.calculateWithSlippageSell = calculateWithSlippageSell; exports.getTxDetails = getTxDetails; exports.sendTx = sendTx; //# sourceMappingURL=util.cjs.map