1 use bitcoin::hashes::hex::FromHex;
2 use bitcoin::{Address, BlockHash, Txid};
3 use lightning_block_sync::http::JsonResponse;
4 use std::convert::TryInto;
12 impl TryInto<FundedTx> for JsonResponse {
13 type Error = std::io::Error;
14 fn try_into(self) -> std::io::Result<FundedTx> {
16 changepos: self.0["changepos"].as_i64().unwrap(),
17 hex: self.0["hex"].as_str().unwrap().to_string(),
22 pub struct RawTx(pub String);
24 impl TryInto<RawTx> for JsonResponse {
25 type Error = std::io::Error;
26 fn try_into(self) -> std::io::Result<RawTx> {
27 Ok(RawTx(self.0.as_str().unwrap().to_string()))
36 impl TryInto<SignedTx> for JsonResponse {
37 type Error = std::io::Error;
38 fn try_into(self) -> std::io::Result<SignedTx> {
40 hex: self.0["hex"].as_str().unwrap().to_string(),
41 complete: self.0["complete"].as_bool().unwrap(),
46 pub struct NewAddress(pub String);
47 impl TryInto<NewAddress> for JsonResponse {
48 type Error = std::io::Error;
49 fn try_into(self) -> std::io::Result<NewAddress> {
50 Ok(NewAddress(self.0.as_str().unwrap().to_string()))
54 pub struct FeeResponse {
55 pub feerate_sat_per_kw: Option<u32>,
59 impl TryInto<FeeResponse> for JsonResponse {
60 type Error = std::io::Error;
61 fn try_into(self) -> std::io::Result<FeeResponse> {
62 let errored = !self.0["errors"].is_null();
65 feerate_sat_per_kw: match self.0["feerate"].as_f64() {
66 // Bitcoin Core gives us a feerate in BTC/KvB, which we need to convert to
67 // satoshis/KW. Thus, we first multiply by 10^8 to get satoshis, then divide by 4
68 // to convert virtual-bytes into weight units.
69 Some(feerate_btc_per_kvbyte) => {
70 Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32)
78 pub struct MempoolMinFeeResponse {
79 pub feerate_sat_per_kw: Option<u32>,
83 impl TryInto<MempoolMinFeeResponse> for JsonResponse {
84 type Error = std::io::Error;
85 fn try_into(self) -> std::io::Result<MempoolMinFeeResponse> {
86 let errored = !self.0["errors"].is_null();
87 assert_eq!(self.0["maxmempool"].as_u64(), Some(300000000));
88 Ok(MempoolMinFeeResponse {
90 feerate_sat_per_kw: match self.0["mempoolminfee"].as_f64() {
91 // Bitcoin Core gives us a feerate in BTC/KvB, which we need to convert to
92 // satoshis/KW. Thus, we first multiply by 10^8 to get satoshis, then divide by 4
93 // to convert virtual-bytes into weight units.
94 Some(feerate_btc_per_kvbyte) => {
95 Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32)
103 pub struct BlockchainInfo {
104 pub latest_height: usize,
105 pub latest_blockhash: BlockHash,
109 impl TryInto<BlockchainInfo> for JsonResponse {
110 type Error = std::io::Error;
111 fn try_into(self) -> std::io::Result<BlockchainInfo> {
113 latest_height: self.0["blocks"].as_u64().unwrap() as usize,
114 latest_blockhash: BlockHash::from_hex(self.0["bestblockhash"].as_str().unwrap())
116 chain: self.0["chain"].as_str().unwrap().to_string(),
121 pub struct ListUnspentUtxo {
125 pub address: Address,
128 pub struct ListUnspentResponse(pub Vec<ListUnspentUtxo>);
130 impl TryInto<ListUnspentResponse> for JsonResponse {
131 type Error = std::io::Error;
132 fn try_into(self) -> Result<ListUnspentResponse, Self::Error> {
138 .map(|utxo| ListUnspentUtxo {
139 txid: Txid::from_str(&utxo["txid"].as_str().unwrap().to_string()).unwrap(),
140 vout: utxo["vout"].as_u64().unwrap() as u32,
141 amount: bitcoin::Amount::from_btc(utxo["amount"].as_f64().unwrap())
144 address: Address::from_str(&utxo["address"].as_str().unwrap().to_string()).unwrap(),
147 Ok(ListUnspentResponse(utxos))