mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-07-30 06:31:39 +00:00
finish fetch-available-offers endpoint
This commit is contained in:
@ -35,6 +35,7 @@ pub struct OrderActivatedResponse {
|
|||||||
pub bond_locked_until_timestamp: u64, // unix timestamp. Do not touch bond till then unless offer gets taken.
|
pub bond_locked_until_timestamp: u64, // unix timestamp. Do not touch bond till then unless offer gets taken.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
pub struct OffersRequest {
|
pub struct OffersRequest {
|
||||||
pub buy_offers: bool, // true if looking for buy offers, false if looking for sell offers
|
pub buy_offers: bool, // true if looking for buy offers, false if looking for sell offers
|
||||||
pub amount_min_sat: u64,
|
pub amount_min_sat: u64,
|
||||||
|
@ -70,26 +70,18 @@ async fn submit_maker_bond(
|
|||||||
|
|
||||||
async fn fetch_available_offers(
|
async fn fetch_available_offers(
|
||||||
Extension(database): Extension<CoordinatorDB>,
|
Extension(database): Extension<CoordinatorDB>,
|
||||||
Extension(_): Extension<CoordinatorWallet>,
|
|
||||||
Json(payload): Json<OffersRequest>,
|
Json(payload): Json<OffersRequest>,
|
||||||
) -> Result<Json<OrderActivatedResponse>, AppError> {
|
) -> Result<Json<PublicOffers>, AppError> {
|
||||||
// let suitable_offers: Option<Vec<PublicOffer>> =
|
let offers: Option<Vec<PublicOffer>> = database.fetch_suitable_offers(&payload).await?;
|
||||||
database.fetch_suitable_offers(&payload).await?;
|
|
||||||
|
|
||||||
Ok(Json(OrderActivatedResponse {
|
Ok(Json(PublicOffers { offers }))
|
||||||
offer_id_hex: "test".to_string(),
|
|
||||||
bond_locked_until_timestamp: 21312313,
|
|
||||||
}))
|
|
||||||
// Ok(Json(PublicOffers {
|
|
||||||
// offers: suitable_offers,
|
|
||||||
// }))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn api_server(database: CoordinatorDB, wallet: CoordinatorWallet) -> Result<()> {
|
pub async fn api_server(database: CoordinatorDB, wallet: CoordinatorWallet) -> Result<()> {
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/create-offer", post(receive_order))
|
.route("/create-offer", post(receive_order))
|
||||||
.route("/submit-maker-bond", post(submit_maker_bond))
|
.route("/submit-maker-bond", post(submit_maker_bond))
|
||||||
// .route("/fetch-available-offers", post(fetch_available_offers))
|
.route("/fetch-available-offers", post(fetch_available_offers))
|
||||||
.layer(Extension(database))
|
.layer(Extension(database))
|
||||||
.layer(Extension(wallet));
|
.layer(Extension(wallet));
|
||||||
// add other routes here
|
// add other routes here
|
||||||
|
@ -202,31 +202,34 @@ impl CoordinatorDB {
|
|||||||
Ok(Some(available_offers))
|
Ok(Some(available_offers))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use anyhow::Ok;
|
use anyhow::Ok;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
async fn create_coordinator()-> Result<database::CoordinatorDB, anyhow::Error> {
|
async fn create_coordinator() -> Result<database::CoordinatorDB, anyhow::Error> {
|
||||||
// Set up the in-memory database
|
// Set up the in-memory database
|
||||||
env::set_var("DATABASE_PATH", ":memory:");
|
env::set_var("DATABASE_PATH", ":memory:");
|
||||||
|
|
||||||
// Initialize the database
|
// Initialize the database
|
||||||
let database= CoordinatorDB::init().await?;
|
let database = CoordinatorDB::init().await?;
|
||||||
Ok(database)
|
Ok(database)
|
||||||
|
|
||||||
}
|
}
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_init() -> Result<()> {
|
async fn test_init() -> Result<()> {
|
||||||
let database = create_coordinator().await?;
|
let database = create_coordinator().await?;
|
||||||
// Verify the table creation
|
// Verify the table creation
|
||||||
let table_exists = sqlx::query("SELECT name FROM sqlite_master WHERE type='table' AND name='maker_requests'")
|
let table_exists = sqlx::query(
|
||||||
.fetch_optional(&*database.db_pool)
|
"SELECT name FROM sqlite_master WHERE type='table' AND name='maker_requests'",
|
||||||
.await?
|
)
|
||||||
.is_some();
|
.fetch_optional(&*database.db_pool)
|
||||||
|
.await?
|
||||||
|
.is_some();
|
||||||
assert!(table_exists, "The maker_requests table should exist.");
|
assert!(table_exists, "The maker_requests table should exist.");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_insert_new_maker_request() -> Result<()> {
|
async fn test_insert_new_maker_request() -> Result<()> {
|
||||||
let database = create_coordinator().await?;
|
let database = create_coordinator().await?;
|
||||||
@ -246,7 +249,9 @@ use super::*;
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Insert the new maker request
|
// Insert the new maker request
|
||||||
database.insert_new_maker_request(&order_request, &bond_requirement_response).await?;
|
database
|
||||||
|
.insert_new_maker_request(&order_request, &bond_requirement_response)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Verify the insertion
|
// Verify the insertion
|
||||||
let row = sqlx::query("SELECT * FROM maker_requests WHERE robohash = ?")
|
let row = sqlx::query("SELECT * FROM maker_requests WHERE robohash = ?")
|
||||||
@ -264,4 +269,3 @@ use super::*;
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user