Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:UnitapApp/unitap-backend into fi…
Browse files Browse the repository at this point in the history
…x/prizetap-auto-tasks
  • Loading branch information
ShayanShiravani committed Oct 28, 2023
2 parents ea00242 + 85cb263 commit 931dde2
Show file tree
Hide file tree
Showing 34 changed files with 801 additions and 756 deletions.
Binary file added .DS_Store
Binary file not shown.
26 changes: 10 additions & 16 deletions authentication/serializers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from django.db import IntegrityError
from authentication.models import (
UserProfile,
Wallet,
)
from rest_framework.authtoken.models import Token
from rest_framework import serializers
from faucet.faucet_manager.claim_manager import LimitedChainClaimManager
from rest_framework.authtoken.models import Token

from authentication.models import UserProfile, Wallet
from faucet.faucet_manager.claim_manager import LimitedChainClaimManager
from faucet.models import GlobalSettings


Expand Down Expand Up @@ -57,7 +53,7 @@ class Meta:

class ProfileSerializer(serializers.ModelSerializer):
wallets = WalletSerializer(many=True, read_only=True)
total_weekly_claims_remaining = serializers.SerializerMethodField()
total_round_claims_remaining = serializers.SerializerMethodField()
token = serializers.SerializerMethodField()

class Meta:
Expand All @@ -69,22 +65,20 @@ class Meta:
"initial_context_id",
"is_meet_verified",
"is_aura_verified",
"total_weekly_claims_remaining",
"total_round_claims_remaining",
"wallets",
]

def get_token(self, instance):
token, bol = Token.objects.get_or_create(user=instance.user)
return token.key

def get_total_weekly_claims_remaining(self, instance):
def get_total_round_claims_remaining(self, instance):
gs = GlobalSettings.objects.first()
if gs is not None:
return (
gs.weekly_chain_claim_limit
- LimitedChainClaimManager.get_total_weekly_claims(instance)
)

return gs.gastap_round_claim_limit - LimitedChainClaimManager.get_total_round_claims(instance)


class SimpleProfilerSerializer(serializers.ModelSerializer):
wallets = WalletSerializer(many=True, read_only=True)
username = serializers.SerializerMethodField()
Expand All @@ -102,4 +96,4 @@ class Meta:
def get_username(self, user_profile: UserProfile):
if not user_profile.username:
return f"User{user_profile.pk}"
return user_profile.username
return user_profile.username
2 changes: 1 addition & 1 deletion brightIDfaucet/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"update_tokentap_claim_for_verified_lightning_claims": {
"task": "faucet.tasks.update_tokentap_claim_for_verified_lightning_claims",
"schedule": 3,
"schedule": 9,
},
"update-tokens-price": {
"task": "faucet.tasks.update_tokens_price",
Expand Down
18 changes: 18 additions & 0 deletions core/migrations/0003_alter_tokenprice_price_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.4 on 2023-10-27 08:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0002_alter_tokenprice_price_url'),
]

operations = [
migrations.AlterField(
model_name='tokenprice',
name='price_url',
field=models.URLField(blank=True, max_length=255, null=True),
),
]
13 changes: 5 additions & 8 deletions core/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from .constraints import *

from .constraints import BrightIDAuraVerification, BrightIDMeetVerification


class UserConstraint(models.Model):
Expand All @@ -19,9 +20,7 @@ class Type(models.TextChoices):
choices=[(c.__name__, c.__name__) for c in constraints],
)
title = models.CharField(max_length=255)
type = models.CharField(
max_length=10, choices=Type.choices, default=Type.VERIFICATION
)
type = models.CharField(max_length=10, choices=Type.choices, default=Type.VERIFICATION)
description = models.TextField(null=True, blank=True)
response = models.TextField(null=True, blank=True)
icon_url = models.CharField(max_length=255, null=True, blank=True)
Expand All @@ -42,10 +41,8 @@ class TokenPrice(models.Model):
usd_price = models.CharField(max_length=255, null=False)
datetime = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True, null=True, blank=True)
price_url = models.URLField(max_length=255, null=True)
symbol = models.CharField(
max_length=255, db_index=True, unique=True, null=False, blank=False
)
price_url = models.URLField(max_length=255, null=True, blank=True)
symbol = models.CharField(max_length=255, db_index=True, unique=True, null=False, blank=False)


