Skip to main content

rialo_s_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 solana_rpc_client {
17    pub mod rpc_client {
18        use std::{cell::RefCell, collections::HashMap, rc::Rc};
19
20        use super::super::rialo_s_sdk::{
21            account::Account, pubkey::Pubkey, signature::Signature, transaction::Transaction,
22        };
23
24        type ClientResult<T> = std::result::Result<T, ClientError>;
25
26        #[derive(thiserror::Error, Debug)]
27        #[error("mock-error")]
28        pub struct ClientError;
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 send_and_confirm_transaction(
41                &self,
42                _transaction: &Transaction,
43            ) -> ClientResult<Signature> {
44                Ok(Signature)
45            }
46
47            pub fn get_minimum_balance_for_rent_exemption(
48                &self,
49                _data_len: usize,
50            ) -> ClientResult<u64> {
51                Ok(0)
52            }
53
54            pub fn get_account(&self, pubkey: &Pubkey) -> ClientResult<Account> {
55                Ok(self
56                    .get_account_responses
57                    .borrow()
58                    .get(pubkey)
59                    .cloned()
60                    .unwrap())
61            }
62
63            pub fn set_get_account_response(&self, pubkey: Pubkey, account: Account) {
64                self.get_account_responses
65                    .borrow_mut()
66                    .insert(pubkey, account);
67            }
68
69            pub fn get_balance(&self, _pubkey: &Pubkey) -> ClientResult<u64> {
70                Ok(0)
71            }
72        }
73    }
74}
75
76pub mod solana_rpc_client_nonce_utils {
77    use rialo_s_nonce::{
78        state::{Data, DurableNonce},
79        versions::Versions,
80    };
81
82    use super::rialo_s_sdk::{account::ReadableAccount, account_utils::StateMut, pubkey::Pubkey};
83
84    #[derive(thiserror::Error, Debug)]
85    #[error("mock-error")]
86    pub struct Error;
87
88    pub fn data_from_account<T: ReadableAccount + StateMut<Versions>>(
89        _account: &T,
90    ) -> Result<Data, Error> {
91        Ok(Data::new(
92            Pubkey::new_unique(),
93            DurableNonce::default(),
94            5000,
95        ))
96    }
97}
98
99pub mod rialo_s_account {
100    use rialo_s_clock::Epoch;
101    use rialo_s_pubkey::Pubkey;
102    #[derive(Clone)]
103    pub struct Account {
104        pub kelvins: u64,
105        pub data: Vec<u8>,
106        pub owner: Pubkey,
107        pub executable: bool,
108        pub rent_epoch: Epoch,
109    }
110
111    pub trait ReadableAccount: Sized {
112        fn data(&self) -> &[u8];
113    }
114
115    impl ReadableAccount for Account {
116        fn data(&self) -> &[u8] {
117            &self.data
118        }
119    }
120
121    pub mod state_traits {
122        use super::Account;
123
124        pub trait StateMut<T> {}
125
126        impl<T> StateMut<T> for Account {}
127    }
128}
129
130pub mod rialo_s_signature {
131    #[derive(Default, Debug)]
132    pub struct Signature;
133}
134
135pub mod rialo_s_signer {
136    use rialo_s_pubkey::Pubkey;
137    use thiserror::Error;
138
139    #[derive(Error, Debug)]
140    #[error("mock-error")]
141    pub struct SignerError;
142    pub trait Signer {
143        fn pubkey(&self) -> Pubkey;
144    }
145
146    pub mod signers {
147        use super::Signer;
148
149        pub trait Signers {}
150
151        impl<T: Signer> Signers for [&T] {}
152        impl<T: Signer> Signers for [&T; 1] {}
153        impl<T: Signer> Signers for [&T; 2] {}
154    }
155}
156
157pub mod rialo_s_keypair {
158    use rialo_s_pubkey::Pubkey;
159
160    use crate::rialo_s_signer::Signer;
161    pub struct Keypair;
162
163    impl Keypair {
164        pub fn new() -> Keypair {
165            Keypair
166        }
167    }
168
169    impl Signer for Keypair {
170        fn pubkey(&self) -> Pubkey {
171            Pubkey::default()
172        }
173    }
174}
175
176pub mod rialo_s_transaction {
177    use rialo_s_instruction::Instruction;
178    use rialo_s_message::Message;
179    use rialo_s_pubkey::Pubkey;
180    use serde_derive::Serialize;
181
182    use crate::rialo_s_signer::{signers::Signers, SignerError};
183
184    pub mod versioned {
185        use rialo_s_message::VersionedMessage;
186
187        use crate::{
188            rialo_s_signature::Signature,
189            rialo_s_signer::{signers::Signers, SignerError},
190        };
191        pub struct VersionedTransaction {
192            pub signatures: Vec<Signature>,
193            pub message: VersionedMessage,
194        }
195
196        impl VersionedTransaction {
197            pub fn try_new<T: Signers + ?Sized>(
198                message: VersionedMessage,
199                _keypairs: &T,
200            ) -> std::result::Result<Self, SignerError> {
201                Ok(VersionedTransaction {
202                    signatures: vec![],
203                    message,
204                })
205            }
206        }
207    }
208
209    #[derive(Serialize)]
210    pub struct Transaction {
211        pub message: Message,
212    }
213
214    impl Transaction {
215        pub fn new<T: Signers + ?Sized>(
216            _from_keypairs: &T,
217            _message: Message,
218            _valid_from: i64,
219        ) -> Transaction {
220            Transaction {
221                message: Message::new(&[], None),
222            }
223        }
224
225        pub fn new_unsigned(_message: Message) -> Self {
226            Transaction {
227                message: Message::new(&[], None),
228            }
229        }
230
231        pub fn new_with_payer(_instructions: &[Instruction], _payer: Option<&Pubkey>) -> Self {
232            Transaction {
233                message: Message::new(&[], None),
234            }
235        }
236
237        pub fn new_signed_with_payer<T: Signers + ?Sized>(
238            instructions: &[Instruction],
239            payer: Option<&Pubkey>,
240            signing_keypairs: &T,
241            valid_from: i64,
242        ) -> Self {
243            let message = Message::new(instructions, payer);
244            Self::new(signing_keypairs, message, valid_from)
245        }
246
247        pub fn sign<T: Signers + ?Sized>(&mut self, _keypairs: &T, _valid_from: i64) {}
248
249        pub fn try_sign<T: Signers + ?Sized>(
250            &mut self,
251            _keypairs: &T,
252            _valid_from: i64,
253        ) -> Result<(), SignerError> {
254            Ok(())
255        }
256    }
257}
258
259/// Re-exports and mocks of solana-program modules that mirror those from
260/// solana-program.
261///
262/// This lets examples in solana-program appear to be written as client
263/// programs.
264pub mod rialo_s_sdk {
265    pub use rialo_hash as hash;
266    pub use rialo_s_clock::Clock;
267    pub use rialo_s_instruction as instruction;
268    pub use rialo_s_keccak_hasher as keccak;
269    pub use rialo_s_message as message;
270    pub use rialo_s_nonce as nonce;
271    pub use rialo_s_pubkey::{self as pubkey, Pubkey};
272    pub use rialo_s_sdk_ids::{
273        system_program,
274        sysvar::{self, clock},
275    };
276    pub use rialo_s_system_interface::instruction as system_instruction;
277
278    pub use crate::{
279        rialo_s_account::{self as account, state_traits as account_utils},
280        rialo_s_signer::{self as signer, signers},
281    };
282
283    pub mod signature {
284        pub use crate::{
285            rialo_s_keypair::Keypair, rialo_s_signature::Signature, rialo_s_signer::Signer,
286        };
287    }
288
289    pub mod transaction {
290        pub use crate::rialo_s_transaction::{versioned::VersionedTransaction, Transaction};
291    }
292}