UNPKG

@pump-fun/pump-swap-sdk

Version:

Official SDK for interacting with Pump Swap AMM protocol on Solana

100 lines (95 loc) 3.71 kB
// src/sdk/buy.ts import BN3 from "bn.js"; // src/sdk/util.ts import BN2 from "bn.js"; import { AnchorProvider, Program } from "@coral-xyz/anchor"; // src/sdk/pda.ts import { PublicKey } from "@solana/web3.js"; import BN from "bn.js"; import { getAssociatedTokenAddressSync, NATIVE_MINT, TOKEN_2022_PROGRAM_ID } from "@solana/spl-token"; var PUMP_AMM_PROGRAM_ID = "pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA"; var PUMP_AMM_PROGRAM_ID_PUBKEY = new PublicKey(PUMP_AMM_PROGRAM_ID); var PUMP_PROGRAM_ID = "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P"; var PUMP_PROGRAM_ID_PUBKEY = new PublicKey(PUMP_PROGRAM_ID); var PUMP_MINT = new PublicKey( "pumpCmXqMfrsAkQ5r49WcJnRayYRqmXz6ae8H7H9Dfn" ); // src/sdk/util.ts function ceilDiv(a, b) { if (b.isZero()) { throw new Error("Cannot divide by zero."); } return a.add(b.subn(1)).div(b); } function fee(amount, basisPoints) { return ceilDiv(amount.mul(basisPoints), new BN2(1e4)); } // src/sdk/buy.ts import { PublicKey as PublicKey2 } from "@solana/web3.js"; function buyBaseInputInternal(base, slippage, baseReserve, quoteReserve, globalConfig, coinCreator) { if (baseReserve.isZero() || quoteReserve.isZero()) { throw new Error( "Invalid input: 'baseReserve' or 'quoteReserve' cannot be zero." ); } if (base.gt(baseReserve)) { throw new Error("Cannot buy more base tokens than the pool reserves."); } const numerator = quoteReserve.mul(base); const denominator = baseReserve.sub(base); if (denominator.isZero()) { throw new Error("Pool would be depleted; denominator is zero."); } const quoteAmountIn = ceilDiv(numerator, denominator); const lpFee = fee(quoteAmountIn, globalConfig.lpFeeBasisPoints); const protocolFee = fee(quoteAmountIn, globalConfig.protocolFeeBasisPoints); const coinCreatorFee = PublicKey2.default.equals(coinCreator) ? new BN3(0) : fee(quoteAmountIn, globalConfig.coinCreatorFeeBasisPoints); const totalQuote = quoteAmountIn.add(lpFee).add(protocolFee).add(coinCreatorFee); const precision = new BN3(1e9); const slippageFactorFloat = (1 + slippage / 100) * 1e9; const slippageFactor = new BN3(Math.floor(slippageFactorFloat)); const maxQuote = totalQuote.mul(slippageFactor).div(precision); return { internalQuoteAmount: quoteAmountIn, uiQuote: totalQuote, // Final total quote after fees maxQuote }; } function buyQuoteInputInternal(quote, slippage, baseReserve, quoteReserve, globalConfig, coinCreator) { if (baseReserve.isZero() || quoteReserve.isZero()) { throw new Error( "Invalid input: 'baseReserve' or 'quoteReserve' cannot be zero." ); } const totalFeeBps = globalConfig.lpFeeBasisPoints.add(globalConfig.protocolFeeBasisPoints).add( PublicKey2.default.equals(coinCreator) ? new BN3(0) : globalConfig.coinCreatorFeeBasisPoints ); const denominator = new BN3(1e4).add(totalFeeBps); const effectiveQuote = quote.mul(new BN3(1e4)).div(denominator); const numerator = baseReserve.mul(effectiveQuote); const denominatorEffective = quoteReserve.add(effectiveQuote); if (denominatorEffective.isZero()) { throw new Error("Pool would be depleted; denominator is zero."); } const baseAmountOut = numerator.div(denominatorEffective); const precision = new BN3(1e9); const slippageFactorFloat = (1 + slippage / 100) * 1e9; const slippageFactor = new BN3(Math.floor(slippageFactorFloat)); const maxQuote = quote.mul(slippageFactor).div(precision); return { base: baseAmountOut, // Base tokens received after fees internalQuoteWithoutFees: effectiveQuote, maxQuote // Maximum quote tokens to pay (with slippage) }; } export { buyBaseInputInternal, buyQuoteInputInternal };