-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdigitalcash.rs
More file actions
122 lines (104 loc) · 3.14 KB
/
digitalcash.rs
File metadata and controls
122 lines (104 loc) · 3.14 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
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 std::collections::HashMap;
use dpp::dashcore::{Address, InstantLock, Network, Txid};
use http::StatusCode;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::errors::Error;
pub struct DigitalCashAPI {
url: String,
}
#[derive(Serialize)]
pub struct JsonRPCHashMapArguments {
method: String,
params: Vec<HashMap<String, Vec<String>>>
}
#[derive(Serialize)]
pub struct JsonRPCArrayArguments {
method: String,
params: Vec<Vec<String>>
}
#[derive(Serialize, Deserialize)]
pub struct UTXO {
pub address: String,
pub txid: String,
#[serde(rename(deserialize = "outputIndex"))]
pub output_index: u32,
pub script: String,
pub satoshis: u64,
pub height: u32
}
#[derive(Serialize, Deserialize)]
pub struct GetTxChainLocksResult {
pub height: i32,
pub chainlock: bool,
pub mempool: bool,
}
#[derive(Serialize, Deserialize)]
pub struct JsonResponse<T> {
result: T,
}
impl DigitalCashAPI {
pub fn new(network: Network) -> Self {
let url = match network {
Network::Dash => "https://rpc.digitalcash.dev",
Network::Testnet => "https://trpc.digitalcash.dev",
_ => panic!("Network {} is not supported by digitalcash.dev", network)
};
return DigitalCashAPI { url: String::from(url) };
}
pub async fn get_address_utxos(&self, address: Address) -> Result<Vec<UTXO>, Error> {
let mut params: HashMap<String, Vec<String>> = HashMap::new();
params.insert(String::from("addresses"), vec![address.to_string()]);
let p = JsonRPCHashMapArguments {
method: String::from("getaddressutxos"),
params: vec![params],
};
let res = reqwest::Client::new()
.post(&self.url)
.json(&p)
.send()
.await.unwrap();
let status_code = res.status();
match status_code.as_u16() {
420 => {
panic!("Rate limit")
},
200 => {
let json = res
.json::<JsonResponse<Vec<UTXO>>>()
.await.unwrap();
Ok(json.result)
},
_ => {
panic!("Unknown status code")
}
}
}
pub async fn get_tx_chain_locks(&self, txid: Txid) -> Result<GetTxChainLocksResult, Error> {
let params: Vec<String> = vec![txid.to_hex()];
let p = JsonRPCArrayArguments {
method: String::from("gettxchainlocks"),
params: vec![params],
};
let res = reqwest::Client::new()
.post(&self.url)
.json(&p)
.send()
.await.unwrap();
let status_code = res.status();
match status_code.as_u16() {
420 => {
panic!("Rate limit")
},
200 => {
let json = res
.json::<JsonResponse<Vec<GetTxChainLocksResult>>>()
.await.unwrap();
return Ok(json.result.into_iter().nth(0).unwrap())
},
_ => {
panic!("Unknown status code")
}
}
}
}