Skip to content

Commit

Permalink
working on key management - added initial check on messages and start…
Browse files Browse the repository at this point in the history
…ed to update all commands - TBD
  • Loading branch information
arkanoider committed Dec 8, 2024
1 parent 4d199c8 commit 9924898
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 42 deletions.
5 changes: 3 additions & 2 deletions migrations/20221222153301_orders.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ CREATE TABLE IF NOT EXISTS orders (
seller_sent_rate integer default 0,
payment_attempts integer default 0,
failed_payment integer default 0,
expires_at integer not null
);
expires_at integer not null,
trade_index integer default 0
);
5 changes: 5 additions & 0 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub async fn add_invoice_action(
Action::IncorrectInvoiceAmount,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand All @@ -107,6 +108,7 @@ pub async fn add_invoice_action(
Action::InvoiceUpdated,
None,
&buyer_pubkey,
None,
)
.await;
return Ok(());
Expand All @@ -118,6 +120,7 @@ pub async fn add_invoice_action(
Action::NotAllowedByStatus,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand Down Expand Up @@ -163,6 +166,7 @@ pub async fn add_invoice_action(
Action::BuyerTookOrder,
Some(Content::Order(order_data.clone())),
&seller_pubkey,
None,
)
.await;
// We send a message to buyer saying seller paid
Expand All @@ -172,6 +176,7 @@ pub async fn add_invoice_action(
Action::HoldInvoicePaymentAccepted,
Some(Content::Order(order_data)),
&buyer_pubkey,
None,
)
.await;
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub async fn fiat_sent_action(
Action::NotAllowedByStatus,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand Down Expand Up @@ -71,6 +72,7 @@ pub async fn fiat_sent_action(
Action::FiatSentOk,
Some(Content::Peer(peer)),
&seller_pubkey,
None,
)
.await;
// We send a message to buyer to wait
Expand All @@ -82,6 +84,7 @@ pub async fn fiat_sent_action(
Action::FiatSentOk,
Some(Content::Peer(peer)),
&event.sender,
None,
)
.await;

Expand Down
6 changes: 4 additions & 2 deletions src/app/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ pub async fn order_action(
// Get request id
let request_id = msg.get_inner_message_kind().request_id;

let pubkey_0 = event.sender;

if let Some(order) = msg.get_inner_message_kind().get_order() {
let mostro_settings = Settings::get_mostro();

Expand All @@ -36,6 +34,7 @@ pub async fn order_action(
Action::IncorrectInvoiceAmount,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand Down Expand Up @@ -92,6 +91,7 @@ pub async fn order_action(
Action::InvalidSatsAmount,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand All @@ -106,6 +106,7 @@ pub async fn order_action(
Action::OutOfRangeSatsAmount,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand All @@ -119,6 +120,7 @@ pub async fn order_action(
&event.sender.to_string(),
event.sender,
request_id,
msg.get_inner_message_kind().trade_index,
)
.await?;
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub async fn take_buy_action(
Action::NotAllowedByStatus,
None,
&seller_pubkey,
None,
)
.await;
return Ok(());
Expand All @@ -85,6 +86,7 @@ pub async fn take_buy_action(
Action::OutOfRangeFiatAmount,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand Down
2 changes: 2 additions & 0 deletions src/app/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub async fn take_sell_action(
Action::NotAllowedByStatus,
None,
&buyer_pubkey,
None,
)
.await;
return Ok(());
Expand All @@ -117,6 +118,7 @@ pub async fn take_sell_action(
Action::OutOfRangeFiatAmount,
None,
&event.sender,
None,
)
.await;
return Ok(());
Expand Down
96 changes: 58 additions & 38 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use nostr_sdk::prelude::*;
use sqlx::SqlitePool;
use sqlx_crud::Crud;
use std::fmt::Write;
use std::path::is_separator;
use std::str::FromStr;
use std::sync::Arc;
use std::thread;
Expand Down Expand Up @@ -163,44 +164,10 @@ pub async fn publish_order(
initiator_pubkey: &str,
ack_pubkey: PublicKey,
request_id: Option<u64>,
trade_index: Option<i64>,
) -> Result<()> {
let mut fee = 0;
if new_order.amount > 0 {
fee = get_fee(new_order.amount);
}

// Get expiration time of the order
let expiry_date = get_expiration_date(new_order.expires_at);

// Prepare a new default order
let mut new_order_db = Order {
id: Uuid::new_v4(),
kind: OrderKind::Sell.to_string(),
status: Status::Pending.to_string(),
creator_pubkey: initiator_pubkey.to_string(),
payment_method: new_order.payment_method.clone(),
amount: new_order.amount,
fee,
fiat_code: new_order.fiat_code.clone(),
min_amount: new_order.min_amount,
max_amount: new_order.max_amount,
fiat_amount: new_order.fiat_amount,
premium: new_order.premium,
buyer_invoice: new_order.buyer_invoice.clone(),
created_at: Timestamp::now().as_u64() as i64,
expires_at: expiry_date,
..Default::default()
};

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

// Request price from API in case amount is 0
new_order_db.price_from_api = new_order.amount == 0;
let new_order_db = prepare_new_order(new_order, initiator_pubkey, trade_index).await?;

// CRUD order creation
let mut order = new_order_db.clone().create(pool).await?;
Expand Down Expand Up @@ -228,6 +195,7 @@ pub async fn publish_order(
Action::NewOrder,
Some(Content::Order(order)),
&ack_pubkey,
trade_index,
)
.await;

Expand All @@ -240,6 +208,54 @@ pub async fn publish_order(
.map_err(|err| err.into())
}

async fn prepare_new_order(
new_order: &SmallOrder,
initiator_pubkey: &str,
trade_index: Option<i64>,
) -> Result<Order> {
let mut fee = 0;
if new_order.amount > 0 {
fee = get_fee(new_order.amount);
}

// Get expiration time of the order
let expiry_date = get_expiration_date(new_order.expires_at);

// Prepare a new default order
let mut new_order_db = Order {
id: Uuid::new_v4(),
kind: OrderKind::Sell.to_string(),
status: Status::Pending.to_string(),
creator_pubkey: initiator_pubkey.to_string(),
payment_method: new_order.payment_method.clone(),
amount: new_order.amount,
fee,
fiat_code: new_order.fiat_code.clone(),
min_amount: new_order.min_amount,
max_amount: new_order.max_amount,
fiat_amount: new_order.fiat_amount,
premium: new_order.premium,
buyer_invoice: new_order.buyer_invoice.clone(),
created_at: Timestamp::now().as_u64() as i64,
expires_at: expiry_date,
..Default::default()
};

if new_order.kind == Some(OrderKind::Buy) {
new_order_db.kind = OrderKind::Buy.to_string();
new_order_db.buyer_pubkey = Some(initiator_pubkey.to_string());
new_order_db.trade_index_buyer = trade_index;
} else {
new_order_db.seller_pubkey = Some(initiator_pubkey.to_string());
new_order_db.trade_index_seller = trade_index;
}

// Request price from API in case amount is 0
new_order_db.price_from_api = new_order.amount == 0;

Ok(new_order_db)
}

pub async fn send_dm(
receiver_pubkey: &PublicKey,
sender_keys: Keys,
Expand Down Expand Up @@ -409,6 +425,7 @@ pub async fn show_hold_invoice(
None,
)),
seller_pubkey,
None,
)
.await;
// We send a message to buyer to know that seller was requested to pay the invoice
Expand All @@ -418,6 +435,7 @@ pub async fn show_hold_invoice(
Action::WaitingSellerToPay,
None,
buyer_pubkey,
None,
)
.await;

Expand Down Expand Up @@ -521,6 +539,7 @@ pub async fn set_waiting_invoice_status(
Action::AddInvoice,
Some(Content::Order(order_data)),
&buyer_pubkey,
None,
)
.await;

Expand Down Expand Up @@ -594,7 +613,7 @@ pub async fn send_cant_do_msg(
let content = message.map(Content::TextMessage);

// Send message to event creator
let message = Message::cant_do(request_id, order_id, content);
let message = Message::cant_do(request_id, order_id, content, None);
if let Ok(message) = message.as_json() {
let sender_keys = crate::util::get_keys().unwrap();
let _ = send_dm(destination_key, sender_keys, message).await;
Expand All @@ -607,9 +626,10 @@ pub async fn send_new_order_msg(
action: Action,
content: Option<Content>,
destination_key: &PublicKey,
trade_index: Option<i64>,
) {
// Send message to event creator
let message = Message::new_order(request_id, order_id, action, content);
let message = Message::new_order(request_id, order_id, None, action, content, None);
if let Ok(message) = message.as_json() {
let sender_keys = crate::util::get_keys().unwrap();
let _ = send_dm(destination_key, sender_keys, message).await;
Expand Down

0 comments on commit 9924898

Please sign in to comment.