Skip to content

Commit

Permalink
optimized code and adjusted to the class structure
Browse files Browse the repository at this point in the history
  • Loading branch information
UzNaZ committed Oct 27, 2024
1 parent 5d295b9 commit eca9902
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions web_app/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,6 @@ class PositionDBConnector(UserDBConnector):
Provides database connection and operations management for the Position model.
"""

@staticmethod
def save_current_price() -> None:
"""
Saves current prices into db.
:return: None
"""
db = get_database()
price_dict = DashboardMixin.get_current_prices()
for token, price in price_dict.items():
if position := db.query(Position).filter_by(token_symbol=token).first():
position.start_price = price
db.commit()

START_PRICE = 0.0

@staticmethod
Expand Down Expand Up @@ -355,6 +342,7 @@ def open_position(self, position_id: uuid.UUID) -> str | None:
position.status = Status.OPENED.value
self.write_to_db(position)
self.create_empty_claim(position.user_id)
self.save_current_price(position)
return position.status
else:
logger.error(f"Position with ID {position_id} not found")
Expand Down Expand Up @@ -393,6 +381,19 @@ def get_total_amounts_for_open_positions(self) -> float | None:
logger.error(f"Error calculating total amount for open positions: {e}")
return None

def save_current_price(self, position: Position) -> None:
"""
Saves current prices into db.
:return: None
"""
price_dict = DashboardMixin.get_current_prices()
start_price = price_dict.get(position.token_symbol)
try:
position.start_price = start_price
self.write_to_db(position)
except SQLAlchemyError as e:
logger.error(f"Error while saving current_price for position: {e}")


class AirDropDBConnector(DBConnector):
"""
Expand Down

0 comments on commit eca9902

Please sign in to comment.