Skip to content

Commit

Permalink
Merge pull request #104 from MostroP2P/update-localdb-id-range-order
Browse files Browse the repository at this point in the history
Update localdb id range order
  • Loading branch information
grunch authored Dec 30, 2024
2 parents 149a253 + a4bdee3 commit 32997a1
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 70 deletions.
60 changes: 41 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ path = "src/main.rs"
[dependencies]
anyhow = "1.0.68"
clap = { version = "4.0.32", features = ["derive"] }
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = ["nip06", "nip44", "nip59"], rev ="70db575d51965240aab1e1b3f1edab782c0ef625"}
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = [
"nip06",
"nip44",
"nip59",
], rev = "70db575d51965240aab1e1b3f1edab782c0ef625" }
# nostr-sdk = { version = "0.37.0", features = ["nip06", "nip44", "nip59"] }
serde = "1.0.215"
serde_json = "1.0.91"
Expand All @@ -41,10 +45,11 @@ dotenvy = "0.15.6"
lightning-invoice = "0.23.0"
reqwest = { version = "0.12.4", features = ["json"] }
mostro-core = { git = "https://github.com/MostroP2P/mostro-core", branch = "test-new-sdk" }
# mostro-core = { path = "../mostro-core" }
bitcoin_hashes = "0.15.0"
lnurl-rs = "0.9.0"
pretty_env_logger = "0.5.0"
openssl = { version = "0.10.68", features = ["vendored"] }
sqlx = { version = "0.8.2", features = ["sqlite", "runtime-tokio-native-tls"] }
bip39 = { version = "2.1.0", features = ["rand"] }
dirs = "5.0.1"
dirs = "5.0.1"
11 changes: 10 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,16 @@ pub async fn run() -> Result<()> {
execute_add_invoice(order_id, invoice, &identity_keys, mostro_key, &client).await?
}
Commands::GetDm { since, from_user } => {
execute_get_dm(since, trade_keys, &client, *from_user).await?
execute_get_dm(
since,
identity_keys,
trade_keys,
trade_index,
mostro_key,
&client,
*from_user,
)
.await?
}
Commands::FiatSent { order_id }
| Commands::Release { order_id }
Expand Down
94 changes: 79 additions & 15 deletions src/cli/get_dm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ use std::collections::HashSet;

use anyhow::Result;
use chrono::DateTime;
use mostro_core::message::{Message, Payload};
use mostro_core::message::{Action, Message, Payload};
use nostr_sdk::prelude::*;
use uuid::Uuid;

use crate::{
db::{connect, Order},
util::get_direct_messages,
db::{connect, Order, User},
util::{get_direct_messages, send_message_sync},
};

pub async fn execute_get_dm(
since: &i64,
identity_keys: Keys,
trade_keys: Keys,
trade_index: i64,
mostro_key: PublicKey,
client: &Client,
from_user: bool,
) -> Result<()> {
Expand Down Expand Up @@ -43,27 +47,87 @@ pub async fn execute_get_dm(
println!();
} else {
for m in dm.iter() {
let message = m.0.get_inner_message_kind();
let date = DateTime::from_timestamp(m.1 as i64, 0).unwrap();
if m.0.get_inner_message_kind().id.is_some() {
if message.id.is_some() {
println!(
"Mostro sent you this message for order id: {} at {}",
m.0.get_inner_message_kind().id.unwrap(),
date
);
}
if let Some(Payload::PaymentRequest(_, inv, _)) = &m.0.get_inner_message_kind().payload
{
println!();
println!("Pay this invoice to continue --> {}", inv);
println!();
} else if let Some(Payload::TextMessage(text)) = &m.0.get_inner_message_kind().payload {
println!();
println!("{text}");
println!();
if let Some(payload) = &message.payload {
match payload {
Payload::PaymentRequest(_, inv, _) => {
println!();
println!("Pay this invoice to continue --> {}", inv);
println!();
}
Payload::TextMessage(text) => {
println!();
println!("{text}");
println!();
}
Payload::CantDo(Some(cant_do_reason)) => {
println!();
println!("Error: {:?}", cant_do_reason);
println!();
}
Payload::Order(new_order) if message.action == Action::NewOrder => {
let db_order =
Order::get_by_id(&pool, &new_order.id.unwrap().to_string()).await;
if db_order.is_err() {
let _ = Order::new(&pool, new_order.clone(), &trade_keys, None)
.await
.map_err(|e| {
anyhow::anyhow!("Failed to create DB order: {:?}", e)
})?;
// Update last trade index
match User::get(&pool).await {
Ok(mut user) => {
user.set_last_trade_index(trade_index + 1);
if let Err(e) = user.save(&pool).await {
println!("Failed to update user: {}", e);
}
}
Err(e) => println!("Failed to get user: {}", e),
}
let request_id = Uuid::new_v4().as_u128() as u64;
// Now we send a message to Mostro letting it know the trade key and
// trade index for this new order
let message = Message::new_order(
Some(new_order.id.unwrap()),
Some(request_id),
Some(trade_index),
Action::TradePubkey,
None,
);
let _ = send_message_sync(
client,
Some(&identity_keys),
&trade_keys,
mostro_key,
message,
false,
false,
)
.await?;
}
println!();
println!("Order: {:#?}", new_order);
println!();
}
_ => {
println!();
println!("Action: {}", message.action);
println!("Payload: {:#?}", message.payload);
println!();
}
}
} else {
println!();
println!("Action: {}", m.0.get_inner_message_kind().action);
println!("Payload: {:#?}", m.0.get_inner_message_kind().payload);
println!("Action: {}", message.action);
println!("Payload: {:#?}", message.payload);
println!();
}
}
Expand Down
19 changes: 15 additions & 4 deletions src/cli/new_order.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use mostro_core::message::{Action, Message, Payload};
use mostro_core::message::{Action, CantDoReason, Message, Payload};
use mostro_core::order::SmallOrder;
use mostro_core::order::{Kind, Status};
use nostr_sdk::prelude::*;
Expand Down Expand Up @@ -144,9 +144,20 @@ pub async fn execute_new_order(
return order.id;
}
}
Action::OutOfRangeFiatAmount | Action::OutOfRangeSatsAmount => {
println!("Error: Amount is outside the allowed range. Please check the order's min/max limits.");
return None;
Action::CantDo => {
if let Some(Payload::CantDo(Some(cant_do_reason))) = &message.payload {
match cant_do_reason {
CantDoReason::OutOfRangeFiatAmount | CantDoReason::OutOfRangeSatsAmount => {
println!("Error: Amount is outside the allowed range. Please check the order's min/max limits.");
}
_ => {
println!("Unknown reason: {:?}", message.payload);
}
}
} else {
println!("Unknown reason: {:?}", message.payload);
return None;
}
}
_ => {
println!("Unknown action: {:?}", message.action);
Expand Down
Loading

0 comments on commit 32997a1

Please sign in to comment.