From c44a5c53d327225a9c2922ee35f1398bd3975f3e Mon Sep 17 00:00:00 2001 From: Mikko Ohtamaa Date: Fri, 21 Feb 2025 00:28:10 +0200 Subject: [PATCH] Hopefully a final fix for Lagoon interest tracker --- tradeexecutor/state/position.py | 11 +++++++---- tradeexecutor/strategy/interest.py | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tradeexecutor/state/position.py b/tradeexecutor/state/position.py index b322dea7d..7ed0edea1 100644 --- a/tradeexecutor/state/position.py +++ b/tradeexecutor/state/position.py @@ -1499,12 +1499,15 @@ def get_last_valued_at(self) -> datetime.datetime: """Get the timestamp when this position was last valued""" if self.is_loan_based(): + # Loan-based positions are updated by update_interest() + # and it does not always update the token price (e.g. aUSDC). + # The timestamp for these positions must be thus taken from the interest update events. timestamps = [] - if self.loan.collateral and self.loan.collateral.last_pricing_at: - timestamps.append(self.loan.collateral.last_pricing_at) - if self.loan.borrowed and self.loan.borrowed.last_pricing_at: + if self.loan.collateral and self.loan.collateral_interest.last_updated_at: + timestamps.append(self.loan.collateral_interest.last_updated_at) + if self.loan.borrowed and self.loan.borrowed_interest.last_updated_at: # Credit position does not have borrowed part of the loan - timestamps.append(self.loan.borrowed.last_pricing_at) + timestamps.append(self.loan.borrowed_interest.last_updated_at) return min(timestamps) else: # Spot tokens diff --git a/tradeexecutor/strategy/interest.py b/tradeexecutor/strategy/interest.py index 1d0bf7442..5b262f3e4 100644 --- a/tradeexecutor/strategy/interest.py +++ b/tradeexecutor/strategy/interest.py @@ -111,13 +111,15 @@ def update_interest( logger.log( log_level, - f"update_interest(), block {block_number or 0:,}, new token amount: %s, old balance: %s, gained interest tokens: %s, position new USD value: %s, previous update at %s, previous block at %s", + f"update_interest(), block {block_number or 0:,}, new token amount: %s, old balance: %s, gained interest tokens: %s, position new USD value: %s, previous update at %s, previous block at %s, current block %s, block time %s", new_token_amount, old_balance, gained_interest, usd_value, previous_update_at, f"{previous_block or 0:,}", + block_number, + event_at, ) gained_interest_percent = gained_interest / old_balance @@ -152,7 +154,6 @@ def update_interest( interest.last_event_at = event_at interest.last_updated_block_number = block_number interest.last_token_amount = new_token_amount - return evt