Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed fn add_order from db.rs #170

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading