From 2ae617d6624f1418bdd7f4e340fbb401d0772f3c Mon Sep 17 00:00:00 2001 From: Arnaud Date: Wed, 7 May 2025 15:54:24 +0200 Subject: [PATCH] Remove the returnedCollateral when the slot is not filled by the host --- codex/sales/states/cancelled.nim | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/codex/sales/states/cancelled.nim b/codex/sales/states/cancelled.nim index d3b4407c..f3c755a3 100644 --- a/codex/sales/states/cancelled.nim +++ b/codex/sales/states/cancelled.nim @@ -31,24 +31,22 @@ method run*( raiseAssert "no sale request" try: - debug "Collecting collateral and partial payout", - requestId = data.requestId, slotIndex = data.slotIndex - - # The returnedCollateral is needed even if the slot is not filled by the host - # because a reservation could be created and the collateral assigned - # to that reservation. So if the slot is not filled by that host, - # the reservation will be deleted during cleanup and the collateral - # must be returned to the host. - let slot = Slot(request: request, slotIndex: data.slotIndex) - let currentCollateral = await market.currentCollateral(slot.id) - let returnedCollateral = currentCollateral.some + var returnedCollateral = UInt256.none if await slotIsFilledByMe(market, data.requestId, data.slotIndex): + debug "Collecting collateral and partial payout", + requestId = data.requestId, slotIndex = data.slotIndex + + let slot = Slot(request: request, slotIndex: data.slotIndex) + let currentCollateral = await market.currentCollateral(slot.id) + try: await market.freeSlot(slot.id) except SlotStateMismatchError as e: warn "Failed to free slot because slot is already free", error = e.msg + returnedCollateral = currentCollateral.some + if onClear =? agent.context.onClear and request =? data.request: onClear(request, data.slotIndex)