carbon_system_program_decoder/instructions/
mod.rs

1use super::SystemProgramDecoder;
2pub mod advance_nonce_account;
3pub mod allocate;
4pub mod allocate_with_seed;
5pub mod assign;
6pub mod assign_with_seed;
7pub mod authorize_nonce_account;
8pub mod create_account;
9pub mod create_account_with_seed;
10pub mod initialize_nonce_account;
11pub mod transfer_sol;
12pub mod transfer_sol_with_seed;
13pub mod upgrade_nonce_account;
14pub mod withdraw_nonce_account;
15
16#[derive(
17    carbon_core::InstructionType, serde::Serialize, serde::Deserialize, PartialEq, Debug, Clone,
18)]
19pub enum SystemProgramInstruction {
20    CreateAccount(create_account::CreateAccount),
21    Assign(assign::Assign),
22    TransferSol(transfer_sol::TransferSol),
23    CreateAccountWithSeed(create_account_with_seed::CreateAccountWithSeed),
24    AdvanceNonceAccount(advance_nonce_account::AdvanceNonceAccount),
25    WithdrawNonceAccount(withdraw_nonce_account::WithdrawNonceAccount),
26    InitializeNonceAccount(initialize_nonce_account::InitializeNonceAccount),
27    AuthorizeNonceAccount(authorize_nonce_account::AuthorizeNonceAccount),
28    Allocate(allocate::Allocate),
29    AllocateWithSeed(allocate_with_seed::AllocateWithSeed),
30    AssignWithSeed(assign_with_seed::AssignWithSeed),
31    TransferSolWithSeed(transfer_sol_with_seed::TransferSolWithSeed),
32    UpgradeNonceAccount(upgrade_nonce_account::UpgradeNonceAccount),
33}
34
35impl carbon_core::instruction::InstructionDecoder<'_> for SystemProgramDecoder {
36    type InstructionType = SystemProgramInstruction;
37
38    fn decode_instruction(
39        &self,
40        instruction: &solana_instruction::Instruction,
41    ) -> Option<carbon_core::instruction::DecodedInstruction<Self::InstructionType>> {
42        if !instruction
43            .program_id
44            .eq(&solana_program::system_program::id())
45        {
46            return None;
47        }
48
49        carbon_core::try_decode_instructions!(instruction,
50            SystemProgramInstruction::CreateAccount => create_account::CreateAccount,
51            SystemProgramInstruction::Assign => assign::Assign,
52            SystemProgramInstruction::TransferSol => transfer_sol::TransferSol,
53            SystemProgramInstruction::CreateAccountWithSeed => create_account_with_seed::CreateAccountWithSeed,
54            SystemProgramInstruction::AdvanceNonceAccount => advance_nonce_account::AdvanceNonceAccount,
55            SystemProgramInstruction::WithdrawNonceAccount => withdraw_nonce_account::WithdrawNonceAccount,
56            SystemProgramInstruction::InitializeNonceAccount => initialize_nonce_account::InitializeNonceAccount,
57            SystemProgramInstruction::AuthorizeNonceAccount => authorize_nonce_account::AuthorizeNonceAccount,
58            SystemProgramInstruction::Allocate => allocate::Allocate,
59            SystemProgramInstruction::AllocateWithSeed => allocate_with_seed::AllocateWithSeed,
60            SystemProgramInstruction::AssignWithSeed => assign_with_seed::AssignWithSeed,
61            SystemProgramInstruction::TransferSolWithSeed => transfer_sol_with_seed::TransferSolWithSeed,
62            SystemProgramInstruction::UpgradeNonceAccount => upgrade_nonce_account::UpgradeNonceAccount,
63        )
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use alloc::{string::ToString, vec};
70    use carbon_core::{
71        deserialize::{ArrangeAccounts, U64PrefixString},
72        instruction::InstructionDecoder,
73    };
74    use solana_instruction::AccountMeta;
75
76    use super::*;
77
78    #[test]
79    fn test_decode_create_with_seed() {
80        // Arrange
81        let expected_ix = SystemProgramInstruction::CreateAccountWithSeed(
82            create_account_with_seed::CreateAccountWithSeed {
83                base: solana_pubkey::Pubkey::from_str_const(
84                    "6bBmDxYqXeFbXN8SmtjTpiA3SrEDKsxK8RG6yhPGpa9G",
85                ),
86                seed: U64PrefixString("CF9nRGJcFhH57xgcPxaamBs5pHxHexP9".to_string()),
87                space: 165,
88                amount: 1283531083,
89                program_address: solana_pubkey::Pubkey::from_str_const(
90                    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
91                ),
92            },
93        );
94        let expected_accounts = vec![
95            AccountMeta::new(
96                solana_pubkey::Pubkey::from_str_const(
97                    "6bBmDxYqXeFbXN8SmtjTpiA3SrEDKsxK8RG6yhPGpa9G",
98                ),
99                true,
100            ),
101            AccountMeta::new(
102                solana_pubkey::Pubkey::from_str_const(
103                    "3MoeLKJVQHNUtTAXEurLAQtCSXpLGAvairYEHpkqW6CC",
104                ),
105                false,
106            ),
107        ];
108        let expected_arranged_accounts =
109            create_account_with_seed::CreateAccountWithSeedInstructionAccounts {
110                payer: solana_pubkey::Pubkey::from_str_const(
111                    "6bBmDxYqXeFbXN8SmtjTpiA3SrEDKsxK8RG6yhPGpa9G",
112                ),
113                new_account: solana_pubkey::Pubkey::from_str_const(
114                    "3MoeLKJVQHNUtTAXEurLAQtCSXpLGAvairYEHpkqW6CC",
115                ),
116                base_account: solana_pubkey::Pubkey::from_str_const(
117                    "6bBmDxYqXeFbXN8SmtjTpiA3SrEDKsxK8RG6yhPGpa9G",
118                ),
119            };
120
121        // Act
122        let decoder = SystemProgramDecoder;
123        let instruction =
124            carbon_test_utils::read_instruction("tests/fixtures/create_with_seed_ix.json")
125                .expect("read fixture");
126        let decoded = decoder
127            .decode_instruction(&instruction)
128            .expect("decode instruction");
129        let decoded_arranged_accounts =
130            create_account_with_seed::CreateAccountWithSeed::arrange_accounts(
131                &instruction.accounts,
132            )
133            .expect("aranage accounts");
134
135        // Assert
136        assert_eq!(decoded.data, expected_ix);
137        assert_eq!(decoded.accounts, expected_accounts);
138        assert_eq!(decoded.program_id, solana_program::system_program::id());
139        assert_eq!(decoded_arranged_accounts, expected_arranged_accounts);
140    }
141}