mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-26 16:09:41 -05:00
Calculate max_giveable based on spending script size
This commit is contained in:
parent
7251588e79
commit
9f0b1c5cbe
@ -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);
|
||||||
|
|
||||||
|
@ -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 {
|
||||||
|
@ -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()?;
|
||||||
|
Loading…
Reference in New Issue
Block a user