diff --git a/safe_transaction_service/tokens/serializers.py b/safe_transaction_service/tokens/serializers.py index e69d722a9..32bcf0d81 100644 --- a/safe_transaction_service/tokens/serializers.py +++ b/safe_transaction_service/tokens/serializers.py @@ -3,7 +3,7 @@ from rest_framework import serializers from safe_eth.eth.django.serializers import EthereumAddressField -from .models import Token +from .models import Token, TokenList class TokenTransferInfoType(Enum): @@ -37,3 +37,9 @@ class TokenPriceResponseSerializer(serializers.Serializer): fiat_code = serializers.CharField() fiat_price = serializers.CharField() timestamp = serializers.DateTimeField() + + +class TokenListSerializer(serializers.ModelSerializer): + class Meta: + model = TokenList + fields = ["url", "description"] diff --git a/safe_transaction_service/tokens/tests/factories.py b/safe_transaction_service/tokens/tests/factories.py index 663de08be..a985c646e 100644 --- a/safe_transaction_service/tokens/tests/factories.py +++ b/safe_transaction_service/tokens/tests/factories.py @@ -24,3 +24,4 @@ class Meta: model = models.TokenList url = factory.Faker("url") + description = factory.Faker("company") diff --git a/safe_transaction_service/tokens/tests/test_views.py b/safe_transaction_service/tokens/tests/test_views.py index 855671222..6912dd1b5 100644 --- a/safe_transaction_service/tokens/tests/test_views.py +++ b/safe_transaction_service/tokens/tests/test_views.py @@ -2,6 +2,7 @@ from unittest import mock from unittest.mock import MagicMock +from django.core.cache import cache from django.urls import reverse from eth_account import Account @@ -12,7 +13,7 @@ from safe_eth.safe.tests.safe_test_case import SafeTestCaseMixin from ..models import Token -from .factories import TokenFactory +from .factories import TokenFactory, TokenListFactory logger = logging.getLogger(__name__) @@ -104,3 +105,23 @@ def test_tokens_view(self): } ], ) + + def test_token_lists_view(self): + response = self.client.get(reverse("v1:tokens:token-lists")) + self.assertEqual(response.data["results"], []) + token_list = TokenListFactory() + # Check cache + self.assertEqual(response.data["results"], []) + + cache.clear() + + response = self.client.get(reverse("v1:tokens:token-lists")) + self.assertEqual( + response.data["results"], + [ + { + "url": token_list.url, + "description": token_list.description, + } + ], + ) diff --git a/safe_transaction_service/tokens/urls.py b/safe_transaction_service/tokens/urls.py index 60e46f12e..7f9268de0 100644 --- a/safe_transaction_service/tokens/urls.py +++ b/safe_transaction_service/tokens/urls.py @@ -6,5 +6,6 @@ urlpatterns = [ path("", views.TokensView.as_view(), name="list"), + path("lists/", views.TokenListsView.as_view(), name="token-lists"), path("/", views.TokenView.as_view(), name="detail"), ] diff --git a/safe_transaction_service/tokens/views.py b/safe_transaction_service/tokens/views.py index ace3b6ac5..ad6b6f9ae 100644 --- a/safe_transaction_service/tokens/views.py +++ b/safe_transaction_service/tokens/views.py @@ -10,7 +10,7 @@ from ..history.serializers import CodeErrorResponse from . import filters, serializers -from .models import Token +from .models import Token, TokenList @extend_schema( @@ -64,3 +64,16 @@ def get(self, request, *args, **kwargs): Returns the list of tokens supported in the Safe Transaction Service """ return super().get(request, *args, **kwargs) + + +class TokenListsView(ListAPIView): + serializer_class = serializers.TokenListSerializer + ordering = ("pk",) + queryset = TokenList.objects.all() + + @method_decorator(cache_page(60 * 15)) # Cache 15 minutes + def get(self, request, *args, **kwargs): + """ + Returns the list of tokens supported in the Safe Transaction Service + """ + return super().get(request, *args, **kwargs)