Skip to content

Commit

Permalink
Get the right next trade fields on release (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch authored Jan 11, 2025
1 parent a17b33b commit cb3ba19
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
1 change: 0 additions & 1 deletion src/app/fiat_sent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ pub async fn fiat_sent_action(
Some(Payload::NextTrade(pubkey, index)) => Some((pubkey.clone(), *index)),
_ => None,
};

// We publish a new replaceable kind nostr event with the status updated
// and update on local database the status and new event id
if let Ok(order_updated) = update_order_event(my_keys, Status::FiatSent, &order).await {
Expand Down
46 changes: 28 additions & 18 deletions src/app/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ pub async fn release_action(
) -> Result<()> {
// Get request id
let request_id = msg.get_inner_message_kind().request_id;
let next_trade: Option<(String, u32)> = match &msg.get_inner_message_kind().payload {
Some(Payload::NextTrade(pubkey, index)) => Some((pubkey.clone(), *index)),
_ => None,
};

let order_id = msg
.get_inner_message_kind()
.id
Expand All @@ -89,28 +84,43 @@ pub async fn release_action(
order_id
)))?;

let current_status =
Status::from_str(&order.status).map_err(|_| Error::msg("Wrong order status"))?;

if !matches!(
current_status,
Status::Active | Status::FiatSent | Status::Dispute
) {
// Only seller can release funds
if &event.rumor.pubkey.to_string() != seller_pubkey_hex {
send_cant_do_msg(
request_id,
Some(order.id),
Some(CantDoReason::NotAllowedByStatus),
Some(CantDoReason::InvalidPeer),
&event.rumor.pubkey,
)
.await;
return Ok(());
}

if &event.rumor.pubkey.to_string() != seller_pubkey_hex {
let next_trade: Option<(String, u32)> = match event.rumor.pubkey.to_string() {
pubkey if pubkey == order.creator_pubkey => {
if let Some(Payload::NextTrade(pubkey, index)) = &msg.get_inner_message_kind().payload {
Some((pubkey.clone(), *index))
} else {
None
}
}
_ => match (order.next_trade_pubkey.as_ref(), order.next_trade_index) {
(Some(pubkey), Some(index)) => Some((pubkey.clone(), index as u32)),
_ => None,
},
};

let current_status =
Status::from_str(&order.status).map_err(|_| Error::msg("Wrong order status"))?;

if !matches!(
current_status,
Status::Active | Status::FiatSent | Status::Dispute
) {
send_cant_do_msg(
request_id,
Some(order.id),
Some(CantDoReason::InvalidPeer),
Some(CantDoReason::NotAllowedByStatus),
&event.rumor.pubkey,
)
.await;
Expand Down Expand Up @@ -198,6 +208,8 @@ async fn handle_child_order(
child_order.creator_pubkey = next_trade_pubkey.clone();
child_order.trade_index_buyer = order.next_trade_index;
}
child_order.next_trade_index = None;
child_order.next_trade_pubkey = None;

let new_order = child_order.as_new_order();
let next_trade_pubkey = PublicKey::from_str(&next_trade_pubkey)?;
Expand All @@ -207,9 +219,7 @@ async fn handle_child_order(
Action::NewOrder,
Some(Payload::Order(new_order)),
&next_trade_pubkey,
child_order
.trade_index_buyer
.or(child_order.trade_index_seller),
Some(next_trade_index as i64),
)
.await;
child_order.clone().update(pool).await?;
Expand Down

0 comments on commit cb3ba19

Please sign in to comment.