X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fconvert.rs;h=64039233e7d5800fc9c1c147c2e800c328ced0fd;hb=2dbe461743604d8ce8219e99ea337dd7b8b498df;hp=c92a92682013966b441b24141fc78a00673e59ef;hpb=224d32a53718a00f01456711d2ee524e59c5ff46;p=ldk-sample diff --git a/src/convert.rs b/src/convert.rs index c92a926..6403923 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,86 +1,93 @@ -use bitcoin::BlockHash; use bitcoin::hashes::hex::FromHex; +use bitcoin::BlockHash; use lightning_block_sync::http::JsonResponse; use std::convert::TryInto; pub struct FundedTx { - pub changepos: i64, - pub hex: String, + pub changepos: i64, + pub hex: String, } impl TryInto for JsonResponse { - type Error = std::io::Error; - fn try_into(self) -> std::io::Result { - Ok(FundedTx { - changepos: self.0["changepos"].as_i64().unwrap(), - hex: self.0["hex"].as_str().unwrap().to_string(), - }) - } + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + Ok(FundedTx { + changepos: self.0["changepos"].as_i64().unwrap(), + hex: self.0["hex"].as_str().unwrap().to_string(), + }) + } } pub struct RawTx(pub String); impl TryInto for JsonResponse { - type Error = std::io::Error; - fn try_into(self) -> std::io::Result { - Ok(RawTx(self.0.as_str().unwrap().to_string())) - } + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + Ok(RawTx(self.0.as_str().unwrap().to_string())) + } } pub struct SignedTx { - pub complete: bool, - pub hex: String, + pub complete: bool, + pub hex: String, } impl TryInto for JsonResponse { - type Error = std::io::Error; - fn try_into(self) -> std::io::Result { - Ok(SignedTx { - hex: self.0["hex"].as_str().unwrap().to_string(), - complete: self.0["complete"].as_bool().unwrap(), - }) - } + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + Ok(SignedTx { + hex: self.0["hex"].as_str().unwrap().to_string(), + complete: self.0["complete"].as_bool().unwrap(), + }) + } } pub struct NewAddress(pub String); impl TryInto for JsonResponse { - type Error = std::io::Error; - fn try_into(self) -> std::io::Result { - Ok(NewAddress(self.0.as_str().unwrap().to_string())) - } + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + Ok(NewAddress(self.0.as_str().unwrap().to_string())) + } } pub struct FeeResponse { - pub feerate: Option, - pub errored: bool, + 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(); - Ok(FeeResponse { - errored, - feerate: match errored { - true => None, - // The feerate from bitcoind is in BTC/kb, and we want satoshis/kb. - false => Some((self.0["feerate"].as_f64().unwrap() * 100_000_000.0).round() as u32) - } - }) - } + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + let errored = !self.0["errors"].is_null(); + Ok(FeeResponse { + errored, + 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 BlockchainInfo { - pub latest_height: usize, - pub latest_blockhash: BlockHash, + pub latest_height: usize, + pub latest_blockhash: BlockHash, + pub chain: String, } impl TryInto for JsonResponse { - type Error = std::io::Error; - fn try_into(self) -> std::io::Result { - Ok(BlockchainInfo { - latest_height: self.0["blocks"].as_u64().unwrap() as usize, - latest_blockhash: BlockHash::from_hex(self.0["bestblockhash"].as_str().unwrap()).unwrap(), - }) - } + type Error = std::io::Error; + fn try_into(self) -> std::io::Result { + Ok(BlockchainInfo { + 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(), + }) + } }