soroban_rs/mock/
account.rs

1use crate::error::SorobanHelperError;
2use crate::{crypto, Account, Env, EnvConfigs};
3use crate::{Signer, SorobanTransactionResponse};
4use ed25519_dalek::SigningKey;
5use std::default::Default;
6use std::str::FromStr;
7use std::sync::Arc;
8use stellar_rpc_client::SimulateTransactionResponse;
9use stellar_strkey::ed25519::PrivateKey;
10use stellar_strkey::Contract as ContractStrKey;
11use stellar_xdr::curr::{
12    AccountEntry, AccountEntryExt, AccountId, PublicKey, String32, Thresholds, Uint256, VecM,
13};
14
15use super::rpc::MockRpcClient;
16
17/// Creates a mock contract ID for testing
18#[allow(dead_code)]
19pub fn mock_contract_id(account: Account, env: &Env) -> ContractStrKey {
20    crypto::calculate_contract_id(&account.account_id(), &Uint256([0; 32]), &env.network_id())
21        .unwrap()
22}
23
24/// Creates a mock environment with configurable responses
25#[allow(dead_code)]
26pub fn mock_env(
27    get_account_result: Option<Result<AccountEntry, SorobanHelperError>>,
28    simulate_transaction_envelope_result: Option<
29        Result<SimulateTransactionResponse, SorobanHelperError>,
30    >,
31    send_transaction_polling_result: Option<Result<SorobanTransactionResponse, SorobanHelperError>>,
32) -> Env {
33    let random_id = rand::random::<u64>();
34    let network_passphrase = format!("Mock Test Random Network {}", random_id);
35
36    Env {
37        configs: EnvConfigs {
38            rpc_url: "http://test.com".to_string(),
39            network_passphrase,
40        },
41        rpc_client: Arc::new(MockRpcClient::new(
42            get_account_result,
43            simulate_transaction_envelope_result,
44            send_transaction_polling_result,
45        )),
46    }
47}
48
49/// Returns a collection of mock signers for testing
50#[allow(dead_code)]
51pub fn all_signers() -> Vec<Signer> {
52    vec![mock_signer1(), mock_signer2(), mock_signer3()]
53}
54
55/// Creates the first mock signer with a predefined private key
56#[allow(dead_code)]
57pub fn mock_signer1() -> Signer {
58    let pk = PrivateKey::from_string("SD3C2X7WPTUYX4YHL2G34PX75JZ35QJDFKM6SXDLYHWIPOWPIQUXFVLE")
59        .unwrap();
60    Signer::new(SigningKey::from_bytes(&pk.0))
61}
62
63/// Creates the second mock signer with a predefined private key
64#[allow(dead_code)]
65pub fn mock_signer2() -> Signer {
66    let pk = PrivateKey::from_string("SDFLNQOG3PV4CYJ4BNUXFXJBBOCQ57MK2NYUK4XUVVJTT2JSA3YDJA3A")
67        .unwrap();
68    Signer::new(SigningKey::from_bytes(&pk.0))
69}
70
71/// Creates the third mock signer with a predefined private key
72#[allow(dead_code)]
73pub fn mock_signer3() -> Signer {
74    let pk = PrivateKey::from_string("SASAXDSRHPRZ55OLOD4EWXIWODQEZPYGIBFYX3XBUZGFFVY7QKLYRF5K")
75        .unwrap();
76    Signer::new(SigningKey::from_bytes(&pk.0))
77}
78
79/// Creates a mock account entry with specified account ID
80pub fn mock_account_entry(account_id: &str) -> AccountEntry {
81    AccountEntry {
82        account_id: AccountId(PublicKey::from_str(account_id).unwrap()),
83        balance: 0,
84        ext: AccountEntryExt::V0,
85        flags: 0,
86        home_domain: String32::default(),
87        inflation_dest: None,
88        seq_num: 0.into(),
89        num_sub_entries: 0,
90        signers: VecM::default(),
91        thresholds: Thresholds([0, 0, 0, 0]),
92    }
93}