Skip to content

Commit

Permalink
First draft with only smallorder
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Nov 30, 2023
1 parent cc64e90 commit 843f318
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
10 changes: 7 additions & 3 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,25 @@ pub async fn add_invoice_action(
if order.preimage.is_some() {
// We send this data related to the order to the parties
let order_data = SmallOrder::new(
order.id,
Some(order.id),
None,
None,
order.amount,
order.fiat_code.clone(),
order.fiat_amount,
pr.clone(),
order.premium,
order.buyer_pubkey.as_ref().cloned(),
order.seller_pubkey.as_ref().cloned(),
None,
None,
);
// We send a confirmation message to seller
let message = Message::new_order(
Some(order.id),
None,
Action::BuyerTookOrder,
Some(Content::SmallOrder(order_data.clone())),
Some(Content::Order(order_data.clone())),
);
let message = message.as_json().unwrap();

Expand All @@ -137,7 +141,7 @@ pub async fn add_invoice_action(
Some(order.id),
None,
Action::HoldInvoicePaymentAccepted,
Some(Content::SmallOrder(order_data)),
Some(Content::Order(order_data)),
);
let message = message.as_json().unwrap();
send_dm(client, my_keys, &buyer_pubkey, message)
Expand Down
10 changes: 5 additions & 5 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use mostro_core::dispute::{Dispute, Status as DisputeStatus};
use mostro_core::order::{Kind, NewOrder, Order, Status};
use mostro_core::order::{Kind, Order, SmallOrder, Status};
use mostro_core::user::User;
use nostr_sdk::prelude::*;
use sqlx::migrate::MigrateDatabase;
Expand Down Expand Up @@ -80,7 +80,7 @@ pub async fn add_dispute(dispute: &Dispute, pool: &SqlitePool) -> anyhow::Result

pub async fn add_order(
pool: &SqlitePool,
order: &NewOrder,
order: &SmallOrder,
event_id: &str,
initiator_pubkey: &str,
master_pubkey: &str,
Expand All @@ -92,15 +92,15 @@ pub async fn add_order(
let mut seller_pubkey: Option<String> = None;
let mut master_seller_pubkey: Option<String> = None;
let created_at = Timestamp::now();
if order.kind == Kind::Buy {
if order.kind == Some(Kind::Buy) {
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 kind = order.kind.to_string();
let status = order.status.to_string();
let kind = order.kind.unwrap().to_string();
let status = order.status.unwrap().to_string();
let price_from_api = order.amount == 0;

let order = sqlx::query_as::<_, Order>(
Expand Down
12 changes: 8 additions & 4 deletions src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ pub async fn hold_invoice_paid(hash: &str) {

// We send this data related to the order to the parties
let mut order_data = SmallOrder::new(
order.id,
Some(order.id),
None,
None,
order.amount,
order.fiat_code.clone(),
order.fiat_amount,
order.payment_method.clone(),
order.premium,
master_buyer_pubkey,
master_seller_pubkey,
None,
None,
);
let status;

Expand All @@ -45,7 +49,7 @@ pub async fn hold_invoice_paid(hash: &str) {
Some(order.id),
None,
Action::BuyerTookOrder,
Some(Content::SmallOrder(order_data.clone())),
Some(Content::Order(order_data.clone())),
);
let message = message.as_json().unwrap();
send_dm(&client, &my_keys, &seller_pubkey, message)
Expand All @@ -56,7 +60,7 @@ pub async fn hold_invoice_paid(hash: &str) {
Some(order.id),
None,
Action::HoldInvoicePaymentAccepted,
Some(Content::SmallOrder(order_data)),
Some(Content::Order(order_data)),
);
let message = message.as_json().unwrap();
send_dm(&client, &my_keys, &buyer_pubkey, message)
Expand All @@ -75,7 +79,7 @@ pub async fn hold_invoice_paid(hash: &str) {
Some(order.id),
None,
Action::AddInvoice,
Some(Content::SmallOrder(order_data.clone())),
Some(Content::Order(order_data.clone())),
);
let message = message.as_json().unwrap();
send_dm(&client, &my_keys, &buyer_pubkey, message)
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ async fn main() -> Result<()> {
#[cfg(test)]
mod tests {
use mostro_core::message::Message;
use mostro_core::order::NewOrder;
use mostro_core::order::SmallOrder;
use std::time::{SystemTime, UNIX_EPOCH};

#[test]
fn test_order_deserialize_serialize() {
let sample_order = r#"{"kind":"Sell","status":"Pending","amount":100,"fiat_code":"XXX","fiat_amount":10,"payment_method":"belo","premium":1,"created_at":0}"#;
let order = NewOrder::from_json(sample_order).unwrap();
let order = SmallOrder::from_json(sample_order).unwrap();
let json_order = order.as_json().unwrap();
assert_eq!(sample_order, json_order);
}
Expand Down
32 changes: 18 additions & 14 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{db, flow};
use anyhow::{Context, Result};
use log::{error, info};
use mostro_core::message::{Action, Content, Message};
use mostro_core::order::{Kind as OrderKind, NewOrder, Order, SmallOrder, Status};
use mostro_core::order::{Kind as OrderKind, Order, SmallOrder, Status};
use nostr_sdk::prelude::*;
use sqlx::types::chrono::Utc;
use sqlx::SqlitePool;
Expand Down Expand Up @@ -88,7 +88,7 @@ pub async fn publish_order(
pool: &SqlitePool,
client: &Client,
keys: &Keys,
new_order: &NewOrder,
new_order: &SmallOrder,
initiator_pubkey: &str,
master_pubkey: &str,
ack_pubkey: XOnlyPublicKey,
Expand All @@ -99,10 +99,10 @@ pub async fn publish_order(
// We transform the order fields to tags to use in the event
let tags = order_to_tags(&order);
// Now we have the order id, we can create a new event adding this id to the Order object
let order = NewOrder::new(
let order = SmallOrder::new(
Some(order_id),
OrderKind::from_str(&order.kind).unwrap(),
Status::Pending,
Some(OrderKind::from_str(&order.kind).unwrap()),
Some(Status::Pending),
order.amount,
order.fiat_code,
order.fiat_amount,
Expand All @@ -111,7 +111,7 @@ pub async fn publish_order(
None,
None,
None,
Utc::now().timestamp(),
Some(Utc::now().timestamp()),
);
let order_string = order.as_json().unwrap();
info!("serialized order: {order_string}");
Expand Down Expand Up @@ -210,10 +210,10 @@ pub async fn update_order_event(
) -> Result<()> {
let kind = OrderKind::from_str(&order.kind).unwrap();
let amount = amount.unwrap_or(order.amount);
let publish_order = NewOrder::new(
let publish_order = SmallOrder::new(
Some(order.id),
kind,
status,
Some(kind),
Some(status),
amount,
order.fiat_code.to_owned(),
order.fiat_amount,
Expand All @@ -222,7 +222,7 @@ pub async fn update_order_event(
None,
None,
None,
order.created_at,
Some(order.created_at),
);
let order_content = publish_order.as_json()?;
let mut order = order.clone();
Expand Down Expand Up @@ -315,7 +315,7 @@ pub async fn show_hold_invoice(
// We need to publish a new event with the new status
update_order_event(pool, client, my_keys, Status::WaitingPayment, order, None).await?;
let mut new_order = order.as_new_order();
new_order.status = Status::WaitingPayment;
new_order.status = Some(Status::WaitingPayment);
// We create a Message to send the hold invoice to seller
let message = Message::new_order(
Some(order.id),
Expand Down Expand Up @@ -393,21 +393,25 @@ pub async fn set_market_order_sats_amount(

// We send this data related to the buyer
let order_data = SmallOrder::new(
order.id,
Some(order.id),
None,
None,
buyer_final_amount,
order.fiat_code.clone(),
order.fiat_amount,
order.payment_method.clone(),
order.premium,
None,
None,
None,
None,
);
// We create a Message
let message = Message::new_order(
Some(order.id),
None,
Action::AddInvoice,
Some(Content::SmallOrder(order_data)),
Some(Content::Order(order_data)),
);
let message = message.as_json()?;

Expand All @@ -434,7 +438,7 @@ pub async fn rate_counterpart(
buyer_pubkey: &XOnlyPublicKey,
seller_pubkey: &XOnlyPublicKey,
my_keys: &Keys,
order: NewOrder,
order: SmallOrder,
) -> Result<()> {
// Send dm to counterparts
// to buyer
Expand Down

0 comments on commit 843f318

Please sign in to comment.