pump-rust-client 0.1.7

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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use anchor_lang::{InstructionData, ToAccountMetas};
use solana_program::{
    instruction::{AccountMeta, Instruction},
    pubkey::Pubkey,
    system_program,
};

use crate::pump_amm::{
    client as amm_client, types::OptionBool as AmmOptionBool, ID as PUMP_AMM_PROGRAM_ID,
};
use crate::state::pump_amm::{GlobalConfig, Pool};
use crate::token::create_associated_token_account_idempotent;
use crate::{constants, pda};

use super::PumpSdk;

/// Pubkeys derived once and threaded into both `Buy` and `Sell` AMM
/// instructions (which share the same account set apart from buy's extra
/// volume-accumulator slots).
struct AmmTradeAccounts {
    coin_creator_vault_authority: Pubkey,
    user_volume_accumulator: Pubkey,
    user_base_token_account: Pubkey,
    user_quote_token_account: Pubkey,
    pool_base_token_account: Pubkey,
    pool_quote_token_account: Pubkey,
    protocol_fee_recipient_token_account: Pubkey,
    coin_creator_vault_ata: Pubkey,
}

impl AmmTradeAccounts {
    fn derive(
        pool: Pubkey,
        base_mint: Pubkey,
        quote_mint: Pubkey,
        base_token_program: Pubkey,
        quote_token_program: Pubkey,
        user: Pubkey,
        coin_creator: Pubkey,
        protocol_fee_recipient: Pubkey,
    ) -> Self {
        let coin_creator_vault_authority =
            pda::pump_amm::coin_creator_vault_authority(&coin_creator).0;
        let user_volume_accumulator = pda::pump_amm::user_volume_accumulator(&user).0;
        let ata = |owner: &Pubkey, token_program: &Pubkey, mint: &Pubkey| {
            pda::associated_token(owner, token_program, mint).0
        };
        Self {
            coin_creator_vault_authority,
            user_volume_accumulator,
            user_base_token_account: ata(&user, &base_token_program, &base_mint),
            user_quote_token_account: ata(&user, &quote_token_program, &quote_mint),
            pool_base_token_account: ata(&pool, &base_token_program, &base_mint),
            pool_quote_token_account: ata(&pool, &quote_token_program, &quote_mint),
            protocol_fee_recipient_token_account: ata(
                &protocol_fee_recipient,
                &quote_token_program,
                &quote_mint,
            ),
            coin_creator_vault_ata: ata(
                &coin_creator_vault_authority,
                &quote_token_program,
                &quote_mint,
            ),
        }
    }
}

impl PumpSdk {
    /// `pump_amm` buy. Fees from [`GlobalConfig`], pool layout from [`Pool`]. Use default `coin_creator` to omit `pool_v2`.
    pub fn buy_amm_instruction(
        &self,
        pool: Pubkey,
        amm_global: &GlobalConfig,
        pool_state: &Pool,
        base_token_program: Pubkey,
        quote_token_program: Pubkey,
        user: Pubkey,
        base_amount_out: u64,
        max_quote_amount_in: u64,
    ) -> Option<Instruction> {
        let (protocol_fee_recipient, buyback_fee_recipient) =
            Self::amm_fee_recipients_pair(amm_global)?;
        Some(self.buy_amm_instruction_with_recipients(
            pool,
            pool_state.base_mint,
            pool_state.quote_mint,
            base_token_program,
            quote_token_program,
            user,
            pool_state.coin_creator,
            protocol_fee_recipient,
            buyback_fee_recipient,
            pool_state.is_cashback_coin,
            base_amount_out,
            max_quote_amount_in,
        ))
    }

