Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint /tokens/lists #2345

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion safe_transaction_service/tokens/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"]
1 change: 1 addition & 0 deletions safe_transaction_service/tokens/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ class Meta:
model = models.TokenList

url = factory.Faker("url")
description = factory.Faker("company")
23 changes: 22 additions & 1 deletion safe_transaction_service/tokens/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)

Expand Down Expand Up @@ -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,
}
],
)
1 change: 1 addition & 0 deletions safe_transaction_service/tokens/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

urlpatterns = [
path("", views.TokensView.as_view(), name="list"),
path("lists/", views.TokenListsView.as_view(), name="token-lists"),
path("<str:address>/", views.TokenView.as_view(), name="detail"),
]
15 changes: 14 additions & 1 deletion safe_transaction_service/tokens/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from ..history.serializers import CodeErrorResponse
from . import filters, serializers
from .models import Token
from .models import Token, TokenList


@extend_schema(
Expand Down Expand Up @@ -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)