trident_fuzz/trident/
system.rs

1use solana_sdk::account::ReadableAccount;
2use solana_sdk::instruction::Instruction;
3use solana_sdk::pubkey::Pubkey;
4
5use crate::trident::Trident;
6
7impl Trident {
8    #[allow(dead_code)]
9    pub(crate) fn create_account_internal(
10        &mut self,
11        address: &Pubkey,
12        from: &Pubkey,
13        space: usize,
14        owner: &Pubkey,
15    ) -> Vec<Instruction> {
16        let account = self.client.get_account(address).unwrap_or_default();
17
18        let rent = solana_sdk::rent::Rent::default();
19        if account.lamports() > 0 {
20            let mut instructions = vec![];
21            let lamports_required = rent.minimum_balance(space);
22
23            let remaining_lamports = lamports_required.saturating_sub(account.lamports());
24
25            if remaining_lamports > 0 {
26                let transfer =
27                    solana_sdk::system_instruction::transfer(from, address, remaining_lamports);
28                instructions.push(transfer);
29            }
30
31            let allocate = solana_sdk::system_instruction::allocate(address, space as u64);
32            instructions.push(allocate);
33
34            let assign = solana_sdk::system_instruction::assign(address, owner);
35            instructions.push(assign);
36
37            instructions
38        } else {
39            let ix = solana_sdk::system_instruction::create_account(
40                from,
41                address,
42                rent.minimum_balance(space),
43                space as u64,
44                owner,
45            );
46            vec![ix]
47        }
48    }
49    /// Creates a new account
50    ///
51    /// Generates a system program create_account instruction to allocate space
52    /// and assign ownership of a new account.
53    ///
54    /// # Arguments
55    /// * `from_pubkey` - The public key of the account funding the new account
56    /// * `to_pubkey` - The public key of the new account to create
57    /// * `lamports` - The number of lamports to transfer to the new account
58    /// * `space` - The number of bytes to allocate for the account data
59    /// * `owner` - The program that will own the new account
60    ///
61    /// # Returns
62    /// An instruction that needs to be executed with `process_transaction`
63    pub fn create_account(
64        &mut self,
65        from_pubkey: &Pubkey,
66        to_pubkey: &Pubkey,
67        lamports: u64,
68        space: u64,
69        owner: &Pubkey,
70    ) -> Instruction {
71        solana_sdk::system_instruction::create_account(
72            from_pubkey,
73            to_pubkey,
74            lamports,
75            space,
76            owner,
77        )
78    }
79
80    /// Allocates space for an account
81    ///
82    /// Generates a system program allocate instruction to allocate the specified
83    /// number of bytes for an account's data.
84    ///
85    /// # Arguments
86    /// * `address` - The public key of the account to allocate space for
87    /// * `space` - The number of bytes to allocate
88    ///
89    /// # Returns
90    /// An instruction that needs to be executed with `process_transaction`
91    ///
92    /// # Note
93    /// This will succeed on PDAs in Trident, but would fail on-chain outside of a program invocation
94    /// since PDAs cannot sign transactions
95    pub fn allocate(&mut self, address: &Pubkey, space: u64) -> Instruction {
96        solana_sdk::system_instruction::allocate(address, space)
97    }
98
99    /// Assigns an account to a program
100    ///
101    /// Generates a system program assign instruction to change the owner
102    /// of an account to the specified program.
103    ///
104    /// # Arguments
105    /// * `address` - The public key of the account to assign
106    /// * `owner` - The public key of the program that will own the account
107    ///
108    /// # Returns
109    /// An instruction that needs to be executed with `process_transaction`
110    ///
111    /// # Note
112    /// This will succeed on PDAs in Trident, but would fail on-chain outside of a program invocation
113    /// since PDAs cannot sign transactions
114    pub fn assign(&mut self, address: &Pubkey, owner: &Pubkey) -> Instruction {
115        solana_sdk::system_instruction::assign(address, owner)
116    }
117
118    /// Transfers SOL from one account to another
119    ///
120    /// Generates a system program transfer instruction to move the specified amount
121    /// of lamports from the source to destination account.
122    ///
123    /// # Arguments
124    /// * `from` - The public key of the account to transfer from
125    /// * `to` - The public key of the account to transfer to
126    /// * `amount` - The number of lamports to transfer
127    ///
128    /// # Returns
129    /// An instruction that needs to be executed with `process_transaction`
130    pub fn transfer(&mut self, from: &Pubkey, to: &Pubkey, amount: u64) -> Instruction {
131        solana_sdk::system_instruction::transfer(from, to, amount)
132    }
133}