-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcoin.rs
More file actions
66 lines (58 loc) · 1.62 KB
/
Copy pathcoin.rs
File metadata and controls
66 lines (58 loc) · 1.62 KB
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
use crate::api::Coin as ProtoCoin;
use crate::Error;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Clone, Serialize, Deserialize)]
pub struct Coin {
pub id: String,
pub name: String,
pub coin_id: i32,
pub symbol: String,
pub decimals: i32,
pub blockchain: String,
pub derivation_path: String,
pub curve: String,
pub public_key_type: String,
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
pub all_info: HashMap<String, serde_json::Value>,
}
impl Coin {
pub fn get_value(&self, key: &str) -> Option<String> {
self.all_info.get(key).map(|x| x.to_string())
}
pub fn get_xpub(&self) -> Option<String> {
self.all_info.get("xpub").map(|x| x.to_string())
}
}
impl PartialEq for Coin {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for Coin {}
impl std::fmt::Display for ProtoCoin {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::str::FromStr for ProtoCoin {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"ethereum" => Ok(ProtoCoin::Ethereum),
"polkadot" => Ok(ProtoCoin::Polkadot),
"solana" => Ok(ProtoCoin::Solana),
"arweave" => Ok(ProtoCoin::Arweave),
_ => Err(Error::NotSupportedCoin),
}
}
}
#[cfg(test)]
mod tests {
use crate::api::Coin as ProtoCoin;
#[test]
fn test_proto_coin_into_str() {
assert_eq!(ProtoCoin::Ethereum.to_string(), "Ethereum");
}
}