Skip to content

Commit

Permalink
Merge pull request #190 from UnitapApp/fix/prizetap-provider-dashboard
Browse files Browse the repository at this point in the history
Prizetap provider dashboard: more validations
  • Loading branch information
ShayanShiravani authored Nov 20, 2023
2 parents 0aa9536 + 4388613 commit 1997cf8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
29 changes: 29 additions & 0 deletions prizetap/migrations/0042_alter_raffle_max_multiplier_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.0.4 on 2023-11-20 14:36

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('prizetap', '0041_raffle_email_url_raffle_necessary_information_and_more'),
]

operations = [
migrations.AlterField(
model_name='raffle',
name='max_multiplier',
field=models.IntegerField(default=1, validators=[django.core.validators.MinValueValidator(1)]),
),
migrations.AlterField(
model_name='raffle',
name='max_number_of_entries',
field=models.IntegerField(validators=[django.core.validators.MinValueValidator(1)]),
),
migrations.AlterField(
model_name='raffle',
name='winners_count',
field=models.IntegerField(default=1, validators=[django.core.validators.MinValueValidator(1)]),
),
]
7 changes: 4 additions & 3 deletions prizetap/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.validators import MinValueValidator
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -67,9 +68,9 @@ class Meta:
created_at = models.DateTimeField(auto_now_add=True, editable=True)
start_at = models.DateTimeField(default=timezone.now)
deadline = models.DateTimeField()
max_number_of_entries = models.IntegerField()
max_multiplier = models.IntegerField(default=1)
winners_count = models.IntegerField(default=1)
max_number_of_entries = models.IntegerField(validators=[MinValueValidator(1)])
max_multiplier = models.IntegerField(default=1, validators=[MinValueValidator(1)])
winners_count = models.IntegerField(default=1, validators=[MinValueValidator(1)])

status = models.CharField(max_length=10, choices=Status.choices, default=Status.PENDING)
rejection_reason = models.TextField(null=True, blank=True)
Expand Down
10 changes: 9 additions & 1 deletion prizetap/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from core.serializers import UserConstraintBaseSerializer
from faucet.serializers import SmallChainSerializer

from .constants import CONTRACT_ADDRESSES
from .models import Constraint, LineaRaffleEntries, Raffle, RaffleEntry, UserConstraint


Expand Down Expand Up @@ -120,8 +121,15 @@ def validate(self, data):
constraint_class.is_valid_param_keys(constraint_params[c.name])
except KeyError as e:
raise serializers.ValidationError({"constraint_params": [{f"{c.name}": str(e)}]})
if data["winners_count"] > data["max_number_of_entries"]:
if "winners_count" in data and data["winners_count"] > data["max_number_of_entries"]:
raise serializers.ValidationError({"winners_count": "Invalid value"})
valid_chains = list(CONTRACT_ADDRESSES.keys())
chain_id = data["chain"].chain_id
if chain_id not in valid_chains:
raise serializers.ValidationError({"chain": "Invalid value"})
valid_contracts = list(CONTRACT_ADDRESSES[chain_id].values())
if data["contract"] not in valid_contracts:
raise serializers.ValidationError({"contract": "Invalid value"})
data["creator_profile"] = self.context["user_profile"]
return data

Expand Down
35 changes: 12 additions & 23 deletions prizetap/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
test_wallet_key = "f57fecd11c6034fd2665d622e866f05f9b07f35f253ebd5563e3d7e76ae66809"
test_rpc_url_private = "https://rpc.ankr.com/eth_sepolia"
fund_manager = "0x5802f1035AbB8B191bc12Ce4668E3815e8B7Efa0"
erc20_contract_address = "0x5363502325735d7b27162b2b3482c107fD4c5B3C"
erc721_contract_address = "0x334ab41d0F93d1d61178a21CD7A71387e5c75688"
erc20_contract_address = "0x5AD9BAf388E6E4F7c40652e21545F700C2104FF0"
erc721_contract_address = "0x9E5c0d8a54D93956f26935447BBADd629f13a0dE"


# # Create your tests here.
Expand All @@ -41,25 +41,15 @@ def setUp(self):
private_key=test_wallet_key,
network_type=NetworkTypes.EVM,
)
self.chain = Chain.objects.create(
chain_name="Sepolia",
wallet=self.wallet,
rpc_url_private=test_rpc_url_private,
explorer_url="https://sepolia.etherscan.io/",
fund_manager_address=fund_manager,
native_currency_name="ETH",
symbol="ETH",
chain_id="11155111",
max_claim_amount=1e11,
)
self.chain = self.create_mumbai_chain()
self.meet_constraint = Constraint.objects.create(
name="core.BrightIDMeetVerification",
title="BrightID meet",
description="You have to be BrightID verified.",
)

def create_polygon_chain(self):
Chain.objects.create(
return Chain.objects.create(
chain_name="Polygon",
wallet=self.wallet,
rpc_url_private="https://rpc.ankr.com/polygon",
Expand All @@ -72,7 +62,7 @@ def create_polygon_chain(self):
)

def create_mumbai_chain(self):
Chain.objects.create(
return Chain.objects.create(
chain_name="Mumbai",
wallet=self.wallet,
rpc_url_private="https://rpc.ankr.com/polygon_mumbai",
Expand Down Expand Up @@ -168,7 +158,7 @@ def setUp(self) -> None:
self.raffle_data = {
"name": "test_create_raffle_api",
"description": "A test raffle",
"contract": "0x5363502325735d7b27162b2b3482c107fD4c5B3C",
"contract": erc20_contract_address,
"creator_name": "unitap",
"creator_address": self.wallet.address,
"twitter_url": "https://twitter.com/unitap_app",
Expand Down Expand Up @@ -304,16 +294,15 @@ def test_set_invalid_raffle_tx(self):

def test_get_valid_chains(self):
self.create_polygon_chain()
self.create_mumbai_chain()
response = self.client.get(reverse("get-valid-chains"))
self.assertEqual(response.status_code, 200)
data = response.data["data"]
self.assertEqual(data[0]["chain_id"], "137")
self.assertEqual(data[0]["erc20_prizetap_addr"], "0xB521C36F76d28Edb287346C9D649Fa1A60754f04")
self.assertEqual(data[0]["erc721_prizetap_addr"], "0xb68D3f2946Bf477978c68b509FD9f85E9e20F869")
self.assertEqual(data[1]["chain_id"], "80001")
self.assertEqual(data[1]["erc20_prizetap_addr"], "0x5AD9BAf388E6E4F7c40652e21545F700C2104FF0")
self.assertEqual(data[1]["erc721_prizetap_addr"], "0x9E5c0d8a54D93956f26935447BBADd629f13a0dE")
self.assertEqual(data[0]["chain_id"], "80001")
self.assertEqual(data[0]["erc20_prizetap_addr"], "0x5AD9BAf388E6E4F7c40652e21545F700C2104FF0")
self.assertEqual(data[0]["erc721_prizetap_addr"], "0x9E5c0d8a54D93956f26935447BBADd629f13a0dE")
self.assertEqual(data[1]["chain_id"], "137")
self.assertEqual(data[1]["erc20_prizetap_addr"], "0xB521C36F76d28Edb287346C9D649Fa1A60754f04")
self.assertEqual(data[1]["erc721_prizetap_addr"], "0xb68D3f2946Bf477978c68b509FD9f85E9e20F869")

def test_get_user_raffles(self):
self.client.force_authenticate(user=self.user_profile.user)
Expand Down

0 comments on commit 1997cf8

Please sign in to comment.