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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! Mock types for use in examples.
//!
//! These represent APIs from crates that themselves depend on this crate, and
//! which are useful for illustrating the examples for APIs in this crate.
//!
//! Directly depending on these crates though would cause problematic circular
//! dependencies, so instead they are mocked out here in a way that allows
//! examples to appear to use crates that this crate must not depend on.
//!
//! Each mod here has the name of a crate, so that examples can be structured to
//! appear to import from that crate.

#![doc(hidden)]
#![allow(clippy::new_without_default)]

pub mod solana_rpc_client {
    pub mod rpc_client {
        use {
            super::super::{
                solana_rpc_client_api::client_error::Result as ClientResult,
                solana_sdk::{
                    account::Account, hash::Hash, pubkey::Pubkey, signature::Signature,
                    transaction::Transaction,
                },
            },
            std::{cell::RefCell, collections::HashMap, rc::Rc},
        };

        #[derive(Default)]
        pub struct RpcClient {
            get_account_responses: Rc<RefCell<HashMap<Pubkey, Account>>>,
        }

        impl RpcClient {
            pub fn new(_url: String) -> Self {
                RpcClient::default()
            }

            pub fn get_latest_blockhash(&self) -> ClientResult<Hash> {
                Ok(Hash::default())
            }

            pub fn send_and_confirm_transaction(
                &self,
                _transaction: &Transaction,
            ) -> ClientResult<Signature> {
                Ok(Signature)
            }

            pub fn get_minimum_balance_for_rent_exemption(
                &self,
                _data_len: usize,
            ) -> ClientResult<u64> {
                Ok(0)
            }

            pub fn get_account(&self, pubkey: &Pubkey) -> ClientResult<Account> {
                Ok(self
                    .get_account_responses
                    .borrow()
                    .get(pubkey)
                    .cloned()
                    .unwrap())
            }

            pub fn set_get_account_response(&self, pubkey: Pubkey, account: Account) {
                self.get_account_responses
                    .borrow_mut()
                    .insert(pubkey, account);
            }

            pub fn get_balance(&self, _pubkey: &Pubkey) -> ClientResult<u64> {
                Ok(0)
            }
        }
    }
}

pub mod solana_rpc_client_api {
    pub mod client_error {
        #[derive(thiserror::Error, Debug)]
        #[error("mock-error")]
        pub struct ClientError;
        pub type Result<T> = std::result::Result<T, ClientError>;
    }
}

pub mod solana_rpc_client_nonce_utils {
    use {
        super::solana_sdk::{account::ReadableAccount, account_utils::StateMut, pubkey::Pubkey},
        crate::nonce::state::{Data, DurableNonce, Versions},
    };

    #[derive(thiserror::Error, Debug)]
    #[error("mock-error")]
    pub struct Error;

    pub fn data_from_account<T: ReadableAccount + StateMut<Versions>>(
        _account: &T,
    ) -> Result<Data, Error> {
        Ok(Data::new(
            Pubkey::new_unique(),
            DurableNonce::default(),
            5000,
        ))
    }
}

/// Re-exports and mocks of solana-program modules that mirror those from
/// solana-program.
///
/// This lets examples in solana-program appear to be written as client
/// programs.
pub mod solana_sdk {
    pub use crate::{
        hash, instruction, keccak, message, nonce,
        pubkey::{self, Pubkey},
        system_instruction, system_program,
        sysvar::{
            self,
            clock::{self, Clock},
        },
    };

    pub mod account {
        use crate::{clock::Epoch, pubkey::Pubkey};
        #[derive(Clone)]
        pub struct Account {
            pub lamports: u64,
            pub data: Vec<u8>,
            pub owner: Pubkey,
            pub executable: bool,
            pub rent_epoch: Epoch,
        }

        pub trait ReadableAccount: Sized {
            fn data(&self) -> &[u8];
        }

        impl ReadableAccount for Account {
            fn data(&self) -> &[u8] {
                &self.data
            }
        }
    }

    pub mod account_utils {
        use super::account::Account;

        pub trait StateMut<T> {}

        impl<T> StateMut<T> for Account {}
    }

    pub mod signature {
        use crate::pubkey::Pubkey;

