-
Notifications
You must be signed in to change notification settings - Fork 62
Add calculate_fee and calculate_fee_rate on wallet #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,17 @@ enum BdkError { | |
| "Psbt", | ||
| }; | ||
|
|
||
| [Error] | ||
| interface CalculateFeeError { | ||
| MissingTxOut(sequence<OutPoint> out_points); | ||
| NegativeFee(i64 fee); | ||
|
Comment on lines
+87
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the first time we use this pattern of defining errors as interfaces in the UDL. Can you confirm that the errors print out what we expect them to? Just making sure it all works well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added two tests in the
... or any other thoughts you had on it too. Thanks, great suggestion! |
||
| }; | ||
|
|
||
| interface FeeRate { | ||
| f32 as_sat_per_vb(); | ||
| f32 sat_per_kwu(); | ||
| }; | ||
|
|
||
| enum ChangeSpendPolicy { | ||
| "ChangeAllowed", | ||
| "OnlyChange", | ||
|
|
@@ -111,6 +122,12 @@ interface Wallet { | |
| SentAndReceivedValues sent_and_received([ByRef] Transaction tx); | ||
|
|
||
| sequence<Transaction> transactions(); | ||
|
|
||
| [Throws=CalculateFeeError] | ||
| u64 calculate_fee([ByRef] Transaction tx); | ||
|
|
||
| [Throws=CalculateFeeError] | ||
| FeeRate calculate_fee_rate([ByRef] Transaction tx); | ||
| }; | ||
|
|
||
| interface Update {}; | ||
|
|
@@ -350,4 +367,4 @@ interface PartiallySignedTransaction { | |
| dictionary OutPoint { | ||
| string txid; | ||
| u32 vout; | ||
| }; | ||
| }; | ||
| 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 { | ||
reez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 {} | ||
reez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[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"); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.