lwk/
test_env.rs

1use std::sync::Arc;
2
3use crate::{types::AssetId, Address, Txid};
4
5/// Represent a test environment with an elements node and an electrum server.
6/// useful for testing only, wrapper over [`lwk_test_util::TestElectrumServer`]
7#[derive(uniffi::Object)]
8pub struct LwkTestEnv {
9    inner: lwk_test_util::TestElectrumServer,
10}
11
12#[uniffi::export]
13impl LwkTestEnv {
14    /// Creates a new test environment
15    #[allow(clippy::new_without_default)]
16    #[uniffi::constructor]
17    pub fn new() -> LwkTestEnv {
18        LwkTestEnv {
19            inner: lwk_test_util::setup_with_esplora(),
20        }
21    }
22
23    /// Generate `blocks` blocks from the node
24    pub fn generate(&self, blocks: u32) {
25        self.inner.elementsd_generate(blocks);
26    }
27
28    /// Get the height of the node
29    pub fn height(&self) -> u64 {
30        self.inner.elementsd_height()
31    }
32
33    /// Send `satoshi` to `address` from the node
34    pub fn send_to_address(&self, address: &Address, satoshi: u64, asset: Option<AssetId>) -> Txid {
35        self.inner
36            .elementsd_sendtoaddress(address.as_ref(), satoshi, asset.map(Into::into))
37            .into()
38    }
39
40    /// Issue `satoshi` of an asset from the node
41    pub fn issue_asset(&self, satoshi: u64) -> AssetId {
42        self.inner.elementsd_issueasset(satoshi).into()
43    }
44
45    /// Get a new address from the node
46    pub fn get_new_address(&self) -> Arc<Address> {
47        Arc::new(self.inner.elementsd_getnewaddress().into())
48    }
49
50    /// Get the Electrum URL of the test environment
51    pub fn electrum_url(&self) -> String {
52        self.inner.electrs.electrum_url.clone()
53    }
54}