fetch_available_offers endpoint

This commit is contained in:
f321x
2024-06-28 13:14:56 +02:00
parent 465b67bb1a
commit 71eeb499d2
5 changed files with 52 additions and 6 deletions

View File

@ -10,7 +10,8 @@ in
pkg-config
zlib
openssl
cargo
cargo
clippy
rustc
rustfmt
gcc

View File

@ -40,3 +40,15 @@ pub struct OffersRequest {
pub amount_min_sat: u64,
pub amount_max_sat: u64,
}
// Offer information of each offer returned by the previous response
#[derive(Deserialize, Serialize, Debug)]
pub struct PublicOffer {
pub amount_sat: u64,
pub offer_id_hex: String,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct PublicOffers {
pub offers: Option<Vec<PublicOffer>>, // don't include offers var in return json if no offers are available
}

View File

@ -70,12 +70,19 @@ async fn submit_maker_bond(
async fn fetch_available_offers(
Extension(database): Extension<CoordinatorDB>,
Extension(wallet): Extension<CoordinatorWallet>,
Extension(_): Extension<CoordinatorWallet>,
Json(payload): Json<OffersRequest>,
) -> Result<Json<Vec<PublicOffer>>, AppError> {
) -> Result<Json<OrderActivatedResponse>, AppError> {
// let suitable_offers: Option<Vec<PublicOffer>> =
database.fetch_suitable_offers(&payload).await?;
// let offers = database.fetch_available_offers(&payload).await?;
// Ok(Json(offers))
Ok(Json(OrderActivatedResponse {
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<()> {

View File

@ -174,4 +174,30 @@ impl CoordinatorDB {
Ok(remaining_offer_information.offer_duration_ts)
}
pub async fn fetch_suitable_offers(
&self,
requested_offer: &OffersRequest,
) -> Result<Option<Vec<PublicOffer>>> {
let fetched_offers = sqlx::query_as::<_, (String, i64)> (
"SELECT offer_id, amount_sat FROM maker_requests WHERE is_buy_order = ? AND amount_sat BETWEEN ? AND ?",
)
.bind(requested_offer.buy_offers)
.bind(requested_offer.amount_min_sat as i64)
.bind(requested_offer.amount_max_sat as i64)
.fetch_all(&*self.db_pool)
.await?;
let available_offers: Vec<PublicOffer> = fetched_offers
.into_iter()
.map(|(offer_id_hex, amount_sat)| PublicOffer {
offer_id_hex,
amount_sat: amount_sat as u64,
})
.collect();
if available_offers.is_empty() {
return Ok(None);
}
Ok(Some(available_offers))
}
}

View File

@ -1,5 +1,5 @@
mod communication;
mod coordinator;
// mod coordinator;
mod database;
mod wallet;