miraland_cli/
test_utils.rs

1use {
2    miraland_rpc_client::rpc_client::RpcClient,
3    miraland_sdk::{
4        clock::{Epoch, DEFAULT_MS_PER_SLOT},
5        commitment_config::CommitmentConfig,
6    },
7    std::{thread::sleep, time::Duration},
8};
9
10#[macro_export]
11macro_rules! check_balance {
12    ($expected_balance:expr, $client:expr, $pubkey:expr) => {
13        (0..5).for_each(|tries| {
14            let balance = $client
15                .get_balance_with_commitment($pubkey, CommitmentConfig::processed())
16                .unwrap()
17                .value;
18            if balance == $expected_balance {
19                return;
20            }
21            if tries == 4 {
22                assert_eq!(balance, $expected_balance);
23            }
24            std::thread::sleep(std::time::Duration::from_millis(500));
25        });
26    };
27    ($expected_balance:expr, $client:expr, $pubkey:expr,) => {
28        check_balance!($expected_balance, $client, $pubkey)
29    };
30}
31
32pub fn check_ready(rpc_client: &RpcClient) {
33    while rpc_client
34        .get_slot_with_commitment(CommitmentConfig::processed())
35        .unwrap()
36        < 5
37    {
38        sleep(Duration::from_millis(DEFAULT_MS_PER_SLOT));
39    }
40}
41
42pub fn wait_n_slots(rpc_client: &RpcClient, n: u64) -> u64 {
43    let slot = rpc_client.get_slot().unwrap();
44    loop {
45        sleep(Duration::from_millis(DEFAULT_MS_PER_SLOT));
46        let new_slot = rpc_client.get_slot().unwrap();
47        if new_slot - slot > n {
48            return new_slot;
49        }
50    }
51}
52
53pub fn wait_for_next_epoch_plus_n_slots(rpc_client: &RpcClient, n: u64) -> (Epoch, u64) {
54    let current_epoch = rpc_client.get_epoch_info().unwrap().epoch;
55    let next_epoch = current_epoch + 1;
56    println!("waiting for epoch {next_epoch} plus {n} slots");
57    loop {
58        sleep(Duration::from_millis(DEFAULT_MS_PER_SLOT));
59
60        let next_epoch = rpc_client.get_epoch_info().unwrap().epoch;
61        if next_epoch > current_epoch {
62            let new_slot = wait_n_slots(rpc_client, n);
63            return (next_epoch, new_slot);
64        }
65    }
66}