Skip to content
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
17 changes: 17 additions & 0 deletions core/migrations/0003_listing_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2025-04-28 11:26

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0002_acceptednft_acceptedtoken_listing_loan_and_more"),
]

operations = [
migrations.AddField(
model_name="listing",
name="status",
field=models.IntegerField(choices=[(1, "OPEN"), (2, "CLOSED")], default=1),
),
]
8 changes: 8 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class LoanRenegotiationStatus(models.IntegerChoices):
ACCEPTED = 3, "Accepted"


class ListingStatus(models.IntegerChoices):
OPEN = 1, "OPEN"
CLOSED = 2, "CLOSED"


# MANAGERS


Expand Down Expand Up @@ -218,6 +223,9 @@ class Listing(BaseModel):
_("Repayment Amount"), null=True, blank=True
)
duration = models.PositiveIntegerField(_("Loan Duration"), null=True, blank=True)
status = models.IntegerField(
choices=ListingStatus.choices, default=ListingStatus.OPEN
)

def __str__(self) -> str:
return f"Token: {self.token_contract_address}, NFT: {self.nft_contract_address}"
Expand Down
102 changes: 102 additions & 0 deletions core/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from rest_framework import exceptions as rest_exceptions
from rest_framework import serializers

Expand Down Expand Up @@ -117,3 +118,104 @@ def save(self) -> dict:
data["is_new"] = is_new
token_info = CoreService.generate_auth_token_data(user)
return {**data, **token_info}


class OfferSerializer(serializers.Serializer):
class Meta:
model = models.Offer
fields = "__all__"


class SimpleOfferSerializer(serializers.Serializer):
class Meta:
model = models.Offer
exclude = [
"signature",
"signature_expiry",
"signature_chain_id",
"signature_unique_id",
]


class MakeOfferSerializer(serializers.Serializer):
listing = serializers.UUIDField()
principal = serializers.IntegerField(min_value=1)
repayment_amount = serializers.IntegerField(min_value=1)
collateral_contract = serializers.CharField()
collateral_id = serializers.IntegerField(min_value=1)
token_contract = serializers.CharField()
loan_duration = serializers.IntegerField(
min_value=settings.MIN_LOAN_DURATION, max_value=settings.MAX_LOAN_DURATION
)
expiry = serializers.IntegerField()
chain_id = serializers.CharField()
unique_id = serializers.IntegerField()
signatures = serializers.ListField(child=serializers.CharField())

def validate(self, attrs):
"""
Validate the request data
"""
# Listing Validation
try:
listing = models.Listing.objects.get(id=attrs["listing"])
except models.Listing.DoesNotExist:
raise serializers.ValidationError({"detail": "Listing does not exist"})
if listing.status != models.ListingStatus.OPEN:
raise serializers.ValidationError({"detail": "Listing is not active"})
# Collateral Contract Validation
if listing.nft_contract_address != attrs["collateral_contract"]:
raise serializers.ValidationError({"detail": "Invalid listing collateral"})

# Principal and Repayment Validation
if attrs["principal"] > attrs["repayment_amount"]:
raise serializers.ValidationError(
{"detail": "Principal should be less tha Repayment amount"}
)

# validate offer token
if not models.AcceptedToken.objects.filter(
contract_address=attrs["token_contract"]
).exists():
raise serializers.ValidationError({"detail": "Token not supported"})

# verify the signature
data = {
"principal": attrs["prinicipal"],
"repayment_amount": attrs["repayment_amount"],
"collateral_contract": attrs["collateral_contract"],
"collateral_id": attrs["collateral_id"],
"token_contract": attrs["token_contract"],
"loan_duration": attrs["loan_duration"],
"expiry": attrs["expiry"],
"chain_id": attrs["chain_id"],
"unique_id": attrs["unique_id"],
}
user = self.context["user"]

check = CoreService.validate_loan_offer_request(data, attrs["signatures"], user)
if not check:
raise serializers.ValidationError({"detail": "Invalid signature message"})

# set the necessary contexts
self.context["listing"] = listing

return attrs

def save(self, **kwargs):
user = self.context["user"]
listing = self.context["listing"]
offer = CoreService.create_offer(
user,
listing,
self.validated_data["principal"],
self.validated_data["repayment_amount"],
self.validated_data["duration"],
self.validated_data["signature"],
self.validated_data["expiry"],
self.validated_data["chain_id"],
self.validated_data["unique_id"],
)
data = OfferSerializer(offer).data

return data
55 changes: 54 additions & 1 deletion core/service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from rest_framework_simplejwt.tokens import RefreshToken
from starknet_py.utils.typed_data import TypedData

