diff --git a/src/dexorder/base/orderlib.py b/src/dexorder/base/orderlib.py index 70510b4..bddd946 100644 --- a/src/dexorder/base/orderlib.py +++ b/src/dexorder/base/orderlib.py @@ -103,7 +103,7 @@ class SwapOrderStatus(SwapStatus): state = SwapOrderState.Canceled if obj[1] else SwapOrderState.Open start = obj[2] ocoGroup = None if obj[3] == NO_OCO else obj[3] - # we ignore any fill values from the on-chain struct, because we will subsequently detect the DexorderFilled events and add them in + # we ignore any fill values from the on-chain struct, because we will subsequently detect the DexorderSwapFilled events and add them in filledIn = 0 filledOut = 0 trancheFilledIn = [0 for _ in range(len(obj[5]))] diff --git a/src/dexorder/order/triggers.py b/src/dexorder/order/triggers.py index d58b109..d47c0d9 100644 --- a/src/dexorder/order/triggers.py +++ b/src/dexorder/order/triggers.py @@ -44,7 +44,7 @@ async def activate_order(order: Order): triggers = OrderTriggers(order, inverted) if triggers.closed: log.debug(f'order {order.key} was immediately closed') - close_order_and_disable_triggers(order, SwapOrderState.Filled if order.remaining <= 0 else SwapOrderState.Expired) + close_order_and_disable_triggers(order, SwapOrderState.Filled if order.remaining == 0 or order.remaining <= order.min_fill_amount else SwapOrderState.Expired) def intersect_ranges( a_low, a_high, b_low, b_high): @@ -75,7 +75,7 @@ class TrancheTrigger: assert order.key.vault == tranche_key.vault and order.key.order_index == tranche_key.order_index self.order = order self.tk = tranche_key - self.status = TrancheStatus.Early + self._status = TrancheStatus.Early tranche = order.order.tranches[self.tk.tranche_index] tranche_amount = tranche.fraction_of(order.amount) @@ -100,14 +100,24 @@ class TrancheTrigger: # compute status and set relevant triggers if tranche_remaining == 0 or tranche_remaining < self.order.min_fill_amount: # min_fill_amount could be 0 (disabled) so we also check for the 0 case separately - self.status = TrancheStatus.Filled + self._status = TrancheStatus.Filled return timestamp = current_block.get().timestamp - self.status = TrancheStatus.Early if timestamp < self.time_constraint[0] else TrancheStatus.Expired if timestamp > self.time_constraint[1] else TrancheStatus.Pricing + self._status = TrancheStatus.Early if timestamp < self.time_constraint[0] else TrancheStatus.Expired if timestamp > self.time_constraint[1] else TrancheStatus.Pricing self.enable_time_trigger() - if self.status == TrancheStatus.Pricing: + if self._status == TrancheStatus.Pricing: self.enable_price_trigger() + @property + def status(self): + return self._status + + @status.setter + def status(self, value): + self._status = value + if value in (TrancheStatus.Filled, TrancheStatus.Expired): + OrderTriggers.instances[self.order.key].check_complete() + def enable_time_trigger(self): if self.time_constraint: log.debug(f'enable_time_trigger') @@ -126,8 +136,6 @@ class TrancheTrigger: log.debug(f'tranche expired {self.tk}') self.status = TrancheStatus.Expired self.disable() - # check for all tranches expired - OrderTriggers.instances[self.order.key].check_complete() elif self.status == TrancheStatus.Early and now >= self.time_constraint[0]: log.debug(f'tranche time enabled {self.tk}') self.status = TrancheStatus.Pricing @@ -213,7 +221,7 @@ class OrderTriggers: def check_complete(self): if all(t.closed for t in self.triggers): - final_state = SwapOrderState.Filled if self.order.remaining <= 0 else SwapOrderState.Expired + final_state = SwapOrderState.Filled if self.order.remaining == 0 or self.order.remaining < self.order.min_fill_amount else SwapOrderState.Expired close_order_and_disable_triggers(self.order, final_state) def fill(self, tranche_index, amount_in, amount_out):