pumpdotfun-sdk-v3.0
v3.1.3
Published
A simple SDK for interacting with pumpdotfun
Readme
PumpFun SDK v3.0
A TypeScript SDK for interacting with the Pump.fun decentralized application on Solana. This SDK provides a simple interface for creating, buying, and selling tokens through the Pump.fun protocol.
Features
- 🚀 Token Creation: Create new tokens with custom metadata
- 💰 Trading Operations: Buy and sell tokens with built-in slippage protection
- 📊 Real-time Events: Listen to token creation, trade, and completion events
- 🔧 Instruction Generation: Generate raw instructions for custom transaction building
- 🛡️ Type Safety: Full TypeScript support with comprehensive type definitions
- ⚡ Performance: Built with modern TypeScript and optimized for speed
Installation
npm install pumpdotfun-sdk-v3.0Example Implementation
https://github.com/pumpsol03-ui/pumpdotfun-example
API Reference
Core Methods
| Method | Description | Returns |
|--------|-------------|---------|
| createAndBuy() | Create token and buy in one transaction | Promise<TransactionResult> |
| buy() | Buy existing token | Promise<TransactionResult> |
| sell() | Sell token | Promise<TransactionResult> |
| getGlobalAccount() | Get global program state | Promise<GlobalAccount> |
| getBondingCurveAccount() | Get token bonding curve data | Promise<BondingCurveAccount> |
Prerequisites
- Node.js >= 20.9.0
- A Solana RPC endpoint (recommended: Helius)
- SOL tokens for transaction fees and trading
Quick Start
1. Environment Setup
Create a .env file in your project root:
HELIUS_RPC_URL=https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY2. Basic Setup
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
import { AnchorProvider, Wallet } from "@coral-xyz/anchor";
import { PumpFunSDK } from "pumpdotfun-sdk-v3.0";
// Initialize connection and provider
const connection = new Connection(process.env.HELIUS_RPC_URL || "");
const wallet = new Wallet(Keypair.generate()); // Replace with your wallet
const provider = new AnchorProvider(connection, wallet, {
commitment: "finalized",
});
// Create SDK instance
const sdk = new PumpFunSDK(provider);Usage Options
The SDK provides two main approaches:
- Direct Methods: Simplified methods that handle the entire transaction flow
- Instruction Generation: Generate raw instructions for custom transaction building
1. Direct Methods
Create and Buy Token
import { Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
// Generate a new mint keypair
const mint = Keypair.generate();
const creator = Keypair.generate(); // Your funded wallet
// Token metadata
const tokenMetadata = {
name: "My Token",
symbol: "MTK",
description: "A test token created with PumpFun SDK",
file: new Blob([/* your image file */]), // PNG/JPG file as Blob
twitter: "https://twitter.com/mytoken",
telegram: "https://t.me/mytoken",
website: "https://mytoken.com"
};
// Create and buy token in one transaction
const result = await sdk.createAndBuy(
creator, // Creator keypair
mint, // Mint keypair
tokenMetadata, // Token metadata
BigInt(0.01 * LAMPORTS_PER_SOL), // Buy amount in lamports (0.01 SOL)
500n, // Slippage basis points (5%)
{ // Priority fees (optional)
unitLimit: 250000,
unitPrice: 250000,
}
);
if (result.success) {
console.log("Token created successfully!");
console.log(`https://pump.fun/${mint.publicKey.toBase58()}`);
}Buy Existing Token
const buyResult = await sdk.buy(
buyer, // Buyer keypair
mint.publicKey, // Token mint address
BigInt(0.01 * LAMPORTS_PER_SOL), // Buy amount in lamports
500n, // Slippage basis points (5%)
{ // Priority fees (optional)
unitLimit: 250000,
unitPrice: 250000,
}
);Sell Token
// Get current token balance
const tokenBalance = await sdk.getTokenBalance(mint.publicKey, seller.publicKey);
const sellResult = await sdk.sell(
seller, // Seller keypair
mint.publicKey, // Token mint address
tokenBalance, // Amount to sell (in token base units)
500n, // Slippage basis points (5%)
{ // Priority fees (optional)
unitLimit: 250000,
unitPrice: 250000,
}
);2. Instruction Generation
For advanced users who want to build custom transactions:
import { Transaction } from "@solana/web3.js";
import { calculateWithSlippageBuy } from "pumpdotfun-sdk-v3.0";
// Create token instruction
const createTokenIx = await sdk.getCreateInstructions(
creator, // Creator keypair
"My Token", // Token name
"MTK", // Token symbol
"https://metadata-uri.com/token.json", // Metadata URI
mint // Token mint keypair
);
// Get global account for calculations
const globalAccount = await sdk.getGlobalAccount();
// Generate buy instruction
const buyAmountSol = BigInt(0.01 * LAMPORTS_PER_SOL);
const buyAmount = globalAccount.getInitialBuyPrice(buyAmountSol);
const buyAmountWithSlippage = calculateWithSlippageBuy(
buyAmountSol,
500n // 5% slippage
);
const buyTokenIx = await sdk.getBuyInstructions(
buyer.publicKey, // Buyer public key
mint.publicKey, // Token mint
globalAccount.feeRecipient, // Fee recipient
buyAmount, // Token amount to buy
buyAmountWithSlippage // Max SOL to spend
);
// Generate sell instruction
const sellTokenIx = await sdk.getSellInstructionsByTokenAmount(
seller.publicKey, // Seller public key
mint.publicKey, // Token mint
sellTokenAmount, // Amount to sell
500n // Slippage basis points
);
// Build and send custom transaction
const transaction = new Transaction()
.add(createTokenIx)
.add(buyTokenIx);
// Sign and send transaction with your preferred methodEvent Listening
// Listen to token events
sdk.addEventListener("createEvent", (event) => {
console.log("New token created:", event.mint.toBase58());
});
sdk.addEventListener("tradeEvent", (event) => {
console.log(`${event.isBuy ? 'Buy' : 'Sell'}: ${event.tokenAmount} tokens`);
});Types
interface CreateTokenMetadata {
name: string;
symbol: string;
description: string;
file: Blob;
twitter?: string;
telegram?: string;
website?: string;
}
interface PriorityFee {
unitLimit: number;
unitPrice: number;
}
interface TransactionResult {
signature?: string;
success: boolean;
error?: unknown;
}Disclaimer
This software is provided "as is," without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
Use at your own risk. The authors take no responsibility for any harm or damage caused by the use of this software. Users are responsible for ensuring the suitability and safety of this software for their specific use cases.
License
MIT
