Skip to content

Commit

Permalink
import OptimismClaimingGasConstraint to prizetap validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad Bastin committed Oct 14, 2023
1 parent 0f28db2 commit 6baf4f6
Showing 1 changed file with 29 additions and 46 deletions.
75 changes: 29 additions & 46 deletions prizetap/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
from authentication.models import UserProfile
from .models import RaffleEntry, Raffle
from .constraints import *
from faucet.constraints import OptimismClaimingGasConstraint


class RaffleEnrollmentValidator:
def __init__(self, *args, **kwargs):
self.user_profile: UserProfile = kwargs['user_profile']
self.raffle: Raffle = kwargs['raffle']
self.user_profile: UserProfile = kwargs["user_profile"]
self.raffle: Raffle = kwargs["raffle"]

def can_enroll_in_raffle(self):
if not self.raffle.is_claimable:
raise PermissionDenied(
"Can't enroll in this raffle"
)

raise PermissionDenied("Can't enroll in this raffle")

def check_user_constraints(self):
try:
param_values = json.loads(self.raffle.constraint_params)
Expand All @@ -29,13 +29,12 @@ def check_user_constraints(self):
except KeyError:
pass
if not constraint.is_observed():
raise PermissionDenied(
constraint.response
)
raise PermissionDenied(constraint.response)

def check_user_has_wallet(self):
if not self.user_profile.wallets.filter(
wallet_type=self.raffle.chain.chain_type).exists():
wallet_type=self.raffle.chain.chain_type
).exists():
raise PermissionDenied(
f"You have not connected an {self.raffle.chain.chain_type} wallet to your account"
)
Expand All @@ -47,85 +46,69 @@ def is_valid(self, data):

self.check_user_has_wallet()


class SetRaffleEntryTxValidator:

def __init__(self, *args, **kwargs):
self.user_profile: UserProfile = kwargs['user_profile']
self.raffle_entry: RaffleEntry = kwargs['raffle_entry']
self.user_profile: UserProfile = kwargs["user_profile"]
self.raffle_entry: RaffleEntry = kwargs["raffle_entry"]

def is_owner_of_raffle_entry(self):
if not self.raffle_entry.user_profile == self.user_profile:
raise PermissionDenied(
"You don't have permission to update this raffle entry"
)

def is_tx_empty(self):
if self.raffle_entry.tx_hash:
raise PermissionDenied(
"This raffle entry is already updated"
)
raise PermissionDenied("This raffle entry is already updated")

def is_valid(self, data):
self.is_owner_of_raffle_entry()
self.is_tx_empty()

tx_hash = data.get("tx_hash", None)
if not tx_hash or len(tx_hash) != 66:
raise PermissionDenied(
"Tx hash is not valid"
)
raise PermissionDenied("Tx hash is not valid")


class SetClaimingPrizeTxValidator:

def __init__(self, *args, **kwargs):
self.raffle_entry: RaffleEntry = kwargs['raffle_entry']
self.raffle_entry: RaffleEntry = kwargs["raffle_entry"]

def is_winner(self):
if not self.raffle_entry.is_winner:
raise PermissionDenied(
"You are not the raffle winner"
)

raise PermissionDenied("You are not the raffle winner")

def is_tx_empty(self):
if self.raffle_entry.claiming_prize_tx:
raise PermissionDenied(
"The tx_hash is already set"
)
raise PermissionDenied("The tx_hash is already set")

def is_valid(self, data):
self.is_winner()
self.is_tx_empty()

tx_hash = data.get("tx_hash", None)
if not tx_hash or len(tx_hash) != 66:
raise PermissionDenied(
"Tx hash is not valid"
)
raise PermissionDenied("Tx hash is not valid")


class SetRaffleTxValidator:

def __init__(self, *args, **kwargs):
self.user_profile: UserProfile = kwargs['user_profile']
self.raffle: Raffle = kwargs['raffle']
self.user_profile: UserProfile = kwargs["user_profile"]
self.raffle: Raffle = kwargs["raffle"]

def is_owner_of_raffle(self):
if not self.raffle.creator_profile == self.user_profile:
raise PermissionDenied(
"You don't have permission to update this raffle"
)

raise PermissionDenied("You don't have permission to update this raffle")

def is_tx_empty(self):
if self.raffle.tx_hash:
raise PermissionDenied(
"This raffle is already updated"
)
raise PermissionDenied("This raffle is already updated")

def is_valid(self, data):
self.is_owner_of_raffle()
self.is_tx_empty()

tx_hash = data.get("tx_hash", None)
if not tx_hash or len(tx_hash) != 66:
raise PermissionDenied(
"Tx hash is not valid"
)
raise PermissionDenied("Tx hash is not valid")

0 comments on commit 6baf4f6

Please sign in to comment.