Add command to withdraw BTC

If no amount is given the wallet will be drained.
This commit is contained in:
Daniel Karzel 2021-03-31 16:55:28 +11:00
parent f5e81bb0ee
commit 96008ec130
2 changed files with 27 additions and 1 deletions

View File

@ -1,6 +1,6 @@
use crate::bitcoin::Amount; use crate::bitcoin::Amount;
use bitcoin::util::amount::ParseAmountError; use bitcoin::util::amount::ParseAmountError;
use bitcoin::Denomination; use bitcoin::{Address, Denomination};
use std::path::PathBuf; use std::path::PathBuf;
#[derive(structopt::StructOpt, Debug)] #[derive(structopt::StructOpt, Debug)]
@ -29,6 +29,15 @@ pub enum Command {
max_buy: Amount, max_buy: Amount,
}, },
History, History,
WithdrawBtc {
#[structopt(
long = "amount",
help = "Optionally specify the amount of Bitcoin to be withdrawn. If not specified the wallet will be drained."
)]
amount: Option<Amount>,
#[structopt(long = "address", help = "The address to receive the Bitcoin.")]
address: Address,
},
} }
fn parse_btc(s: &str) -> Result<Amount, ParseAmountError> { fn parse_btc(s: &str) -> Result<Amount, ParseAmountError> {

View File

@ -127,6 +127,23 @@ async fn main() -> Result<()> {
// Print the table to stdout // Print the table to stdout
table.printstd(); table.printstd();
} }
Command::WithdrawBtc { amount, address } => {
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
let amount = match amount {
Some(amount) => amount,
None => {
bitcoin_wallet
.max_giveable(address.script_pubkey().len())
.await?
}
};
let psbt = bitcoin_wallet.send_to_address(address, amount).await?;
let signed_tx = bitcoin_wallet.sign_and_finalize(psbt).await?;
bitcoin_wallet.broadcast(signed_tx, "withdraw").await?;
}
}; };
Ok(()) Ok(())