pump-rust-client 0.1.5

Rust SDK for the pump and pump_amm Solana programs: instruction builders, quoting, PDA helpers, and optional RPC client features.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! RPC wrapper around [`crate::sdk::PumpSdk`].

use std::sync::Arc;

use solana_client::{
    nonblocking::rpc_client::RpcClient, rpc_client::SerializableTransaction,
    rpc_response::RpcSimulateTransactionResult,
};
use solana_sdk::{
    account::Account, compute_budget::ComputeBudgetInstruction, hash::Hash,
    instruction::Instruction, pubkey::Pubkey, signature::Signature, signer::Signer,
    transaction::Transaction,
};

use crate::{
    accounts::{
        decode_bonding_curve, decode_fee_config, decode_global, decode_global_volume_accumulator,
        decode_user_volume_accumulator_nullable,
    },
    errors::{PumpClientError, Result},
    pda,
    sdk::PumpSdk,
    state::{BondingCurve, FeeConfig, Global, GlobalVolumeAccumulator, UserVolumeAccumulator},
};

#[derive(Clone)]
pub struct AsyncPumpClient {
    rpc: Arc<RpcClient>,
    sdk: PumpSdk,
}

/// Account snapshot for building a buy.
#[derive(Debug)]
pub struct BuyState {
    pub bonding_curve_account: Account,
    pub bonding_curve: BondingCurve,
    pub associated_user_account: Option<Account>,
}

/// Account snapshot for building a sell.
#[derive(Debug)]
pub struct SellState {
    pub bonding_curve_account: Account,
    pub bonding_curve: BondingCurve,
}

/// Optional compute-budget instructions prepended to built transactions (limit, then price).
#[derive(Clone, Copy, Debug, Default)]
pub struct ComputeBudget {
    pub units: Option<u32>,
    pub micro_lamports_per_unit: Option<u64>,
}

impl ComputeBudget {
    fn prepend_into(&self, base: &[Instruction]) -> Vec<Instruction> {
        let extra = self.units.is_some() as usize + self.micro_lamports_per_unit.is_some() as usize;
        let mut out = Vec::with_capacity(base.len() + extra);
        if let Some(units) = self.units {
            out.push(ComputeBudgetInstruction::set_compute_unit_limit(units));
        }
        if let Some(price) = self.micro_lamports_per_unit {
            out.push(ComputeBudgetInstruction::set_compute_unit_price(price));
        }
        out.extend_from_slice(base);
        out
    }
}

impl AsyncPumpClient {
    pub fn new(rpc: Arc<RpcClient>) -> Self {
        Self {
            rpc,
            sdk: PumpSdk::new(),
        }
    }

    pub fn rpc(&self) -> &Arc<RpcClient> {
        &self.rpc
    }

    pub fn sdk(&self) -> &PumpSdk {
        &self.sdk
    }

    pub async fn fetch_global(&self) -> Result<Global> {
        let address = pda::pump::global().0;
        let account = self.get_account(&address, "global").await?;
        decode_global(&account.data)
    }

    pub async fn fetch_fee_config(&self) -> Result<FeeConfig> {
        let address = pda::pump::fee_config().0;
        let account = self.get_account(&address, "fee_config").await?;
        decode_fee_config(&account.data)
    }

    pub async fn fetch_bonding_curve(&self, mint: &Pubkey) -> Result<BondingCurve> {
        let address = pda::pump::bonding_curve(mint).0;
        let account = self.get_account(&address, "bonding_curve").await?;
        decode_bonding_curve(&account.data)
    }

    pub async fn fetch_global_volume_accumulator(&self) -> Result<GlobalVolumeAccumulator> {
        let address = pda::pump::global_volume_accumulator().0;
        let account = self
            .get_account(&address, "global_volume_accumulator")
            .await?;
        decode_global_volume_accumulator(&account.data)
    }

    pub async fn fetch_user_volume_accumulator(
        &self,
        user: &Pubkey,
    ) -> Result<Option<UserVolumeAccumulator>> {
        let address = pda::pump::user_volume_accumulator(user).0;
        match self
            .rpc
            .get_account_with_commitment(&address, self.rpc.commitment())
            .await
        {
            Ok(response) => match response.value {
                Some(account) => Ok(decode_user_volume_accumulator_nullable(&account.data)),
                None => Ok(None),
            },
            Err(e) => Err(PumpClientError::from(e)),
        }
    }

