Skip to content

Commit 9deb2d5

Browse files
author
Gyan Ranjan Panda
committed
fix: minimize PR changes, return BuilderResult
1 parent 45506e0 commit 9deb2d5

11 files changed

Lines changed: 422 additions & 407 deletions

File tree

examples/electrum.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ fn main() -> Result<(), anyhow::Error> {
8888
tx_builder.add_recipient(address.script_pubkey(), SEND_AMOUNT);
8989
tx_builder.fee_rate(target_fee_rate);
9090

91-
let mut psbt = tx_builder.finish()?;
92-
let finalized = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
91+
let mut psbt = tx_builder.finish()?.psbt;
92+
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
9393
assert!(finalized);
94-
let original_fee = psbt.psbt.fee_amount().unwrap();
95-
let tx_feerate = psbt.fee_rate;
96-
let tx = psbt.psbt.extract_tx()?;
94+
let original_fee = psbt.fee_amount().unwrap();
95+
let tx_feerate = psbt.fee_rate().unwrap();
96+
let tx = psbt.extract_tx()?;
9797
client.transaction_broadcast(&tx)?;
9898
let txid = tx.compute_txid();
9999
println!("Tx broadcasted! Txid: https://mempool.space/testnet4/tx/{txid}");
@@ -123,11 +123,11 @@ fn main() -> Result<(), anyhow::Error> {
123123
let feerate = FeeRate::from_sat_per_vb(tx_feerate.to_sat_per_vb_ceil() + 1).unwrap();
124124
let mut builder = wallet.build_fee_bump(txid).expect("failed to bump tx");
125125
builder.fee_rate(feerate);
126-
let mut bumped_psbt = builder.finish().unwrap();
126+
let mut bumped_psbt = builder.finish().unwrap().psbt;
127127
let finalize_btx = wallet.sign(&mut bumped_psbt, SignOptions::default())?;
128128
assert!(finalize_btx);
129-
let new_fee = bumped_psbt.psbt.fee_amount().unwrap();
130-
let bumped_tx = bumped_psbt.psbt.extract_tx()?;
129+
let new_fee = bumped_psbt.fee_amount().unwrap();
130+
let bumped_tx = bumped_psbt.extract_tx()?;
131131
assert_eq!(
132132
bumped_tx
133133
.output

examples/esplora_async.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ async fn main() -> Result<(), anyhow::Error> {
8282
tx_builder.add_recipient(address.script_pubkey(), SEND_AMOUNT);
8383
tx_builder.fee_rate(target_fee_rate);
8484

85-
let mut psbt = tx_builder.finish()?;
86-
let finalized = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
85+
let mut psbt = tx_builder.finish()?.psbt;
86+
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
8787
assert!(finalized);
88-
let original_fee = psbt.psbt.fee_amount().unwrap();
89-
let tx_feerate = psbt.fee_rate;
90-
let tx = psbt.psbt.extract_tx()?;
88+
let original_fee = psbt.fee_amount().unwrap();
89+
let tx_feerate = psbt.fee_rate().unwrap();
90+
let tx = psbt.extract_tx()?;
9191
client.broadcast(&tx).await?;
9292
let txid = tx.compute_txid();
9393
println!("Tx broadcasted! Txid: https://mempool.space/testnet4/tx/{txid}");
@@ -116,11 +116,11 @@ async fn main() -> Result<(), anyhow::Error> {
116116
let feerate = FeeRate::from_sat_per_vb(tx_feerate.to_sat_per_vb_ceil() + 1).unwrap();
117117
let mut builder = wallet.build_fee_bump(txid).expect("failed to bump tx");
118118
builder.fee_rate(feerate);
119-
let mut bumped_psbt = builder.finish().unwrap();
119+
let mut bumped_psbt = builder.finish().unwrap().psbt;
120120
let finalize_btx = wallet.sign(&mut bumped_psbt, SignOptions::default())?;
121121
assert!(finalize_btx);
122-
let new_fee = bumped_psbt.psbt.fee_amount().unwrap();
123-
let bumped_tx = bumped_psbt.psbt.extract_tx()?;
122+
let new_fee = bumped_psbt.fee_amount().unwrap();
123+
let bumped_tx = bumped_psbt.extract_tx()?;
124124
assert_eq!(
125125
bumped_tx
126126
.output

examples/esplora_blocking.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ fn main() -> Result<(), anyhow::Error> {
7777
tx_builder.add_recipient(address.script_pubkey(), SEND_AMOUNT);
7878
tx_builder.fee_rate(target_fee_rate);
7979

80-
let mut psbt = tx_builder.finish()?;
81-
let finalized = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
80+
let mut psbt = tx_builder.finish()?.psbt;
81+
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
8282
assert!(finalized);
83-
let original_fee = psbt.psbt.fee_amount().unwrap();
84-
let tx_feerate = psbt.fee_rate;
85-
let tx = psbt.psbt.extract_tx()?;
83+
let original_fee = psbt.fee_amount().unwrap();
84+
let tx_feerate = psbt.fee_rate().unwrap();
85+
let tx = psbt.extract_tx()?;
8686
client.broadcast(&tx)?;
8787
let txid = tx.compute_txid();
8888
println!("Tx broadcasted! Txid: https://mempool.space/testnet4/tx/{txid}");
@@ -112,11 +112,11 @@ fn main() -> Result<(), anyhow::Error> {
112112
let feerate = FeeRate::from_sat_per_vb(tx_feerate.to_sat_per_vb_ceil() + 1).unwrap();
113113
let mut builder = wallet.build_fee_bump(txid).unwrap();
114114
builder.fee_rate(feerate);
115-
let mut new_psbt = builder.finish().unwrap();
115+
let mut new_psbt = builder.finish().unwrap().psbt;
116116
let finalize_tx = wallet.sign(&mut new_psbt, SignOptions::default())?;
117117
assert!(finalize_tx);
118-
let new_fee = new_psbt.psbt.fee_amount().unwrap();
119-
let bumped_tx = new_psbt.psbt.extract_tx()?;
118+
let new_fee = new_psbt.fee_amount().unwrap();
119+
let bumped_tx = new_psbt.extract_tx()?;
120120
assert_eq!(
121121
bumped_tx
122122
.output

src/wallet/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,10 +1679,7 @@ impl Wallet {
16791679
}
16801680
}
16811681

1682-
Ok(tx_builder::BuilderResult {
1683-
psbt,
1684-
fee_rate,
1685-
})
1682+
Ok(tx_builder::BuilderResult { psbt, fee_rate })
16861683
}
16871684

16881685
/// Bump the fee of a transaction previously created with this wallet.
@@ -1710,8 +1707,8 @@ impl Wallet {
17101707
/// .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000));
17111708
/// builder.finish()?
17121709
/// };
1713-
/// let _ = wallet.sign(&mut psbt, SignOptions::default())?;
1714-
/// let tx = psbt.clone().extract_tx().expect("tx");
1710+
/// let _ = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
1711+
/// let tx = psbt.psbt.clone().extract_tx().expect("tx");
17151712
/// // broadcast tx but it's taking too long to confirm so we want to bump the fee
17161713
/// let mut psbt = {
17171714
/// let mut builder = wallet.build_fee_bump(tx.compute_txid())?;
@@ -1720,8 +1717,8 @@ impl Wallet {
17201717
/// builder.finish()?
17211718
/// };
17221719
///
1723-
/// let _ = wallet.sign(&mut psbt, SignOptions::default())?;
1724-
/// let fee_bumped_tx = psbt.extract_tx();
1720+
/// let _ = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
1721+
/// let fee_bumped_tx = psbt.psbt.extract_tx();
17251722
/// // broadcast fee_bumped_tx to replace original
17261723
/// # Ok::<(), anyhow::Error>(())
17271724
/// ```
@@ -1886,7 +1883,7 @@ impl Wallet {
18861883
/// builder.add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000));
18871884
/// builder.finish()?
18881885
/// };
1889-
/// let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
1886+
/// let finalized = wallet.sign(&mut psbt.psbt, SignOptions::default())?;
18901887
/// assert!(finalized, "we should have signed all the inputs");
18911888
/// # Ok::<(),anyhow::Error>(())
18921889
pub fn sign(&self, psbt: &mut Psbt, sign_options: SignOptions) -> Result<bool, SignerError> {

src/wallet/tx_builder.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use crate::{KeychainKind, LocalOutput, Utxo, WeightedUtxo};
9595
/// builder.finish()?
9696
/// };
9797
///
98-
/// assert_eq!(psbt1.unsigned_tx.output[..2], psbt2.unsigned_tx.output[..2]);
98+
/// assert_eq!(psbt1.psbt.unsigned_tx.output[..2], psbt2.psbt.unsigned_tx.output[..2]);
9999
/// # Ok::<(), anyhow::Error>(())
100100
/// ```
101101
///
@@ -773,7 +773,10 @@ impl<Cs: CoinSelectionAlgorithm> TxBuilder<'_, Cs> {
773773
///
774774
/// **WARNING**: To avoid change address reuse you must persist the changes resulting from one
775775
/// or more calls to this method before closing the wallet. See [`Wallet::reveal_next_address`].
776-
pub fn finish_with_aux_rand(self, rng: &mut impl RngCore) -> Result<BuilderResult, CreateTxError> {
776+
pub fn finish_with_aux_rand(
777+
self,
778+
rng: &mut impl RngCore,
779+
) -> Result<BuilderResult, CreateTxError> {
777780
self.wallet.create_tx(self.coin_selection, self.params, rng)
778781
}
779782
}
@@ -1235,7 +1238,12 @@ mod test {
12351238
.drain_wallet()
12361239
.drain_to(recipient.script_pubkey());
12371240
let tx = builder.finish().unwrap();
1238-
let output = tx.unsigned_tx.output.first().expect("must have one output");
1241+
let output = tx
1242+
.psbt
1243+
.unsigned_tx
1244+
.output
1245+
.first()
1246+
.expect("must have one output");
12391247
assert_eq!(output.value, Amount::ONE_BTC * 6);
12401248
}
12411249

@@ -1248,7 +1256,12 @@ mod test {
12481256
.drain_wallet()
12491257
.drain_to(recipient.script_pubkey());
12501258
let tx = builder.finish().unwrap();
1251-
let output = tx.unsigned_tx.output.first().expect("must have one output");
1259+
let output = tx
1260+
.psbt
1261+
.unsigned_tx
1262+
.output
1263+
.first()
1264+
.expect("must have one output");
12521265
assert_eq!(output.value, Amount::ONE_BTC * 3);
12531266
}
12541267

@@ -1261,7 +1274,12 @@ mod test {
12611274
.drain_wallet()
12621275
.drain_to(recipient.script_pubkey());
12631276
let tx = builder.finish().unwrap();
1264-
let output = tx.unsigned_tx.output.first().expect("must have one output");
1277+
let output = tx
1278+
.psbt
1279+
.unsigned_tx
1280+
.output
1281+
.first()
1282+
.expect("must have one output");
12651283
assert_eq!(output.value, Amount::ONE_BTC);
12661284
}
12671285
}
@@ -1297,7 +1315,7 @@ mod test {
12971315
builder.fee_absolute(Amount::from_sat(1_000));
12981316
let psbt = builder.finish().unwrap();
12991317

1300-
let tx = psbt.extract_tx().unwrap();
1318+
let tx = psbt.psbt.extract_tx().unwrap();
13011319
let txid = tx.compute_txid();
13021320
let feerate = wallet.calculate_fee_rate(&tx).unwrap().to_sat_per_kwu();
13031321
insert_tx(&mut wallet, tx);

tests/add_foreign_utxo.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ fn test_add_foreign_utxo() {
3535
.only_witness_utxo()
3636
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
3737
.unwrap();
38-
let mut psbt = builder.finish().unwrap();
38+
let mut psbt = builder.finish().unwrap().psbt;
3939
wallet1.insert_txout(utxo.outpoint, utxo.txout);
4040
let fee = check_fee!(wallet1, psbt);
4141
let (sent, received) =
42-
wallet1.sent_and_received(&psbt.clone().psbt.extract_tx().expect("failed to extract tx"));
42+
wallet1.sent_and_received(&psbt.clone().extract_tx().expect("failed to extract tx"));
4343

4444
assert_eq!(
4545
(sent - received),
@@ -48,7 +48,7 @@ fn test_add_foreign_utxo() {
4848
);
4949

5050
assert!(
51-
psbt.psbt.unsigned_tx
51+
psbt.unsigned_tx
5252
.input
5353
.iter()
5454
.any(|input| input.previous_output == utxo.outpoint),
@@ -109,8 +109,8 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
109109
.only_witness_utxo()
110110
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
111111
.unwrap();
112-
let psbt = builder.finish().unwrap();
113-
let tx = psbt.psbt.extract_tx().expect("failed to extract tx");
112+
let psbt = builder.finish().unwrap().psbt;
113+
let tx = psbt.extract_tx().expect("failed to extract tx");
114114
let res = wallet1.calculate_fee(&tx);
115115
assert!(
116116
matches!(res, Err(CalculateFeeError::MissingTxOut(outpoints)) if outpoints[0] == utxo.outpoint)
@@ -270,9 +270,9 @@ fn test_taproot_foreign_utxo() {
270270
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000))
271271
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction)
272272
.unwrap();
273-
let psbt = builder.finish().unwrap();
273+
let psbt = builder.finish().unwrap().psbt;
274274
let (sent, received) =
275-
wallet1.sent_and_received(&psbt.clone().psbt.extract_tx().expect("failed to extract tx"));
275+
wallet1.sent_and_received(&psbt.clone().extract_tx().expect("failed to extract tx"));
276276
wallet1.insert_txout(utxo.outpoint, utxo.txout);
277277
let fee = check_fee!(wallet1, psbt);
278278

@@ -283,7 +283,7 @@ fn test_taproot_foreign_utxo() {
283283
);
284284

285285
assert!(
286-
psbt.psbt.unsigned_tx
286+
psbt.unsigned_tx
287287
.input
288288
.iter()
289289
.any(|input| input.previous_output == utxo.outpoint),

0 commit comments

Comments
 (0)