Skip to content

Commit

Permalink
Create main subroutine download-offers
Browse files Browse the repository at this point in the history
It downloads all the offers from makers out there, the same as if
it was a taker, and then displays them. Optionally the user can
pass a single maker address which will make the download-offers
method try to download and display only that maker.
  • Loading branch information
chris-belcher committed Feb 15, 2022
1 parent 29b867c commit 4b1c89e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
59 changes: 58 additions & 1 deletion src/lib.rs
Expand Up @@ -3,6 +3,7 @@ extern crate bitcoin_wallet;
extern crate bitcoincore_rpc;

use dirs::home_dir;
use std::collections::HashMap;
use std::io;
use std::iter::repeat;
use std::path::PathBuf;
Expand All @@ -28,10 +29,12 @@ use maker_protocol::MakerBehavior;
pub mod taker_protocol;
use taker_protocol::TakerConfig;

pub mod offerbook_sync;
use offerbook_sync::{get_advertised_maker_hosts, sync_offerbook_with_hostnames, MakerAddress};

pub mod directory_servers;
pub mod error;
pub mod messages;
pub mod offerbook_sync;
pub mod watchtower_client;
pub mod watchtower_protocol;

Expand Down Expand Up @@ -501,6 +504,60 @@ pub fn recover_from_incomplete_coinswap(
}
}

#[tokio::main]
pub async fn download_and_display_offers(maker_address: Option<String>) {
let maker_addresses = if let Some(maker_addr) = maker_address {
vec![MakerAddress::Tor {
address: maker_addr,
}]
} else {
get_advertised_maker_hosts()
.await
.expect("unable to sync maker addresses from directory servers")
};
let offers_addresses = sync_offerbook_with_hostnames(maker_addresses.clone()).await;
let mut addresses_offers_map = HashMap::new();
for offer_address in offers_addresses.iter() {
let address_str = match &offer_address.address {
MakerAddress::Clearnet { address } => address,
MakerAddress::Tor { address } => address,
};
addresses_offers_map.insert(address_str, offer_address);
}

println!(
"{:70} {:12} {:12} {:12} {:12} {:12} {:12}",
"maker address",
"max size",
"min size",
"abs fee",
"amt rel fee",
"time rel fee",
"minlocktime"
);
for address in maker_addresses {
let address_str = match &address {
MakerAddress::Clearnet { address } => address,
MakerAddress::Tor { address } => address,
};
if let Some(offer_address) = addresses_offers_map.get(&address_str) {
let o = &offer_address.offer;
println!(
"{:70} {:12} {:12} {:12} {:12} {:12} {:12}",
address,
o.max_size,
o.min_size,
o.absolute_fee_sat,
o.amount_relative_fee_ppb,
o.time_relative_fee_ppb,
o.minimum_locktime
);
} else {
println!("{:70} UNREACHABLE", address);
}
}
}

pub fn direct_send(
wallet_file_name: &PathBuf,
send_amount: SendAmount,
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Expand Up @@ -74,6 +74,9 @@ enum Subcommand {
hashvalue: Hash160,
},

/// Download all offers from all makers out there. Optionally download from one given maker
DownloadOffers { maker_address: Option<String> },

/// Send a transaction from the wallet
DirectSend {
/// Amount to send (in sats), or "sweep" for sweep
Expand Down Expand Up @@ -150,6 +153,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
args.dont_broadcast,
);
}
Subcommand::DownloadOffers { maker_address } => {
teleport::download_and_display_offers(maker_address);
}
Subcommand::DirectSend {
send_amount,
destination,
Expand Down
21 changes: 12 additions & 9 deletions src/offerbook_sync.rs
Expand Up @@ -126,7 +126,9 @@ async fn download_maker_offer(address: MakerAddress) -> Option<OfferAndAddress>
}
}

async fn sync_offerbook_with_hostnames(maker_addresses: Vec<MakerAddress>) -> Vec<OfferAndAddress> {
pub async fn sync_offerbook_with_hostnames(
maker_addresses: Vec<MakerAddress>,
) -> Vec<OfferAndAddress> {
let (offers_writer_m, mut offers_reader) = mpsc::channel::<Option<OfferAndAddress>>(100);
//unbounded_channel makes more sense here, but results in a compile
//error i cant figure out
Expand All @@ -149,13 +151,14 @@ async fn sync_offerbook_with_hostnames(maker_addresses: Vec<MakerAddress>) -> Ve
result
}

pub async fn get_advertised_maker_hosts() -> Result<Vec<MakerAddress>, DirectoryServerError> {
Ok(if NETWORK == Network::Regtest {
get_regtest_maker_addresses()
} else {
sync_maker_hosts_from_directory_servers(NETWORK).await?
})
}

pub async fn sync_offerbook() -> Result<Vec<OfferAndAddress>, DirectoryServerError> {
Ok(
sync_offerbook_with_hostnames(if NETWORK == Network::Regtest {
get_regtest_maker_addresses()
} else {
sync_maker_hosts_from_directory_servers(NETWORK).await?
})
.await,
)
Ok(sync_offerbook_with_hostnames(get_advertised_maker_hosts().await?).await)
}

0 comments on commit 4b1c89e

Please sign in to comment.