Skip to content

Commit

Permalink
small improvement on new-order creation suggested by the rabbit
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Dec 15, 2024
1 parent 3a63750 commit 6f9af77
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum MostroError {
LnAddressWrongAmount,
LnPaymentError(String),
LnNodeError(String),
InvalidOrderKind,
}

impl std::error::Error for MostroError {}
Expand All @@ -37,6 +38,7 @@ impl fmt::Display for MostroError {
MostroError::LnAddressParseError => write!(f, "Ln address parsing error - please check your address"),
MostroError::LnPaymentError(e) => write!(f, "Lightning payment failure cause: {}",e),
MostroError::LnNodeError(e) => write!(f, "Lightning node connection failure caused by: {}",e),
MostroError::InvalidOrderKind => write!(f, "Invalid order kind"),
}
}
}
Expand Down
48 changes: 35 additions & 13 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ pub async fn publish_order(
trade_index: Option<i64>,
) -> Result<()> {
// Prepare a new default order
let new_order_db = prepare_new_order(new_order, initiator_pubkey, trade_index, trade_pubkey).await?;
let new_order_db =
match prepare_new_order(new_order, initiator_pubkey, trade_index, trade_pubkey).await {
Some(order) => order,
None => {
return Ok(());
}
};

// CRUD order creation
let mut order = new_order_db.clone().create(pool).await?;
Expand Down Expand Up @@ -213,7 +219,7 @@ async fn prepare_new_order(
initiator_pubkey: &str,
trade_index: Option<i64>,
trade_pubkey: PublicKey,
) -> Result<Order> {
) -> Option<Order> {
let mut fee = 0;
if new_order.amount > 0 {
fee = get_fee(new_order.amount);
Expand Down Expand Up @@ -242,28 +248,44 @@ async fn prepare_new_order(
..Default::default()
};

if new_order.kind == Some(OrderKind::Buy) {
new_order_db.kind = OrderKind::Buy.to_string();
new_order_db.buyer_pubkey = Some(trade_pubkey.to_string());
new_order_db.trade_index_buyer = trade_index;
} else {
new_order_db.seller_pubkey = Some(trade_pubkey.to_string());
new_order_db.trade_index_seller = trade_index;
match new_order.kind {
Some(OrderKind::Buy) => {
new_order_db.kind = OrderKind::Buy.to_string();
new_order_db.buyer_pubkey = Some(trade_pubkey.to_string());
new_order_db.trade_index_buyer = trade_index;
}
Some(OrderKind::Sell) => {
new_order_db.kind = OrderKind::Sell.to_string();
new_order_db.seller_pubkey = Some(trade_pubkey.to_string());
new_order_db.trade_index_seller = trade_index;
}
None => {
send_cant_do_msg(
None,
None,
Some(CantDoReason::InvalidOrderKind),
&trade_pubkey,
)
.await;
return None;
}
}

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

Ok(new_order_db)
Some(new_order_db)
}

pub async fn send_dm(
receiver_pubkey: &PublicKey,
sender_keys: Keys,
payload: String,
) -> Result<()> {

info!("sender key {} - receiver key {}", sender_keys.public_key().to_hex(), receiver_pubkey.to_hex());
info!(
"sender key {} - receiver key {}",
sender_keys.public_key().to_hex(),
receiver_pubkey.to_hex()
);

let event = gift_wrap(&sender_keys, *receiver_pubkey, payload.clone(), None)?;
info!(
Expand Down

0 comments on commit 6f9af77

Please sign in to comment.