Skip to content
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

Alternative implementation for maxfeerate and maxburnamount #377

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,27 @@ pub trait RpcApi: Sized {
self.call("sendrawtransaction", &[tx.raw_hex().into()])
}

fn send_raw_transaction_advanced<R: RawTx>(
&self,
tx: R,
maxfeerate: Option<f64>,
maxburnamount: Option<f64>,
) -> Result<bitcoin::Txid> {
let mut params = vec![serde_json::json!(tx.raw_hex())];

if let Some(fee_rate) = maxfeerate {
params.push(serde_json::json!(fee_rate));
} else {
params.push(serde_json::Value::Null);
}

if let Some(burn_amount) = maxburnamount {
params.push(serde_json::json!(burn_amount));
}

self.call("sendrawtransaction", &params)
}

fn estimate_smart_fee(
&self,
conf_target: u16,
Expand Down
52 changes: 52 additions & 0 deletions integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ fn main() {
test_lock_unspent_unlock_unspent(&cl);
test_get_block_filter(&cl);
test_sign_raw_transaction_with_send_raw_transaction(&cl);
test_send_raw_transaction_advanced(&cl);
test_invalidate_block_reconsider_block(&cl);
test_key_pool_refill(&cl);
test_create_raw_transaction(&cl);
Expand Down Expand Up @@ -640,6 +641,57 @@ fn test_sign_raw_transaction_with_send_raw_transaction(cl: &Client) {
let _ = cl.send_raw_transaction(&res.transaction().unwrap()).unwrap();
}

fn test_send_raw_transaction_advanced(cl: &Client) {
let sk = PrivateKey {
network: Network::Regtest.into(),
inner: secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()),
compressed: true,
};
let pk = CompressedPublicKey::from_private_key(&SECP, &sk).unwrap();
let addr = Address::p2wpkh(&pk, Network::Regtest);

let options = json::ListUnspentQueryOptions {
minimum_amount: Some(btc(2)),
..Default::default()
};
let unspent = cl.list_unspent(Some(6), None, None, None, Some(options)).unwrap();
let unspent = unspent.into_iter().nth(0).unwrap();

let tx = Transaction {
version: transaction::Version::ONE,
lock_time: LockTime::ZERO,
input: vec![TxIn {
previous_output: OutPoint {
txid: unspent.txid,
vout: unspent.vout,
},
sequence: Sequence::MAX,
script_sig: ScriptBuf::new(),
witness: Witness::new(),
}],
output: vec![TxOut {
value: (unspent.amount - *FEE),
script_pubkey: addr.script_pubkey(),
}],
};

let signed_tx = cl.sign_raw_transaction_with_wallet(&tx, None, None)
.unwrap()
.transaction()
.unwrap();

let tx_hex = serialize_hex(&signed_tx);

let maxfeerate = Some(0.01);
let maxburnamount = Some(0.02);

let txid = cl.send_raw_transaction_advanced(tx_hex, maxfeerate, maxburnamount).unwrap();

assert!(!txid.to_string().is_empty(), "Transaction ID should not be empty");

println!("Transaction sent successfully with txid: {}", txid);
}

fn test_invalidate_block_reconsider_block(cl: &Client) {
let hash = cl.get_best_block_hash().unwrap();
cl.invalidate_block(&hash).unwrap();
Expand Down