247: Calculate max_giveable based on spending script size r=da-kami a=thomaseizinger



Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
This commit is contained in:
bors[bot] 2021-03-01 05:27:28 +00:00 committed by GitHub
commit 1de3fa486e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 8 deletions

View File

@ -19,7 +19,7 @@ use std::{path::Path, sync::Arc, time::Duration};
use structopt::StructOpt; use structopt::StructOpt;
use swap::{ use swap::{
bitcoin, bitcoin,
bitcoin::Amount, bitcoin::{Amount, TxLock},
cli::{ cli::{
command::{Arguments, Command}, command::{Arguments, Command},
config::{read_config, Config}, config::{read_config, Config},
@ -112,7 +112,7 @@ async fn main() -> Result<()> {
debug!("Received {}", bitcoin_wallet.balance().await?); debug!("Received {}", bitcoin_wallet.balance().await?);
} }
let send_bitcoin = bitcoin_wallet.max_giveable().await?; let send_bitcoin = bitcoin_wallet.max_giveable(TxLock::script_size()).await?;
info!("Swapping {} ...", send_bitcoin); info!("Swapping {} ...", send_bitcoin);

View File

@ -3,7 +3,9 @@ use crate::bitcoin::{
}; };
use ::bitcoin::{util::psbt::PartiallySignedTransaction, OutPoint, TxIn, TxOut, Txid}; use ::bitcoin::{util::psbt::PartiallySignedTransaction, OutPoint, TxIn, TxOut, Txid};
use anyhow::Result; use anyhow::Result;
use ecdsa_fun::fun::Point;
use miniscript::{Descriptor, DescriptorTrait}; use miniscript::{Descriptor, DescriptorTrait};
use rand::thread_rng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
@ -42,6 +44,16 @@ impl TxLock {
OutPoint::new(self.txid(), self.lock_output_vout() as u32) OutPoint::new(self.txid(), self.lock_output_vout() as u32)
} }
/// Calculate the size of the script used by this transaction.
pub fn script_size() -> usize {
build_shared_output_descriptor(
Point::random(&mut thread_rng()),
Point::random(&mut thread_rng()),
)
.script_pubkey()
.len()
}
/// Retreive the index of the locked output in the transaction outputs /// Retreive the index of the locked output in the transaction outputs
/// vector /// vector
fn lock_output_vout(&self) -> usize { fn lock_output_vout(&self) -> usize {

View File

@ -132,17 +132,13 @@ impl Wallet {
/// We define this as the maximum amount we can pay to a single output, /// We define this as the maximum amount we can pay to a single output,
/// already accounting for the fees we need to spend to get the /// already accounting for the fees we need to spend to get the
/// transaction confirmed. /// transaction confirmed.
pub async fn max_giveable(&self) -> Result<Amount> { pub async fn max_giveable(&self, locking_script_size: usize) -> Result<Amount> {
let wallet = self.inner.lock().await; let wallet = self.inner.lock().await;
let mut tx_builder = wallet.build_tx(); let mut tx_builder = wallet.build_tx();
// create a dummy script to make the txbuilder pass let dummy_script = Script::from(vec![0u8; locking_script_size]);
// we don't intend to send this transaction, we just want to know the max amount
// we can spend
let dummy_script = Script::default();
tx_builder.set_single_recipient(dummy_script); tx_builder.set_single_recipient(dummy_script);
tx_builder.drain_wallet(); tx_builder.drain_wallet();
tx_builder.fee_rate(self.select_feerate()); tx_builder.fee_rate(self.select_feerate());
let (_, details) = tx_builder.finish()?; let (_, details) = tx_builder.finish()?;