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
#[allow(unused)]
#[allow(clippy::identity_op)]
mod generated;

pub use generated::programs::LIGHTHOUSE_ID;
pub use generated::programs::LIGHTHOUSE_ID as ID;
use solana_program::pubkey::{Pubkey, PubkeyError};

pub use generated::types;

pub mod instructions {
    pub use crate::generated::instructions::{
        AssertAccountDataBuilder, AssertAccountDeltaBuilder, AssertAccountInfoBuilder,
        AssertMerkleTreeAccountBuilder, AssertMintAccountBuilder, AssertMintAccountMultiBuilder,
        AssertStakeAccountBuilder, AssertStakeAccountMultiBuilder, AssertSysvarClockBuilder,
        AssertTokenAccountBuilder, AssertTokenAccountMultiBuilder,
        AssertUpgradeableLoaderAccountBuilder, AssertUpgradeableLoaderAccountMultiBuilder,
        MemoryCloseBuilder, MemoryWriteBuilder,
    };
}

#[cfg(feature = "cpi")]
pub mod cpi {
    pub use crate::generated::instructions::{
        AssertAccountDataBuilder, AssertAccountDeltaBuilder, AssertAccountInfoBuilder,
        AssertAccountInfoMultiBuilder, AssertMerkleTreeAccountBuilder, AssertMintAccountBuilder,
        AssertMintAccountMultiBuilder, AssertStakeAccountBuilder, AssertStakeAccountMultiBuilder,
        AssertSysvarClockBuilder, AssertTokenAccountBuilder, AssertTokenAccountMultiBuilder,
        AssertUpgradeableLoaderAccountBuilder, MemoryCloseCpiBuilder, MemoryWriteCpiBuilder,
    };
}

pub mod errors {
    pub use crate::generated::errors::*;
}

pub fn find_memory_pda(payer: Pubkey, memory_id: u8) -> (solana_program::pubkey::Pubkey, u8) {
    solana_program::pubkey::Pubkey::find_program_address(
        &["memory".to_string().as_ref(), payer.as_ref(), &[memory_id]],
        &crate::ID,
    )
}

pub fn find_memory_pda_bump_iterate(
    payer: Pubkey,
    memory_id: u8,
    bump_skip: u8,
    start_bump: Option<u8>,
) -> Option<(solana_program::pubkey::Pubkey, u8)> {
    let memory_ref = "memory".to_string();
    let seeds = [memory_ref.as_ref(), payer.as_ref(), &[memory_id]];

    let mut bump_seed = [start_bump.unwrap_or(std::u8::MAX)];
    let mut bump_skip = bump_skip as usize;

    for _ in 0..std::u8::MAX {
        let mut seeds_with_bump = seeds.to_vec();
        seeds_with_bump.push(&bump_seed);
        match Pubkey::create_program_address(&seeds_with_bump, &crate::ID) {
            Ok(address) => {
                if bump_skip == 0 {
                    return Some((address, bump_seed[0]));
                } else {
                    bump_skip -= 1;
                }
            }
            Err(PubkeyError::InvalidSeeds) => {}
            _ => break,
        }
        bump_seed[0] -= 1;

        println!("bump_seed: {:?}", bump_seed[0])
    }

    None
}

#[cfg(feature = "sdk")]
pub mod utils {
    use solana_sdk::{
        instruction::{AccountMeta, Instruction},
        message::{legacy, v0, CompileError, Message, VersionedMessage},
        signer::{Signer, SignerError},
        transaction::{Transaction, VersionedTransaction},
    };

    #[derive(Debug, thiserror::Error, PartialEq, Eq)]
    #[repr(u32)]
    pub enum ClientError {
        #[error("Transaction already signed")]
        TransactionAlreadySigned,
        #[error("Address table lookups not supported")]
        AddressTableLookupsNotSupported,
        #[error("Empty transaction")]
        EmptyTransaction,
        #[error("...")]
        CompileError(CompileError),
        #[error("...")]
        SignerError(SignerError),
    }

