Skip to content

Commit

Permalink
Update mostro core and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Dec 15, 2023
1 parent 0c585b8 commit 14966bb
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sqlx = { version = "0.6.2", features = [
"uuid",
"offline",
] }
sqlx-crud = { version = "0.3.2", features = ["runtime-tokio-rustls"] }
sqlx-crud = { version = "0.4.0", features = ["runtime-tokio-rustls"] }
tokio = { version = "1.23.0", features = ["full"] }
tonic_openssl_lnd = "0.2.0"
uuid = { version = "1.3.0", features = [
Expand All @@ -36,7 +36,7 @@ uuid = { version = "1.3.0", features = [
"serde",
] }
reqwest = { version = "0.11", features = ["json"] }
mostro-core = "0.4.0"
mostro-core = "0.4.2"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
config = "0.13.3"
Expand Down
4 changes: 2 additions & 2 deletions src/app/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub async fn add_invoice_action(
let seller_pubkey = XOnlyPublicKey::from_bech32(seller_pubkey)?;
// We save the invoice on db
order.buyer_invoice = Some(pr.clone());
order.update(pool).await?;
let order = order.update(pool).await?;

if order.preimage.is_some() {
// We send this data related to the order to the parties
Expand Down Expand Up @@ -171,7 +171,7 @@ pub async fn add_invoice_action(
None,
&buyer_pubkey,
&seller_pubkey,
&mut order,
order.id,
)
.await?;
}
Expand Down
10 changes: 6 additions & 4 deletions src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use mostro_core::user::User;
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
use tracing::error;
use tracing::{error, info};

pub async fn admin_add_solver_action(
msg: Message,
Expand Down Expand Up @@ -41,13 +41,15 @@ pub async fn admin_add_solver_action(
}
let user = User::new(npubkey.to_string(), 0, 1, 0, 0);
// Use CRUD to create user
user.create(pool).await?;

match user.create(pool).await {
Ok(r) => info!("Solver added: {:#?}", r),
Err(ee) => error!("Error creating solver: {:#?}", ee),
}
// We create a Message for admin
let message = Message::new_dispute(None, None, Action::AdminAddSolver, None);
let message = message.as_json()?;
// Send the message
send_dm(client, my_keys, &event.pubkey, message.clone()).await?;
send_dm(client, my_keys, &event.pubkey, message).await?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/app/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub async fn cancel_action(
);
}
// Update db
order.update(pool).await?;
let mut order = order.update(pool).await?;
order.status = "CooperativelyCanceled".to_string();
// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
Expand Down Expand Up @@ -117,7 +117,7 @@ pub async fn cancel_action(
None => {
order.cancel_initiator_pubkey = Some(user_pubkey.clone());
// update db
order.update(pool).await?;
let order = order.update(pool).await?;
// We create a Message to start a cooperative cancel and send it to both parties
let message = Message::new_order(
Some(order.id),
Expand Down
10 changes: 5 additions & 5 deletions src/app/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use mostro_core::message::{Action, Message};
use mostro_core::order::Order;
use nostr_sdk::prelude::*;
use sqlx::{Pool, Sqlite};
use sqlx_crud::Crud;
use sqlx_crud::traits::Crud;
use tracing::{error, info};

pub async fn dispute_action(
Expand Down Expand Up @@ -71,18 +71,18 @@ pub async fn dispute_action(
// Need to update dispute status
order.update(pool).await?;
}
let dispute = Dispute::new(order.id);
let dispute = Dispute::new(order_id);
// Use CRUD create method
dispute.create(pool).await?;
let dispute = dispute.create(pool).await?;

// We create a Message for the initiator
let message = Message::new_order(Some(order.id), None, Action::DisputeInitiatedByYou, None);
let message = Message::new_order(Some(order_id), None, Action::DisputeInitiatedByYou, None);
let message = message.as_json()?;
let initiator_pubkey = XOnlyPublicKey::from_bech32(message_sender)?;
send_dm(client, my_keys, &initiator_pubkey, message).await?;

// We create a Message for the counterpart
let message = Message::new_order(Some(order.id), None, Action::DisputeInitiatedByPeer, None);
let message = Message::new_order(Some(order_id), None, Action::DisputeInitiatedByPeer, None);
let message = message.as_json()?;
let counterpart_pubkey = XOnlyPublicKey::from_bech32(counterpart)?;
send_dm(client, my_keys, &counterpart_pubkey, message).await?;
Expand Down
13 changes: 6 additions & 7 deletions src/app/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ pub async fn take_buy_action(
return Ok(());
}

// Timestamp order take time
if order.taken_at == 0 {
order.taken_at = Timestamp::now().as_i64();
order.update(pool).await?;
}

// Check market price value in sats - if order was with market price then calculate
if order.amount == 0 {
order.amount =
Expand All @@ -102,14 +96,19 @@ pub async fn take_buy_action(
};
}

// Timestamp order take time
order.taken_at = Timestamp::now().as_i64();
let order_id = order.id;
order.update(pool).await?;

show_hold_invoice(
pool,
client,
my_keys,
None,
&buyer_pubkey,
&seller_pubkey,
&mut order,
order_id,
)
.await?;
Ok(())
Expand Down
9 changes: 4 additions & 5 deletions src/app/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,9 @@ pub async fn take_sell_action(
// Add buyer pubkey to order
edit_buyer_pubkey_order(pool, order_id, buyer_pubkey_bech32).await?;
// Timestamp take order time
if order.taken_at == 0 {
order.taken_at = Timestamp::now().as_i64();
order.update(pool).await?;
}
order.taken_at = Timestamp::now().as_i64();
let order_id = order.id;
let mut order = order.update(pool).await?;
// Check market price value in sats - if order was with market price then calculate it and send a DM to buyer
if order.amount == 0 {
set_market_order_sats_amount(&mut order, buyer_pubkey, my_keys, pool, client).await?;
Expand All @@ -140,7 +139,7 @@ pub async fn take_sell_action(
pr,
&buyer_pubkey,
&seller_pubkey,
&mut order,
order_id,
)
.await?;
}
Expand Down
17 changes: 11 additions & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,11 @@ pub async fn update_user_rating_event(
// We update the order vote status
if buyer_sent_rate {
order.buyer_sent_rate = buyer_sent_rate;
order.update(pool).await?;
}
if seller_sent_rate {
order.seller_sent_rate = seller_sent_rate;
order.update(pool).await?;
}
order.update(pool).await?;

// Add event message to global list
rate_list.lock().await.push(event);
Expand Down Expand Up @@ -254,8 +253,15 @@ pub async fn show_hold_invoice(
payment_request: Option<String>,
buyer_pubkey: &XOnlyPublicKey,
seller_pubkey: &XOnlyPublicKey,
order: &mut Order,
order_id: Uuid,
) -> anyhow::Result<()> {
let mut order = match Order::by_id(pool, order_id).await? {
Some(order) => order,
None => {
error!("Order Id {order_id} not found!");
return Ok(());
}
};
let mut ln_client = lightning::LndConnector::new().await;
let mostro_settings = Settings::get_mostro();
// Add fee of seller to hold invoice
Expand All @@ -279,7 +285,6 @@ pub async fn show_hold_invoice(
.await?;
if let Some(invoice) = payment_request {
order.buyer_invoice = Some(invoice);
order.update(pool).await?;
};

// Using CRUD to update all fiels
Expand All @@ -288,10 +293,10 @@ pub async fn show_hold_invoice(
order.status = Status::WaitingPayment.to_string();
order.buyer_pubkey = Some(buyer_pubkey.to_bech32()?);
order.seller_pubkey = Some(seller_pubkey.to_bech32()?);
order.update(pool).await?;
let order = order.update(pool).await?;

// We need to publish a new event with the new status
update_order_event(pool, client, my_keys, Status::WaitingPayment, order, None).await?;
update_order_event(pool, client, my_keys, Status::WaitingPayment, &order, None).await?;
let mut new_order = order.as_new_order();
new_order.status = Some(Status::WaitingPayment);
// We create a Message to send the hold invoice to seller
Expand Down

0 comments on commit 14966bb

Please sign in to comment.