pumpdotfun-sdk-repumped
Version:
Pumpfun SDK — create, buy, sell tokens with support for Jito bundles and multiple relayer integrations. Rebuilt and fixed pumpdotfun-sdk.
161 lines (157 loc) • 9.33 kB
JavaScript
'use strict';
var bn = require('../_virtual/bn.cjs');
var splToken = require('@solana/spl-token');
var web3_js = require('@solana/web3.js');
var globalAccount = require('../globalAccount.cjs');
var pumpFun_consts = require('../pumpFun.consts.cjs');
var slippage = require('../slippage.cjs');
var tx = require('../tx.cjs');
class TradeModule {
sdk;
constructor(sdk) {
this.sdk = sdk;
}
async createAndBuy(creator, mint, metadata, buyAmountSol, slippageBasisPoints = 500n, priorityFees, commitment = pumpFun_consts.DEFAULT_COMMITMENT, finality = pumpFun_consts.DEFAULT_FINALITY) {
const tokenMetadata = await this.sdk.token.createTokenMetadata(metadata);
const createIx = await this.getCreateInstructions(creator.publicKey, metadata.name, metadata.symbol, tokenMetadata.metadataUri, mint);
const transaction = new web3_js.Transaction().add(createIx);
if (buyAmountSol > 0n) {
const globalAccount = await this.sdk.token.getGlobalAccount(commitment);
const buyAmount = globalAccount.getInitialBuyPrice(buyAmountSol);
const buyAmountWithSlippage = slippage.calculateWithSlippageBuy(buyAmountSol, slippageBasisPoints);
await this.buildBuyIx(creator.publicKey, mint.publicKey, buyAmount, buyAmountWithSlippage, transaction, commitment, true);
}
return await tx.sendTx(this.sdk.connection, transaction, creator.publicKey, [creator, mint], priorityFees, commitment, finality);
}
async buy(buyer, mint, buyAmountSol, slippageBasisPoints = 500n, priorityFees, commitment = pumpFun_consts.DEFAULT_COMMITMENT, finality = pumpFun_consts.DEFAULT_FINALITY) {
const bondingAccount = await this.sdk.token.getBondingCurveAccount(mint, commitment);
if (!bondingAccount) {
throw new Error(`Bonding curve account not found: ${mint.toBase58()}`);
}
const buyAmount = bondingAccount.getBuyPrice(buyAmountSol);
const buyAmountWithSlippage = slippage.calculateWithSlippageBuy(buyAmountSol, slippageBasisPoints);
const transaction = new web3_js.Transaction();
await this.buildBuyIx(buyer.publicKey, mint, buyAmount, buyAmountWithSlippage, transaction, commitment, false);
return await tx.sendTx(this.sdk.connection, transaction, buyer.publicKey, [buyer], priorityFees, commitment, finality);
}
async getBuyInstructionsBySolAmount(buyer, mint, buyAmountSol, slippageBasisPoints = 500n, commitment = pumpFun_consts.DEFAULT_COMMITMENT) {
const bondingCurveAccount = await this.sdk.token.getBondingCurveAccount(mint, commitment);
if (!bondingCurveAccount) {
throw new Error(`Bonding curve account not found: ${mint.toBase58()}`);
}
const buyAmount = bondingCurveAccount.getBuyPrice(buyAmountSol);
const buyAmountWithSlippage = slippage.calculateWithSlippageBuy(buyAmountSol, slippageBasisPoints);
const transaction = new web3_js.Transaction();
await this.buildBuyIx(buyer, mint, buyAmount, buyAmountWithSlippage, transaction, commitment, false);
return transaction;
}
async buildBuyIx(buyer, mint, amount, maxSolCost, tx, commitment, shouldUseBuyerAsBonding) {
const bondingCurve = this.sdk.pda.getBondingCurvePDA(mint);
const associatedBonding = await splToken.getAssociatedTokenAddress(mint, bondingCurve, true);
const associatedUser = await this.sdk.token.createAssociatedTokenAccountIfNeeded(buyer, buyer, mint, tx, commitment);
const globalPda = this.sdk.pda.getGlobalAccountPda();
const globalAccBuf = await this.sdk.connection.getAccountInfo(globalPda, commitment);
const feeRecipient = globalAccount.GlobalAccount.fromBuffer(globalAccBuf.data).feeRecipient;
const bondingCreator = shouldUseBuyerAsBonding
? this.sdk.pda.getCreatorVaultPda(buyer)
: await this.sdk.token.getBondingCurveCreator(bondingCurve, commitment);
const creatorVault = shouldUseBuyerAsBonding
? bondingCreator
: this.sdk.pda.getCreatorVaultPda(bondingCreator);
const eventAuthority = this.sdk.pda.getEventAuthorityPda();
const ix = await this.sdk.program.methods
.buy(new bn.default(amount.toString()), new bn.default(maxSolCost.toString()))
.accounts({
global: globalPda,
feeRecipient,
mint,
bondingCurve,
associatedBondingCurve: associatedBonding,
associatedUser,
user: buyer,
creatorVault,
eventAuthority,
globalVolumeAccumulator: this.sdk.pda.getGlobalVolumeAccumulatorPda(),
userVolumeAccumulator: this.sdk.pda.getUserVolumeAccumulatorPda(buyer),
})
.instruction();
tx.add(ix);
}
//create token instructions
async getCreateInstructions(creator, name, symbol, uri, mint) {
const mintAuthority = this.sdk.pda.getMintAuthorityPDA();
const bondingCurve = this.sdk.pda.getBondingCurvePDA(mint.publicKey);
const associatedBonding = await splToken.getAssociatedTokenAddress(mint.publicKey, bondingCurve, true);
const global = this.sdk.pda.getGlobalAccountPda();
const metadata = this.sdk.pda.getMetadataPDA(mint.publicKey);
const eventAuthority = this.sdk.pda.getEventAuthorityPda();
const ix = await this.sdk.program.methods
.create(name, symbol, uri, creator)
.accounts({
mint: mint.publicKey,
mintAuthority,
bondingCurve,
associatedBondingCurve: associatedBonding,
global,
metadata,
user: creator,
eventAuthority,
})
.instruction();
return new web3_js.Transaction().add(ix);
}
async buildSellIx(seller, mint, tokenAmount, minSolOutput, tx, commitment) {
const bondingCurve = this.sdk.pda.getBondingCurvePDA(mint);
const associatedBonding = await splToken.getAssociatedTokenAddress(mint, bondingCurve, true);
const associatedUser = await this.sdk.token.createAssociatedTokenAccountIfNeeded(seller, seller, mint, tx, commitment);
const globalPda = this.sdk.pda.getGlobalAccountPda();
const globalBuf = await this.sdk.connection.getAccountInfo(globalPda, commitment);
const feeRecipient = globalAccount.GlobalAccount.fromBuffer(globalBuf.data).feeRecipient;
const bondingCreator = await this.sdk.token.getBondingCurveCreator(bondingCurve, commitment);
const creatorVault = this.sdk.pda.getCreatorVaultPda(bondingCreator);
const eventAuthority = this.sdk.pda.getEventAuthorityPda();
const ix = await this.sdk.program.methods
.sell(new bn.default(tokenAmount.toString()), new bn.default(minSolOutput.toString()))
.accounts({
global: globalPda,
feeRecipient,
mint,
bondingCurve,
associatedBondingCurve: associatedBonding,
associatedUser,
user: seller,
creatorVault,
eventAuthority,
})
.instruction();
tx.add(ix);
}
async sell(seller, mint, sellTokenAmount, slippageBasisPoints = 500n, priorityFees, commitment = pumpFun_consts.DEFAULT_COMMITMENT, finality = pumpFun_consts.DEFAULT_FINALITY) {
const bondingAccount = await this.sdk.token.getBondingCurveAccount(mint, commitment);
if (!bondingAccount)
throw new Error(`Bonding curve account not found: ${mint.toBase58()}`);
const globalAccount = await this.sdk.token.getGlobalAccount(commitment);
const minSolOutput = bondingAccount.getSellPrice(sellTokenAmount, globalAccount.feeBasisPoints);
let sellAmountWithSlippage = slippage.calculateWithSlippageSell(minSolOutput, slippageBasisPoints);
if (sellAmountWithSlippage < 1n)
sellAmountWithSlippage = 1n;
const transaction = new web3_js.Transaction();
await this.buildSellIx(seller.publicKey, mint, sellTokenAmount, sellAmountWithSlippage, transaction, commitment);
return await tx.sendTx(this.sdk.connection, transaction, seller.publicKey, [seller], priorityFees, commitment, finality);
}
async getSellInstructionsByTokenAmount(seller, mint, sellTokenAmount, slippageBasisPoints = 500n, commitment = pumpFun_consts.DEFAULT_COMMITMENT) {
const bondingAccount = await this.sdk.token.getBondingCurveAccount(mint, commitment);
if (!bondingAccount)
throw new Error(`Bonding curve account not found: ${mint.toBase58()}`);
const globalAccount = await this.sdk.token.getGlobalAccount(commitment);
const minSolOutput = bondingAccount.getSellPrice(sellTokenAmount, globalAccount.feeBasisPoints);
let sellAmountWithSlippage = slippage.calculateWithSlippageSell(minSolOutput, slippageBasisPoints);
if (sellAmountWithSlippage < 1n)
sellAmountWithSlippage = 1n;
const transaction = new web3_js.Transaction();
await this.buildSellIx(seller, mint, sellTokenAmount, sellAmountWithSlippage, transaction, commitment);
return transaction;
}
}
exports.TradeModule = TradeModule;
//# sourceMappingURL=TradeModule.cjs.map