Skip to content

Commit

Permalink
fix: optimize collateral price check in compute_collateral_usd method
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRoudyk committed Dec 14, 2024
1 parent 3b3d240 commit dde76ef
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
4 changes: 4 additions & 0 deletions apps/legacy_app/src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
17 changes: 8 additions & 9 deletions apps/legacy_app/src/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions apps/legacy_app/src/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
20 changes: 14 additions & 6 deletions apps/shared/loan_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit dde76ef

Please sign in to comment.