UNPKG

pumpdotfun-sdk-sp

Version:

A simple SDK for interacting with pumpdotfun

110 lines (106 loc) 4.4 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, }); }; const estimateTokenReceived = (buyAmountSol, solDecimals = 9, tokenDecimals = 6) => { const solInLp = 85000000000n; // 85 SOL, adjust based on solDecimals const tokenInLp = 206900000000000n; // 206,900,000 tokens, adjust based on tokenDecimals // Adjust for decimals const solInLpAdjusted = solInLp * 10n ** BigInt(solDecimals - 9); const tokenInLpAdjusted = tokenInLp * 10n ** BigInt(tokenDecimals - 6); const buyAmountSolAdjusted = buyAmountSol * 10n ** BigInt(solDecimals - 9); // Constant product: solInLp * tokenInLp = k const k = solInLpAdjusted * tokenInLpAdjusted; // After buying, new SOL in LP const newSolInLp = solInLpAdjusted + buyAmountSolAdjusted; // New token amount based on constant product: k / newSolInLp const newTokenInLp = k / newSolInLp; // Tokens received = old amount - new amount const tokensReceived = tokenInLpAdjusted - newTokenInLp; // Adjust tokens received back to original decimal scale for return value const tokensReceivedAdjusted = tokensReceived / 10n ** BigInt(tokenDecimals - 6); return tokensReceivedAdjusted; }; exports.DEFAULT_COMMITMENT = DEFAULT_COMMITMENT; exports.DEFAULT_FINALITY = DEFAULT_FINALITY; exports.buildVersionedTx = buildVersionedTx; exports.calculateWithSlippageBuy = calculateWithSlippageBuy; exports.calculateWithSlippageSell = calculateWithSlippageSell; exports.estimateTokenReceived = estimateTokenReceived; exports.getTxDetails = getTxDetails; exports.sendTx = sendTx; //# sourceMappingURL=util.cjs.map