Skip to content

Commit

Permalink
Add fee rate parameter to main.rs
Browse files Browse the repository at this point in the history
It is used in do-coinswap and direct-send
  • Loading branch information
chris-belcher committed Feb 16, 2022
1 parent 96a86a4 commit 85610a0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/direct_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Wallet {
pub fn create_direct_send(
&mut self,
rpc: &Client,
fee_rate: u64,
send_amount: SendAmount,
destination: Destination,
coins_to_spend: &[CoinToSpend],
Expand Down Expand Up @@ -178,7 +179,7 @@ impl Wallet {
if dest_addr.network != NETWORK {
panic!("wrong address network type (e.g. testnet, regtest)");
}
let miner_fee = 2000; //TODO do this calculation properly
let miner_fee = 500 * fee_rate / 1000; //TODO this is just a rough estimate now

let mut output = Vec::<TxOut>::new();
let total_input_value = unspent_inputs
Expand Down
26 changes: 17 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ pub fn run_maker(
pub fn run_taker(
wallet_file_name: &PathBuf,
sync_amount: WalletSyncAddressAmount,
fee_rate: u64,
send_amount: u64,
maker_count: u16,
tx_count: u32,
Expand Down Expand Up @@ -430,7 +431,7 @@ pub fn run_taker(
maker_count,
tx_count,
required_confirms: 1,
fee_rate: 1000, //satoshis per thousand vbytes, i.e. 1000 = 1 sat/vb
fee_rate,
},
);
}
Expand Down Expand Up @@ -561,6 +562,7 @@ pub async fn download_and_display_offers(maker_address: Option<String>) {

pub fn direct_send(
wallet_file_name: &PathBuf,
fee_rate: u64,
send_amount: SendAmount,
destination: Destination,
coins_to_spend: &[CoinToSpend],
Expand All @@ -583,16 +585,22 @@ pub fn direct_send(
};
wallet.startup_sync(&rpc).unwrap();
let tx = wallet
.create_direct_send(&rpc, send_amount, destination, coins_to_spend)
.create_direct_send(&rpc, fee_rate, send_amount, destination, coins_to_spend)
.unwrap();
if dont_broadcast {
let txhex = bitcoin::consensus::encode::serialize_hex(&tx);
let accepted = rpc
.test_mempool_accept(&[txhex.clone()])
let txhex = bitcoin::consensus::encode::serialize_hex(&tx);
let test_mempool_accept_result = &rpc.test_mempool_accept(&[txhex.clone()]).unwrap()[0];
assert!(test_mempool_accept_result.allowed);
println!(
"actual fee rate = {:.3} sat/vb",
test_mempool_accept_result
.fees
.as_ref()
.unwrap()
.iter()
.any(|tma| tma.allowed);
assert!(accepted);
.base
.as_sat() as f64
/ test_mempool_accept_result.vsize.unwrap() as f64
);
if dont_broadcast {
println!("tx = \n{}", txhex);
} else {
let txid = rpc.send_raw_transaction(&tx).unwrap();
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ struct ArgsWithWalletFile {
#[structopt(short, long)]
dont_broadcast: bool,

/// Miner fee rate, in satoshis per thousand vbytes, i.e. 1000 = 1 sat/vb
#[structopt(default_value = "1000", short = "f", long)]
fee_rate: u64,

/// Subcommand
#[structopt(flatten)]
subcommand: Subcommand,
Expand Down Expand Up @@ -141,6 +145,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
teleport::run_taker(
&args.wallet_file_name,
WalletSyncAddressAmount::Normal,
args.fee_rate,
send_amount,
maker_count.unwrap_or(2),
tx_count.unwrap_or(3),
Expand All @@ -163,6 +168,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} => {
teleport::direct_send(
&args.wallet_file_name,
args.fee_rate,
send_amount,
destination,
&coins_to_spend,
Expand Down
5 changes: 4 additions & 1 deletion src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,10 @@ impl Wallet {

fn is_swapcoin_descriptor_imported(&self, rpc: &Client, descriptor: &str) -> bool {
let addr = rpc.derive_addresses(&descriptor, None).unwrap()[0].clone();
rpc.get_address_info(&addr).unwrap().is_watchonly.unwrap_or(false)
rpc.get_address_info(&addr)
.unwrap()
.is_watchonly
.unwrap_or(false)
}

pub fn get_hd_wallet_descriptors(&self, rpc: &Client) -> Result<Vec<String>, Error> {
Expand Down

0 comments on commit 85610a0

Please sign in to comment.