From 1b1cc3afba6c476b58aaae5b0a8c3f6d363babda Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Thu, 9 Nov 2023 11:43:36 +0330 Subject: [PATCH 1/4] Prizetap: update provider dashboard depending on latest contracts changes --- .../0038_rename_nft_id_raffle_nft_ids.py | 18 ++++++++++++++++++ .../migrations/0039_alter_raffle_nft_ids.py | 18 ++++++++++++++++++ prizetap/models.py | 2 +- prizetap/serializers.py | 5 ++++- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 prizetap/migrations/0038_rename_nft_id_raffle_nft_ids.py create mode 100644 prizetap/migrations/0039_alter_raffle_nft_ids.py diff --git a/prizetap/migrations/0038_rename_nft_id_raffle_nft_ids.py b/prizetap/migrations/0038_rename_nft_id_raffle_nft_ids.py new file mode 100644 index 00000000..884946c9 --- /dev/null +++ b/prizetap/migrations/0038_rename_nft_id_raffle_nft_ids.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2023-11-09 07:02 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('prizetap', '0037_alter_raffle_status'), + ] + + operations = [ + migrations.RenameField( + model_name='raffle', + old_name='nft_id', + new_name='nft_ids', + ), + ] diff --git a/prizetap/migrations/0039_alter_raffle_nft_ids.py b/prizetap/migrations/0039_alter_raffle_nft_ids.py new file mode 100644 index 00000000..98100d0d --- /dev/null +++ b/prizetap/migrations/0039_alter_raffle_nft_ids.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2023-11-09 07:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('prizetap', '0038_rename_nft_id_raffle_nft_ids'), + ] + + operations = [ + migrations.AlterField( + model_name='raffle', + name='nft_ids', + field=models.TextField(blank=True, null=True), + ), + ] diff --git a/prizetap/models.py b/prizetap/models.py index 3b8cc83a..913ea732 100644 --- a/prizetap/models.py +++ b/prizetap/models.py @@ -53,7 +53,7 @@ class Meta: decimals = models.IntegerField(default=18) is_prize_nft = models.BooleanField(default=False) - nft_id = models.CharField(max_length=256, null=True, blank=True) + nft_ids = models.TextField(null=True, blank=True) token_uri = models.TextField(null=True, blank=True) chain = models.ForeignKey(Chain, on_delete=models.CASCADE, related_name="raffles") diff --git a/prizetap/serializers.py b/prizetap/serializers.py index 6136eb61..f0b11e21 100644 --- a/prizetap/serializers.py +++ b/prizetap/serializers.py @@ -121,6 +121,8 @@ 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"]: + raise serializers.ValidationError({"winners_count": "Invalid value"}) data["creator_profile"] = self.context["user_profile"] return data @@ -151,7 +153,7 @@ class Meta: "prize_symbol", "decimals", "is_prize_nft", - "nft_id", + "nft_ids", "token_uri", "chain", "contract", @@ -173,6 +175,7 @@ class Meta: "number_of_entries", "number_of_onchain_entries", "max_multiplier", + "winners_count", ] def get_user_entry(self, raffle: Raffle): From 03347fb94332271bea468a7fed1afd5067a21a9b Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Sat, 11 Nov 2023 19:38:44 +0330 Subject: [PATCH 2/4] fix migration conflicts --- ...d_raffle_nft_ids.py => 0039_rename_nft_id_raffle_nft_ids.py} | 2 +- ...039_alter_raffle_nft_ids.py => 0040_alter_raffle_nft_ids.py} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename prizetap/migrations/{0038_rename_nft_id_raffle_nft_ids.py => 0039_rename_nft_id_raffle_nft_ids.py} (85%) rename prizetap/migrations/{0039_alter_raffle_nft_ids.py => 0040_alter_raffle_nft_ids.py} (85%) diff --git a/prizetap/migrations/0038_rename_nft_id_raffle_nft_ids.py b/prizetap/migrations/0039_rename_nft_id_raffle_nft_ids.py similarity index 85% rename from prizetap/migrations/0038_rename_nft_id_raffle_nft_ids.py rename to prizetap/migrations/0039_rename_nft_id_raffle_nft_ids.py index 884946c9..46da3485 100644 --- a/prizetap/migrations/0038_rename_nft_id_raffle_nft_ids.py +++ b/prizetap/migrations/0039_rename_nft_id_raffle_nft_ids.py @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ('prizetap', '0037_alter_raffle_status'), + ('prizetap', '0038_alter_constraint_name'), ] operations = [ diff --git a/prizetap/migrations/0039_alter_raffle_nft_ids.py b/prizetap/migrations/0040_alter_raffle_nft_ids.py similarity index 85% rename from prizetap/migrations/0039_alter_raffle_nft_ids.py rename to prizetap/migrations/0040_alter_raffle_nft_ids.py index 98100d0d..6dcc7973 100644 --- a/prizetap/migrations/0039_alter_raffle_nft_ids.py +++ b/prizetap/migrations/0040_alter_raffle_nft_ids.py @@ -6,7 +6,7 @@ class Migration(migrations.Migration): dependencies = [ - ('prizetap', '0038_rename_nft_id_raffle_nft_ids'), + ('prizetap', '0039_rename_nft_id_raffle_nft_ids'), ] operations = [ From b498f06352382a584cfe097dae5816e227c48402 Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Sun, 12 Nov 2023 11:41:30 +0330 Subject: [PATCH 3/4] Prizetap: set raffleId --- brightIDfaucet/celery.py | 1 + prizetap/tasks.py | 68 ++++++++++++++++++++++++++++++++++++++++ prizetap/utils.py | 12 ++++++- 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/brightIDfaucet/celery.py b/brightIDfaucet/celery.py index bfa25893..e1cf7a43 100644 --- a/brightIDfaucet/celery.py +++ b/brightIDfaucet/celery.py @@ -53,6 +53,7 @@ "set-raffle-random-words": {"task": "prizetap.tasks.set_raffle_random_words", "schedule": 120}, "set-raffle-winners": {"task": "prizetap.tasks.set_raffle_winners", "schedule": 300}, "get-raffle-winners": {"task": "prizetap.tasks.get_raffle_winners", "schedule": 300}, + "set-raffle-ids": {"task": "prizetap.tasks.set_raffle_ids", "schedule": 300}, } # Load task modules from all registered Django apps. diff --git a/prizetap/tasks.py b/prizetap/tasks.py index bfdc1d57..53bcbbfd 100644 --- a/prizetap/tasks.py +++ b/prizetap/tasks.py @@ -1,3 +1,4 @@ +import logging import time import requests @@ -149,3 +150,70 @@ def request_random_words(raffle: Raffle): if tx_hash: raffle.vrf_tx_hash = tx_hash raffle.save() + + +@shared_task(bind=True) +def set_raffle_ids(self): + id = f"{self.name}-LOCK" + + with memcache_lock(id, self.app.oid) as acquired: + if not acquired: + print(f"Could not acquire process lock at {self.name}") + return + raffles_queryset = ( + Raffle.objects.filter(status=Raffle.Status.PENDING).filter(raffleId__isnull=True).order_by("id") + ) + if raffles_queryset.count() > 0: + for raffle in raffles_queryset: + contract_client = PrizetapContractClient(raffle) + + receipt = contract_client.get_transaction_receipt(raffle.tx_hash) + raffle_created_log = receipt["logs"][1] + log = contract_client.contract.events.RaffleCreated().process_log(raffle_created_log) + + raffle.raffleId = log["args"]["raffleId"] + onchain_raffle = contract_client.get_raffle() + is_valid = True + if onchain_raffle["status"] != 0: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} status") + if onchain_raffle["lastParticipantIndex"] != 0: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} lastParticipantIndex") + if onchain_raffle["lastWinnerIndex"] != 0: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} lastWinnerIndex") + if onchain_raffle["participantsCount"] != 0: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} participantsCount") + if raffle.creator_address != onchain_raffle["initiator"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} initiator") + if raffle.max_number_of_entries != onchain_raffle["maxParticipants"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} maxParticipants") + if raffle.max_multiplier != onchain_raffle["maxMultiplier"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} maxMultiplier") + if raffle.start_at != onchain_raffle["startTime"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} startTime") + if raffle.deadline != onchain_raffle["endTime"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} endTime") + if raffle.winners_count != onchain_raffle["winnersCount"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} winnersCount") + if raffle.is_prize_nft: + if raffle.prize_asset != onchain_raffle["collection"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} collection") + else: + if raffle.prize_amount != onchain_raffle["prizeAmount"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} prizeAmount") + if raffle.prize_asset != onchain_raffle["currency"]: + is_valid = False + logging.error(f"Mismatch raffle {raffle.raffleId} currency") + if is_valid: + raffle.save() diff --git a/prizetap/utils.py b/prizetap/utils.py index 6c8b272b..a7946a85 100644 --- a/prizetap/utils.py +++ b/prizetap/utils.py @@ -30,7 +30,8 @@ def set_raffle_random_words(self, expiration_time, random_words, reqId, muon_sig def get_raffle(self): func = self.contract.functions.raffles(self.raffle.raffleId) - return self.contract_call(func) + output = self.contract_call(func) + return self.__process_raffle(output) def get_last_winner_index(self): raffle = self.get_raffle() @@ -59,6 +60,15 @@ def get_raffle_winners_count(self): func = self.contract.functions.getWinnersCount(self.raffle.raffleId) return self.contract_call(func) + def __process_raffle(self, output): + raffles_abi = [item for item in self.contract.abi if item.get("name") == "raffles"] + assert len(raffles_abi) == 1, "The raffles abi not found" + raffles_abi = raffles_abi[0] + result = {} + for index, item in enumerate(raffles_abi["outputs"]): + result[item["name"]] = output[index] + return result + class VRFClientContractClient(Web3Utils): def __init__(self, chain) -> None: From 480b549eeb895aaf2dca1b504639896a9ba79cf0 Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Sun, 12 Nov 2023 11:47:21 +0330 Subject: [PATCH 4/4] Prizetap: refactor get_last_winner_index --- prizetap/utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/prizetap/utils.py b/prizetap/utils.py index a7946a85..8f905f9c 100644 --- a/prizetap/utils.py +++ b/prizetap/utils.py @@ -35,8 +35,7 @@ def get_raffle(self): def get_last_winner_index(self): raffle = self.get_raffle() - last_index = raffle[8] if not self.raffle.is_prize_nft else raffle[7] - return last_index + return raffle["lastWinnerIndex"] def set_winners(self): winners_count = self.raffle.winners_count