        #[derive(Default, Debug)]
        pub struct Signature;

        pub struct Keypair;

        impl Keypair {
            pub fn new() -> Keypair {
                Keypair
            }
        }

        impl Signer for Keypair {
            fn pubkey(&self) -> Pubkey {
                Pubkey::default()
            }
        }

        pub trait Signer {
            fn pubkey(&self) -> Pubkey;
        }
    }

    pub mod signers {
        use super::signature::Signer;

        pub trait Signers {}

        impl<T: Signer> Signers for [&T] {}
        impl<T: Signer> Signers for [&T; 1] {}
        impl<T: Signer> Signers for [&T; 2] {}
    }

    pub mod signer {
        use thiserror::Error;

        #[derive(Error, Debug)]
        #[error("mock-error")]
        pub struct SignerError;
    }

    pub mod transaction {
        use {
            super::{signature::Signature, signer::SignerError, signers::Signers},
            crate::{
                hash::Hash,
                instruction::Instruction,
                message::{Message, VersionedMessage},
                pubkey::Pubkey,
            },
            serde_derive::Serialize,
        };

        pub struct VersionedTransaction {
            pub signatures: Vec<Signature>,
            pub message: VersionedMessage,
        }

        impl VersionedTransaction {
            pub fn try_new<T: Signers + ?Sized>(
                message: VersionedMessage,
                _keypairs: &T,
            ) -> std::result::Result<Self, SignerError> {
                Ok(VersionedTransaction {
                    signatures: vec![],
                    message,
                })
            }
        }

        #[derive(Serialize)]
        pub struct Transaction {
            pub message: Message,
        }

        impl Transaction {
            pub fn new<T: Signers + ?Sized>(
                _from_keypairs: &T,
                _message: Message,
                _recent_blockhash: Hash,
            ) -> Transaction {
                Transaction {
                    message: Message::new(&[], None),
                }
            }

            pub fn new_unsigned(_message: Message) -> Self {
                Transaction {
                    message: Message::new(&[], None),
                }
            }

            pub fn new_with_payer(_instructions: &[Instruction], _payer: Option<&Pubkey>) -> Self {
                Transaction {
                    message: Message::new(&[], None),
                }
            }

            pub fn new_signed_with_payer<T: Signers + ?Sized>(
                instructions: &[Instruction],
                payer: Option<&Pubkey>,
                signing_keypairs: &T,
                recent_blockhash: Hash,
            ) -> Self {
                let message = Message::new(instructions, payer);
                Self::new(signing_keypairs, message, recent_blockhash)
            }

            pub fn sign<T: Signers + ?Sized>(&mut self, _keypairs: &T, _recent_blockhash: Hash) {}

            pub fn try_sign<T: Signers + ?Sized>(
                &mut self,
                _keypairs: &T,
                _recent_blockhash: Hash,
            ) -> Result<(), SignerError> {
                Ok(())
            }
        }
    }

    #[deprecated(
        since = "1.17.0",
        note = "Please use `solana_sdk::address_lookup_table` instead"
    )]
    pub use crate::address_lookup_table as address_lookup_table_account;
}

#[deprecated(
    since = "1.17.0",
    note = "Please use `solana_sdk::address_lookup_table` instead"
)]
pub mod solana_address_lookup_table_program {
    pub use crate::address_lookup_table::program::{check_id, id, ID};

    pub mod state {
        use {
            crate::{instruction::InstructionError, pubkey::Pubkey},
            std::borrow::Cow,
        };

        pub struct AddressLookupTable<'a> {
            pub addresses: Cow<'a, [Pubkey]>,
        }

        impl<'a> AddressLookupTable<'a> {
            pub fn serialize_for_tests(self) -> Result<Vec<u8>, InstructionError> {
                let mut data = vec![];
                self.addresses.iter().for_each(|address| {
                    data.extend_from_slice(address.as_ref());
                });
                Ok(data)
            }

            pub fn deserialize(data: &'a [u8]) -> Result<AddressLookupTable<'a>, InstructionError> {
                Ok(Self {
                    addresses: Cow::Borrowed(bytemuck::try_cast_slice(data).unwrap()),
                })
            }
        }
    }
}