mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-09 06:38:12 -05:00
create new error type for btc client
This commit is contained in:
22
src/error.rs
22
src/error.rs
@@ -42,6 +42,7 @@ pub enum Error {
|
||||
ZmqError(String),
|
||||
VerifyFailed,
|
||||
ClientFailed(String),
|
||||
BtcFailed(String),
|
||||
TryIntoError,
|
||||
TryFromError,
|
||||
JsonRpcError(String),
|
||||
@@ -54,8 +55,6 @@ pub enum Error {
|
||||
CashierNoReply,
|
||||
Base58EncodeError(String),
|
||||
Base58DecodeError(String),
|
||||
BadBTCAddress(String),
|
||||
BtcClientError,
|
||||
ConfigNotFound,
|
||||
}
|
||||
|
||||
@@ -95,6 +94,7 @@ impl fmt::Display for Error {
|
||||
Error::ZmqError(ref err) => write!(f, "ZmqError: {}", err),
|
||||
Error::VerifyFailed => f.write_str("Verify failed"),
|
||||
Error::ClientFailed(ref err) => write!(f, "Client failed: {}", err),
|
||||
Error::BtcFailed(ref err) => write!(f, "Btc client failed: {}", err),
|
||||
Error::TryIntoError => f.write_str("TryInto error"),
|
||||
Error::TryFromError => f.write_str("TryFrom error"),
|
||||
Error::RocksdbError(ref err) => write!(f, "Rocksdb Error: {}", err),
|
||||
@@ -107,8 +107,6 @@ impl fmt::Display for Error {
|
||||
Error::Base58EncodeError(ref err) => write!(f, "bs58 encode error: {}", err),
|
||||
Error::Base58DecodeError(ref err) => write!(f, "bs58 decode error: {}", err),
|
||||
Error::CashierNoReply => f.write_str("Cashier did not reply with BTC address"),
|
||||
Error::BadBTCAddress(ref err) => write!(f, "could not parse BTC address: {}", err),
|
||||
Error::BtcClientError => f.write_str("Unable to create Electrum Client"),
|
||||
Error::ConfigNotFound => {
|
||||
f.write_str("No config file detected. Please create a config file")
|
||||
}
|
||||
@@ -219,6 +217,12 @@ impl From<client::ClientFailed> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::service::BtcFailed> for Error {
|
||||
fn from(err: crate::service::BtcFailed) -> Error {
|
||||
Error::BtcFailed(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<surf::Error> for Error {
|
||||
fn from(err: surf::Error) -> Error {
|
||||
Error::SurfHttpError(err.to_string())
|
||||
@@ -249,13 +253,3 @@ impl From<bs58::decode::Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bitcoin::util::address::Error> for Error {
|
||||
fn from(err: bitcoin::util::address::Error) -> Error {
|
||||
Error::BadBTCAddress(err.to_string())
|
||||
}
|
||||
}
|
||||
impl From<electrum_client::Error> for Error {
|
||||
fn from(_err: electrum_client::Error) -> Error {
|
||||
Error::BtcClientError
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Error, Result};
|
||||
use crate::Result;
|
||||
use rand::distributions::Alphanumeric;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
@@ -64,7 +64,7 @@ impl BitcoinKeys {
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn start_subscribe(self: Arc<Self>) -> Result<Option<GetBalanceRes>> {
|
||||
pub async fn start_subscribe(self: Arc<Self>) -> BtcResult<Option<GetBalanceRes>> {
|
||||
debug!(target: "BTC CLIENT", "Subscribe to scriptpubkey");
|
||||
let client = &self.btc_client;
|
||||
// Check if script is already subscribed
|
||||
@@ -94,7 +94,9 @@ impl BitcoinKeys {
|
||||
};
|
||||
} // Endloop
|
||||
} else {
|
||||
return Err(Error::ServicesError("Did not subscribe to scriptpubkey"));
|
||||
return Err(BtcFailed::ElectrumError(
|
||||
"Did not subscribe to scriptpubkey".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,3 +127,49 @@ impl BitcoinKeys {
|
||||
&self.script
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BtcFailed {
|
||||
NotEnoughValue(u64),
|
||||
BadBTCAddress(String),
|
||||
ElectrumError(String),
|
||||
BtcError(String),
|
||||
}
|
||||
|
||||
impl std::error::Error for BtcFailed {}
|
||||
|
||||
impl std::fmt::Display for BtcFailed {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
BtcFailed::NotEnoughValue(i) => {
|
||||
write!(f, "There is no enough value {}", i)
|
||||
}
|
||||
BtcFailed::BadBTCAddress(ref err) => {
|
||||
write!(f, "Unable to create Electrum Client: {}", err)
|
||||
}
|
||||
BtcFailed::ElectrumError(ref err) => write!(f, "could not parse BTC address: {}", err),
|
||||
BtcFailed::BtcError(i) => {
|
||||
write!(f, "BtcFailed: {}", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::error::Error> for BtcFailed {
|
||||
fn from(err: crate::error::Error) -> BtcFailed {
|
||||
BtcFailed::BtcError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bitcoin::util::address::Error> for BtcFailed {
|
||||
fn from(err: bitcoin::util::address::Error) -> BtcFailed {
|
||||
BtcFailed::BadBTCAddress(err.to_string())
|
||||
}
|
||||
}
|
||||
impl From<electrum_client::Error> for BtcFailed {
|
||||
fn from(err: electrum_client::Error) -> BtcFailed {
|
||||
BtcFailed::ElectrumError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub type BtcResult<T> = std::result::Result<T, BtcFailed>;
|
||||
|
||||
@@ -50,7 +50,10 @@ impl CashierService {
|
||||
let client_address = btc_endpoint;
|
||||
|
||||
// create btc client
|
||||
let btc_client = Arc::new(ElectrumClient::new(&client_address)?);
|
||||
let btc_client = Arc::new(
|
||||
ElectrumClient::new(&client_address)
|
||||
.map_err(|err| crate::Error::from(super::BtcFailed::from(err)))?,
|
||||
);
|
||||
|
||||
let rocks = Rocks::new(&cashier_database_path)?;
|
||||
|
||||
@@ -207,7 +210,10 @@ impl CashierService {
|
||||
debug!(target: "CASHIER DAEMON", "Received withdraw request");
|
||||
let btc_address = request.get_payload();
|
||||
let btc_address: String = deserialize(&btc_address)?;
|
||||
let btc_address = bitcoin::util::address::Address::from_str(&btc_address)?;
|
||||
let btc_address =
|
||||
bitcoin::util::address::Address::from_str(&btc_address).map_err(|err| {
|
||||
crate::Error::from(super::BtcFailed::from(err))
|
||||
})?;
|
||||
|
||||
let cashier_public: jubjub::SubgroupPoint;
|
||||
|
||||
|
||||
@@ -8,4 +8,4 @@ pub use gateway::{GatewayClient, GatewayService, GatewaySlabsSubscriber};
|
||||
|
||||
pub use cashier::{CashierClient, CashierService};
|
||||
|
||||
pub use btc::{BitcoinKeys, PubAddress};
|
||||
pub use btc::{BitcoinKeys, PubAddress, BtcFailed, BtcResult};
|
||||
|
||||
Reference in New Issue
Block a user