add coordinator endpoint for maker psbt polling

This commit is contained in:
f321x
2024-07-01 16:28:18 +00:00
parent 12304df468
commit 171aa62fa0
3 changed files with 50 additions and 2 deletions

View File

@ -67,3 +67,9 @@ pub struct OfferPsbtRequest {
pub offer: PublicOffer,
pub trade_data: BondSubmissionRequest,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct OfferTakenRequest {
pub robohash_hex: String,
pub order_id_hex: String,
}

View File

@ -120,14 +120,29 @@ async fn submit_taker_bond(
.into_response())
}
async fn request_offer_status_maker(
Extension(database): Extension<CoordinatorDB>,
Json(payload): Json<OfferTakenRequest>,
) -> Result<Response, AppError> {
let offer = database
.fetch_taken_offer_maker(&payload.order_id_hex, &payload.robohash_hex)
.await?;
match offer {
Some(offer) => Ok(Json(OfferTakenResponse {
trade_psbt_hex_to_sign: offer,
})
.into_response()),
None => Ok(StatusCode::NO_CONTENT.into_response()),
}
}
pub async fn api_server(database: CoordinatorDB, wallet: CoordinatorWallet) -> Result<()> {
let app = Router::new()
.route("/create-offer", post(receive_order))
.route("/submit-maker-bond", post(submit_maker_bond))
.route("/fetch-available-offers", post(fetch_available_offers))
.route("/submit-taker-bond", post(submit_taker_bond))
// submit-taker-bond
// request-offer-status
.route("/request-offer-status", post(request_offer_status_maker))
.layer(Extension(database))
.layer(Extension(wallet));
// add other routes here

View File

@ -346,6 +346,33 @@ impl CoordinatorDB {
Ok(())
}
pub async fn fetch_taken_offer_maker(
&self,
offer_id_hex: &String,
robohash_hex_maker: &String,
) -> Result<Option<String>> {
let offer = sqlx::query(
"SELECT escrow_psbt_hex_maker, robohash_maker FROM taken_offers WHERE offer_id = ?",
)
.bind(offer_id_hex)
.fetch_optional(&*self.db_pool)
.await?;
let offer = match offer {
Some(offer) => offer,
None => return Ok(None),
};
match offer.try_get::<Vec<u8>, _>("robohash_maker") {
Ok(robohash) => {
if hex::encode(robohash) == *robohash_hex_maker {
Ok(offer.try_get("escrow_psbt_hex_maker")?)
} else {
Ok(None)
}
}
Err(_) => Ok(None),
}
}
}
#[cfg(test)]