abstract_interface/
daemon_state.rs

1use rust_embed::RustEmbed;
2
3#[derive(RustEmbed)]
4#[folder = "./"]
5#[include = "state.json"]
6struct State;
7
8impl State {
9    pub fn load_state() -> serde_json::Value {
10        let state_file =
11            State::get("state.json").expect("Unable to read abstract-interface state.json");
12        serde_json::from_slice(&state_file.data).unwrap()
13    }
14}
15
16/// State of abstract deployments
17pub struct AbstractDaemonState(serde_json::Value);
18
19impl Default for AbstractDaemonState {
20    fn default() -> Self {
21        Self(State::load_state())
22    }
23}
24
25impl AbstractDaemonState {
26    /// Get address of the abstract contract by contract_id
27    pub fn contract_addr(&self, chain_id: &str, contract_id: &str) -> Option<cosmwasm_std::Addr> {
28        self.0[chain_id]["default"][contract_id]
29            .as_str()
30            .map(cosmwasm_std::Addr::unchecked)
31    }
32
33    /// Get code id of the abstract contract by contract_id
34    pub fn contract_code_id(&self, chain_id: &str, contract_id: &str) -> Option<u64> {
35        self.0[chain_id]["code_ids"][contract_id].as_u64()
36    }
37
38    /// Get raw state of the abstract deployments
39    pub fn state(&self) -> serde_json::Value {
40        self.0.clone()
41    }
42}
43
44#[cfg(test)]
45mod test {
46    #![allow(clippy::needless_borrows_for_generic_args)]
47    use std::borrow::Cow;
48
49    use abstract_std::REGISTRY;
50
51    use super::*;
52
53    #[test]
54    fn only_state_json_included() {
55        let files = State::iter().collect::<Vec<_>>();
56        assert_eq!(files, vec![Cow::Borrowed("state.json")]);
57        State::get("state.json").unwrap();
58    }
59
60    #[test]
61    fn have_some_state() {
62        let state = AbstractDaemonState::default();
63        let vc_juno = state.contract_code_id("pion-1", REGISTRY);
64        assert!(vc_juno.is_some());
65    }
66}