From 79616e80368385b762051e3103602db719714dfd Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Wed, 20 Oct 2021 21:00:53 +1100 Subject: [PATCH] Add guard to disallow adding cfd for same order This should make this error more explicit and add understanding what goes wrong in the logs. --- daemon/src/cfd_actors.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/daemon/src/cfd_actors.rs b/daemon/src/cfd_actors.rs index 8484812..74e60a3 100644 --- a/daemon/src/cfd_actors.rs +++ b/daemon/src/cfd_actors.rs @@ -1,3 +1,4 @@ +use crate::db::load_cfd_by_order_id; use crate::model::cfd::{Attestation, Cfd, CfdState, CfdStateChangeEvent, OrderId}; use crate::wallet::Wallet; use crate::{db, monitor, oracle}; @@ -11,6 +12,13 @@ pub async fn insert_cfd( conn: &mut PoolConnection, update_sender: &watch::Sender>, ) -> Result<()> { + if load_cfd_by_order_id(cfd.order.id, conn).await.is_ok() { + bail!( + "Cannot insert cfd because there is already a cfd for order id {}", + cfd.order.id + ) + } + db::insert_cfd(cfd, conn).await?; update_sender.send(db::load_all_cfds(conn).await?)?; Ok(())