1
1
use crate :: esplora:: EsploraClient ;
2
2
use crate :: wallet:: current;
3
3
use crate :: { BTC_ASSET_ID , ESPLORA_API_URL , LOADED_WALLET , USDT_ASSET_ID } ;
4
+ use baru:: avg_vbytes;
4
5
use baru:: wallet:: Wallet ;
5
6
use elements:: bitcoin:: secp256k1:: SecretKey ;
6
7
use elements:: bitcoin:: util:: amount:: Denomination ;
@@ -72,91 +73,50 @@ async fn make_create_swap_payload(
72
73
sell_asset : AssetId ,
73
74
fee_asset : AssetId ,
74
75
) -> Result < CreateSwapPayload , Error > {
76
+ let ( bobs_fee_rate, fee_offset) = if fee_asset == sell_asset {
77
+ // Bob currently hardcodes a fee-rate of 1 sat / vbyte, hence
78
+ // there is no need for us to perform fee estimation. Later
79
+ // on, both parties should probably agree on a block-target
80
+ // and use the same estimation service.
81
+ let bobs_fee_rate = Amount :: ONE_SAT ;
82
+ let fee_offset = calculate_fee_offset ( bobs_fee_rate) ;
83
+
84
+ ( bobs_fee_rate. as_sat ( ) as f32 , fee_offset)
85
+ } else {
86
+ ( 0.0 , Amount :: ZERO )
87
+ } ;
88
+
75
89
let client = {
76
90
let guard = ESPLORA_API_URL . lock ( ) . expect_throw ( "can get lock" ) ;
77
91
EsploraClient :: new ( guard. clone ( ) )
78
92
} ;
79
93
80
- let ( utxos , secret_key, address, blinding_key) = {
94
+ let ( outputs , secret_key, address, blinding_key) = {
81
95
let mut wallet = current ( & wallet_name, & LOADED_WALLET )
82
96
. await
83
97
. map_err ( Error :: LoadWallet ) ?;
84
98
wallet. sync ( & client) . await . map_err ( Error :: SyncWallet ) ?;
99
+ let outputs = wallet
100
+ . coin_selection ( sell_amount, sell_asset, bobs_fee_rate, fee_offset)
101
+ . map_err ( Error :: CoinSelection ) ?;
85
102
(
86
- wallet . utxos ( ) ,
103
+ outputs ,
87
104
wallet. secret_key ( ) ,
88
105
wallet. address ( ) ,
89
106
wallet. blinding_secret_key ( ) ,
90
107
)
91
108
} ;
92
109
93
- let utxos = utxos
94
- . into_iter ( )
95
- . filter_map ( |( utxo, txout) | {
96
- let outpoint = OutPoint {
97
- txid : utxo. txid ,
98
- vout : utxo. vout ,
99
- } ;
100
-
101
- let unblinded_txout = match txout. unblind ( SECP256K1 , secret_key) {
102
- Ok ( txout) => txout,
103
- Err ( _) => {
104
- log:: warn!( "could not unblind utxo {}, ignoring" , outpoint) ;
105
- return None ;
106
- }
107
- } ;
108
- let candidate_asset = unblinded_txout. asset ;
109
-
110
- if candidate_asset == sell_asset {
111
- Some ( coin_selection:: Utxo {
112
- outpoint,
113
- value : unblinded_txout. value ,
114
- script_pubkey : txout. script_pubkey ,
115
- asset : candidate_asset,
116
- } )
117
- } else {
118
- log:: debug!(
119
- "utxo {} with asset id {} is not the sell asset, ignoring" ,
120
- outpoint,
121
- candidate_asset
122
- ) ;
123
- None
124
- }
125
- } )
126
- . collect :: < Vec < _ > > ( ) ;
127
-
128
- let ( bobs_fee_rate, fee_offset) = if fee_asset == sell_asset {
129
- // Bob currently hardcodes a fee-rate of 1 sat / vbyte, hence
130
- // there is no need for us to perform fee estimation. Later
131
- // on, both parties should probably agree on a block-target
132
- // and use the same estimation service.
133
- let bobs_fee_rate = Amount :: from_sat ( 1 ) ;
134
- let fee_offset = calculate_fee_offset ( bobs_fee_rate) ;
135
-
136
- ( bobs_fee_rate, fee_offset)
137
- } else {
138
- ( Amount :: ZERO , Amount :: ZERO )
139
- } ;
140
-
141
- let output = coin_select (
142
- utxos,
143
- sell_amount,
144
- bobs_fee_rate. as_sat ( ) as f32 ,
145
- fee_offset,
146
- )
147
- . map_err ( Error :: CoinSelection ) ?;
148
-
149
110
Ok ( CreateSwapPayload {
150
111
address,
151
- alice_inputs : output
152
- . coins
112
+ alice_inputs : outputs
153
113
. into_iter ( )
154
114
. map ( |utxo| SwapUtxo {
155
- outpoint : utxo. outpoint ,
115
+ outpoint : utxo. txin ,
156
116
blinding_key,
157
117
} )
158
118
. collect ( ) ,
159
- amount : output . target_amount ,
119
+ amount : sell_amount ,
160
120
} )
161
121
}
162
122
@@ -167,7 +127,7 @@ pub enum Error {
167
127
#[ error( "Could not sync waller: {0}" ) ]
168
128
SyncWallet ( anyhow:: Error ) ,
169
129
#[ error( "Coin selection: {0}" ) ]
170
- CoinSelection ( coin_selection :: Error ) ,
130
+ CoinSelection ( anyhow :: Error ) ,
171
131
}
172
132
/// Calculate the fee offset required for the coin selection algorithm.
173
133
///
0 commit comments