Skip to content

Commit

Permalink
Merge pull request #220 from UnitapApp/fix/weekly_round
Browse files Browse the repository at this point in the history
Fix/weekly round
  • Loading branch information
Bastin authored Dec 16, 2023
2 parents 528357e + e583c67 commit 2e291bf
Showing 1 changed file with 45 additions and 61 deletions.
106 changes: 45 additions & 61 deletions faucet/faucet_manager/credit_strategy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import abc
import datetime
from abc import ABC
from time import time

import pytz
from django.db.models import Sum
from django.utils import timezone

from authentication.models import UserProfile
from faucet.models import Chain, ClaimReceipt
Expand Down Expand Up @@ -50,64 +52,6 @@ def get_unclaimed(self):
return int(self.chain.max_claim_amount) - int(self.get_claimed())


# class WeeklyCreditStrategy(SimpleCreditStrategy):
# def __int__(self, chain: Chain, user_profile: UserProfile):
# self.chain = chain
# self.user_profile = user_profile

# def get_claim_receipts(self):
# return ClaimReceipt.objects.filter(
# chain=self.chain,
# user_profile=self.user_profile,
# _status=ClaimReceipt.VERIFIED,
# datetime__gte=self.get_last_monday(),
# )

# @staticmethod
# def get_last_monday():
# now = int(time())
# day = 86400 # seconds in a day
# week = 7 * day
# weeks = now // week # number of weeks since epoch
# monday = 345600 # first monday midnight
# last_monday_midnight = monday + (weeks * week)

# # last monday could be off by one week
# if last_monday_midnight > now:
# last_monday_midnight -= week

# return timezone.make_aware(datetime.datetime.fromtimestamp(last_monday_midnight))

# @staticmethod
# def get_second_last_monday():
# now = int(time())
# day = 86400 # seconds in a day
# week = 7 * day
# weeks = now // week # number of weeks since epoch
# monday = 345600 # first monday midnight
# last_monday_midnight = monday + (weeks * week)

# # last monday could be off by one week
# if last_monday_midnight > now:
# last_monday_midnight -= week

# return timezone.make_aware(datetime.datetime.fromtimestamp(last_monday_midnight - week))


# class ArbitrumCreditStrategy(WeeklyCreditStrategy):
# def get_unclaimed(self):
# contract_address = "0x631a12430F94207De980D9b6A744AEB4093DCeC1"
# max_claim_amount = self.chain.max_claim_amount
# is_verified_user = BrightIdUserRegistry(self.chain, contract_address).is_verified_user(
# self.user_profile.initial_context_id
# )

# if is_verified_user:
# max_claim_amount = 5000000000000000

# return max_claim_amount - self.get_claimed()


class RoundCreditStrategy(SimpleCreditStrategy):
def __int__(self, chain: Chain, user_profile: UserProfile):
self.chain = chain
Expand All @@ -118,15 +62,17 @@ def get_claim_receipts(self):
chain=self.chain,
user_profile=self.user_profile,
_status=ClaimReceipt.VERIFIED,
datetime__gte=self._get_first_day_of_the_month(),
datetime__gte=self.get_start_of_the_round(),
)

@staticmethod
def get_start_of_the_round():
return RoundCreditStrategy._get_first_day_of_the_week()
return RoundCreditStrategy._get_first_day_of_the_month()

@staticmethod
def get_start_of_previous_round():
return RoundCreditStrategy._get_first_day_of_last_week()
return RoundCreditStrategy._get_first_day_of_last_month()

@classmethod
Expand All @@ -140,9 +86,45 @@ def _get_first_day_of_last_month(cls):
now = datetime.datetime.now(pytz.timezone("UTC"))
first_day = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
last_month = first_day - datetime.timedelta(days=1)
first_day_of_last_month = last_month.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
first_day_of_last_month = last_month.replace(
day=1, hour=0, minute=0, second=0, microsecond=0
)
return first_day_of_last_month

@classmethod
def _get_first_day_of_the_week(cls):
now = int(time())
day = 86400 # seconds in a day
week = 7 * day
weeks = now // week # number of weeks since epoch
monday = 345600 # first monday midnight
last_monday_midnight = monday + (weeks * week)

# last monday could be off by one week
if last_monday_midnight > now:
last_monday_midnight -= week

return timezone.make_aware(
datetime.datetime.fromtimestamp(last_monday_midnight)
)

@classmethod
def _get_first_day_of_last_week(cls):
now = int(time())
day = 86400 # seconds in a day
week = 7 * day
weeks = now // week # number of weeks since epoch
monday = 345600 # first monday midnight
last_monday_midnight = monday + (weeks * week)

# last monday could be off by one week
if last_monday_midnight > now:
last_monday_midnight -= week

return timezone.make_aware(
datetime.datetime.fromtimestamp(last_monday_midnight - week)
)


class OneTimeCreditStrategy(SimpleCreditStrategy):
def __int__(self, chain: Chain, user_profile: UserProfile):
Expand All @@ -155,7 +137,9 @@ def get_claim_receipts(self):
user_profile=self.user_profile,
_status=ClaimReceipt.VERIFIED,
# 1 December 2023
datetime__gte=datetime.datetime(2023, 12, 1, 0, 0, 0, 0, pytz.timezone("UTC")), # also change in views.py
datetime__gte=datetime.datetime(
2023, 12, 1, 0, 0, 0, 0, pytz.timezone("UTC")
), # also change in views.py
)


Expand Down

0 comments on commit 2e291bf

Please sign in to comment.