Skip to content

Commit

Permalink
rpc_api: fund_raw_transaction: Use option change_position.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Sep 2, 2024
1 parent 6f1709e commit b78cbfa
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/client/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl RpcApi for Client {
fn fund_raw_transaction<R: bitcoincore_rpc::RawTx>(
&self,
tx: R,
_options: Option<&json::FundRawTransactionOptions>,
options: Option<&json::FundRawTransactionOptions>,
_is_witness: Option<bool>,
) -> bitcoincore_rpc::Result<json::FundRawTransactionResult> {
let mut transaction: Transaction = encode::deserialize_hex(&tx.raw_hex())?;
Expand Down Expand Up @@ -421,15 +421,22 @@ impl RpcApi for Client {
..Default::default()
};

transaction.input.insert(0, txin);
let insert_idx = match options {
Some(option) => option
.change_position
.unwrap_or((transaction.input.len()) as u32),
None => (transaction.input.len()) as u32,
};

transaction.input.insert(insert_idx as usize, txin);
tracing::debug!("New transaction: {transaction:?}");

let hex = serialize(&transaction);

Ok(json::FundRawTransactionResult {
hex,
fee: Amount::from_sat(0),
change_position: 0,
change_position: insert_idx as i32,
})
}

Expand Down Expand Up @@ -822,7 +829,7 @@ mod tests {
let tx = deserialize::<Transaction>(&res.hex).unwrap();

assert_ne!(og_tx, tx);
assert_eq!(res.change_position, 0);
assert_ne!(res.change_position, -1);

let res = rpc.fund_raw_transaction(&tx, None, None).unwrap();
let new_tx = String::consensus_decode(&mut res.hex.as_slice()).unwrap();
Expand Down

0 comments on commit b78cbfa

Please sign in to comment.