Skip to main content

pumpdotfun_sdk/instructions/
create.rs

1use crate::{
2    constants::{
3        ASSOCIATED_TOKEN_PROGRAM, EVENT_AUTHORITY, MPL_TOKEN_METADATA, RENT_SYSVAR, SYSTEM_PROGRAM,
4        TOKEN_PROGRAM,
5    },
6    pda::{
7        get_associated_bonding_curve, get_bonding_curve_pda, get_global_pda, get_metadata_pda,
8        get_mint_authority_pda,
9    },
10    PUMP_DOT_FUN_PROGRAM_ID,
11};
12use anchor_lang::prelude::*;
13use solana_sdk::instruction::{AccountMeta, Instruction};
14
15#[derive(AnchorDeserialize, AnchorSerialize)]
16pub struct CreateArgs {
17    pub name: String,
18    pub symbol: String,
19    pub uri: String,
20    pub creator: Pubkey,
21}
22
23pub struct CreateAccounts {
24    pub mint: Pubkey,
25    pub user: Pubkey,
26}
27
28pub fn create_ix(program_id: &Pubkey, accounts: CreateAccounts, args: CreateArgs) -> Instruction {
29    let discriminator = [24, 30, 200, 40, 5, 28, 7, 119];
30    let mut data = discriminator.to_vec();
31    data.extend(args.try_to_vec().unwrap());
32
33    let bonding_curve = get_bonding_curve_pda(&accounts.mint);
34    let associated_bonding_curve = get_associated_bonding_curve(&accounts.mint);
35    let metadata_pda = get_metadata_pda(&accounts.mint);
36    let global_pda = get_global_pda();
37    let mint_authority_pda = get_mint_authority_pda();
38
39    let accounts = vec![
40        AccountMeta::new(accounts.mint, true),
41        AccountMeta::new(mint_authority_pda, false),
42        AccountMeta::new(bonding_curve, false),
43        AccountMeta::new(associated_bonding_curve, false),
44        AccountMeta::new(global_pda, false),
45        AccountMeta::new_readonly(MPL_TOKEN_METADATA, false),
46        AccountMeta::new(metadata_pda, false),
47        AccountMeta::new(accounts.user, true),
48        AccountMeta::new_readonly(SYSTEM_PROGRAM, false),
49        AccountMeta::new_readonly(TOKEN_PROGRAM, false),
50        AccountMeta::new_readonly(ASSOCIATED_TOKEN_PROGRAM, false),
51        AccountMeta::new_readonly(RENT_SYSVAR, false),
52        AccountMeta::new_readonly(EVENT_AUTHORITY, false),
53        AccountMeta::new_readonly(PUMP_DOT_FUN_PROGRAM_ID, false),
54    ];
55
56    Instruction {
57        program_id: *program_id,
58        accounts,
59        data,
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use solana_sdk::pubkey::Pubkey;
67
68    #[test]
69    fn test_create_ix() {
70        // Mock data for CreateAccounts
71        let accounts = CreateAccounts {
72            mint: Pubkey::new_unique(),
73            user: Pubkey::new_unique(),
74        };
75
76        // Mock data for CreateArgs
77        let args = CreateArgs {
78            name: "Test Token".to_string(),
79            symbol: "TEST".to_string(),
80            uri: "http://test.uri".to_string(),
81            creator: Pubkey::new_unique(),
82        };
83
84        let program_id = Pubkey::new_unique(); // Dummy program ID for testing
85
86        let instruction = create_ix(&program_id, accounts, args);
87
88        // Assertions
89        assert_eq!(instruction.program_id, program_id);
90        // Add more assertions here to check accounts and data
91        // For example:
92        // assert_eq!(instruction.accounts.len(), 13); // Check the number of accounts
93        // assert!(instruction.data.starts_with(&[24, 30, 200, 40, 5, 28, 7, 119])); // Check discriminator
94    }
95}