clone_solana_example_mocks/
lib.rs

1//! Mock types for use in examples.
2//!
3//! These represent APIs from crates that themselves depend on this crate, and
4//! which are useful for illustrating the examples for APIs in this crate.
5//!
6//! Directly depending on these crates though would cause problematic circular
7//! dependencies, so instead they are mocked out here in a way that allows
8//! examples to appear to use crates that this crate must not depend on.
9//!
10//! Each mod here has the name of a crate, so that examples can be structured to
11//! appear to import from that crate.
12
13#![doc(hidden)]
14#![allow(clippy::new_without_default)]
15
16pub mod clone_solana_rpc_client {
17    pub mod rpc_client {
18        use {
19            super::super::{
20                clone_solana_rpc_client_api::client_error::Result as ClientResult,
21                clone_solana_sdk::{
22                    account::Account, hash::Hash, pubkey::Pubkey, signature::Signature,
23                    transaction::Transaction,
24                },
25            },
26            std::{cell::RefCell, collections::HashMap, rc::Rc},
27        };
28
29        #[derive(Default)]
30        pub struct RpcClient {
31            get_account_responses: Rc<RefCell<HashMap<Pubkey, Account>>>,
32        }
33
34        impl RpcClient {
35            pub fn new(_url: String) -> Self {
36                RpcClient::default()
37            }
38
39            pub fn get_latest_blockhash(&self) -> ClientResult<Hash> {
40                Ok(Hash::default())
41            }
42
43            pub fn send_and_confirm_transaction(
44                &self,
45                _transaction: &Transaction,
46            ) -> ClientResult<Signature> {
47                Ok(Signature)
48            }
49
50            pub fn get_minimum_balance_for_rent_exemption(
51                &self,
52                _data_len: usize,
53            ) -> ClientResult<u64> {
54                Ok(0)
55            }
56
57            pub fn get_account(&self, pubkey: &Pubkey) -> ClientResult<Account> {
58                Ok(self
59                    .get_account_responses
60                    .borrow()
61                    .get(pubkey)
62                    .cloned()
63                    .unwrap())
64            }
65
66            pub fn set_get_account_response(&self, pubkey: Pubkey, account: Account) {
67                self.get_account_responses
68                    .borrow_mut()
69                    .insert(pubkey, account);
70            }
71
72            pub fn get_balance(&self, _pubkey: &Pubkey) -> ClientResult<u64> {
73                Ok(0)
74            }
75        }
76    }
77}
78
79pub mod clone_solana_rpc_client_api {
80    pub mod client_error {
81        #[derive(thiserror::Error, Debug)]
82        #[error("mock-error")]
83        pub struct ClientError;
84        pub type Result<T> = std::result::Result<T, ClientError>;
85    }
86}
87
88pub mod clone_solana_rpc_client_nonce_utils {
89    use {
90        super::clone_solana_sdk::{
91            account::ReadableAccount, account_utils::StateMut, pubkey::Pubkey,
92        },
93        clone_solana_nonce::{
94            state::{Data, DurableNonce},
95            versions::Versions,
96        },
97    };
98
99    #[derive(thiserror::Error, Debug)]
100    #[error("mock-error")]
101    pub struct Error;
102
103    pub fn data_from_account<T: ReadableAccount + StateMut<Versions>>(
104        _account: &T,
105    ) -> Result<Data, Error> {
106        Ok(Data::new(
107            Pubkey::new_unique(),
108            DurableNonce::default(),
109            5000,
110        ))
111    }
112}
113
114pub mod clone_solana_account {
115    use {clone_solana_clock::Epoch, clone_solana_pubkey::Pubkey};
116    #[derive(Clone)]
117    pub struct Account {
118        pub lamports: u64,
119        pub data: Vec<u8>,
120        pub owner: Pubkey,
121        pub executable: bool,
122        pub rent_epoch: Epoch,
123    }
124
125    pub trait ReadableAccount: Sized {
126        fn data(&self) -> &[u8];
127    }
128
129    impl ReadableAccount for Account {
130        fn data(&self) -> &[u8] {
131            &self.data
132        }
133    }
134
135    pub mod state_traits {
136        use super::Account;
137
138        pub trait StateMut<T> {}
139
140        impl<T> StateMut<T> for Account {}
141    }
142}
143
144pub mod clone_solana_signature {
145    #[derive(Default, Debug)]
146    pub struct Signature;
147}
148
149pub mod clone_solana_signer {
150    use {clone_solana_pubkey::Pubkey, thiserror::Error};
151
152    #[derive(Error, Debug)]
153    #[error("mock-error")]
154    pub struct SignerError;
155    pub trait Signer {
156        fn pubkey(&self) -> Pubkey;
157    }
158
159    pub mod signers {
160        use super::Signer;
161
162        pub trait Signers {}
163
164        impl<T: Signer> Signers for [&T] {}
165        impl<T: Signer> Signers for [&T; 1] {}
166        impl<T: Signer> Signers for [&T; 2] {}
167    }
168}
169
170pub mod clone_solana_keypair {
171    use {crate::clone_solana_signer::Signer, clone_solana_pubkey::Pubkey};
172    pub struct Keypair;
173
174    impl Keypair {
175        pub fn new() -> Keypair {
176            Keypair
177        }
178    }
179
180    impl Signer for Keypair {
181        fn pubkey(&self) -> Pubkey {
182            Pubkey::default()
183        }
184    }
185}
186
187pub mod clone_solana_transaction {
188    use {
189        crate::clone_solana_signer::{signers::Signers, SignerError},
190        clone_solana_hash::Hash,
191        clone_solana_instruction::Instruction,
192        clone_solana_message::Message,
193        clone_solana_pubkey::Pubkey,
194        serde_derive::Serialize,
195    };
196
197    pub mod versioned {
198        use {
199            crate::{
200                clone_solana_signature::Signature,
201                clone_solana_signer::{signers::Signers, SignerError},
202            },
203            clone_solana_message::VersionedMessage,
204        };
205        pub struct VersionedTransaction {
206            pub signatures: Vec<Signature>,
207            pub message: VersionedMessage,
208        }
209
210        impl VersionedTransaction {
211            pub fn try_new<T: Signers + ?Sized>(
212                message: VersionedMessage,
213                _keypairs: &T,
214            ) -> std::result::Result<Self, SignerError> {
215                Ok(VersionedTransaction {
216                    signatures: vec![],
217                    message,
218                })
219            }
220        }
221    }
222
223    #[derive(Serialize)]
224    pub struct Transaction {
225        pub message: Message,
226    }
227
228    impl Transaction {
229        pub fn new<T: Signers + ?Sized>(
230            _from_keypairs: &T,
231            _message: Message,
232            _recent_blockhash: Hash,
233        ) -> Transaction {
234            Transaction {
235                message: Message::new(&[], None),
236            }
237        }
238
239        pub fn new_unsigned(_message: Message) -> Self {
240            Transaction {
241                message: Message::new(&[], None),
242            }
243        }
244
245        pub fn new_with_payer(_instructions: &[Instruction], _payer: Option<&Pubkey>) -> Self {
246            Transaction {
247                message: Message::new(&[], None),
248            }
249        }
250
251        pub fn new_signed_with_payer<T: Signers + ?Sized>(
252            instructions: &[Instruction],
253            payer: Option<&Pubkey>,
254            signing_keypairs: &T,
255            recent_blockhash: Hash,
256        ) -> Self {
257            let message = Message::new(instructions, payer);
258            Self::new(signing_keypairs, message, recent_blockhash)
259        }
260
261        pub fn sign<T: Signers + ?Sized>(&mut self, _keypairs: &T, _recent_blockhash: Hash) {}
262
263        pub fn try_sign<T: Signers + ?Sized>(
264            &mut self,
265            _keypairs: &T,
266            _recent_blockhash: Hash,
267        ) -> Result<(), SignerError> {
268            Ok(())
269        }
270    }
271}
272
273/// Re-exports and mocks of solana-program modules that mirror those from
274/// solana-program.
275///
276/// This lets examples in solana-program appear to be written as client
277/// programs.
278pub mod clone_solana_sdk {
279    pub use {
280        crate::{
281            clone_solana_account::{self as account, state_traits as account_utils},
282            clone_solana_signer::{self as signer, signers},
283        },
284        clone_solana_clock::Clock,
285        clone_solana_hash as hash, clone_solana_instruction as instruction,
286        clone_solana_keccak_hasher as keccak, clone_solana_message as message,
287        clone_solana_nonce as nonce,
288        clone_solana_pubkey::{self as pubkey, Pubkey},
289        clone_solana_sdk_ids::{
290            system_program,
291            sysvar::{self, clock},
292        },
293        clone_solana_system_interface::instruction as system_instruction,
294    };
295
296    pub mod signature {
297        pub use crate::{
298            clone_solana_keypair::Keypair, clone_solana_signature::Signature,
299            clone_solana_signer::Signer,
300        };
301    }
302
303    pub mod transaction {
304        pub use crate::clone_solana_transaction::{versioned::VersionedTransaction, Transaction};
305    }
306
307    pub mod address_lookup_table {
308        pub use {
309            clone_solana_address_lookup_table_interface::{error, instruction, program, state},
310            clone_solana_message::AddressLookupTableAccount,
311        };
312    }
313}