File tree Expand file tree Collapse file tree 4 files changed +56
-0
lines changed Expand file tree Collapse file tree 4 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -82,6 +82,12 @@ enum BdkError {
8282 "Psbt",
8383};
8484
85+ [Error]
86+ enum BdkCalculateFeeError {
87+ "MissingTxOut",
88+ "NegativeFee",
89+ };
90+
8591enum ChangeSpendPolicy {
8692 "ChangeAllowed",
8793 "OnlyChange",
@@ -111,6 +117,9 @@ interface Wallet {
111117 SentAndReceivedValues sent_and_received([ByRef] Transaction tx);
112118
113119 sequence<Transaction> transactions();
120+
121+ [Throws=BdkCalculateFeeError]
122+ u64 calculate_fee([ByRef] Transaction tx);
114123};
115124
116125interface Update {};
Original file line number Diff line number Diff line change 1+ use std:: fmt;
2+ use bdk:: chain:: tx_graph:: CalculateFeeError ;
3+ use bdk:: bitcoin:: OutPoint as BdkOutPoint ;
4+
5+ #[ derive( Debug ) ]
6+ pub enum BdkCalculateFeeError {
7+ MissingTxOut { out_points : Vec < BdkOutPoint > } ,
8+ NegativeFee { fee : i64 } ,
9+ }
10+
11+ impl fmt:: Display for BdkCalculateFeeError {
12+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
13+ match self {
14+ BdkCalculateFeeError :: MissingTxOut { out_points } =>
15+ write ! ( f, "Missing transaction output: {:?}" , out_points) ,
16+ BdkCalculateFeeError :: NegativeFee { fee } =>
17+ write ! ( f, "Negative fee value: {}" , fee) ,
18+ }
19+ }
20+ }
21+
22+ impl From < CalculateFeeError > for BdkCalculateFeeError {
23+ fn from ( error : CalculateFeeError ) -> Self {
24+ match error {
25+ CalculateFeeError :: MissingTxOut ( out_points) => BdkCalculateFeeError :: MissingTxOut {
26+ out_points : out_points
27+ . into_iter ( )
28+ . map ( |out_point| BdkOutPoint {
29+ txid : out_point. txid ,
30+ vout : out_point. vout ,
31+ } )
32+ . collect ( ) ,
33+ } ,
34+ CalculateFeeError :: NegativeFee ( fee) => BdkCalculateFeeError :: NegativeFee { fee } ,
35+ }
36+ }
37+ }
38+
39+ impl std:: error:: Error for BdkCalculateFeeError { }
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ mod esplora;
44mod keys;
55mod types;
66mod wallet;
7+ mod error;
78
89use crate :: bitcoin:: Address ;
910use crate :: bitcoin:: Network ;
@@ -24,6 +25,7 @@ use crate::types::Balance;
2425use crate :: types:: LocalUtxo ;
2526use crate :: types:: ScriptAmount ;
2627use crate :: wallet:: BumpFeeTxBuilder ;
28+ use crate :: error:: BdkCalculateFeeError ;
2729use crate :: wallet:: SentAndReceivedValues ;
2830use crate :: wallet:: TxBuilder ;
2931use crate :: wallet:: Update ;
Original file line number Diff line number Diff line change 11use crate :: bitcoin:: { OutPoint , PartiallySignedTransaction , Transaction } ;
22use crate :: descriptor:: Descriptor ;
3+ use crate :: error:: BdkCalculateFeeError ;
34use crate :: types:: Balance ;
45use crate :: types:: ScriptAmount ;
56use crate :: Script ;
@@ -98,6 +99,11 @@ impl Wallet {
9899 . map ( |tx| Arc :: new ( tx. tx_node . tx . clone ( ) . into ( ) ) )
99100 . collect ( )
100101 }
102+
103+ pub fn calculate_fee ( & self , tx : & Transaction ) -> Result < u64 , BdkCalculateFeeError > {
104+ let fee_result = self . get_wallet ( ) . calculate_fee ( & tx. clone ( ) . into ( ) ) ;
105+ fee_result. map_err ( |err| err. into ( ) )
106+ }
101107}
102108
103109pub struct SentAndReceivedValues {
You can’t perform that action at this time.
0 commit comments