X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fconvert.rs;h=7f1bf108828753df82a0a754e82c22bc332a8664;hb=828df536e27155d23d08a99d3edb59ca313ceb3f;hp=7dbd8cceb13caca3938a35077f205b1ff96aa386;hpb=6199433ab8f13e4f78b12e13a683eb33c999114b;p=ldk-sample diff --git a/src/convert.rs b/src/convert.rs index 7dbd8cc..7f1bf10 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,7 +1,8 @@ use bitcoin::hashes::hex::FromHex; -use bitcoin::BlockHash; +use bitcoin::{Address, BlockHash, Txid}; use lightning_block_sync::http::JsonResponse; use std::convert::TryInto; +use std::str::FromStr; pub struct FundedTx { pub changepos: i64, @@ -51,7 +52,7 @@ impl TryInto for JsonResponse { } pub struct FeeResponse { - pub feerate: Option, + pub feerate_sat_per_kw: Option, pub errored: bool, } @@ -61,8 +62,38 @@ impl TryInto for JsonResponse { let errored = !self.0["errors"].is_null(); Ok(FeeResponse { errored, - feerate: match self.0["feerate"].as_f64() { - Some(fee) => Some((fee * 100_000_000.0).round() as u32), + feerate_sat_per_kw: match self.0["feerate"].as_f64() { + // Bitcoin Core gives us a feerate in BTC/KvB, which we need to convert to + // satoshis/KW. Thus, we first multiply by 10^8 to get satoshis, then divide by 4 + // to convert virtual-bytes into weight units. + Some(feerate_btc_per_kvbyte) => { + Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32) + } + None => None, + }, + }) + } +} + +pub struct MempoolMinFeeResponse { + pub feerate_sat_per_kw: Option, + pub errored: bool, +} + +impl TryInto for JsonResponse { + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + let errored = !self.0["errors"].is_null(); + assert_eq!(self.0["maxmempool"].as_u64(), Some(300000000)); + Ok(MempoolMinFeeResponse { + errored, + feerate_sat_per_kw: match self.0["mempoolminfee"].as_f64() { + // Bitcoin Core gives us a feerate in BTC/KvB, which we need to convert to + // satoshis/KW. Thus, we first multiply by 10^8 to get satoshis, then divide by 4 + // to convert virtual-bytes into weight units. + Some(feerate_btc_per_kvbyte) => { + Some((feerate_btc_per_kvbyte * 100_000_000.0 / 4.0).round() as u32) + } None => None, }, }) @@ -72,6 +103,7 @@ impl TryInto for JsonResponse { pub struct BlockchainInfo { pub latest_height: usize, pub latest_blockhash: BlockHash, + pub chain: String, } impl TryInto for JsonResponse { @@ -81,6 +113,37 @@ impl TryInto for JsonResponse { latest_height: self.0["blocks"].as_u64().unwrap() as usize, latest_blockhash: BlockHash::from_hex(self.0["bestblockhash"].as_str().unwrap()) .unwrap(), + chain: self.0["chain"].as_str().unwrap().to_string(), }) } } + +pub struct ListUnspentUtxo { + pub txid: Txid, + pub vout: u32, + pub amount: u64, + pub address: Address, +} + +pub struct ListUnspentResponse(pub Vec); + +impl TryInto for JsonResponse { + type Error = std::io::Error; + fn try_into(self) -> Result { + let utxos = self + .0 + .as_array() + .unwrap() + .iter() + .map(|utxo| ListUnspentUtxo { + txid: Txid::from_str(&utxo["txid"].as_str().unwrap().to_string()).unwrap(), + vout: utxo["vout"].as_u64().unwrap() as u32, + amount: bitcoin::Amount::from_btc(utxo["amount"].as_f64().unwrap()) + .unwrap() + .to_sat(), + address: Address::from_str(&utxo["address"].as_str().unwrap().to_string()).unwrap(), + }) + .collect(); + Ok(ListUnspentResponse(utxos)) + } +}