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