Skip to main content

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#![cfg_attr(docsrs, feature(doc_cfg))]
16
17pub mod solana_rpc_client {
18    pub mod rpc_client {
19        use {
20            super::super::{
21                solana_rpc_client_api::client_error::Result as ClientResult,
22                solana_sdk::{
23                    account::Account, hash::Hash, pubkey::Pubkey, signature::Signature,
24                    transaction::Transaction,
25                },
26            },
27            std::{cell::RefCell, collections::HashMap, rc::Rc},
28        };
29
30        #[derive(Default)]
31        pub struct RpcClient {
32            get_account_responses: Rc<RefCell<HashMap<Pubkey, Account>>>,
33        }
34
35        impl RpcClient {
36            pub fn new(_url: String) -> Self {
37                RpcClient::default()
38            }
39
40            pub fn get_latest_blockhash(&self) -> ClientResult<Hash> {
41                Ok(Hash::default())
42            }
43
44            pub fn send_and_confirm_transaction(
45                &self,
46                _transaction: &Transaction,
47            ) -> ClientResult<Signature> {
48                Ok(Signature)
49            }
50
51            pub fn get_minimum_balance_for_rent_exemption(
52                &self,
53                _data_len: usize,
54            ) -> ClientResult<u64> {
55                Ok(0)
56            }
57
58            pub fn get_account(&self, pubkey: &Pubkey) -> ClientResult<Account> {
59                Ok(self
60                    .get_account_responses
61                    .borrow()
62                    .get(pubkey)
63                    .cloned()
64                    .unwrap())
65            }
66
67            pub fn set_get_account_response(&self, pubkey: Pubkey, account: Account) {
68                self.get_account_responses
69                    .borrow_mut()
70                    .insert(pubkey, account);
71            }
72
73            pub fn get_balance(&self, _pubkey: &Pubkey) -> ClientResult<u64> {
74                Ok(0)
75            }
76        }
77    }
78}
79
80pub mod solana_rpc_client_api {
81    pub mod client_error {
82        #[derive(thiserror::Error, Debug)]
83        #[error("mock-error")]
84        pub struct ClientError;
85        pub type Result<T> = std::result::Result<T, ClientError>;
86    }
87}
88
89pub mod solana_rpc_client_nonce_utils {
90    use {
91        super::solana_sdk::{account::ReadableAccount, account_utils::StateMut, pubkey::Pubkey},
92        solana_nonce::{
93            state::{Data, DurableNonce},
94            versions::Versions,
95        },
96    };
97
98    #[derive(thiserror::Error, Debug)]
99    #[error("mock-error")]
100    pub struct Error;
101
102    pub fn data_from_account<T: ReadableAccount + StateMut<Versions>>(
103        _account: &T,
104    ) -> Result<Data, Error> {
105        Ok(Data::new(
106            Pubkey::new_unique(),
107            DurableNonce::default(),
108            5000,
109        ))
110    }
111}
112
113pub mod solana_account {
114    use solana_pubkey::Pubkey;
115    #[derive(Clone)]
116    pub struct Account {
117        pub lamports: u64,
118        pub data: Vec<u8>,
119        pub owner: Pubkey,
120        pub executable: bool,
121    }
122
123    pub trait ReadableAccount: Sized {
124        fn data(&self) -> &[u8];
125    }
126
127    impl ReadableAccount for Account {
128        fn data(&self) -> &[u8] {
129            &self.data
130        }
131    }
132
133    pub mod state_traits {
134        use super::Account;
135
136        pub trait StateMut<T> {}
137
138        impl<T> StateMut<T> for Account {}
139    }
140}
141
142pub mod solana_signature {
143    #[derive(Default, Debug)]
144    pub struct Signature;
145}
146
147pub mod solana_signer {
148    use {solana_pubkey::Pubkey, thiserror::Error};
149
150    #[derive(Error, Debug)]
151    #[error("mock-error")]
152    pub struct SignerError;
153    pub trait Signer {
154        fn pubkey(&self) -> Pubkey;
155    }
156
157    pub mod signers {
158        use super::Signer;
159
160        pub trait Signers {}
161
162        impl<T: Signer> Signers for [&T] {}
163        impl<T: Signer> Signers for [&T; 1] {}
164        impl<T: Signer> Signers for [&T; 2] {}
165    }
166}
167
168pub mod solana_keypair {
169    use {crate::solana_signer::Signer, solana_pubkey::Pubkey};
170    pub struct Keypair;
171
172    impl Keypair {
173        pub fn new() -> Keypair {
174            Keypair
175        }
176    }
177
178    impl Signer for Keypair {
179        fn pubkey(&self) -> Pubkey {
180            Pubkey::default()
181        }
182    }
183}
184
185pub mod solana_transaction {
186    use {
187        crate::solana_signer::{signers::Signers, SignerError},
188        serde_derive::Serialize,
189        solana_hash::Hash,
190        solana_instruction::Instruction,
191        solana_pubkey::Pubkey,
192    };
193
194    #[derive(Serialize)]
195    pub struct Transaction;
196
197    impl Transaction {
198        pub fn new<T: Signers + ?Sized, M>(
199            _from_keypairs: &T,
200            _message: M,
201            _recent_blockhash: Hash,
202        ) -> Self {
203            Self
204        }
205
206        pub fn new_unsigned<M>(_message: M) -> Self {
207            Self
208        }
209
210        pub fn new_with_payer(_instructions: &[Instruction], _payer: Option<&Pubkey>) -> Self {
211            Self
212        }
213
214        pub fn new_signed_with_payer<T: Signers + ?Sized>(
215            _instructions: &[Instruction],
216            _payer: Option<&Pubkey>,
217            _signing_keypairs: &T,
218            _recent_blockhash: Hash,
219        ) -> Self {
220            Self
221        }
222
223        pub fn sign<T: Signers + ?Sized>(&mut self, _keypairs: &T, _recent_blockhash: Hash) {}
224
225        pub fn try_sign<T: Signers + ?Sized>(
226            &mut self,
227            _keypairs: &T,
228            _recent_blockhash: Hash,
229        ) -> Result<(), SignerError> {
230            Ok(())
231        }
232    }
233}
234
235/// Re-exports and mocks of solana-program modules that mirror those from
236/// solana-program.
237///
238/// This lets examples in solana-program appear to be written as client
239/// programs.
240pub mod solana_sdk {
241    pub use {
242        crate::{
243            solana_account::{self as account, state_traits as account_utils},
244            solana_signer::{self as signer, signers},
245        },
246        solana_hash as hash, solana_instruction as instruction, solana_nonce as nonce,
247        solana_pubkey::{self as pubkey, Pubkey},
248        solana_sdk_ids::{
249            system_program,
250            sysvar::{self, clock},
251        },
252        solana_system_interface::instruction as system_instruction,
253    };
254
255    pub mod signature {
256        pub use crate::{
257            solana_keypair::Keypair, solana_signature::Signature, solana_signer::Signer,
258        };
259    }
260
261    pub mod transaction {
262        pub use crate::solana_transaction::Transaction;
263    }
264}