experimenting with returning response for bond details

This commit is contained in:
aaravm
2024-06-19 02:04:56 +05:30
parent eb438388db
commit 6d3cf0c7c7
4 changed files with 81 additions and 27 deletions

View File

@ -0,0 +1,38 @@
use super::*;
// Receiving this struct as input to the server
#[derive(Deserialize, Serialize, Debug)]
pub struct OrderRequest {
pub robohash_hex: String, // identifier of the trader
pub amount_satoshi: u64, // amount in satoshi to buy or sell
pub is_buy_order: bool, // true if buy, false if sell
pub bond_ratio: u8, // [2, 50]% of trading amount
pub offer_duration_ts: u64, // unix timestamp how long the offer should stay available
}
// Define a struct representing your response data
#[derive(Serialize)]
pub struct BondRequirementResponse {
pub bond_address: String,
pub locking_amount_sat: u64,// min amount of the bond output in sat
}
// maker step 2
// (submission of signed bond and other data neccessary to coordinate the trade)
#[derive(Serialize, Debug)]
pub struct BondSubmissionRequest {
pub robohash_hex: String,
pub signed_bond_hex: String, // signed bond transaction, hex encoded
pub payout_address: String, // does this make sense here?
pub musig_pub_nonce_hex: String,
pub musig_pubkey_hex: String,
}
// Response after step2 if offer creation was successful and the offer is now online in the orderbook
#[derive(Debug, Deserialize)]
pub struct OrderActivatedResponse {
pub order_id_hex: String,
pub bond_locked_until_timestamp: u128, // unix timestamp. Do not touch bond till then unless offer gets taken.
}

View File

@ -1,47 +1,62 @@
use axum::{routing::post, Json, Router};
pub mod api;
use reqwest::StatusCode;
use axum::{routing::post, Json, Router, response::{IntoResponse, Response}, };
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use tokio::net::TcpListener;
use api::{
BondRequirementResponse, BondSubmissionRequest, OrderActivatedResponse, OrderRequest,
};
#[derive(Deserialize, Serialize, Debug)]
struct OrderRequest {
robohash_hex: String,
amount_satoshi: u64,
order_type: String,
bond_ratio: u8,
}
// Handler function to process the received data
async fn receive_order(Json(order): Json<OrderRequest>) {
async fn receive_order(Json(order): Json<OrderRequest>)-> Json<BondRequirementResponse> {
// Print the received data to the console
println!("Received order: {:?}", order);
// Access individual fields
let robohash = &order.robohash_hex;
// let robohash = &order.robohash_hex;
let amount = order.amount_satoshi;
let order_type = &order.order_type;
// let order_type = &order.is_buy_order;
let bond_ratio = order.bond_ratio;
// let offer_duration= order.offer_duration_ts;
// Process the data as needed
// For example, you can log the data, save it to a database, etc.
println!("Robohash: {}", robohash);
println!("Amount (satoshi): {}", amount);
println!("Order type: {}", order_type);
println!("Bond ratio: {}", bond_ratio);
// Create a response struct
let response = BondRequirementResponse {
bond_address: "Order received successfully".to_string(),
// Add any other fields you want to include in your response
locking_amount_sat: (amount * bond_ratio as u64 / 100),
};
// Example of further processing
if order_type == "buy" {
println!("Processing a buy order...");
// Add your buy order logic here
} else if order_type == "sell" {
println!("Processing a sell order...");
// Add your sell order logic here
}
// Return the response as JSON
Json(response)
}
async fn submit_maker_bond(
Json(payload): Json<BondSubmissionRequest>,
) -> Response {
// Process the payload
// 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
let json = Json(response);
// Create the full response with status code
(StatusCode::OK, json).into_response()
}
#[tokio::main]
pub async fn webserver() {
// Build our application with a single route
let app = Router::new().route("/receive-order", post(receive_order));
let app = Router::new()
.route("/create-offer", post(receive_order))
.route("/submit-maker-bond", post(submit_maker_bond));
// Run the server on localhost:3000
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

View File

@ -22,7 +22,7 @@ pub struct BondRequirementResponse {
// maker step 2
// (submission of signed bond and other data neccessary to coordinate the trade)
#[derive(Serialize, Debug)]
pub struct BondSubmissionRequest {
pub struct BondSubmissionRequest {
pub robohash_hex: String,
pub signed_bond_hex: String, // signed bond transaction, hex encoded
pub payout_address: String, // does this make sense here?

View File

@ -126,3 +126,4 @@ impl OfferTakenResponse {
}
}
}