From f6371c7018b2c58a37f393a3a1ab30e5d94a99f1 Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 16 Oct 2024 18:32:27 -0400 Subject: [PATCH] time trigger fixes --- src/dexorder/order/triggers.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/dexorder/order/triggers.py b/src/dexorder/order/triggers.py index 063b81e..88f1760 100644 --- a/src/dexorder/order/triggers.py +++ b/src/dexorder/order/triggers.py @@ -125,6 +125,7 @@ async def end_trigger_updates(): working_set = _dirty _dirty = set() for tk in working_set: + log.debug(f'check dirty tranche {tk}') if _trigger_state.get(tk,0) == 0: # all clear for execution. add to active list with any necessary proofs active_tranches[tk] = PriceProof(0) @@ -222,20 +223,20 @@ async def has_funds(tk: TrancheKey): async def input_amount_is_sufficient(order, token_balance): - log.debug(f'input is sufficient? {order.min_fill_amount}') + # log.debug(f'input is sufficient? {order.min_fill_amount}') if order.amount_is_input: - log.debug(f'amount is input: {token_balance} >= {order.min_fill_amount}') + # log.debug(f'amount is input: {token_balance} >= {order.min_fill_amount}') return token_balance >= order.min_fill_amount # amount is an output amount, so we need to know the price price = pool_prices.get(order.pool_address) - log.debug(f'amount is output amount. price={price}') + # log.debug(f'amount is output amount. price={price}') if price is None: return token_balance > 0 # we don't know the price so we allow any nonzero amount to be sufficient pool = await get_pool(order.pool_address) price *= dec(10) ** -pool['decimals'] inverted = order.order.tokenIn != pool['base'] minimum = dec(order.min_fill_amount)*price if inverted else dec(order.min_fill_amount)/price - log.debug(f'order minimum amount is {order.min_fill_amount} '+ ("input" if order.amount_is_input else f"output @ {price} = {minimum} ")+f'< {token_balance} balance') + # log.debug(f'order minimum amount is {order.min_fill_amount} '+ ("input" if order.amount_is_input else f"output @ {price} = {minimum} ")+f'< {token_balance} balance') return token_balance >= minimum @@ -245,20 +246,18 @@ class BalanceTrigger (Trigger): @staticmethod async def create(tk: TrancheKey): value = await has_funds(tk) - bt = BalanceTrigger(tk, value) - log.debug(f'got bt {id(bt)}') - return bt + return BalanceTrigger(tk, value) def __init__(self, tk: TrancheKey, value: bool): super().__init__(0, tk, value) self.order = Order.of(self.tk) self.vault_token = self.tk.vault, self.order.status.order.tokenIn BalanceTrigger.by_vault_token[self.vault_token].add(self) - log.debug(f'initializing Balance Trigger {id(self)} {tk} {value} {self.value}') + # log.debug(f'initializing Balance Trigger {id(self)} {tk} {value} {self.value}') async def update(self, balance): self.value = await input_amount_is_sufficient(self.order, balance) - log.debug(f'update balance {balance} was sufficient? {self.value}') + # log.debug(f'update balance {balance} was sufficient? {self.value}') def remove(self): try: @@ -287,7 +286,8 @@ class TimeTrigger (Trigger): self._time = time self.active = False # whether this trigger is in the `all` list or not (has the time passed?) super().__init__(trigger_type, tk, value) - log.debug(f'created time trigger {self} {from_timestamp(time)} now={from_timestamp(time_now)} {value}') + self.update_active(time_now, time) + # log.debug(f'created time trigger {self} {from_timestamp(time)} now={from_timestamp(time_now)} {value}') @property def time(self): @@ -304,6 +304,7 @@ class TimeTrigger (Trigger): if self.active: # remove old trigger TimeTrigger.all.remove(self) + self.active = False self.update_active(time_now) def update_active(self, time_now: int = None, time: int = None): @@ -311,10 +312,11 @@ class TimeTrigger (Trigger): time_now = current_clock.get().timestamp if time is None: time = self._time - next_active = (time_now > time) is self.is_start + next_active = time_now < time activate = not self.active and next_active + log.debug(f'update_active {self} | {self.active} => {next_active} = {activate}') if activate: - log.debug(f'adding time trigger {self}') + # log.debug(f'adding time trigger {self}') TimeTrigger.all.add(self) self.active = next_active @@ -323,7 +325,7 @@ class TimeTrigger (Trigger): def update(self): # called when our self.time has been reached - log.debug(f'update time trigger {self}') + # log.debug(f'update time trigger {self}') self.value = self.is_start if not self.is_start and not self.value: OrderTriggers.instances[self.tk.order_key].expire_tranche(self.tk.tranche_index) @@ -332,7 +334,7 @@ class TimeTrigger (Trigger): def remove(self): if self.active: try: - log.debug(f'remove time trigger {self}') + # log.debug(f'remove time trigger {self}') TimeTrigger.all.remove(self) except ValueError: pass @@ -340,7 +342,7 @@ class TimeTrigger (Trigger): @staticmethod def update_all(time): - log.debug(f'update time triggers {time}') + # log.debug(f'update time triggers {time}') while TimeTrigger.all and TimeTrigger.all[0].time <= time: # todo this doesnt work across reorgs. we need to keep a BlockState cursor of the last time handled, # then activate any time triggers from that past time through the present. time triggers may only @@ -592,7 +594,6 @@ class TrancheTrigger: def disable(self): # permanently stop this trigger and deconstruct - log.debug(f'removing bt {id(self.balance_trigger)}') self.balance_trigger.remove() if self.activation_trigger is not None: self.activation_trigger.remove()