diff --git a/taptrade-cli-demo/coordinator/Cargo.lock b/taptrade-cli-demo/coordinator/Cargo.lock index a18a9c7..92d0281 100644 --- a/taptrade-cli-demo/coordinator/Cargo.lock +++ b/taptrade-cli-demo/coordinator/Cargo.lock @@ -317,6 +317,7 @@ dependencies = [ "bdk", "dotenv", "hex", + "rand", "reqwest", "serde", "sqlx", diff --git a/taptrade-cli-demo/coordinator/Cargo.toml b/taptrade-cli-demo/coordinator/Cargo.toml index ffea849..c5ffe8b 100644 --- a/taptrade-cli-demo/coordinator/Cargo.toml +++ b/taptrade-cli-demo/coordinator/Cargo.toml @@ -9,6 +9,7 @@ axum = { version = "0.7.5", features = ["tokio", "json"] } bdk = { version = "0.29.0", features = ["key-value-db", "bitcoinconsensus", "verify"] } dotenv = "0.15.0" hex = "0.4.3" +rand = "0.8.5" reqwest = { version = "0.12.4", features = ["blocking", "json"] } serde = "1.0.203" sqlx = { version = "0.7.4", features = ["runtime-tokio", "sqlite"] } diff --git a/taptrade-cli-demo/coordinator/src/communication/mod.rs b/taptrade-cli-demo/coordinator/src/communication/mod.rs index 54411c2..87caca5 100755 --- a/taptrade-cli-demo/coordinator/src/communication/mod.rs +++ b/taptrade-cli-demo/coordinator/src/communication/mod.rs @@ -10,11 +10,27 @@ use axum::{ routing::post, Extension, Json, Router, }; +use rand::{distributions::Alphanumeric, Rng}; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; use tokio::net::TcpListener; // use crate::coordinator::verify_psbt; +fn generate_random_order_id(len: usize) -> String { + // Generate `len` random bytes + let bytes: Vec = rand::thread_rng() + .sample_iter(&rand::distributions::Standard) + .take(len) + .collect(); + + // Convert bytes to hex string + let hex_string = hex::encode(bytes); + hex_string +} + +// +// Axum handler functions +// // Handler function to process the received data async fn receive_order( Extension(database): Extension, @@ -48,6 +64,7 @@ async fn submit_maker_bond( Json(payload): Json, ) -> Result, AppError> { let bond_requirements = database.fetch_maker_request(&payload.robohash_hex).await?; + let offer_id_hex = generate_random_order_id(16); // 16 bytes random offer id, maybe a different system makes more sense later on? (uuid or increasing counter...) // validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate) wallet @@ -56,7 +73,7 @@ async fn submit_maker_bond( // insert bond into sql database database - .move_offer_to_active(&payload, &bond_requirements) + .move_offer_to_active(&payload, &offer_id_hex) .await?; // begin monitoring bond diff --git a/taptrade-cli-demo/coordinator/src/database/mod.rs b/taptrade-cli-demo/coordinator/src/database/mod.rs index 45b50bc..5538ec1 100644 --- a/taptrade-cli-demo/coordinator/src/database/mod.rs +++ b/taptrade-cli-demo/coordinator/src/database/mod.rs @@ -26,7 +26,7 @@ impl CoordinatorDB { // Create the trades table if it doesn't exist sqlx::query( - // robohash is binary hash + // robohash is hash as bytes "CREATE TABLE IF NOT EXISTS maker_requests ( robohash BLOB PRIMARY KEY, is_buy_order INTEGER, @@ -40,16 +40,16 @@ impl CoordinatorDB { .execute(&db_pool) .await?; sqlx::query( - // robohash is binary hash + // robohash is hash as bytes "CREATE TABLE IF NOT EXISTS active_maker_offers ( - trade_id BLOB PRIMARY KEY, + offer_id TEXT PRIMARY KEY, robohash BLOB, is_buy_order INTEGER, amount_sat INTEGER NOT NULL, bond_ratio INTEGER NOT NULL, offer_duration_ts INTEGER NOT NULL, bond_address TEXT NOT NULL, - bond_amount_sat INTEGER NOT NULL + bond_amount_sat INTEGER NOT NULL, bond_tx_hex TEXT NOT NULL )", ) @@ -101,5 +101,28 @@ impl CoordinatorDB { }) } - pub async fn move_offer_to_active(&self) -> Result<()> {} + pub async fn move_offer_to_active( + &self, + data: &BondSubmissionRequest, + offer_id: &String, + ) -> Result<()> { + // let bool_to_sql_int = |flag: bool| if flag { Some(1) } else { None }; + + sqlx::query( + "INSERT OR REPLACE INTO active_maker_offers (offer_id, robohash, is_buy_order, amount_sat, + bond_ratio, offer_duration_ts, bond_address, bond_amount_sat, bond_tx_hex) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", + ) + .bind(hex::decode(&order.robohash_hex)?) + // .bind(bool_to_sql_int(order.is_buy_order)) + // .bind(order.amount_satoshi as i64) + // .bind(order.bond_ratio) + // .bind(order.offer_duration_ts as i64) + // .bind(bond_requirements.bond_address.clone()) + // .bind(bond_requirements.locking_amount_sat as i64) + .execute(&*self.db_pool) + .await?; + + Ok(()) + } }