1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use chain_core::packer::Codec;
use chain_core::property::Deserialize;
use chain_crypto::{bech32::Bech32, Ed25519, PublicKey};
use chain_impl_mockchain::fragment::{Fragment, FragmentId};
pub use jormungandr_automation::jormungandr::{JormungandrRest, RestError, RestSettings};
use jormungandr_lib::interfaces::{
    AccountState, AccountVotes, Address, FragmentLog, FragmentStatus, NodeStatsDto, SettingsDto,
    VotePlanId, VotePlanStatus,
};
use std::collections::HashMap;
use std::str::FromStr;
use url::Url;
use wallet::AccountId;

#[derive(Clone)]
pub struct WalletNodeRestClient {
    rest_client: JormungandrRest,
}

impl WalletNodeRestClient {
    pub fn new(address: Url, settings: RestSettings) -> Self {
        Self {
            rest_client: JormungandrRest::new_with_custom_settings(address.to_string(), settings),
        }
    }

    pub fn send_fragment(&self, body: Vec<u8>) -> Result<(), RestError> {
        self.rest_client.send_raw_fragment(body)
    }

    pub fn send_fragments(&self, bodies: Vec<Vec<u8>>, use_v1: bool) -> Result<(), RestError> {
        if use_v1 {
            self.rest_client.send_fragment_batch(
                bodies
                    .iter()
                    .map(|tx| Fragment::deserialize(&mut Codec::new(tx.as_slice())).unwrap())
                    .collect(),
                true,
            )?;
            Ok(())
        } else {
            self.rest_client.send_raw_fragments(bodies)
        }
    }

    pub fn fragment_logs(&self) -> Result<HashMap<FragmentId, FragmentLog>, RestError> {
        Ok(self
            .rest_client
            .fragment_logs()?
            .iter()
            .map(|(id, entry)| {
                let str = id.to_string();
                (FragmentId::from_str(&str).unwrap(), entry.clone())
            })
            .collect())
    }

    pub fn fragment_statuses(
        &self,
        statuses: Vec<String>,
    ) -> Result<HashMap<FragmentId, FragmentStatus>, RestError> {
        Ok(self
            .rest_client
            .fragments_statuses(statuses)?
            .iter()
            .map(|(id, entry)| {
                let str = id.to_string();
                (FragmentId::from_str(&str).unwrap(), entry.clone())
            })
            .collect())
    }

    pub fn disable_logs(&mut self) {
        self.rest_client.disable_logger();
    }

    pub fn enable_logs(&mut self) {
        self.rest_client.enable_logger();
    }

    pub fn stats(&self) -> Result<NodeStatsDto, RestError> {
        self.rest_client.stats()
    }

    pub fn account_state(&self, account_id: AccountId) -> Result<AccountState, RestError> {
        let public_key: PublicKey<Ed25519> = account_id.into();
        self.account_state_by_pk(public_key.to_bech32_str())
    }

    pub fn account_state_by_pk(&self, bech32: String) -> Result<AccountState, RestError> {
        self.rest_client.account_state_by_pk(&bech32)
    }

    pub fn settings(&self) -> Result<SettingsDto, RestError> {
        self.rest_client.settings()
    }

    pub fn account_exists(&self, account_id: AccountId) -> Result<bool, RestError> {
        let public_key: PublicKey<Ed25519> = account_id.into();
        let response_text = self
            .rest_client
            .account_state_by_pk_raw(&public_key.to_bech32_str())?;
        Ok(!response_text.is_empty())
    }

    pub fn vote_plan_statuses(&self) -> Result<Vec<VotePlanStatus>, RestError> {
        self.rest_client.vote_plan_statuses()
    }

    pub fn account_votes_for_plan(
        &self,
        vote_plan_id: VotePlanId,
        address: Address,
    ) -> Result<Option<Vec<u8>>, RestError> {
        self.rest_client
            .account_votes_with_plan_id(vote_plan_id, address)
    }

    pub fn account_votes(&self, address: Address) -> Result<Option<Vec<AccountVotes>>, RestError> {
        self.rest_client.account_votes(address)
    }
}