forked from bitcoindevkit/bdk-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add calculate_fee and calculate_fee_rate on wallet
- Loading branch information
Showing
11 changed files
with
180 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::bitcoin::OutPoint; | ||
|
||
use bdk::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError; | ||
|
||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
pub enum CalculateFeeError { | ||
MissingTxOut { out_points: Vec<OutPoint> }, | ||
NegativeFee { fee: i64 }, | ||
} | ||
|
||
impl fmt::Display for CalculateFeeError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
CalculateFeeError::MissingTxOut { out_points } => { | ||
write!(f, "Missing transaction output: {:?}", out_points) | ||
} | ||
CalculateFeeError::NegativeFee { fee } => write!(f, "Negative fee value: {}", fee), | ||
} | ||
} | ||
} | ||
|
||
impl From<BdkCalculateFeeError> for CalculateFeeError { | ||
fn from(error: BdkCalculateFeeError) -> Self { | ||
match error { | ||
BdkCalculateFeeError::MissingTxOut(out_points) => CalculateFeeError::MissingTxOut { | ||
out_points: out_points.iter().map(|op| op.into()).collect(), | ||
}, | ||
BdkCalculateFeeError::NegativeFee(fee) => CalculateFeeError::NegativeFee { fee }, | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for CalculateFeeError {} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::CalculateFeeError; | ||
use crate::OutPoint; | ||
|
||
#[test] | ||
fn test_error_missing_tx_out() { | ||
let out_points: Vec<OutPoint> = vec![ | ||
OutPoint { | ||
txid: "0000000000000000000000000000000000000000000000000000000000000001" | ||
.to_string(), | ||
vout: 0, | ||
}, | ||
OutPoint { | ||
txid: "0000000000000000000000000000000000000000000000000000000000000002" | ||
.to_string(), | ||
vout: 1, | ||
}, | ||
]; | ||
|
||
let error = CalculateFeeError::MissingTxOut { out_points }; | ||
|
||
let expected_message: String = format!( | ||
"Missing transaction output: [{:?}, {:?}]", | ||
OutPoint { | ||
txid: "0000000000000000000000000000000000000000000000000000000000000001" | ||
.to_string(), | ||
vout: 0 | ||
}, | ||
OutPoint { | ||
txid: "0000000000000000000000000000000000000000000000000000000000000002" | ||
.to_string(), | ||
vout: 1 | ||
} | ||
); | ||
|
||
assert_eq!(error.to_string(), expected_message); | ||
} | ||
|
||
#[test] | ||
fn test_error_negative_fee() { | ||
let error = CalculateFeeError::NegativeFee { fee: -100 }; | ||
|
||
assert_eq!(error.to_string(), "Negative fee value: -100"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters