atlas_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_auto_cfg))]
16
17pub mod atlas_rpc_client {
18    pub mod rpc_client {
19        use {
20            super::super::{
21                atlas_rpc_client_api::client_error::Result as ClientResult,
22                atlas_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 atlas_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 atlas_rpc_client_nonce_utils {
90    use {
91        super::atlas_sdk::{account::ReadableAccount, account_utils::StateMut, pubkey::Pubkey},
92        atlas_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 atlas_account {
114    use atlas_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 atlas_signature {
143    #[derive(Default, Debug)]
144    pub struct Signature;
145}
146
147pub mod atlas_signer {
148    use {atlas_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 atlas_keypair {
169    use {crate::atlas_signer::Signer, atlas_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 atlas_transaction {
186    use {
187        crate::atlas_signer::{signers::Signers, SignerError},
188        serde_derive::Serialize,
189        atlas_hash::Hash,
190        atlas_instruction::Instruction,
191        atlas_message::Message,
192        atlas_pubkey::Pubkey,
193    };
194
195    pub mod versioned {
196        use {
197            crate::{
198                atlas_signature::Signature,
199                atlas_signer::{signers::Signers, SignerError},
200            },
201            atlas_message::VersionedMessage,
202        };
203        pub struct VersionedTransaction {
204            pub signatures: Vec<Signature>,
205            pub message: VersionedMessage,
206        }
207
208        impl VersionedTransaction {
209            pub fn try_new<T: Signers + ?Sized>(
210                message: VersionedMessage,
211                _keypairs: &T,
212            ) -> std::result::Result<Self, SignerError> {
213                Ok(VersionedTransaction {
214                    signatures: vec![],
215                    message,
216                })
217            }
218        }
219    }
220
221    #[derive(Serialize)]
222    pub struct Transaction {
223        pub message: Message,
224    }
225
226    impl Transaction {
227        pub fn new<T: Signers + ?Sized>(
228            _from_keypairs: &T,
229            _message: Message,
230            _recent_blockhash: Hash,
231        ) -> Transaction {
232            Transaction {
233                message: Message::new(&[], None),
234            }
235        }
236
237        pub fn new_unsigned(_message: Message) -> Self {
238            Transaction {
239                message: Message::new(&[], None),
240            }
241        }
242
243        pub fn new_with_payer(_instructions: &[Instruction], _payer: Option<&Pubkey>) -> Self {
244            Transaction {
245                message: Message::new(&[], None),
246            }
247        }
248
249        pub fn new_signed_with_payer<T: Signers + ?Sized>(
250            instructions: &[Instruction],
251            payer: Option<&Pubkey>,
252            signing_keypairs: &T,
253            recent_blockhash: Hash,
254        ) -> Self {
255            let message = Message::new(instructions, payer);
256            Self::new(signing_keypairs, message, recent_blockhash)
257        }
258
259        pub fn sign<T: Signers + ?Sized>(&mut self, _keypairs: &T, _recent_blockhash: Hash) {}
260
261        pub fn try_sign<T: Signers + ?Sized>(
262            &mut self,
263            _keypairs: &T,
264            _recent_blockhash: Hash,
265        ) -> Result<(), SignerError> {
266            Ok(())
267        }
268    }
269}
270
271/// Re-exports and mocks of atlas-program modules that mirror those from
272/// atlas-program.
273///
274/// This lets examples in atlas-program appear to be written as client
275/// programs.
276pub mod atlas_sdk {
277    pub use {
278        crate::{
279            atlas_account::{self as account, state_traits as account_utils},
280            atlas_signer::{self as signer, signers},
281        },
282        atlas_clock::Clock,
283        atlas_hash as hash, atlas_instruction as instruction, atlas_keccak_hasher as keccak,
284        atlas_message as message, atlas_nonce as nonce,
285        atlas_pubkey::{self as pubkey, Pubkey},
286        atlas_sdk_ids::{
287            system_program,
288            sysvar::{self, clock},
289        },
290        atlas_system_interface::instruction as system_instruction,
291    };
292
293    pub mod signature {
294        pub use crate::{
295            atlas_keypair::Keypair, atlas_signature::Signature, atlas_signer::Signer,
296        };
297    }
298
299    pub mod transaction {
300        pub use crate::atlas_transaction::{versioned::VersionedTransaction, Transaction};
301    }
302
303    pub mod address_lookup_table {
304        pub use {
305            atlas_address_lookup_table_interface::{error, instruction, program, state},
306            atlas_message::AddressLookupTableAccount,
307        };
308    }
309}