diff --git a/prizetap/migrations/0042_alter_raffle_max_multiplier_and_more.py b/prizetap/migrations/0042_alter_raffle_max_multiplier_and_more.py new file mode 100644 index 00000000..1f46431f --- /dev/null +++ b/prizetap/migrations/0042_alter_raffle_max_multiplier_and_more.py @@ -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)]), + ), + ] diff --git a/prizetap/models.py b/prizetap/models.py index db96daab..585be4e6 100644 --- a/prizetap/models.py +++ b/prizetap/models.py @@ -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 _ @@ -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) diff --git a/prizetap/serializers.py b/prizetap/serializers.py index 74f1ff84..dc551eac 100644 --- a/prizetap/serializers.py +++ b/prizetap/serializers.py @@ -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 @@ -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 diff --git a/prizetap/tests.py b/prizetap/tests.py index b38d1a0e..89ab1788 100644 --- a/prizetap/tests.py +++ b/prizetap/tests.py @@ -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. @@ -41,17 +41,7 @@ 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", @@ -59,7 +49,7 @@ def setUp(self): ) 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", @@ -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", @@ -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", @@ -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)