mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-07-22 18:53:17 +00:00
finished OrderActivatedResponse endpoint
This commit is contained in:
@ -31,6 +31,6 @@ pub struct BondSubmissionRequest {
|
|||||||
// Response after step2 if offer creation was successful and the offer is now online in the orderbook
|
// Response after step2 if offer creation was successful and the offer is now online in the orderbook
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct OrderActivatedResponse {
|
pub struct OrderActivatedResponse {
|
||||||
pub order_id_hex: String,
|
pub offer_id_hex: String,
|
||||||
pub bond_locked_until_timestamp: u128, // 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.
|
||||||
}
|
}
|
||||||
|
@ -52,12 +52,6 @@ async fn receive_order(
|
|||||||
Ok(Json(bond_requirements))
|
Ok(Json(bond_requirements))
|
||||||
}
|
}
|
||||||
|
|
||||||
// BondSubmissionRequest {
|
|
||||||
// pub robohash_hex: String,
|
|
||||||
// pub signed_bond_hex: String, // signed bond transaction, hex encoded
|
|
||||||
// pub payout_address: String,
|
|
||||||
// pub musig_pub_nonce_hex: String,
|
|
||||||
// pub musig_pubkey_hex: String,
|
|
||||||
async fn submit_maker_bond(
|
async fn submit_maker_bond(
|
||||||
Extension(database): Extension<CoordinatorDB>,
|
Extension(database): Extension<CoordinatorDB>,
|
||||||
Extension(wallet): Extension<CoordinatorWallet>,
|
Extension(wallet): Extension<CoordinatorWallet>,
|
||||||
@ -72,21 +66,18 @@ async fn submit_maker_bond(
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// insert bond into sql database
|
// insert bond into sql database
|
||||||
database
|
let bond_locked_until_timestamp = database
|
||||||
.move_offer_to_active(&payload, &offer_id_hex)
|
.move_offer_to_active(&payload, &offer_id_hex)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// begin monitoring bond
|
// begin monitoring bond -> async loop monitoring bonds in sql table "active_maker_offers" -> see ../coordinator/monitoring.rs
|
||||||
|
// show trade to orderbook -> orderbook endpoint will scan sql table "active_maker_offers"
|
||||||
|
|
||||||
// move trade to orderbook
|
|
||||||
|
|
||||||
// For now, we'll just return a dummy success response
|
|
||||||
let response = OrderActivatedResponse {
|
|
||||||
bond_locked_until_timestamp: 0 as u128,
|
|
||||||
order_id_hex: "Bond submitted successfully".to_string(),
|
|
||||||
};
|
|
||||||
// Create the JSON response
|
// Create the JSON response
|
||||||
Ok(Json(response))
|
Ok(Json(OrderActivatedResponse {
|
||||||
|
bond_locked_until_timestamp,
|
||||||
|
offer_id_hex,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn api_server(database: CoordinatorDB, wallet: CoordinatorWallet) -> Result<()> {
|
pub async fn api_server(database: CoordinatorDB, wallet: CoordinatorWallet) -> Result<()> {
|
||||||
|
@ -8,6 +8,17 @@ pub struct CoordinatorDB {
|
|||||||
pub db_pool: Arc<Pool<Sqlite>>,
|
pub db_pool: Arc<Pool<Sqlite>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// db structure of offers awaiting bond submission in table maker_requests
|
||||||
|
pub struct AwaitingBondOffer {
|
||||||
|
pub robohash_hex: String,
|
||||||
|
pub is_buy_order: bool,
|
||||||
|
pub amount_satoshi: u64,
|
||||||
|
pub bond_ratio: u8,
|
||||||
|
pub offer_duration_ts: u64,
|
||||||
|
pub bond_address: String,
|
||||||
|
pub bond_amount_sat: u64,
|
||||||
|
}
|
||||||
|
|
||||||
// is our implementation resistant against sql injections?
|
// is our implementation resistant against sql injections?
|
||||||
impl CoordinatorDB {
|
impl CoordinatorDB {
|
||||||
// will either create a new db or load existing one. Will create according tables in new db
|
// will either create a new db or load existing one. Will create according tables in new db
|
||||||
@ -50,7 +61,10 @@ impl CoordinatorDB {
|
|||||||
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,
|
||||||
|
payout_address TEXT NOT NULL,
|
||||||
|
musig_pub_nonce_hex TEXT NOT NULL,
|
||||||
|
musig_pubkey_hex TEXT NOT NULL
|
||||||
)",
|
)",
|
||||||
)
|
)
|
||||||
.execute(&db_pool)
|
.execute(&db_pool)
|
||||||
@ -101,28 +115,63 @@ impl CoordinatorDB {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn fetch_and_delete_offer_from_bond_table(
|
||||||
|
&self,
|
||||||
|
robohash_hex: &str,
|
||||||
|
) -> Result<AwaitingBondOffer> {
|
||||||
|
let fetched_values = sqlx::query_as::<_, (String, bool, i64, u8, i64, String, i64)> (
|
||||||
|
"SELECT robohash, is_buy_order, amount_sat, bond_ratio, offer_duration_ts, bond_address, bond_amount_sat FROM maker_requests WHERE <unique_identifier_column> = ?",
|
||||||
|
)
|
||||||
|
.bind(hex::decode(robohash_hex)?)
|
||||||
|
.fetch_one(&*self.db_pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// Delete the database entry.
|
||||||
|
sqlx::query("DELETE FROM maker_requests WHERE <unique_identifier_column> = ?")
|
||||||
|
.bind(hex::decode(robohash_hex)?)
|
||||||
|
.execute(&*self.db_pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(AwaitingBondOffer {
|
||||||
|
robohash_hex: hex::encode(fetched_values.0),
|
||||||
|
is_buy_order: fetched_values.1,
|
||||||
|
amount_satoshi: fetched_values.2 as u64,
|
||||||
|
bond_ratio: fetched_values.3,
|
||||||
|
offer_duration_ts: fetched_values.4 as u64,
|
||||||
|
bond_address: fetched_values.5,
|
||||||
|
bond_amount_sat: fetched_values.6 as u64,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn move_offer_to_active(
|
pub async fn move_offer_to_active(
|
||||||
&self,
|
&self,
|
||||||
data: &BondSubmissionRequest,
|
data: &BondSubmissionRequest,
|
||||||
offer_id: &String,
|
offer_id: &String,
|
||||||
) -> Result<()> {
|
) -> Result<u64> {
|
||||||
// let bool_to_sql_int = |flag: bool| if flag { Some(1) } else { None };
|
let remaining_offer_information = self
|
||||||
|
.fetch_and_delete_offer_from_bond_table(&data.robohash_hex)
|
||||||
|
.await?;
|
||||||
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT OR REPLACE INTO active_maker_offers (offer_id, robohash, is_buy_order, amount_sat,
|
"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)
|
bond_ratio, offer_duration_ts, bond_address, bond_amount_sat, bond_tx_hex, payout_address, musig_pub_nonce_hex, musig_pubkey_hex)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||||
)
|
)
|
||||||
.bind(hex::decode(&order.robohash_hex)?)
|
.bind(offer_id)
|
||||||
// .bind(bool_to_sql_int(order.is_buy_order))
|
.bind(hex::decode(&data.robohash_hex)?)
|
||||||
// .bind(order.amount_satoshi as i64)
|
.bind(remaining_offer_information.is_buy_order)
|
||||||
// .bind(order.bond_ratio)
|
.bind(remaining_offer_information.amount_satoshi as i64)
|
||||||
// .bind(order.offer_duration_ts as i64)
|
.bind(remaining_offer_information.bond_ratio)
|
||||||
// .bind(bond_requirements.bond_address.clone())
|
.bind(remaining_offer_information.offer_duration_ts as i64)
|
||||||
// .bind(bond_requirements.locking_amount_sat as i64)
|
.bind(remaining_offer_information.bond_address.clone())
|
||||||
|
.bind(remaining_offer_information.bond_amount_sat as i64)
|
||||||
|
.bind(data.signed_bond_hex.clone())
|
||||||
|
.bind(data.payout_address.clone())
|
||||||
|
.bind(data.musig_pub_nonce_hex.clone())
|
||||||
|
.bind(data.musig_pubkey_hex.clone())
|
||||||
.execute(&*self.db_pool)
|
.execute(&*self.db_pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(remaining_offer_information.offer_duration_ts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user