Skip to content

Commit

Permalink
add endpoint to add tx hash for circle deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
prettyirrelevant committed Aug 29, 2023
1 parent d5a9c1c commit 1473769
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 6 additions & 0 deletions backend/bridgebloc/apps/conversions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .views import (
CCTPTokenConversionInitialisationAPIView,
CircleAPITokenConversionInitialisationAPIView,
CircleTokenConversionDepositTxHashUpdateAPIView,
LxLyTokenConversionInitialisationAPIView,
TokenConversionAPIView,
TokenConversionsAPIView,
Expand All @@ -16,6 +17,11 @@
CircleAPITokenConversionInitialisationAPIView.as_view(),
name='bridge-with-circle-api',
),
path(
'conversions/circle-api/<str:uuid>/add-deposit-hash',
CircleTokenConversionDepositTxHashUpdateAPIView.as_view(),
name='add-deposit-tx-hash',
),
path('conversions/cctp', CCTPTokenConversionInitialisationAPIView.as_view(), name='bridge-with-cctp'),
path('conversions/lxly', LxLyTokenConversionInitialisationAPIView.as_view(), name='bridge-with-lxly'),
path('conversions/routes', ValidTokenConversionRoutesAPIView.as_view(), name='valid-routes'),
Expand Down
35 changes: 34 additions & 1 deletion backend/bridgebloc/apps/conversions/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from collections import defaultdict
from typing import Any

Expand All @@ -11,7 +12,7 @@
from rest_framework.views import APIView

from bridgebloc.apps.accounts.permissions import IsAuthenticated
from bridgebloc.common.helpers import success_response
from bridgebloc.common.helpers import error_response, success_response
from bridgebloc.common.types import AuthenticatedRequest

from .constants import VALID_CONVERSION_ROUTES
Expand Down Expand Up @@ -43,6 +44,38 @@ def get(self, request: Request, *args: Any, **kwargs: Any) -> Response: # noqa:
return success_response(data=data)


class CircleTokenConversionDepositTxHashUpdateAPIView(GenericAPIView):
queryset = TokenConversion.objects.select_related('creator').prefetch_related('conversion_steps')
permission_classes = (IsAuthenticated, IsOwner)
lookup_field = 'uuid'

def post(self, request: Request, *args: Any, **kwargs: Any) -> Response: # noqa: ARG002
obj = self.get_object()
step = obj.conversion_steps.filter(step_type=CircleAPIConversionStepType.CONFIRM_DEPOSIT).first()
if step is None:
return error_response(
errors=None,
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
message='Token conversion is not of Circle API type or deposit address has not been created',
)

tx_hash = request.data['tx_hash']

def is_transaction_valid(val: str) -> bool:
return bool(re.fullmatch('^0x[a-fA-F0-9]{64}', val))

if not is_transaction_valid(tx_hash):
return error_response(
errors=None,
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
message='Malformed transaction hash provided',
)

step.metadata['deposit_tx_hash'] = tx_hash
step.save()
return success_response(data=None)


class TokenConversionAPIView(RetrieveAPIView):
queryset = TokenConversion.objects.select_related('creator').prefetch_related('conversion_steps')
permission_classes = (IsAuthenticated, IsOwner)
Expand Down
3 changes: 1 addition & 2 deletions backend/bridgebloc/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from django.http import HttpRequest, JsonResponse

from rest_framework import status
from rest_framework import serializers
from rest_framework import serializers, status
from rest_framework.exceptions import APIException
from rest_framework.response import Response
from rest_framework.views import exception_handler
Expand Down

0 comments on commit 1473769

Please sign in to comment.