simple_example/
simple_example.rs1use pumpdotfun_sdk::{
2 instructions::{
3 buy::{Buy, BuyAccounts},
4 create::{CreateAccounts, CreateArgs},
5 },
6 PumpDotFunSdk, Sell, SellAccounts,
7};
8use solana_client::rpc_client::RpcClient;
9use solana_sdk::{
10 native_token::LAMPORTS_PER_SOL,
11 pubkey::Pubkey,
12 signature::{read_keypair_file, Keypair},
13 signer::Signer,
14 transaction::Transaction,
15};
16use std::sync::Arc;
17
18fn main() -> Result<(), Box<dyn std::error::Error>> {
25 println!("š Simple pump.fun SDK Example");
26
27 let rpc_client = Arc::new(RpcClient::new("https://api.devnet.solana.com".to_string()));
29 let sdk = PumpDotFunSdk::new(rpc_client.clone());
30
31 let wallet_path = std::env::var("SOLANA_WALLET").unwrap_or_else(|_| {
33 let home = std::env::var("HOME").unwrap_or_else(|_| "/home/user".to_string());
34 format!("{}/.config/solana/devnet-wallet.json", home)
35 });
36
37 let user_keypair = match read_keypair_file(&wallet_path) {
38 Ok(keypair) => keypair,
39 Err(_) => {
40 println!("ā Could not load wallet from {}", wallet_path);
41 println!(
42 "š” Generate one with: solana-keygen new --outfile {}",
43 wallet_path
44 );
45 println!("š” Fund it with: solana airdrop 2 --url devnet");
46 return Ok(());
47 }
48 };
49
50 println!("ā
Loaded wallet: {}", user_keypair.pubkey());
51
52 let balance = rpc_client.get_balance(&user_keypair.pubkey())?;
54 println!(
55 "š° Balance: {:.4} SOL",
56 balance as f64 / LAMPORTS_PER_SOL as f64
57 );
58
59 if balance < LAMPORTS_PER_SOL / 10 {
60 println!("ā ļø Low balance! Run: solana airdrop 2 --url devnet");
61 return Ok(());
62 }
63
64 println!("\nš Creating a new token...");
66 let mint_pubkey = create_simple_token(&sdk, &user_keypair)?;
67
68 println!("\nš° Buying tokens...");
70 buy_simple_tokens(&sdk, &user_keypair, &mint_pubkey)?;
71
72 println!("\nš Simple example completed! Check the transactions on Solana Explorer (devnet)");
73
74 println!("\nš° Selling tokens...");
75 sell_simple_tokens(&sdk, &user_keypair, &mint_pubkey)?;
76
77 Ok(())
78}
79
80fn create_simple_token(
81 sdk: &PumpDotFunSdk,
82 user_keypair: &Keypair,
83) -> Result<Pubkey, Box<dyn std::error::Error>> {
84 let mint_keypair = Keypair::new();
85 let mint_pubkey = mint_keypair.pubkey();
86
87 let accounts = CreateAccounts {
88 mint: mint_pubkey,
89 user: user_keypair.pubkey(),
90 };
91
92 let args = CreateArgs {
93 name: "Test Pump Token".to_string(),
94 symbol: "TEST".to_string(),
95 uri: "https://raw.githubusercontent.com/solana-labs/token-list/main/assets/mainnet/So11111111111111111111111111111111111111112/logo.png".to_string(),
96 creator: user_keypair.pubkey(),
97 };
98
99 let instruction = sdk.create(accounts, args);
100 let recent_blockhash = sdk.rpc.get_latest_blockhash()?;
101
102 let mut transaction = Transaction::new_with_payer(&[instruction], Some(&user_keypair.pubkey()));
103 transaction.sign(&[user_keypair, &mint_keypair], recent_blockhash);
104
105 let signature = sdk.rpc.send_and_confirm_transaction(&transaction)?;
106 println!(
107 "ā
Token created! Mint: {} | TX: {}",
108 mint_pubkey, signature
109 );
110
111 Ok(mint_pubkey)
112}
113
114fn buy_simple_tokens(
115 sdk: &PumpDotFunSdk,
116 user_keypair: &Keypair,
117 mint: &Pubkey,
118) -> Result<(), Box<dyn std::error::Error>> {
119 let accounts = BuyAccounts {
120 mint: *mint,
121 user: user_keypair.pubkey(),
122 };
123
124 let args = Buy {
125 amount: 100_000_000, max_sol_cost: LAMPORTS_PER_SOL / 1000, slippage: 10, };
129
130 let instructions = sdk
131 .buy(accounts, args)
132 .map_err(|e| format!("Buy error: {:?}", e))?;
133 let recent_blockhash = sdk.rpc.get_latest_blockhash()?;
134
135 let mut transaction = Transaction::new_with_payer(&instructions, Some(&user_keypair.pubkey()));
136 transaction.sign(&[user_keypair], recent_blockhash);
137
138 let signature = sdk.rpc.send_and_confirm_transaction(&transaction)?;
139 println!("ā
Tokens purchased! TX: {}", signature);
140
141 Ok(())
142}
143
144fn sell_simple_tokens(
145 sdk: &PumpDotFunSdk,
146 user_keypair: &Keypair,
147 mint: &Pubkey,
148) -> Result<(), Box<dyn std::error::Error>> {
149 let accounts = SellAccounts {
150 mint: *mint,
151 user: user_keypair.pubkey(),
152 };
153
154 let args = Sell {
155 amount: 50_000_000, min_sol_output: LAMPORTS_PER_SOL / 1000000,
157 slippage: 10, };
159
160 let instructions = sdk
161 .sell(accounts, args)
162 .map_err(|e| format!("Sell error: {:?}", e))?;
163 let recent_blockhash = sdk.rpc.get_latest_blockhash()?;
164
165 let mut transaction = Transaction::new_with_payer(&instructions, Some(&user_keypair.pubkey()));
166 transaction.sign(&[user_keypair], recent_blockhash);
167
168 let signature = sdk.rpc.send_and_confirm_transaction(&transaction)?;
169 println!("ā
Tokens sold! TX: {}", signature);
170
171 Ok(())
172}