    pub(crate) fn buy_amm_instruction_with_recipients(
        &self,
        pool: Pubkey,
        base_mint: Pubkey,
        quote_mint: Pubkey,
        base_token_program: Pubkey,
        quote_token_program: Pubkey,
        user: Pubkey,
        coin_creator: Pubkey,
        protocol_fee_recipient: Pubkey,
        buyback_fee_recipient: Pubkey,
        is_cashback_coin: bool,
        base_amount_out: u64,
        max_quote_amount_in: u64,
    ) -> Instruction {
        let a = AmmTradeAccounts::derive(
            pool,
            base_mint,
            quote_mint,
            base_token_program,
            quote_token_program,
            user,
            coin_creator,
            protocol_fee_recipient,
        );
        let accounts = amm_client::accounts::Buy {
            pool,
            user,
            global_config: pda::pump_amm::global_config().0,
            base_mint,
            quote_mint,
            user_base_token_account: a.user_base_token_account,
            user_quote_token_account: a.user_quote_token_account,
            pool_base_token_account: a.pool_base_token_account,
            pool_quote_token_account: a.pool_quote_token_account,
            protocol_fee_recipient,
            protocol_fee_recipient_token_account: a.protocol_fee_recipient_token_account,
            base_token_program,
            quote_token_program,
            system_program: system_program::ID,
            associated_token_program: constants::SPL_ATA_PROGRAM_ID,
            event_authority: pda::pump_amm::event_authority().0,
            program: PUMP_AMM_PROGRAM_ID,
            coin_creator_vault_ata: a.coin_creator_vault_ata,
            coin_creator_vault_authority: a.coin_creator_vault_authority,
            global_volume_accumulator: pda::pump_amm::global_volume_accumulator().0,
            user_volume_accumulator: a.user_volume_accumulator,
            fee_config: pda::pump_amm::fee_config().0,
            fee_program: constants::FEE_PROGRAM_ID,
        };
        let args = amm_client::args::Buy {
            base_amount_out,
            max_quote_amount_in,
            track_volume: AmmOptionBool(true),
        };
        let mut metas = accounts.to_account_metas(None);
        if is_cashback_coin {
            metas.push(AccountMeta::new(
                pda::associated_token(
                    &a.user_volume_accumulator,
                    &quote_token_program,
                    &constants::NATIVE_MINT,
                )
                .0,
                false,
            ));
        }
        if coin_creator != Pubkey::default() {
            metas.push(AccountMeta::new_readonly(
                pda::pump_amm::pool_v2(&base_mint).0,
                false,
            ));
        }
        metas.push(AccountMeta::new(buyback_fee_recipient, false));
        metas.push(AccountMeta::new(
            pda::associated_token(&buyback_fee_recipient, &quote_token_program, &quote_mint).0,
            false,
        ));
        Instruction {
            program_id: PUMP_AMM_PROGRAM_ID,
            accounts: metas,
            data: args.data(),
        }
    }

    /// [`Self::buy_amm_instruction`] plus idempotent user base ATA create.
    pub fn buy_amm_instructions(
        &self,
        pool: Pubkey,
        amm_global: &GlobalConfig,
        pool_state: &Pool,
        base_token_program: Pubkey,
        quote_token_program: Pubkey,
        user: Pubkey,
        base_amount_out: u64,
        max_quote_amount_in: u64,
    ) -> Option<Vec<Instruction>> {
        let buy = self.buy_amm_instruction(
            pool,
            amm_global,
            pool_state,
            base_token_program,
            quote_token_program,
            user,
            base_amount_out,
            max_quote_amount_in,
        )?;
        Some(vec![
            create_associated_token_account_idempotent(
                &user,
                &user,
                &pool_state.base_mint,
                &base_token_program,
            ),
            buy,
        ])
    }