    pub async fn fetch_buy_state(
        &self,
        mint: &Pubkey,
        user: &Pubkey,
        token_program: &Pubkey,
    ) -> Result<BuyState> {
        let (bonding_curve_account, bonding_curve, associated_user_account) = self
            .fetch_bonding_curve_with_user_token_account(mint, user, token_program)
            .await?;
        Ok(BuyState {
            bonding_curve_account,
            bonding_curve,
            associated_user_account,
        })
    }

    pub async fn fetch_sell_state(
        &self,
        mint: &Pubkey,
        user: &Pubkey,
        token_program: &Pubkey,
    ) -> Result<SellState> {
        let (bonding_curve_account, bonding_curve, associated_user_account) = self
            .fetch_bonding_curve_with_user_token_account(mint, user, token_program)
            .await?;
        if associated_user_account.is_none() {
            return Err(PumpClientError::AccountNotFound {
                name: "associated_user",
                address: pda::associated_token(user, token_program, mint).0,
            });
        }
        Ok(SellState {
            bonding_curve_account,
            bonding_curve,
        })
    }

    async fn fetch_bonding_curve_with_user_token_account(
        &self,
        mint: &Pubkey,
        user: &Pubkey,
        token_program: &Pubkey,
    ) -> Result<(Account, BondingCurve, Option<Account>)> {
        let bonding_curve_address = pda::pump::bonding_curve(mint).0;
        let associated_user = pda::associated_token(user, token_program, mint).0;

        let mut accounts = self
            .rpc
            .get_multiple_accounts(&[bonding_curve_address, associated_user])
            .await
            .map_err(PumpClientError::from)?
            .into_iter();

        let bonding_curve_account =
            accounts
                .next()
                .flatten()
                .ok_or(PumpClientError::AccountNotFound {
                    name: "bonding_curve",
                    address: bonding_curve_address,
                })?;
        let associated_user_account = accounts.next().flatten();
        let bonding_curve = decode_bonding_curve(&bonding_curve_account.data)?;
        Ok((bonding_curve_account, bonding_curve, associated_user_account))
    }

    /// Spendable lamports in the creator vault (above rent); 0 if missing or rent-only.
    pub async fn get_creator_vault_balance(&self, creator: &Pubkey) -> Result<u64> {
        let creator_vault = pda::pump::creator_vault(creator).0;

        let account = match self
            .rpc
            .get_account_with_commitment(&creator_vault, self.rpc.commitment())
            .await
            .map_err(PumpClientError::from)?
            .value
        {
            Some(account) => account,
            None => return Ok(0),
        };

        let rent_exempt = self
            .rpc
            .get_minimum_balance_for_rent_exemption(account.data.len())
            .await
            .map_err(PumpClientError::from)?;

        if account.lamports <= rent_exempt {
            return Ok(0);
        }
        Ok(account.lamports - rent_exempt)
    }

    /// Recent blockhash at the client's commitment.
    pub async fn latest_blockhash(&self) -> Result<Hash> {
        self.rpc
            .get_latest_blockhash()
            .await
            .map_err(PumpClientError::from)
    }

    /// Fetches a blockhash then signs. Prefer [`Self::build_transaction_with_blockhash`] if you already have a hash.
    pub async fn build_transaction(
        &self,
        ixs: &[Instruction],
        payer: &Pubkey,
        signers: &[&dyn Signer],
        compute_budget: Option<ComputeBudget>,
    ) -> Result<Transaction> {
        let recent_blockhash = self.latest_blockhash().await?;
        Ok(self.build_transaction_with_blockhash(
            ixs,
            payer,
            signers,
            recent_blockhash,
            compute_budget,
        ))
    }

    /// Prepends compute-budget instructions when set, then signs.
    pub fn build_transaction_with_blockhash(
        &self,
        ixs: &[Instruction],
        payer: &Pubkey,
        signers: &[&dyn Signer],
        recent_blockhash: Hash,
        compute_budget: Option<ComputeBudget>,
    ) -> Transaction {
        let full_ixs = match compute_budget {
            Some(cb) => cb.prepend_into(ixs),
            None => ixs.to_vec(),
        };
        Transaction::new_signed_with_payer(&full_ixs, Some(payer), signers, recent_blockhash)
    }

    /// Simulate a transaction.
    pub async fn simulate_transaction<T: SerializableTransaction>(
        &self,
        tx: &T,
    ) -> Result<RpcSimulateTransactionResult> {
        let response = self
            .rpc
            .simulate_transaction(tx)
            .await
            .map_err(PumpClientError::from)?;
        Ok(response.value)
    }

    /// Send without waiting for confirmation.
    pub async fn send_transaction<T: SerializableTransaction>(&self, tx: &T) -> Result<Signature> {
        self.rpc
            .send_transaction(tx)
            .await
            .map_err(PumpClientError::from)
    }

