From 91d014a98dbae2daf7b71320fa95f437396e88b9 Mon Sep 17 00:00:00 2001 From: Isaac Adewumi Date: Wed, 16 Oct 2024 18:31:48 +0100 Subject: [PATCH] add valid conversion routes --- backend/bridgebloc/apps/conversions/urls.py | 2 ++ backend/bridgebloc/apps/conversions/views.py | 23 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/backend/bridgebloc/apps/conversions/urls.py b/backend/bridgebloc/apps/conversions/urls.py index 67c7137..6af150e 100644 --- a/backend/bridgebloc/apps/conversions/urls.py +++ b/backend/bridgebloc/apps/conversions/urls.py @@ -2,12 +2,14 @@ from .views import ( CCTPTokenConversionInitialisationAPIView, + ValidTokenConversionRoutesAPIView, TokenConversionAPIView, TokenConversionsAPIView, ) urlpatterns = [ path('conversions', TokenConversionsAPIView.as_view(), name='all-conversions-by-user'), + path('conversions/routes', ValidTokenConversionRoutesAPIView.as_view(), name='conversion-routes'), path('conversions/cctp', CCTPTokenConversionInitialisationAPIView.as_view(), name='bridge-with-cctp'), path('conversions/', TokenConversionAPIView.as_view(), name='get-conversion'), ] diff --git a/backend/bridgebloc/apps/conversions/views.py b/backend/bridgebloc/apps/conversions/views.py index 7cf82b5..462f643 100644 --- a/backend/bridgebloc/apps/conversions/views.py +++ b/backend/bridgebloc/apps/conversions/views.py @@ -1,3 +1,4 @@ +from collections import defaultdict from typing import Any from django.db import transaction @@ -7,6 +8,7 @@ from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView from rest_framework.request import Request from rest_framework.response import Response +from rest_framework.views import APIView from bridgebloc.apps.accounts.permissions import IsAuthenticated from bridgebloc.common.helpers import success_response @@ -17,6 +19,27 @@ from .permissions import IsOwner from .serializers import CCTPTokenConversionInitialisationSerializer, TokenConversionSerializer from .types import ConversionMethod +from ...evm.types import ChainID + + +class ValidTokenConversionRoutesAPIView(APIView): + def get(self, request: Request, *args: Any, **kwargs: Any) -> Response: # noqa: ARG002 + data: dict[str, Any] = defaultdict(lambda: defaultdict()) # pylint:disable=unnecessary-lambda + + mainnet_chains = [chain for chain in ChainID if not chain.name.endswith('TESTNET')] + testnet_chains = [chain for chain in ChainID if chain.name.endswith('TESTNET')] + + for source_chain in mainnet_chains: + for target_chain in mainnet_chains: + if source_chain != target_chain: + data[source_chain.name.lower()][target_chain.name.lower()] = ConversionMethod.CCTP + + for source_chain in testnet_chains: + for target_chain in testnet_chains: + if source_chain != target_chain: + data[source_chain.name.lower()][target_chain.name.lower()] = ConversionMethod.CCTP + + return success_response(data=data) class TokenConversionAPIView(RetrieveAPIView):