Skip to main content

Buy/Sell Transaction Guide

What you'll learn
  • How to create buy and sell transaction instructions
  • Handle different amount formats (SOL, tokens, percentages)
  • Implement proper error handling
  • Send and confirm transactions

Introduction

This guide explains how to create and execute buy/sell transactions on pump.fun through the Transaction API. You'll learn how to structure requests, handle responses, and manage transactions effectively.

Access Requirements

Transaction Parameters

interface CreateTransaction {
signer: string; // Your wallet public key
transactionType: 'Buy' | 'Sell'; // Transaction type
mint: string; // Token mint address
amount: string; // Amount to trade
slippage: number; // Maximum slippage tolerance
priorityFee: number; // Priority fee in mLamports
}

Basic Usage Examples

1. Buy Transaction

Create a buy transaction for a specific token:

const buyRequest = {
signer: "AaA2aA5aA8aA4aA7aA1aA9aAa3aA6aA2aA8aA5aA1a",
transactionType: "Buy",
mint: "BbB3bB7bB2bB9bB4bB8bB1bBb5bB2bB6bB3bB9bB4bB",
amount: "0.1", // 0.1 SOL
slippage: 1.5, // 1.5% slippage
priorityFee: 1000, // 1000 mLamports
};

2. Sell Transaction

Create a sell transaction using percentage:

const sellRequest = {
signer: "AaA2aA5aA8aA4aA7aA1aA9aAa3aA6aA2aA8aA5aA1a",
transactionType: "Sell",
mint: "BbB3bB7bB2bB9bB4bB8bB1bBb5bB2bB6bB3bB9bB4bB",
amount: "50%", // Sell 50% of holdings
slippage: 3.5, // 3.5% slippage
priorityFee: 1000, // 1000 mLamports
};

Complete Implementation

import { 
Connection,
VersionedTransaction,
Keypair
} from '@solana/web3.js';
import bs58 from 'bs58';

class PumpFunTradeClient {
private connection: Connection;
private apiKey: string;

constructor(rpcEndpoint: string, apiKey: string) {
this.connection = new Connection(rpcEndpoint, 'confirmed');
this.apiKey = apiKey;
}

async createTradeTransaction(
signer: string,
transactionType: 'Buy' | 'Sell',
mint: string,
amount: string,
slippage: number,
priorityFee: number
) {
const response = await fetch(
'https://api.callstaticrpc.com/pumpfun/v1/transactions/getInstruction',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
signer,
transactionType,
mint,
amount,
slippage,
priorityFee
})
}
);

if (!response.ok) {
throw new Error(`API error: ${response.statusText}`);
}

const result = await response.json();
if (!result.success || !result.data) {
throw new Error('Failed to generate transaction');
}

return VersionedTransaction.deserialize(new Uint8Array(result.data));
}

async sendTransaction(transaction: VersionedTransaction, privateKey: string) {
const signerKeyPair = Keypair.fromSecretKey(bs58.decode(privateKey));
transaction.sign([signerKeyPair]);

const signature = await this.connection.sendTransaction(transaction);
return signature;
}

}

Error Handling

The API returns standard error messages:

try {
const transaction = await client.createTradeTransaction(/* params */);
} catch (error) {
if (error.message.includes('Authentication')) {
console.error('Invalid API key');
} else if (error.message.includes('Invalid parameters')) {
console.error('Invalid transaction parameters');
} else {
console.error('Unknown error:', error);
}
}

Best Practices

  1. Transaction Safety

    • Always verify transaction contents before signing
    • Use appropriate slippage for market conditions
    • Monitor transaction status after sending
  2. Error Management

    • Implement proper error handling
    • Use try-catch blocks consistently
    • Handle network errors gracefully
  3. Security

    • Never expose private keys
    • Store API keys securely
    • Use environment variables for sensitive data

Next Steps

Now that you understand how to create trade transactions, you can:

  1. 🚀 Build a trading interface
  2. 📊 Implement automated trading strategies
  3. ⚡ Create a trading bot
  4. 🔄 Monitor transaction status
Need Help?

For additional support: