finish submit_maker_bond endpoint coordinator

This commit is contained in:
f321x
2024-07-03 12:52:00 +00:00
parent b7a3522069
commit e7aaeab27a
3 changed files with 26 additions and 15 deletions

View File

@ -4,6 +4,7 @@ mod utils;
use self::api::*;
use self::utils::*;
use super::*;
use crate::wallet::*;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
@ -14,7 +15,6 @@ use rand::Rng;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use tokio::net::TcpListener;
// use crate::coordinator::verify_psbt;
//
// Axum handler functions
@ -46,16 +46,26 @@ async fn submit_maker_bond(
Extension(wallet): Extension<CoordinatorWallet>,
Json(payload): Json<BondSubmissionRequest>,
) -> Result<Response, AppError> {
let bond_requirements = database.fetch_maker_request(&payload.robohash_hex).await?;
let bond_requirements = if let Ok(requirements) = database
.fetch_bond_requirements(&payload.robohash_hex)
.await
{
requirements
} else {
return Ok(StatusCode::NOT_FOUND.into_response());
};
// validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate)
// if !wallet
// .validate_bond_tx_hex(&payload.signed_bond_hex)
// .await?
// {
// return Ok(StatusCode::NOT_ACCEPTABLE.into_response());
// }
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...)
match wallet
.validate_bond_tx_hex(&payload.signed_bond_hex, bond_requirements)
.await
{
Ok(()) => (),
Err(e) => {
dbg!(e);
return Ok(StatusCode::NOT_ACCEPTABLE.into_response());
}
}
let offer_id_hex: String = generate_random_order_id(16); // 16 bytes random offer id, maybe a different system makes more sense later on? (uuid or increasing counter...)
// create address for taker bond
let new_taker_bond_address = wallet.get_new_address().await?;
// insert bond into sql database and move offer to different table

View File

@ -146,17 +146,18 @@ impl CoordinatorDB {
Ok(())
}
pub async fn fetch_maker_request(&self, robohash: &String) -> Result<BondRequirementResponse> {
pub async fn fetch_bond_requirements(&self, robohash: &String) -> Result<BondRequirements> {
let maker_request = sqlx::query(
"SELECT bond_address, bond_amount_sat FROM maker_requests WHERE robohash = ?",
"SELECT bond_address, bond_amount_sat, amount_sat FROM maker_requests WHERE robohash = ?",
)
.bind(hex::decode(robohash)?)
.fetch_one(&*self.db_pool)
.await?;
Ok(BondRequirementResponse {
Ok(BondRequirements {
bond_address: maker_request.try_get("bond_address")?,
locking_amount_sat: maker_request.try_get::<i64, _>("bond_amount_sat")? as u64,
min_input_sum_sat: maker_request.try_get::<i64, _>("amount_sat")? as u64,
})
}

View File

@ -10,7 +10,7 @@ use dotenv::dotenv;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, sync::Arc};
use tokio::sync::Mutex;
use wallet::CoordinatorWallet;
use wallet::*;
// populate .env with values before starting
#[tokio::main]