    pub fn append_instructions_to_transaction(
        transaction: &Transaction,
        ixs: Vec<Instruction>,
    ) -> Result<Transaction, ClientError> {
        if !transaction.signatures.is_empty() {
            return Err(ClientError::TransactionAlreadySigned);
        }

        let mut merged_ixs = decompile_instruction_from_transaction(transaction)?;
        merged_ixs.extend(ixs);

        let transaction = Transaction::new_unsigned(Message::new(
            &merged_ixs,
            Some(
                transaction
                    .message
                    .account_keys
                    .first()
                    .ok_or(ClientError::EmptyTransaction)?,
            ),
        ));

        Ok(transaction)
    }

    pub fn append_instructions_to_versioned_transaction(
        transaction: &VersionedTransaction,
        ixs: Vec<Instruction>,
        signers: &[&dyn Signer],
    ) -> Result<VersionedTransaction, ClientError> {
        if !transaction.signatures.is_empty() {
            return Err(ClientError::TransactionAlreadySigned);
        }

        if transaction.message.address_table_lookups().is_some() {
            return Err(ClientError::AddressTableLookupsNotSupported);
        }

        let mut merged_ixs = decompile_instruction_from_versioned_transaction(transaction)?;
        merged_ixs.extend(ixs);

        let payer = transaction
            .message
            .static_account_keys()
            .first()
            .ok_or(ClientError::EmptyTransaction)?;

        let versioned_messsage = match transaction.message {
            VersionedMessage::Legacy(_) => {
                VersionedMessage::Legacy(legacy::Message::new(&merged_ixs, Some(payer)))
            }
            VersionedMessage::V0(_) => VersionedMessage::V0(
                v0::Message::try_compile(
                    payer,
                    &merged_ixs,
                    &[],
                    *transaction.message.recent_blockhash(),
                )
                .map_err(ClientError::CompileError)?,
            ),
        };

        let mut transaction = VersionedTransaction::try_new(versioned_messsage, signers)
            .map_err(ClientError::SignerError)?;

        transaction
            .message
            .set_recent_blockhash(*transaction.message.recent_blockhash());

        Ok(transaction)
    }

    pub fn decompile_instruction_from_versioned_transaction(
        transaction: &VersionedTransaction,
    ) -> Result<Vec<Instruction>, ClientError> {
        if !transaction.signatures.is_empty() {
            return Err(ClientError::TransactionAlreadySigned);
        }

        if transaction.message.address_table_lookups().is_some() {
            return Err(ClientError::AddressTableLookupsNotSupported);
        }

        let mut modified_ixs = vec![];
        let compiled_ixs = transaction.message.instructions();

        for instruction in compiled_ixs {
            let account_keys = transaction.message.static_account_keys();

            modified_ixs.push(Instruction {
                program_id: account_keys[instruction.program_id_index as usize],
                accounts: instruction
                    .accounts
                    .iter()
                    .map(|index| AccountMeta {
                        pubkey: account_keys[*index as usize],
                        is_signer: index < &transaction.message.header().num_required_signatures,
                        is_writable: transaction.message.is_maybe_writable(*index as usize),
                    })
                    .collect(),
                data: instruction.data.clone(),
            });
        }

        Ok(modified_ixs)
    }

    pub fn decompile_instruction_from_transaction(
        transaction: &Transaction,
    ) -> Result<Vec<Instruction>, ClientError> {
        if !transaction.signatures.is_empty() {
            return Err(ClientError::TransactionAlreadySigned);
        }

        let mut modified_ixs = vec![];

        for instruction in &transaction.message.instructions {
            modified_ixs.push(Instruction {
                program_id: transaction.message.account_keys[instruction.program_id_index as usize],
                accounts: instruction
                    .accounts
                    .iter()
                    .map(|index| AccountMeta {
                        pubkey: transaction.message.account_keys[*index as usize],
                        is_signer: index < &transaction.message.header.num_required_signatures,
                        is_writable: transaction.message.is_writable(*index as usize),
                    })
                    .collect(),
                data: instruction.data.clone(),
            });
        }

        Ok(modified_ixs)
    }
}