mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-07-23 11:13:17 +00:00
continuing with sql offer table
This commit is contained in:
1
taptrade-cli-demo/coordinator/Cargo.lock
generated
1
taptrade-cli-demo/coordinator/Cargo.lock
generated
@ -317,6 +317,7 @@ dependencies = [
|
|||||||
"bdk",
|
"bdk",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"hex",
|
"hex",
|
||||||
|
"rand",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
|
@ -9,6 +9,7 @@ axum = { version = "0.7.5", features = ["tokio", "json"] }
|
|||||||
bdk = { version = "0.29.0", features = ["key-value-db", "bitcoinconsensus", "verify"] }
|
bdk = { version = "0.29.0", features = ["key-value-db", "bitcoinconsensus", "verify"] }
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
|
rand = "0.8.5"
|
||||||
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
|
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
|
||||||
serde = "1.0.203"
|
serde = "1.0.203"
|
||||||
sqlx = { version = "0.7.4", features = ["runtime-tokio", "sqlite"] }
|
sqlx = { version = "0.7.4", features = ["runtime-tokio", "sqlite"] }
|
||||||
|
@ -10,11 +10,27 @@ use axum::{
|
|||||||
routing::post,
|
routing::post,
|
||||||
Extension, Json, Router,
|
Extension, Json, Router,
|
||||||
};
|
};
|
||||||
|
use rand::{distributions::Alphanumeric, Rng};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
// use crate::coordinator::verify_psbt;
|
// use crate::coordinator::verify_psbt;
|
||||||
|
|
||||||
|
fn generate_random_order_id(len: usize) -> String {
|
||||||
|
// Generate `len` random bytes
|
||||||
|
let bytes: Vec<u8> = 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
|
// Handler function to process the received data
|
||||||
async fn receive_order(
|
async fn receive_order(
|
||||||
Extension(database): Extension<CoordinatorDB>,
|
Extension(database): Extension<CoordinatorDB>,
|
||||||
@ -48,6 +64,7 @@ async fn submit_maker_bond(
|
|||||||
Json(payload): Json<BondSubmissionRequest>,
|
Json(payload): Json<BondSubmissionRequest>,
|
||||||
) -> Result<Json<OrderActivatedResponse>, AppError> {
|
) -> Result<Json<OrderActivatedResponse>, AppError> {
|
||||||
let bond_requirements = database.fetch_maker_request(&payload.robohash_hex).await?;
|
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)
|
// validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate)
|
||||||
wallet
|
wallet
|
||||||
@ -56,7 +73,7 @@ async fn submit_maker_bond(
|
|||||||
|
|
||||||
// insert bond into sql database
|
// insert bond into sql database
|
||||||
database
|
database
|
||||||
.move_offer_to_active(&payload, &bond_requirements)
|
.move_offer_to_active(&payload, &offer_id_hex)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// begin monitoring bond
|
// begin monitoring bond
|
||||||
|
@ -26,7 +26,7 @@ impl CoordinatorDB {
|
|||||||
|
|
||||||
// Create the trades table if it doesn't exist
|
// Create the trades table if it doesn't exist
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
// robohash is binary hash
|
// robohash is hash as bytes
|
||||||
"CREATE TABLE IF NOT EXISTS maker_requests (
|
"CREATE TABLE IF NOT EXISTS maker_requests (
|
||||||
robohash BLOB PRIMARY KEY,
|
robohash BLOB PRIMARY KEY,
|
||||||
is_buy_order INTEGER,
|
is_buy_order INTEGER,
|
||||||
@ -40,16 +40,16 @@ impl CoordinatorDB {
|
|||||||
.execute(&db_pool)
|
.execute(&db_pool)
|
||||||
.await?;
|
.await?;
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
// robohash is binary hash
|
// robohash is hash as bytes
|
||||||
"CREATE TABLE IF NOT EXISTS active_maker_offers (
|
"CREATE TABLE IF NOT EXISTS active_maker_offers (
|
||||||
trade_id BLOB PRIMARY KEY,
|
offer_id TEXT PRIMARY KEY,
|
||||||
robohash BLOB,
|
robohash BLOB,
|
||||||
is_buy_order INTEGER,
|
is_buy_order INTEGER,
|
||||||
amount_sat INTEGER NOT NULL,
|
amount_sat INTEGER NOT NULL,
|
||||||
bond_ratio INTEGER NOT NULL,
|
bond_ratio INTEGER NOT NULL,
|
||||||
offer_duration_ts INTEGER NOT NULL,
|
offer_duration_ts INTEGER NOT NULL,
|
||||||
bond_address TEXT 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
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user