Skip to content

Commit

Permalink
removed fn add_order from db.rs (#170)
Browse files Browse the repository at this point in the history
* removed fn add_order from db.rs

* some cosmetics on publish_order function

* removed redundant check on new order status
  • Loading branch information
arkanoider authored Dec 20, 2023
1 parent d8eec5d commit 00b7abb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 83 deletions.
79 changes: 1 addition & 78 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mostro_core::order::{Kind, Order, SmallOrder, Status};
use mostro_core::order::{Order, Status};
use nostr_sdk::prelude::*;
use sqlx::migrate::MigrateDatabase;
use sqlx::pool::Pool;
Expand All @@ -20,83 +20,6 @@ pub async fn connect() -> Result<Pool<Sqlite>, sqlx::Error> {
Ok(pool)
}

pub async fn add_order(
pool: &SqlitePool,
order: &SmallOrder,
event_id: &str,
initiator_pubkey: &str,
master_pubkey: &str,
) -> anyhow::Result<Order> {
let mut conn = pool.acquire().await?;
let uuid = Uuid::new_v4();
let mut buyer_pubkey: Option<String> = None;
let mut master_buyer_pubkey: Option<String> = None;
let mut seller_pubkey: Option<String> = None;
let mut master_seller_pubkey: Option<String> = None;
let created_at = Timestamp::now();
let mut kind = "Sell".to_string();
if order.kind == Some(Kind::Buy) {
kind = "Buy".to_string();
buyer_pubkey = Some(initiator_pubkey.to_string());
master_buyer_pubkey = Some(master_pubkey.to_string());
} else {
seller_pubkey = Some(initiator_pubkey.to_string());
master_seller_pubkey = Some(master_pubkey.to_string());
}
let status = if let Some(status) = order.status {
status.to_string()
} else {
"Pending".to_string()
};
let price_from_api = order.amount == 0;

let order = sqlx::query_as::<_, Order>(
r#"
INSERT INTO orders (
id,
kind,
event_id,
creator_pubkey,
buyer_pubkey,
master_buyer_pubkey,
seller_pubkey,
master_seller_pubkey,
status,
premium,
payment_method,
amount,
price_from_api,
fiat_code,
fiat_amount,
buyer_invoice,
created_at
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17)
RETURNING *
"#,
)
.bind(uuid)
.bind(kind)
.bind(event_id)
.bind(initiator_pubkey)
.bind(buyer_pubkey)
.bind(master_buyer_pubkey)
.bind(seller_pubkey)
.bind(master_seller_pubkey)
.bind(status)
.bind(order.premium)
.bind(&order.payment_method)
.bind(order.amount)
.bind(price_from_api)
.bind(&order.fiat_code)
.bind(order.fiat_amount)
.bind(order.buyer_invoice.as_ref())
.bind(created_at.as_i64())
.fetch_one(&mut conn)
.await?;

Ok(order)
}

pub async fn edit_buyer_pubkey_order(
pool: &SqlitePool,
order_id: Uuid,
Expand Down
31 changes: 26 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,32 @@ pub async fn publish_order(
master_pubkey: &str,
ack_pubkey: XOnlyPublicKey,
) -> Result<()> {
let order = crate::db::add_order(pool, new_order, "", initiator_pubkey, master_pubkey).await?;
let order_id = order.id;
// Prepare a new default order
let mut new_order_db = Order {
id: Uuid::new_v4(),
kind: "Sell".to_string(),
status: "Pending".to_string(),
created_at: Timestamp::now().as_i64(),
..Default::default()
};

if new_order.kind == Some(OrderKind::Buy) {
new_order_db.kind = "Buy".to_string();
new_order_db.buyer_pubkey = Some(initiator_pubkey.to_string());
new_order_db.master_buyer_pubkey = Some(master_pubkey.to_string());
} else {
new_order_db.seller_pubkey = Some(initiator_pubkey.to_string());
new_order_db.master_seller_pubkey = Some(master_pubkey.to_string());
}

// Request price from API in case amount is 0
new_order_db.price_from_api = new_order.amount == 0;
// CRUD order creation
new_order_db.clone().create(pool).await?;
let order_id = new_order_db.id;
info!("New order saved Id: {}", order_id);
// We transform the order fields to tags to use in the event
let tags = order_to_tags(&order);
let tags = order_to_tags(&new_order_db);

info!("order tags to be published: {:#?}", tags);
// nip33 kind with order fields as tags and order id as identifier
Expand All @@ -112,10 +133,10 @@ pub async fn publish_order(
order_id,
&Status::Pending,
&event_id,
order.amount,
new_order_db.amount,
)
.await?;
let mut order = order.as_new_order();
let mut order = new_order_db.as_new_order();
order.id = Some(order_id);

// Send message as ack with small order
Expand Down

0 comments on commit 00b7abb

Please sign in to comment.