time trigger fixes

This commit is contained in:
tim
2024-10-16 18:32:27 -04:00
parent 5c19358e38
commit f6371c7018

View File

@@ -125,6 +125,7 @@ async def end_trigger_updates():
working_set = _dirty working_set = _dirty
_dirty = set() _dirty = set()
for tk in working_set: for tk in working_set:
log.debug(f'check dirty tranche {tk}')
if _trigger_state.get(tk,0) == 0: if _trigger_state.get(tk,0) == 0:
# all clear for execution. add to active list with any necessary proofs # all clear for execution. add to active list with any necessary proofs
active_tranches[tk] = PriceProof(0) active_tranches[tk] = PriceProof(0)
@@ -222,20 +223,20 @@ async def has_funds(tk: TrancheKey):
async def input_amount_is_sufficient(order, token_balance): 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: 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 return token_balance >= order.min_fill_amount
# amount is an output amount, so we need to know the price # amount is an output amount, so we need to know the price
price = pool_prices.get(order.pool_address) 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: if price is None:
return token_balance > 0 # we don't know the price so we allow any nonzero amount to be sufficient 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) pool = await get_pool(order.pool_address)
price *= dec(10) ** -pool['decimals'] price *= dec(10) ** -pool['decimals']
inverted = order.order.tokenIn != pool['base'] inverted = order.order.tokenIn != pool['base']
minimum = dec(order.min_fill_amount)*price if inverted else dec(order.min_fill_amount)/price 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 return token_balance >= minimum
@@ -245,20 +246,18 @@ class BalanceTrigger (Trigger):
@staticmethod @staticmethod
async def create(tk: TrancheKey): async def create(tk: TrancheKey):
value = await has_funds(tk) value = await has_funds(tk)
bt = BalanceTrigger(tk, value) return BalanceTrigger(tk, value)
log.debug(f'got bt {id(bt)}')
return bt
def __init__(self, tk: TrancheKey, value: bool): def __init__(self, tk: TrancheKey, value: bool):
super().__init__(0, tk, value) super().__init__(0, tk, value)
self.order = Order.of(self.tk) self.order = Order.of(self.tk)
self.vault_token = self.tk.vault, self.order.status.order.tokenIn self.vault_token = self.tk.vault, self.order.status.order.tokenIn
BalanceTrigger.by_vault_token[self.vault_token].add(self) 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): async def update(self, balance):
self.value = await input_amount_is_sufficient(self.order, 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): def remove(self):
try: try:
@@ -287,7 +286,8 @@ class TimeTrigger (Trigger):
self._time = time self._time = time
self.active = False # whether this trigger is in the `all` list or not (has the time passed?) self.active = False # whether this trigger is in the `all` list or not (has the time passed?)
super().__init__(trigger_type, tk, value) 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 @property
def time(self): def time(self):
@@ -304,6 +304,7 @@ class TimeTrigger (Trigger):
if self.active: if self.active:
# remove old trigger # remove old trigger
TimeTrigger.all.remove(self) TimeTrigger.all.remove(self)
self.active = False
self.update_active(time_now) self.update_active(time_now)
def update_active(self, time_now: int = None, time: int = None): def update_active(self, time_now: int = None, time: int = None):
@@ -311,10 +312,11 @@ class TimeTrigger (Trigger):
time_now = current_clock.get().timestamp time_now = current_clock.get().timestamp
if time is None: if time is None:
time = self._time time = self._time
next_active = (time_now > time) is self.is_start next_active = time_now < time
activate = not self.active and next_active activate = not self.active and next_active
log.debug(f'update_active {self} | {self.active} => {next_active} = {activate}')
if activate: if activate:
log.debug(f'adding time trigger {self}') # log.debug(f'adding time trigger {self}')
TimeTrigger.all.add(self) TimeTrigger.all.add(self)
self.active = next_active self.active = next_active
@@ -323,7 +325,7 @@ class TimeTrigger (Trigger):
def update(self): def update(self):
# called when our self.time has been reached # 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 self.value = self.is_start
if not self.is_start and not self.value: if not self.is_start and not self.value:
OrderTriggers.instances[self.tk.order_key].expire_tranche(self.tk.tranche_index) OrderTriggers.instances[self.tk.order_key].expire_tranche(self.tk.tranche_index)
@@ -332,7 +334,7 @@ class TimeTrigger (Trigger):
def remove(self): def remove(self):
if self.active: if self.active:
try: try:
log.debug(f'remove time trigger {self}') # log.debug(f'remove time trigger {self}')
TimeTrigger.all.remove(self) TimeTrigger.all.remove(self)
except ValueError: except ValueError:
pass pass
@@ -340,7 +342,7 @@ class TimeTrigger (Trigger):
@staticmethod @staticmethod
def update_all(time): 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: 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, # 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 # 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): def disable(self):
# permanently stop this trigger and deconstruct # permanently stop this trigger and deconstruct
log.debug(f'removing bt {id(self.balance_trigger)}')
self.balance_trigger.remove() self.balance_trigger.remove()
if self.activation_trigger is not None: if self.activation_trigger is not None:
self.activation_trigger.remove() self.activation_trigger.remove()