Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/relocate chain configs #213

Merged
merged 3 commits into from
Dec 9, 2023
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
33 changes: 31 additions & 2 deletions core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@

from core.constraints import ConstraintVerification, get_constraint

from .models import UserConstraint
from .models import Chain, UserConstraint


class ChainSerializer(serializers.ModelSerializer):
class Meta:
model = Chain
fields = [
"pk",
"chain_name",
"chain_id",
"native_currency_name",
"symbol",
"decimals",
"explorer_url",
"rpc_url",
"logo_url",
"modal_url",
"is_testnet",
"chain_type",
]


class UserConstraintBaseSerializer(serializers.Serializer):
Expand All @@ -17,7 +36,17 @@ class UserConstraintBaseSerializer(serializers.Serializer):
params = serializers.SerializerMethodField()

class Meta:
fields = ["pk", "name", "title", "type", "description", "explanation", "response", "icon_url", "params"]
fields = [
"pk",
"name",
"title",
"type",
"description",
"explanation",
"response",
"icon_url",
"params",
]
read_only_fields = [
"pk",
"name",
Expand Down
9 changes: 6 additions & 3 deletions prizetap/constraints.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from core.constraints import *
from faucet.models import Chain
from authentication.models import UserProfile
from core.constraints import ConstraintVerification
from core.models import Chain

from .utils import UnitapPassClient


Expand All @@ -11,7 +13,8 @@ def is_observed(self, *args, **kwargs):
chain = Chain.objects.get(chain_id=1)
self.unitappass_client = UnitapPassClient(chain)
user_address: str = self.user_profile.wallets.get(
wallet_type=chain.chain_type).address
wallet_type=chain.chain_type
).address
user_address = self.unitappass_client.w3.to_checksum_address(
user_address.lower()
)
Expand Down
44 changes: 33 additions & 11 deletions prizetap/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

from authentication.serializers import SimpleProfilerSerializer
from core.constraints import ConstraintVerification, get_constraint
from core.serializers import UserConstraintBaseSerializer
from faucet.serializers import SmallChainSerializer
from core.serializers import ChainSerializer, UserConstraintBaseSerializer

from .constants import CONTRACT_ADDRESSES
from .models import Constraint, LineaRaffleEntries, Raffle, RaffleEntry, UserConstraint
Expand Down Expand Up @@ -72,7 +71,9 @@ def get_chain(self, entry: RaffleEntry):
return entry.raffle.chain.chain_id

def get_wallet(self, entry: RaffleEntry):
return entry.user_profile.wallets.get(wallet_type=entry.raffle.chain.chain_type).address
return entry.user_profile.wallets.get(
wallet_type=entry.raffle.chain.chain_type
).address


class WinnerEntrySerializer(serializers.ModelSerializer):
Expand All @@ -81,7 +82,15 @@ class WinnerEntrySerializer(serializers.ModelSerializer):

class Meta:
model = RaffleEntry
fields = ["pk", "user_profile", "wallet", "created_at", "multiplier", "tx_hash", "claiming_prize_tx"]
fields = [
"pk",
"user_profile",
"wallet",
"created_at",
"multiplier",
"tx_hash",
"claiming_prize_tx",
]
read_only_fields = [
"pk",
"user_profile",
Expand All @@ -91,7 +100,9 @@ class Meta:
]

def get_wallet(self, entry: RaffleEntry):
return entry.user_profile.wallets.get(wallet_type=entry.raffle.chain.chain_type).address
return entry.user_profile.wallets.get(
wallet_type=entry.raffle.chain.chain_type
).address


class CreateRaffleSerializer(serializers.ModelSerializer):
Expand All @@ -112,7 +123,9 @@ class Meta:
def validate(self, data):
constraints = data["constraints"]
constraint_params = json.loads(base64.b64decode(data["constraint_params"]))
data["constraint_params"] = base64.b64decode(data["constraint_params"]).decode("utf-8")
data["constraint_params"] = base64.b64decode(data["constraint_params"]).decode(
"utf-8"
)
reversed_constraints = []
if "reversed_constraints" in data:
reversed_constraints = str(data["reversed_constraints"]).split(",")
Expand All @@ -123,13 +136,20 @@ def validate(self, data):
if len(constraint_class.param_keys()) != 0:
constraint_class.is_valid_param_keys(constraint_params[c.name])
except KeyError as e:
raise serializers.ValidationError({"constraint_params": [{f"{c.name}": str(e)}]})
raise serializers.ValidationError(
{"constraint_params": [{f"{c.name}": str(e)}]}
)
valid_constraints = [str(c.pk) for c in constraints]
if len(reversed_constraints) > 0:
for c in reversed_constraints:
if c not in valid_constraints:
raise serializers.ValidationError({"reversed_constraints": [{f"{c}": "Invalid constraint pk"}]})
if "winners_count" in data and data["winners_count"] > data["max_number_of_entries"]:
raise serializers.ValidationError(
{"reversed_constraints": [{f"{c}": "Invalid constraint pk"}]}
)
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
Expand All @@ -143,7 +163,7 @@ def validate(self, data):


class RaffleSerializer(serializers.ModelSerializer):
chain = SmallChainSerializer()
chain = ChainSerializer()
winner_entry = WinnerEntrySerializer()
user_entry = serializers.SerializerMethodField()
constraints = ConstraintSerializer(many=True, read_only=True)
Expand Down Expand Up @@ -199,7 +219,9 @@ class Meta:

def get_user_entry(self, raffle: Raffle):
try:
return RaffleEntrySerializer(raffle.entries.get(user_profile=self.context["user"])).data
return RaffleEntrySerializer(
raffle.entries.get(user_profile=self.context["user"])
).data
except RaffleEntry.DoesNotExist:
return None

Expand Down
Loading
Loading