    /// Send and confirm at the client's commitment.
    pub async fn send_and_confirm_transaction<T: SerializableTransaction>(
        &self,
        tx: &T,
    ) -> Result<Signature> {
        self.rpc
            .send_and_confirm_transaction(tx)
            .await
            .map_err(PumpClientError::from)
    }

    async fn get_account(&self, address: &Pubkey, name: &'static str) -> Result<Account> {
        let value = self
            .rpc
            .get_account_with_commitment(address, self.rpc.commitment())
            .await
            .map_err(PumpClientError::from)?
            .value;
        value.ok_or(PumpClientError::AccountNotFound {
            name,
            address: *address,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use solana_sdk::{signature::Keypair, system_instruction};

    #[test]
    fn client_is_send_sync_clone() {
        fn assert_traits<T: Send + Sync + Clone>() {}
        assert_traits::<AsyncPumpClient>();
    }

    #[test]
    fn constructible_against_localhost() {
        let rpc = Arc::new(RpcClient::new("http://localhost:8899".to_string()));
        let client = AsyncPumpClient::new(rpc);
        let _: &PumpSdk = client.sdk();
    }

    fn local_client() -> AsyncPumpClient {
        AsyncPumpClient::new(Arc::new(RpcClient::new(
            "http://localhost:8899".to_string(),
        )))
    }

    fn discriminator(ix: &Instruction) -> Option<u8> {
        ix.data.first().copied()
    }

    #[test]
    fn build_transaction_with_blockhash_prepends_compute_budget_ixs() {
        let client = local_client();
        let payer = Keypair::new();
        let recipient = Pubkey::new_unique();
        let transfer = system_instruction::transfer(&payer.pubkey(), &recipient, 1);
        let blockhash = Hash::new_unique();

        let tx = client.build_transaction_with_blockhash(
            &[transfer],
            &payer.pubkey(),
            &[&payer],
            blockhash,
            Some(ComputeBudget {
                units: Some(200_000),
                micro_lamports_per_unit: Some(1_000),
            }),
        );

        assert_eq!(tx.message.instructions.len(), 3);
        let cb_program = solana_sdk::compute_budget::id();
        assert_eq!(
            tx.message.account_keys[tx.message.instructions[0].program_id_index as usize],
            cb_program,
        );
        assert_eq!(tx.message.instructions[0].data.first(), Some(&2u8));
        assert_eq!(
            tx.message.account_keys[tx.message.instructions[1].program_id_index as usize],
            cb_program,
        );
        assert_eq!(tx.message.instructions[1].data.first(), Some(&3u8));
    }

    #[test]
    fn build_transaction_with_blockhash_emits_only_requested_compute_budget_ixs() {
        let client = local_client();
        let payer = Keypair::new();
        let recipient = Pubkey::new_unique();
        let transfer = system_instruction::transfer(&payer.pubkey(), &recipient, 1);

        let tx = client.build_transaction_with_blockhash(
            &[transfer.clone()],
            &payer.pubkey(),
            &[&payer],
            Hash::new_unique(),
            Some(ComputeBudget {
                units: Some(50_000),
                micro_lamports_per_unit: None,
            }),
        );
        assert_eq!(tx.message.instructions.len(), 2);
        assert_eq!(tx.message.instructions[0].data.first(), Some(&2u8));

        let tx = client.build_transaction_with_blockhash(
            &[transfer],
            &payer.pubkey(),
            &[&payer],
            Hash::new_unique(),
            None,
        );
        assert_eq!(tx.message.instructions.len(), 1);
    }

    #[test]
    fn build_transaction_with_blockhash_signs_with_payer() {
        let client = local_client();
        let payer = Keypair::new();
        let recipient = Pubkey::new_unique();
        let transfer = system_instruction::transfer(&payer.pubkey(), &recipient, 1);
        let blockhash = Hash::new_unique();

        let tx = client.build_transaction_with_blockhash(
            &[transfer],
            &payer.pubkey(),
            &[&payer],
            blockhash,
            None,
        );

        assert!(tx.is_signed());
        assert_eq!(tx.message.recent_blockhash, blockhash);
        assert_eq!(tx.message.account_keys[0], payer.pubkey());
    }

    #[test]
    fn compute_budget_helper_emits_known_discriminators() {
        let limit = ComputeBudgetInstruction::set_compute_unit_limit(0);
        let price = ComputeBudgetInstruction::set_compute_unit_price(0);
        assert_eq!(discriminator(&limit), Some(2));
        assert_eq!(discriminator(&price), Some(3));
    }
}