Skip to content

Commit 6956639

Browse files
author
fbock
committed
add atomic bool to disable logging output for escrow cli input
1 parent dce1a05 commit 6956639

File tree

6 files changed

+112
-37
lines changed

6 files changed

+112
-37
lines changed

taptrade-cli-demo/coordinator/Cargo.lock

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

taptrade-cli-demo/coordinator/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ sha2 = "0.10"
2828
validator = { version = "0.18", features = ["derive"] }
2929
musig2 = "0.0.11"
3030
bincode = "1.3.3"
31+
chrono = "0.4.38"
3132

3233
[profile.release]
3334
lto = true

taptrade-cli-demo/coordinator/src/communication/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ async fn submit_obligation_confirmation(
191191
// or
192192

193193
// gets called if one of the traders wants to initiate escrow (e.g. claiming they didn't receive the fiat)
194-
// before timeout ends
194+
// before timeout ends, just sets the maker unhappy and escrow onging flag in the db
195195
async fn request_escrow(
196196
Extension(coordinator): Extension<Arc<Coordinator>>,
197197
Json(payload): Json<TradeObligationsUnsatisfied>,

taptrade-cli-demo/coordinator/src/database/mod.rs

+4-25
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl CoordinatorDB {
150150
)
151151
.execute(&db_pool)
152152
.await?;
153-
dbg!("Database initialized");
153+
debug!("Database initialized");
154154
let shared_db_pool = Arc::new(db_pool);
155155
Ok(Self {
156156
db_pool: shared_db_pool,
@@ -745,6 +745,8 @@ impl CoordinatorDB {
745745
Ok(())
746746
}
747747

748+
// checked by the payout handler on request to determine if the trade is ready for payout and
749+
// if escrow is required
748750
pub async fn fetch_trader_happiness(&self, offer_id: &str) -> Result<TraderHappiness> {
749751
let row = sqlx::query(
750752
"SELECT maker_happy, taker_happy, escrow_ongoing FROM taken_offers WHERE offer_id = ?",
@@ -764,6 +766,7 @@ impl CoordinatorDB {
764766
})
765767
}
766768

769+
// this will be checked by the payout handler on request, the escrow winner will be set trough CLI input
767770
pub async fn fetch_escrow_result(&self, offer_id: &str) -> Result<Option<String>> {
768771
let row = sqlx::query("SELECT escrow_winner_robohash FROM taken_offers WHERE offer_id = ?")
769772
.bind(offer_id)
@@ -776,30 +779,6 @@ impl CoordinatorDB {
776779
Ok(winner_robohash)
777780
}
778781

779-
// pub async fn fetch_escrow_tx_payout_data(
780-
// &self,
781-
// offer_id: &str,
782-
// ) -> Result<EscrowPsbtConstructionData> {
783-
// let row = sqlx::query("SELECT taproot_xonly_pubkey_hex_maker, taproot_xonly_pubkey_hex_taker, musig_pubkey_compressed_hex_maker, musig_pubkey_compressed_hex_taker FROM taken_offers WHERE offer_id = ?")
784-
// .bind(offer_id)
785-
// .fetch_one(&*self.db_pool)
786-
// .await?;
787-
788-
// let taproot_xonly_pubkey_hex_maker: String = row.get("taproot_xonly_pubkey_hex_maker");
789-
// let taproot_xonly_pubkey_hex_taker: String = row.get("taproot_xonly_pubkey_hex_taker");
790-
// let musig_pubkey_compressed_hex_maker: String =
791-
// row.get("musig_pubkey_compressed_hex_maker");
792-
// let musig_pubkey_compressed_hex_taker: String =
793-
// row.get("musig_pubkey_compressed_hex_taker");
794-
795-
// Ok(EscrowPsbtConstructionData {
796-
// taproot_xonly_pubkey_hex_maker,
797-
// taproot_xonly_pubkey_hex_taker,
798-
// musig_pubkey_compressed_hex_maker,
799-
// musig_pubkey_compressed_hex_taker,
800-
// })
801-
// }
802-
803782
pub async fn get_escrow_tx_amounts(
804783
&self,
805784
trade_id: &str,

taptrade-cli-demo/coordinator/src/main.rs

+43-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@ mod coordinator;
33
mod database;
44
mod wallet;
55

6-
use std::{
7-
collections::{HashMap, HashSet},
8-
env, fmt,
9-
net::SocketAddr,
10-
ops::Deref,
11-
str::FromStr,
12-
sync::{Arc, RwLock},
13-
time::{SystemTime, UNIX_EPOCH},
14-
};
15-
166
use anyhow::{anyhow, Context, Result};
177
use axum::{
188
http::StatusCode,
@@ -42,6 +32,7 @@ use bdk::{
4232
wallet::verify::*,
4333
KeychainKind, SignOptions, SyncOptions, Wallet,
4434
};
35+
use chrono::Local;
4536
use communication::{api::*, api_server, communication_utils::*, handler_errors::*};
4637
use coordinator::{
4738
bond_monitoring::*, coordinator_utils::*, mempool_monitoring::MempoolHandler,
@@ -58,6 +49,19 @@ use musig2::{
5849
use rand::Rng;
5950
use serde::{Deserialize, Serialize};
6051
use sqlx::{sqlite::SqlitePoolOptions, Pool, Row, Sqlite};
52+
use std::{
53+
collections::{HashMap, HashSet},
54+
env, fmt,
55+
io::Write,
56+
net::SocketAddr,
57+
ops::Deref,
58+
str::FromStr,
59+
sync::{
60+
atomic::{AtomicBool, Ordering},
61+
Arc, RwLock,
62+
},
63+
time::{SystemTime, UNIX_EPOCH},
64+
};
6165
use tokio::{
6266
net::TcpListener,
6367
sync::{oneshot, Mutex},
@@ -70,12 +74,41 @@ pub struct Coordinator {
7074
pub coordinator_wallet: Arc<CoordinatorWallet<MemoryDatabase>>,
7175
}
7276

77+
static LOGGING_ENABLED: AtomicBool = AtomicBool::new(true);
78+
79+
fn get_logging_color_code(level: log::Level) -> &'static str {
80+
match level {
81+
log::Level::Error => "\x1B[31m", // Red
82+
log::Level::Warn => "\x1B[33m", // Yellow
83+
log::Level::Info => "\x1B[32m", // Green
84+
log::Level::Debug => "\x1B[34m", // Blue
85+
log::Level::Trace => "\x1B[36m", // Cyan
86+
}
87+
}
88+
7389
// populate .env with values before starting
7490
#[tokio::main]
7591
async fn main() -> Result<()> {
7692
env_logger::builder()
7793
.filter_module("coordinator", log::LevelFilter::Trace)
7894
.filter_level(log::LevelFilter::Info)
95+
.format(|buf, record| {
96+
if LOGGING_ENABLED.load(Ordering::Relaxed) {
97+
let level = record.level();
98+
let color_code = get_logging_color_code(level);
99+
writeln!(
100+
buf,
101+
"{} [{}{}{}] - {}",
102+
Local::now().format("%Y-%m-%d %H:%M:%S"),
103+
color_code,
104+
level,
105+
"\x1B[0m", // Reset color
106+
record.args()
107+
)
108+
} else {
109+
Ok(())
110+
}
111+
})
79112
.init();
80113
dotenv().ok();
81114
debug!("Starting coordinator");

taptrade-cli-demo/coordinator/src/wallet/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub async fn init_coordinator_wallet() -> Result<CoordinatorWallet<MemoryDatabas
7676
wallet
7777
.sync(&backend, SyncOptions::default())
7878
.context("Connection to blockchain server failed.")?; // we could also use Esplora to make this async
79-
dbg!(wallet.get_balance()?);
79+
info!("{}", wallet.get_balance()?);
8080
Ok(CoordinatorWallet {
8181
wallet: Arc::new(Mutex::new(wallet)),
8282
backend: Arc::new(backend),

0 commit comments

Comments
 (0)