diff --git a/taptrade-cli-demo/coordinator/Cargo.lock b/taptrade-cli-demo/coordinator/Cargo.lock index c67f747..9cf41bf 100644 --- a/taptrade-cli-demo/coordinator/Cargo.lock +++ b/taptrade-cli-demo/coordinator/Cargo.lock @@ -30,12 +30,70 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "allocator-api2" version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -379,6 +437,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + [[package]] name = "const-oid" version = "0.9.6" @@ -394,8 +458,10 @@ dependencies = [ "bdk", "bitcoin 0.32.2", "dotenv", + "env_logger", "futures-util", "hex", + "log", "miniscript 12.0.0", "rand", "reqwest", @@ -560,6 +626,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -940,6 +1029,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "1.4.0" @@ -1049,6 +1144,12 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itoa" version = "1.0.11" @@ -1545,6 +1646,35 @@ dependencies = [ "bitflags 2.6.0", ] +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + [[package]] name = "reqwest" version = "0.12.5" @@ -2535,6 +2665,12 @@ version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/taptrade-cli-demo/coordinator/Cargo.toml b/taptrade-cli-demo/coordinator/Cargo.toml index 7d9b505..4307e8b 100644 --- a/taptrade-cli-demo/coordinator/Cargo.toml +++ b/taptrade-cli-demo/coordinator/Cargo.toml @@ -21,6 +21,8 @@ serde = "1.0.203" sqlx = { version = "0.7.4", features = ["runtime-tokio", "sqlite"] } tokio = { version = "1.38.0", features = ["full"] } tower = "0.4.13" +log = "0.4.22" +env_logger = "0.11.3" [profile.release] lto = true diff --git a/taptrade-cli-demo/coordinator/src/communication/mod.rs b/taptrade-cli-demo/coordinator/src/communication/mod.rs index 003d715..dbd983e 100755 --- a/taptrade-cli-demo/coordinator/src/communication/mod.rs +++ b/taptrade-cli-demo/coordinator/src/communication/mod.rs @@ -38,7 +38,7 @@ async fn receive_order( database .insert_new_maker_request(&order, &bond_requirements) .await?; - println!("Coordinator received new offer: {:?}", order); + debug!("Coordinator received new offer: {:?}", order); Ok(Json(bond_requirements)) } @@ -48,7 +48,7 @@ async fn submit_maker_bond( Extension(wallet): Extension>, Json(payload): Json, ) -> Result { - println!("\n\nReceived maker bond: {:?}", payload); + debug!("\n\nReceived maker bond: {:?}", payload); let bond_requirements = if let Ok(requirements) = database .fetch_bond_requirements(&payload.robohash_hex) .await @@ -68,7 +68,7 @@ async fn submit_maker_bond( return Ok(StatusCode::NOT_ACCEPTABLE.into_response()); } } - println!("\nBond validation successful"); + trace!("\nBond validation successful"); let offer_id_hex: String = generate_random_order_id(16); // 16 bytes random offer id, maybe a different system makes more sense later on? (uuid or increasing counter...) // create address for taker bond let new_taker_bond_address = wallet.get_new_address().await.context(format!( @@ -236,7 +236,7 @@ pub async fn api_server(coordinator: Arc) -> Result<()> { let port: u16 = env::var("PORT") .unwrap_or_else(|_| "9999".to_string()) .parse()?; - println!("Listening on {}", port); + info!("Listening on {}", port); let addr = SocketAddr::from(([127, 0, 0, 1], port)); let tcp = TcpListener::bind(&addr).await.unwrap(); axum::serve(tcp, app).await?; diff --git a/taptrade-cli-demo/coordinator/src/coordinator/create_taproot.rs b/taptrade-cli-demo/coordinator/src/coordinator/create_taproot.rs index 14dbf78..36db9b2 100644 --- a/taptrade-cli-demo/coordinator/src/coordinator/create_taproot.rs +++ b/taptrade-cli-demo/coordinator/src/coordinator/create_taproot.rs @@ -97,7 +97,7 @@ async fn create_script( // Create the descriptor let descriptor = Descriptor::new_tr(dummy_internal_key, Some(tap_tree))?; - println!("{}", descriptor); + debug!("{}", descriptor); Ok(descriptor) } @@ -117,7 +117,7 @@ pub async fn create_psbt( )?; // Step 2: Print the first address - println!( + info!( "Deposit funds here: {:?}", wallet.get_address(AddressIndex::New)? ); @@ -129,7 +129,7 @@ pub async fn create_psbt( // Step 4: Print balance let blockchain = EsploraBlockchain::new("https://blockstream.info/testnet/api", 20); wallet.sync(&blockchain, SyncOptions::default())?; - println!("{:#?}", wallet.get_balance()?); + info!("{:#?}", wallet.get_balance()?); let maker_utxos = vec![/* UTXO details here */]; let taker_utxos = vec![/* UTXO details here */]; @@ -149,7 +149,7 @@ pub async fn create_psbt( .policy_path(BTreeMap::new(), KeychainKind::External); let (psbt, tx_details) = tx_builder.finish()?; - println!("PSBT: {:?}", psbt); + debug!("PSBT: {:?}", psbt); Ok(psbt) } @@ -165,7 +165,7 @@ fn taker_unresponsive( ) -> Result<(), Box> { // Maker signs the PSBT let maker_signed_psbt = wallet.sign(&mut psbt.clone(), SignOptions::default())?; - println!("Maker signed PSBT: {:?}", maker_signed_psbt); + debug!("Maker signed PSBT: {:?}", maker_signed_psbt); // If taker is unresponsive, coordinator signs using alternative path let taker_responsive = false; // Assume taker is unresponsive @@ -187,7 +187,7 @@ fn taker_unresponsive( let (coordinator_psbt, _details) = coordinator_tx_builder.finish()?; let coordinator_signed_psbt = wallet.sign(&mut coordinator_psbt, SignOptions::default())?; - println!("Coordinator signed PSBT: {:?}", coordinator_signed_psbt); + debug!("Coordinator signed PSBT: {:?}", coordinator_signed_psbt); } Ok(()) } diff --git a/taptrade-cli-demo/coordinator/src/database/mod.rs b/taptrade-cli-demo/coordinator/src/database/mod.rs index aa47617..e60d8f4 100644 --- a/taptrade-cli-demo/coordinator/src/database/mod.rs +++ b/taptrade-cli-demo/coordinator/src/database/mod.rs @@ -252,7 +252,7 @@ impl CoordinatorDB { ) .collect(); if available_offers.is_empty() { - println!("empty"); + debug!("No available offers in db..."); return Ok(None); } Ok(Some(available_offers)) diff --git a/taptrade-cli-demo/coordinator/src/main.rs b/taptrade-cli-demo/coordinator/src/main.rs index f063709..6436de9 100755 --- a/taptrade-cli-demo/coordinator/src/main.rs +++ b/taptrade-cli-demo/coordinator/src/main.rs @@ -9,6 +9,7 @@ use coordinator::monitoring::monitor_bonds; use coordinator::monitoring::*; use database::CoordinatorDB; use dotenv::dotenv; +use log::{debug, error, info, trace, warn}; use std::time::{SystemTime, UNIX_EPOCH}; use std::{env, sync::Arc}; use tokio::sync::Mutex; @@ -22,6 +23,9 @@ pub struct Coordinator { // populate .env with values before starting #[tokio::main] async fn main() -> Result<()> { + env_logger::builder() + .filter_level(log::LevelFilter::Info) + .init(); dotenv().ok(); // Initialize the database pool diff --git a/taptrade-cli-demo/trader/src/communication/mod.rs b/taptrade-cli-demo/trader/src/communication/mod.rs index 88728b9..8cab918 100644 --- a/taptrade-cli-demo/trader/src/communication/mod.rs +++ b/taptrade-cli-demo/trader/src/communication/mod.rs @@ -1,6 +1,7 @@ pub mod api; pub mod taker_requests; +use super::*; use crate::{ cli::{OfferType, TraderSettings}, trading::utils::ActiveOffer, @@ -40,13 +41,19 @@ impl BondRequirementResponse { } pub fn fetch(trader_setup: &TraderSettings) -> Result { + trace!("Fetching bond requirements from coordinator. (create-offer)"); let client = reqwest::blocking::Client::new(); let endpoint = format!("{}{}", trader_setup.coordinator_endpoint, "/create-offer"); - let res = client + let res = match client .post(endpoint) .json(&Self::_format_request(trader_setup)) - .send()?; + .send() + { + Ok(res) => res, + Err(e) => return Err(anyhow!("Error calling /create-offer: {}", e)), + }; let status_code = res.status(); + debug!("/create-offer Response status code: {}", status_code); match res.json::() { Ok(response) => Ok(response), Err(e) => Err(anyhow!( diff --git a/taptrade-cli-demo/trader/src/main.rs b/taptrade-cli-demo/trader/src/main.rs index b74a085..298d76b 100644 --- a/taptrade-cli-demo/trader/src/main.rs +++ b/taptrade-cli-demo/trader/src/main.rs @@ -6,7 +6,7 @@ mod wallet; use anyhow::{anyhow, Result}; use cli::CliSettings; -use core::panic; +use log::{debug, error, info, trace, warn}; fn start_trade_pipeline(cli_input: &CliSettings) -> Result<()> { match cli_input { @@ -20,10 +20,12 @@ fn start_trade_pipeline(cli_input: &CliSettings) -> Result<()> { } fn main() -> Result<()> { - // env_logger::builder().filter_level(log::LevelFilter::Debug).init(); // enable to show extended BDK debug messages + env_logger::builder() + .filter_level(log::LevelFilter::Info) + .init(); let mode = CliSettings::parse_cli_args()?; - dbg!("CLI input :", &mode); + info!("CLI input : {:#?}", &mode); start_trade_pipeline(&mode)?; Ok(()) diff --git a/taptrade-cli-demo/trader/src/trading/maker_utils.rs b/taptrade-cli-demo/trader/src/trading/maker_utils.rs index 41dba3e..69a1d3a 100644 --- a/taptrade-cli-demo/trader/src/trading/maker_utils.rs +++ b/taptrade-cli-demo/trader/src/trading/maker_utils.rs @@ -7,13 +7,7 @@ impl ActiveOffer { maker_config: &TraderSettings, ) -> Result { let offer_conditions = BondRequirementResponse::fetch(maker_config)?; - // let offer_conditions = OfferCreationResponse { - // // hardcoded for testing, locking_address is owned by .env xprv - // locking_amount_sat: 90000, - // bond_address: "tb1pfdvgfzwp8vhmelpv8w9kezz7nsmxw68jz6yehgze6mzx0t6r9t2qv9ynmm" - // .to_string(), - // }; - + debug!("Offer conditions fetched: {:#?}", &offer_conditions); let (bond, mut musig_data, payout_address) = trading_wallet.trade_onchain_assembly(&offer_conditions, maker_config)?; let submission_result = BondSubmissionRequest::send_maker( diff --git a/taptrade-cli-demo/trader/src/trading/mod.rs b/taptrade-cli-demo/trader/src/trading/mod.rs index 3e4d5e3..602d3b2 100644 --- a/taptrade-cli-demo/trader/src/trading/mod.rs +++ b/taptrade-cli-demo/trader/src/trading/mod.rs @@ -3,6 +3,7 @@ pub mod taker_utils; pub mod utils; use self::utils::ActiveOffer; +use super::*; use crate::{ cli::TraderSettings, communication::api::{ @@ -16,7 +17,6 @@ use crate::{ TradingWallet, }, }; -use anyhow::Result; use bdk::{ bitcoin::{amount::serde::as_btc::deserialize, psbt::PartiallySignedTransaction}, database::MemoryDatabase, @@ -28,7 +28,7 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> { let wallet = TradingWallet::load_wallet(maker_config)?; // initialize the wallet with xprv let offer = ActiveOffer::create(&wallet, maker_config)?; - dbg!(&offer); + info!("Maker offer created: {:#?}", &offer); let mut escrow_contract_psbt = offer.wait_until_taken(maker_config)?; wallet @@ -47,10 +47,10 @@ pub fn run_maker(maker_config: &TraderSettings) -> Result<()> { if offer.fiat_confirmation_cli_input(maker_config)? { // this represents the "confirm payment" / "confirm fiat recieved" button TradeObligationsSatisfied::submit(&offer.offer_id_hex, maker_config)?; - println!("Waiting for other party to confirm the trade."); + info!("Waiting for other party to confirm the trade."); let payout_keyspend_psbt = IsOfferReadyRequest::poll_payout(maker_config, &offer)?; } else { - println!("Trade failed."); + error!("Trade failed."); panic!("Escrow to be implemented!"); } Ok(())