tob/
protocol.rs

1use crate::Result;
2use crate::{
3    client::{Client, ClientType},
4    error::ClientError,
5    types::ClientOptions,
6};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum Protocol {
10    Handshake,
11    Nimiq,
12    Bitcoin,
13    Ethereum,
14    EthereumClassic,
15    Ravencoin,
16    Siacoin,
17    Chia,
18    Turtl,
19    Starcoin,
20    //Only constructed as the default. Will fail operations.
21    Unknown,
22}
23
24impl Protocol {
25    pub fn get_client(
26        &self,
27        client_type: ClientType,
28        options: ClientOptions,
29    ) -> Result<Box<Client>> {
30        match *self {
31            Protocol::Bitcoin => Ok(crate::clients::bitcoin::get_client(client_type, options)?),
32            Protocol::Nimiq => Ok(crate::clients::nimiq::get_client(client_type, options)?),
33            Protocol::Handshake => Ok(crate::clients::handshake::get_client(client_type, options)?),
34            Protocol::Ethereum => Ok(crate::clients::ethereum::get_client(client_type, options)?),
35            Protocol::EthereumClassic => Ok(crate::clients::ethereum_classic::get_client(
36                client_type,
37                options,
38            )?),
39            Protocol::Ravencoin => Ok(crate::clients::ravencoin::get_client(client_type, options)?),
40            Protocol::Siacoin => Ok(crate::clients::siacoin::get_client(client_type, options)?),
41            Protocol::Chia => Ok(crate::clients::chia::get_client(client_type, options)?),
42            Protocol::Turtl => Ok(crate::clients::turtl::get_client(client_type, options)?),
43            Protocol::Starcoin => Ok(crate::clients::starcoin::get_client(client_type, options)?),
44            Protocol::Unknown => Err(ClientError::UnknownProtocol),
45        }
46    }
47
48    //@todo cast everything to lowercase first.
49    pub fn from_str(s: &str) -> Protocol {
50        match s {
51            "handshake" | "hns" => Protocol::Handshake,
52            "nimiq" | "nim" => Protocol::Nimiq,
53            "bitcoin" | "btc" => Protocol::Bitcoin,
54            "ethereum" | "eth" => Protocol::Ethereum,
55            "ethereum classic" | "ethereum_classic" | "etc" => Protocol::EthereumClassic,
56            "ravencoin" | "raven" | "rvn" => Protocol::Ravencoin,
57            "siacoin" | "sc" | "sia" => Protocol::Siacoin,
58            "chiacoin" | "chia" => Protocol::Chia,
59            "turtl" | "turtlcoin" => Protocol::Turtl,
60            "starcoin" | "stc" => Protocol::Starcoin,
61            _ => Protocol::Unknown,
62        }
63    }
64
65    //@todo I think this would be nicer if we just have all of the parts of the enum implement
66    //Protocol which requires some set of functions -> Similar to client. But for now, this works.
67    pub fn hashrate_multiplier(&self) -> u64 {
68        match *self {
69            //@todo unconfirmed, but I'm relatively confident. for all below up to Nimiq
70            Protocol::Bitcoin => 2_u64.pow(32),
71            Protocol::Ethereum => 2_u64.pow(32),
72            Protocol::EthereumClassic => 2_u64.pow(32),
73            Protocol::Ravencoin => 2_u64.pow(32),
74            Protocol::Siacoin => 2_u64.pow(32),
75            //@todo unclear how this is calculated since I believe it's mostly space?
76            Protocol::Chia => 1,
77            //@todo neither of these have the correct values... YEt.
78            Protocol::Turtl => 1,
79            Protocol::Starcoin => 1,
80
81            //Confirmed
82            Protocol::Nimiq => 2_u64.pow(16),
83            Protocol::Handshake => 2_u64.pow(32),
84            Protocol::Unknown => 1,
85        }
86    }
87
88    pub fn decimals(&self) -> u32 {
89        match *self {
90            Protocol::Bitcoin => 8,
91            //@todo
92            _ => 0,
93        }
94    }
95
96    pub fn decimal_multiplier(&self) -> u64 {
97        match *self {
98            Protocol::Bitcoin => 100_000_000,
99            //@todo
100            _ => 0,
101        }
102    }
103}
104
105impl From<String> for Protocol {
106    fn from(s: String) -> Self {
107        Protocol::from_str(&s)
108    }
109}
110
111impl From<&str> for Protocol {
112    fn from(s: &str) -> Self {
113        Protocol::from_str(s)
114    }
115}