    /// `pump_amm` sell (remaining accounts differ from buy for cashback / buyback).
    pub fn sell_amm_instruction(
        &self,
        pool: Pubkey,
        amm_global: &GlobalConfig,
        pool_state: &Pool,
        base_token_program: Pubkey,
        quote_token_program: Pubkey,
        user: Pubkey,
        base_amount_in: u64,
        min_quote_amount_out: u64,
    ) -> Option<Instruction> {
        let (protocol_fee_recipient, buyback_fee_recipient) =
            Self::amm_fee_recipients_pair(amm_global)?;
        Some(self.sell_amm_instruction_with_recipients(
            pool,
            pool_state.base_mint,
            pool_state.quote_mint,
            base_token_program,
            quote_token_program,
            user,
            pool_state.coin_creator,
            protocol_fee_recipient,
            buyback_fee_recipient,
            pool_state.is_cashback_coin,
            base_amount_in,
            min_quote_amount_out,
        ))
    }

    pub(crate) fn sell_amm_instruction_with_recipients(
        &self,
        pool: Pubkey,
        base_mint: Pubkey,
        quote_mint: Pubkey,
        base_token_program: Pubkey,
        quote_token_program: Pubkey,
        user: Pubkey,
        coin_creator: Pubkey,
        protocol_fee_recipient: Pubkey,
        buyback_fee_recipient: Pubkey,
        is_cashback_coin: bool,
        base_amount_in: u64,
        min_quote_amount_out: u64,
    ) -> Instruction {
        let a = AmmTradeAccounts::derive(
            pool,
            base_mint,
            quote_mint,
            base_token_program,
            quote_token_program,
            user,
            coin_creator,
            protocol_fee_recipient,
        );
        let accounts = amm_client::accounts::Sell {
            pool,
            user,
            global_config: pda::pump_amm::global_config().0,
            base_mint,
            quote_mint,
            user_base_token_account: a.user_base_token_account,
            user_quote_token_account: a.user_quote_token_account,
            pool_base_token_account: a.pool_base_token_account,
            pool_quote_token_account: a.pool_quote_token_account,
            protocol_fee_recipient,
            protocol_fee_recipient_token_account: a.protocol_fee_recipient_token_account,
            base_token_program,
            quote_token_program,
            system_program: system_program::ID,
            associated_token_program: constants::SPL_ATA_PROGRAM_ID,
            event_authority: pda::pump_amm::event_authority().0,
            program: PUMP_AMM_PROGRAM_ID,
            coin_creator_vault_ata: a.coin_creator_vault_ata,
            coin_creator_vault_authority: a.coin_creator_vault_authority,
            fee_config: pda::pump_amm::fee_config().0,
            fee_program: constants::FEE_PROGRAM_ID,
        };
        let args = amm_client::args::Sell {
            base_amount_in,
            min_quote_amount_out,
        };
        let mut metas = accounts.to_account_metas(None);
        if is_cashback_coin {
            metas.push(AccountMeta::new(
                pda::associated_token(
                    &a.user_volume_accumulator,
                    &quote_token_program,
                    &quote_mint,
                )
                .0,
                false,
            ));
            metas.push(AccountMeta::new(a.user_volume_accumulator, false));
        }
        if coin_creator != Pubkey::default() {
            metas.push(AccountMeta::new_readonly(
                pda::pump_amm::pool_v2(&base_mint).0,
                false,
            ));
        }
        metas.push(AccountMeta::new_readonly(buyback_fee_recipient, false));
        metas.push(AccountMeta::new(
            pda::associated_token(&buyback_fee_recipient, &quote_token_program, &quote_mint).0,
            false,
        ));
        Instruction {
            program_id: PUMP_AMM_PROGRAM_ID,
            accounts: metas,
            data: args.data(),
        }
    }

    /// [`Self::sell_amm_instruction`] plus idempotent user quote ATA create.
    pub fn sell_amm_instructions(
        &self,
        pool: Pubkey,
        amm_global: &GlobalConfig,
        pool_state: &Pool,
        base_token_program: Pubkey,
        quote_token_program: Pubkey,
        user: Pubkey,
        base_amount_in: u64,
        min_quote_amount_out: u64,
    ) -> Option<Vec<Instruction>> {
        let sell = self.sell_amm_instruction(
            pool,
            amm_global,
            pool_state,
            base_token_program,
            quote_token_program,
            user,
            base_amount_in,
            min_quote_amount_out,
        )?;
        Some(vec![
            create_associated_token_account_idempotent(
                &user,
                &user,
                &pool_state.quote_mint,
                &quote_token_program,
            ),
            sell,
        ])
    }

