diff --git a/apps/legacy_app/src/helpers.py b/apps/legacy_app/src/helpers.py index ccb19c78..955c39c7 100644 --- a/apps/legacy_app/src/helpers.py +++ b/apps/legacy_app/src/helpers.py @@ -159,6 +159,10 @@ def get_prices(token_decimals: dict[str, int]) -> dict[str, float]: # Remove duplicates. token_info = [dict(y) for y in {tuple(x.items()) for x in token_info}] + if not token_info: + logging.error("Token %s not found in response.", token) + continue + # Perform sanity checks. assert len(token_info) == 1 assert decimals == token_info[0]["decimals"] diff --git a/apps/legacy_app/src/state.py b/apps/legacy_app/src/state.py index 035784d1..0b4071c5 100644 --- a/apps/legacy_app/src/state.py +++ b/apps/legacy_app/src/state.py @@ -48,16 +48,15 @@ def set_loan_entities(self, loan_entities: pandas.DataFrame) -> None: # Fill up `self.loan_entities` with `loan_entities`. for _, loan_entity in loan_entities.iterrows(): user = loan_entity["user"] - for collateral_token, collateral_amount in json.loads( - loan_entity["collateral"].decode("utf-8") - ).items(): + for collateral_token, collateral_amount in loan_entity[ + "collateral" + ].items(): if collateral_amount: - self.loan_entities[user].collateral[ - collateral_token - ] = decimal.Decimal(str(collateral_amount)) - for debt_token, debt_amount in json.loads( - loan_entity["debt"].decode("utf-8") - ).items(): + self.loan_entities[user].collateral[collateral_token] = ( + decimal.Decimal(str(collateral_amount)) + ) + + for debt_token, debt_amount in loan_entity["debt"].items(): if debt_amount: self.loan_entities[user].debt[debt_token] = decimal.Decimal( str(debt_amount) diff --git a/apps/legacy_app/src/types.py b/apps/legacy_app/src/types.py index 9e2bd2b4..7275e989 100644 --- a/apps/legacy_app/src/types.py +++ b/apps/legacy_app/src/types.py @@ -328,8 +328,14 @@ def compute_collateral_usd( else 1.0 ) * float(collateral_interest_rate_model[token]) - * prices[collateral_token_parameters[token].underlying_address] + * prices[underlying_address] for token, token_amount in self.collateral.items() + if ( + underlying_address := collateral_token_parameters[ + token + ].underlying_address + ) + in prices ) def compute_debt_usd( @@ -344,8 +350,10 @@ def compute_debt_usd( / (10 ** debt_token_parameters[token].decimals) / (debt_token_parameters[token].debt_factor if risk_adjusted else 1.0) * float(debt_interest_rate_model[token]) - * prices[debt_token_parameters[token].underlying_address] + * prices[underlying_address] for token, token_amount in self.debt.items() + if (underlying_address := debt_token_parameters[token].underlying_address) + in prices ) @abc.abstractmethod diff --git a/apps/shared/loan_entity.py b/apps/shared/loan_entity.py index 7fdd3c06..ad62544b 100644 --- a/apps/shared/loan_entity.py +++ b/apps/shared/loan_entity.py @@ -50,9 +50,15 @@ def compute_collateral_usd( if risk_adjusted else 1.0 ) - * float(collateral_interest_rate_model[token]) - * prices[collateral_token_parameters[token].underlying_address] - for token, token_amount in self.collateral.items() + * float(collateral_interest_rate_model.get(token, 1.0)) + * prices[underlying_address] + for token, token_amount in self.collateral.values.items() + if ( + underlying_address := collateral_token_parameters[ + token + ].underlying_address + ) + in prices ) def compute_debt_usd( @@ -74,9 +80,11 @@ def compute_debt_usd( float(token_amount) / (10 ** debt_token_parameters[token].decimals) / (debt_token_parameters[token].debt_factor if risk_adjusted else 1.0) - * float(debt_interest_rate_model[token]) - * prices[debt_token_parameters[token].underlying_address] - for token, token_amount in self.debt.items() + * float(debt_interest_rate_model.get(token, 1.0)) + * prices[underlying_address] + for token, token_amount in self.debt.values.items() + if (underlying_address := debt_token_parameters[token].underlying_address) + in prices ) @abstractmethod