class BigNumField(models.Field):
Expand Down
90 changes: 48 additions & 42 deletions core/utils.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,64 @@
import datetime

import pytz
from time import time
from web3 import Web3
from web3.middleware import geth_poa_middleware
from web3.contract.contract import Contract, ContractFunction
from web3.types import Type, TxParams
from django.utils import timezone
from web3.middleware import geth_poa_middleware
from web3.types import TxParams, Type


class TimeUtils:
@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)
)
# @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)
# )

@staticmethod
def get_first_day_of_the_month():
now = datetime.datetime.now(pytz.timezone("UTC"))
first_day = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
return first_day

@staticmethod
def get_first_day_of_last_month():
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)
return first_day_of_last_month


class Web3Utils:
def __init__(self, rpc_url, poa = False) -> None:
def __init__(self, rpc_url, poa=False) -> None:
self._rpc_url = rpc_url
self._w3 = None
self._account = None
Expand All @@ -71,7 +78,7 @@ def w3(self) -> Web3:
return self._w3

raise Exception(f"RPC provider is not connected ({self._rpc_url})")

@property
def poa(self):
return self._poa
Expand Down Expand Up @@ -102,8 +109,7 @@ def contract_call(self, func: Type[ContractFunction], from_address=None):

def build_contract_txn(self, func: Type[ContractFunction]):
nonce = self.w3.eth.get_transaction_count(self.account.address)
tx_data = func.build_transaction(
{"from": self.account.address, "nonce": nonce})
tx_data = func.build_transaction({"from": self.account.address, "nonce": nonce})
return self.sign_tx(tx_data)

def sign_tx(self, tx_data: TxParams):
Expand All @@ -117,6 +123,6 @@ def wait_for_transaction_receipt(self, tx_hash):

def current_block(self):
return self.w3.eth.block_number

def get_transaction_by_hash(self, hash):
return self.w3.eth.get_transaction(hash)
33 changes: 18 additions & 15 deletions faucet/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from django.contrib import admin
from .models import *

from .models import (
BrightUser,
Chain,
ClaimReceipt,
DonationReceipt,
GlobalSettings,
LightningConfig,
TransactionBatch,
WalletAccount,
)


class ChainAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -28,8 +38,8 @@ def last_updated_with_seconds(obj):


class TXHashFilter(admin.SimpleListFilter):
title = 'has tx hash' # or use _('country') for translated title
parameter_name = 'has_tx_hash'
title = "has tx hash" # or use _('country') for translated title
parameter_name = "has_tx_hash"

def lookups(self, request, model_admin):
return (
Expand Down Expand Up @@ -66,8 +76,8 @@ class WalletAccountAdmin(admin.ModelAdmin):


class GlobalSettingsAdmin(admin.ModelAdmin):
list_display = ["pk", "weekly_chain_claim_limit", "tokentap_weekly_claim_limit"]
list_editable = ["weekly_chain_claim_limit", "tokentap_weekly_claim_limit"]
list_display = ["pk", "gastap_round_claim_limit", "tokentap_round_claim_limit"]
list_editable = ["gastap_round_claim_limit", "tokentap_round_claim_limit"]


class TransactionBatchAdmin(admin.ModelAdmin):
Expand All @@ -92,16 +102,9 @@ class LightningConfigAdmin(admin.ModelAdmin):


class DonationReceiptAdmin(admin.ModelAdmin):
list_display = [
'tx_hash',
'user_profile',
'chain',
'value',
'total_price',
'datetime'
]
search_fields = ['tx_hash']
list_filter = ['chain', 'user_profile']
list_display = ["tx_hash", "user_profile", "chain", "value", "total_price", "datetime"]
search_fields = ["tx_hash"]
list_filter = ["chain", "user_profile"]


admin.site.register(WalletAccount, WalletAccountAdmin)
Expand Down
Loading

0 comments on commit 931dde2

Please sign in to comment.