From d49e2efa83bc6eae525230dbcbc61d4a00b85fa0 Mon Sep 17 00:00:00 2001 From: scratchy Date: Tue, 16 Nov 2021 14:42:12 +1100 Subject: [PATCH 1/2] Should not be able to trigger close in `PendingOpen` Triggering a settlement proposal prior to being `Open` does not make sense. --- taker-frontend/src/components/History.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/taker-frontend/src/components/History.tsx b/taker-frontend/src/components/History.tsx index 8e06108..5960003 100644 --- a/taker-frontend/src/components/History.tsx +++ b/taker-frontend/src/components/History.tsx @@ -98,9 +98,7 @@ const CfdDetails = ({ cfd }: CfdDetailsProps) => { }); const disableCloseButton = cfd.state.getGroup() === StateGroupKey.CLOSED - || [StateKey.OPEN_COMMITTED, StateKey.OUTGOING_SETTLEMENT_PROPOSAL, StateKey.PENDING_CLOSE].includes( - cfd.state.key, - ); + || !(cfd.state.key === StateKey.OPEN); return ( From 825b80d6df2ba8cf1402e882c37c14b22dcdc1ae Mon Sep 17 00:00:00 2001 From: scratchy Date: Tue, 16 Nov 2021 15:33:53 +1100 Subject: [PATCH 2/2] Add guard for triggering settlement proposal It only makes sense to allow proposing a settlement when in `Open` state. Theoretically we could argue that it should be allowed in `PendingRefund` and `PendingCommit`, but I don't see an advantage in that. Note: This only guards the taker side, no guards were added to the maker side because it remains unclear if the deamon should contain logic for this, or we always want to handle acceptance criteria for settlement from the outside. --- daemon/src/model/cfd.rs | 4 ++++ daemon/src/taker_cfd.rs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index 46b44f9..625585d 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -1221,6 +1221,10 @@ impl Cfd { ) } + pub fn is_collaborative_settle_possible(&self) -> bool { + matches!(self.state.clone(), CfdState::Open { .. }) + } + pub fn role(&self) -> Role { self.order.origin.into() } diff --git a/daemon/src/taker_cfd.rs b/daemon/src/taker_cfd.rs index bcbe12b..ecdbf87 100644 --- a/daemon/src/taker_cfd.rs +++ b/daemon/src/taker_cfd.rs @@ -188,6 +188,14 @@ where let mut conn = self.db.acquire().await?; let cfd = load_cfd_by_order_id(order_id, &mut conn).await?; + if !cfd.is_collaborative_settle_possible() { + anyhow::bail!( + "Settlement proposal not possible because for cfd {} is in state {} which cannot be collaboratively settled", + order_id, + cfd.state + ) + } + let proposal = cfd.calculate_settlement(current_price, self.n_payouts)?; if self