From 2c716638eeccb6cc69a84f3201860b1df7518e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20=C5=BBelazko?= Date: Tue, 19 Mar 2024 12:46:34 +0100 Subject: [PATCH] feat: automatically canonize user address arg in routers --- backend/app/infrastructure/__init__.py | 16 +++++++--- backend/app/infrastructure/routes/deposits.py | 29 ++++++++++--------- backend/app/infrastructure/routes/rewards.py | 8 ++--- backend/app/infrastructure/routes/user.py | 4 --- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/backend/app/infrastructure/__init__.py b/backend/app/infrastructure/__init__.py index 5e61c8c8e9..d205ec2d6d 100644 --- a/backend/app/infrastructure/__init__.py +++ b/backend/app/infrastructure/__init__.py @@ -22,7 +22,7 @@ def __init__(self, *args, **kwargs): Resource.__init__(self, *args, *kwargs) @classmethod - def canonize_address(cls, field_name: str = "user_address", force=True): + def canonize_address(cls, field_name: str, force=True): def _add_address_canonization(handler): def _decorated(*args, **kwargs): field_value = kwargs.get(field_name) @@ -36,14 +36,22 @@ def _decorated(*args, **kwargs): return _add_address_canonization - def __getattribute__(self, name): - user_address_canonizer = OctantResource.canonize_address(force=False) + @classmethod + def _default_address_canonizer(cls, attr): + user_address_canonizer = OctantResource.canonize_address( + field_name="user_address", force=False + ) + proposal_address_canonizer = OctantResource.canonize_address( + field_name="proposal_address", force=False + ) + return user_address_canonizer(proposal_address_canonizer(attr)) + def __getattribute__(self, name): attr = object.__getattribute__(self, name) decorator = default_decorators.get(name) if decorator is not None: - attr = user_address_canonizer(decorator(attr)) + attr = OctantResource._default_address_canonizer(decorator(attr)) return attr diff --git a/backend/app/infrastructure/routes/deposits.py b/backend/app/infrastructure/routes/deposits.py index 00e17dd050..87a2adfd70 100644 --- a/backend/app/infrastructure/routes/deposits.py +++ b/backend/app/infrastructure/routes/deposits.py @@ -1,4 +1,3 @@ -from eth_utils import to_checksum_address from flask import current_app as app from flask_restx import Namespace, fields @@ -92,44 +91,48 @@ def get(self, epoch): return {"lockedRatio": locked_ratio} -@ns.route("/users//") +@ns.route("/users//") @ns.doc( description="Returns user's effective deposit for a finialized or pending epoch.", params={ "epoch": "Epoch number", - "address": "User ethereum address in hexadecimal form (case-insensitive, prefixed with 0x)", + "user_address": "User ethereum address in hexadecimal form (case-insensitive, prefixed with 0x)", }, ) class UserEffectiveDeposit(OctantResource): @ns.marshal_with(user_effective_deposit_model) @ns.response(200, "User effective deposit successfully retrieved") - def get(self, address: str, epoch: int): - app.logger.debug(f"Getting user {address} effective deposit in epoch {epoch}") - result = get_user_effective_deposit(to_checksum_address(address), epoch) - app.logger.debug(f"User {address} effective deposit in epoch {epoch}: {result}") + def get(self, user_address: str, epoch: int): + app.logger.debug( + f"Getting user {user_address} effective deposit in epoch {epoch}" + ) + result = get_user_effective_deposit(user_address, epoch) + app.logger.debug( + f"User {user_address} effective deposit in epoch {epoch}: {result}" + ) return { "effectiveDeposit": result, } -@ns.route("/users//estimated_effective_deposit") +@ns.route("/users//estimated_effective_deposit") @ns.doc( description="Returns user's estimated effective deposit for the current epoch.", params={ - "address": "User ethereum address in hexadecimal form (case-insensitive, prefixed with 0x)", + "user_address": "User ethereum address in hexadecimal form (case-insensitive, prefixed with 0x)", }, ) class UserEstimatedEffectiveDeposit(OctantResource): @ns.marshal_with(user_effective_deposit_model) @ns.response(200, "User estimated effective deposit successfully retrieved") - def get(self, address: str): + def get(self, user_address: str): app.logger.debug( - f"Getting user {address} estimated effective deposit in the current epoch" + f"Getting user {user_address} estimated effective deposit in the current epoch" ) - result = estimate_user_effective_deposit(to_checksum_address(address)) + result = estimate_user_effective_deposit(user_address) app.logger.debug( - f"User {address} estimated effective deposit in the current epoch: {result}" + f"User {user_address} estimated effective deposit in the current epoch: {result}" ) return { diff --git a/backend/app/infrastructure/routes/rewards.py b/backend/app/infrastructure/routes/rewards.py index 06922a7cf3..7ee5aa887d 100644 --- a/backend/app/infrastructure/routes/rewards.py +++ b/backend/app/infrastructure/routes/rewards.py @@ -1,4 +1,3 @@ -from eth_utils import to_checksum_address from flask import current_app as app from flask_restx import Namespace, fields @@ -191,10 +190,9 @@ class UserBudget(OctantResource): @ns.marshal_with(user_budget_model) @ns.response(200, "Budget successfully retrieved") def get(self, user_address, epoch): - checksum_address = to_checksum_address(user_address) - app.logger.debug(f"Getting user {checksum_address} budget in epoch {epoch}") - budget = get_budget(checksum_address, epoch) - app.logger.debug(f"User {checksum_address} budget in epoch {epoch}: {budget}") + app.logger.debug(f"Getting user {user_address} budget in epoch {epoch}") + budget = get_budget(user_address, epoch) + app.logger.debug(f"User {user_address} budget in epoch {epoch}: {budget}") return {"budget": budget} diff --git a/backend/app/infrastructure/routes/user.py b/backend/app/infrastructure/routes/user.py index c6a59d0141..b5c88844ca 100644 --- a/backend/app/infrastructure/routes/user.py +++ b/backend/app/infrastructure/routes/user.py @@ -1,4 +1,3 @@ -from eth_utils import to_checksum_address from flask import current_app as app, request from flask_restx import Namespace, fields from flask_restx import reqparse @@ -126,8 +125,6 @@ class PatronMode(OctantResource): @ns.marshal_with(user_patron_mode_status_model) @ns.response(200, "User's patron mode status retrieved") def get(self, user_address: str): - user_address = to_checksum_address(user_address) - app.logger.debug(f"Getting user {user_address} patron mode status") patron_mode_status = user_controller.get_patron_mode_status(user_address) app.logger.debug( @@ -144,7 +141,6 @@ def get(self, user_address: str): @ns.response(204, "User's patron mode status updated.") @ns.response(400, "Could not update patron mode status.") def patch(self, user_address: str): - user_address = to_checksum_address(user_address) signature = ns.payload.get("signature") app.logger.info(f"Updating user {user_address} patron mode status")