Expand All @@ -23,14 +25,32 @@ def generate_auth_token_data(user: models.User) -> dict:
}
return token_data

@classmethod
def validate_loan_offer_request(
cls, data: dict, signatures: list[str], user: models.User
):
"""
Validate the signed data that verifies the offer create functionality.
The data is signed by the user's wallet and it's components
are sent for verification
Args:
data(dict): the data required to complete the signature request
signatures(list[str]): the signatures of the message
user: The user that is signing the message,

"""
request_format = SignatureUtils.offer_typed_data_format()
typed_data = SignatureUtils.generate_signature_typed_data(data, request_format)
return SignatureUtils.verify_signatures(typed_data, signatures, user.public_key)

@classmethod
def validate_login_request(
cls, signatures: list[str], public_key: list[str]
) -> bool:
"""
Validate the Signed data that verifies the login of the user.
The data is signed by the user's wallet and it's components
are sent for verifiction
are sent for verification
Args:
signatures(list[str]): the signatures of the message
public_key: The public key of the signer.
Expand All @@ -55,3 +75,36 @@ def login_or_register_user(cls, public_key: str) -> tuple[models.User, bool]:
"""
user, created = models.User.objects.get_or_create(public_key=public_key)
return user, created

@classmethod
def create_offer(
cls,
user: models.User,
listing: models.Listing,
principal: int,
repayment_amount: int,
duration: int,
signature: list[str],
signature_expiry: int,
signature_chain_id: int,
signature_unique_id: int,
) -> models.Offer:
"""
Create an Offer with all the required data.
convert the signature to string json and save it as string in the db
Returns:
models.Offer: the newly created offer instance
"""
offer = models.Offer()
offer.user = user
offer.listing = listing
offer.borrow_amount = principal
offer.repayment_amount = repayment_amount
offer.duration = duration
# convert signature to json string and save as string to db
offer.signature = json.dumps(signature)
offer.signature_expiry = signature_expiry
offer.signature_chain_id = signature_chain_id
offer.signature_unique_id = signature_unique_id
offer.save()
return offer
1 change: 1 addition & 0 deletions core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
views.SignInAPIView.as_view(),
name="signin",
),
path("offer/create/", views.OfferCreateAPIView.as_view(), name="create-offer"),
]
40 changes: 40 additions & 0 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ def login_typed_data_format(cls) -> dict:
}
return data.copy()

@classmethod
def offer_typed_data_format(cls) -> dict:
"""
This represents the signature request of the offer operation.

Returns:
dict: The signature request structure of the offer functionality.
"""
data = {
"domain": Domain(
**{
"name": DOMAIN_NAME,
"chain_id": CHAIN_ID,
"version": VERSION,
}
),
"types": {
"StarknetDomain": [
Parameter(**{"name": "name", "type": "felt"}),
Parameter(**{"name": "chainId", "type": "felt"}),
Parameter(**{"name": "version", "type": "felt"}),
],
"Message": [
Parameter(**{"name": "principal", "type": "felt"}),
Parameter(**{"name": "repayment_amount", "type": "felt"}),
Parameter(**{"name": "collateral_contract", "type": "felt"}),
Parameter(**{"name": "collateral_id", "type": "felt"}),
Parameter(**{"name": "token_contract", "type": "felt"}),
Parameter(**{"name": "loan_duration", "type": "felt"}),
Parameter(**{"name": "lender", "type": "felt"}),
Parameter(**{"name": "expiry", "type": "felt"}),
Parameter(**{"name": "chain_id", "type": "felt"}),
Parameter(**{"name": "unique_id", "type": "felt"}),
],
},
"primary_type": "Message",
"message": {},
}
return data.copy()

@classmethod
def generate_signature_typed_data(cls, data: dict, type_format: dict) -> TypedData:
"""
Expand Down
13 changes: 13 additions & 0 deletions core/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from rest_framework import serializers


def is_positive_number(value: str):
"""
Validate that the value is a positive integer
"""
try:
val = int(value)
if val <= 0:
raise ValueError
except Exception:
raise serializers.ValidationError("This field must be a positive integer")
14 changes: 14 additions & 0 deletions core/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Create your views here.
from rest_framework import status
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from . import models, serializers
Expand All @@ -24,3 +26,15 @@ def post(self, request):
data = serializer.save()

return Response(data)


class OfferCreateAPIView(GenericAPIView):
serializer_class = serializers.MakeOfferSerializer
permission_classes = [IsAuthenticated]

def post(self, request):
user = request.user
serializer = self.serializer_class(data=request.data, context={"user": user})
serializer.is_valid(raise_exception=True)
data = serializer.save()
return Response(data, status=status.HTTP_201_CREATED)
Loading