    /// `collect_coin_creator_fee`. Sweeps the AMM coin-creator vault's
    /// `quote_mint` balance into the coin creator's own quote ATA.
    /// Works for any supported quote mint (wSOL or non-SOL).
    pub fn collect_coin_creator_fee_instruction(
        &self,
        coin_creator: Pubkey,
        quote_mint: Pubkey,
        quote_token_program: Pubkey,
    ) -> Instruction {
        let coin_creator_vault_authority =
            pda::pump_amm::coin_creator_vault_authority(&coin_creator).0;
        let coin_creator_vault_ata = pda::associated_token(
            &coin_creator_vault_authority,
            &quote_token_program,
            &quote_mint,
        )
        .0;
        let coin_creator_token_account =
            pda::associated_token(&coin_creator, &quote_token_program, &quote_mint).0;
        let accounts = amm_client::accounts::CollectCoinCreatorFee {
            quote_mint,
            quote_token_program,
            coin_creator,
            coin_creator_vault_authority,
            coin_creator_vault_ata,
            coin_creator_token_account,
            event_authority: pda::pump_amm::event_authority().0,
            program: PUMP_AMM_PROGRAM_ID,
        };
        Instruction {
            program_id: PUMP_AMM_PROGRAM_ID,
            accounts: accounts.to_account_metas(None),
            data: amm_client::args::CollectCoinCreatorFee.data(),
        }
    }

    /// [`Self::collect_coin_creator_fee_instruction`] with an optional
    /// idempotent ATA create for the coin creator's quote ATA prepended.
    /// `payer` only matters when the ATA create runs.
    pub fn collect_coin_creator_fee_instructions(
        &self,
        payer: Pubkey,
        coin_creator: Pubkey,
        quote_mint: Pubkey,
        quote_token_program: Pubkey,
        create_coin_creator_ata: bool,
    ) -> Vec<Instruction> {
        let mut ixs = Vec::with_capacity(2);
        if create_coin_creator_ata {
            ixs.push(create_associated_token_account_idempotent(
                &payer,
                &coin_creator,
                &quote_mint,
                &quote_token_program,
            ));
        }
        ixs.push(self.collect_coin_creator_fee_instruction(
            coin_creator,
            quote_mint,
            quote_token_program,
        ));
        ixs
    }

    pub fn transfer_creator_fees_to_pump_v2_instruction(
        &self,
        payer: Pubkey,
        coin_creator: Pubkey,
        quote_mint: Pubkey,
        token_program: Pubkey,
    ) -> Instruction {
        let coin_creator_vault_authority =
            pda::pump_amm::coin_creator_vault_authority(&coin_creator).0;
        let coin_creator_vault_ata =
            pda::associated_token(&coin_creator_vault_authority, &token_program, &quote_mint).0;
        let pump_creator_vault = pda::pump::creator_vault(&coin_creator).0;
        let pump_creator_vault_ata =
            pda::associated_token(&pump_creator_vault, &token_program, &quote_mint).0;
        let accounts = amm_client::accounts::TransferCreatorFeesToPumpV2 {
            payer,
            quote_mint,
            token_program,
            system_program: system_program::ID,
            associated_token_program: constants::SPL_ATA_PROGRAM_ID,
            coin_creator,
            coin_creator_vault_authority,
            coin_creator_vault_ata,
            pump_creator_vault,
            pump_creator_vault_ata,
            event_authority: pda::pump_amm::event_authority().0,
            program: PUMP_AMM_PROGRAM_ID,
        };
        Instruction {
            program_id: PUMP_AMM_PROGRAM_ID,
            accounts: accounts.to_account_metas(None),
            data: amm_client::args::TransferCreatorFeesToPumpV2.data(),
        }
    }
}