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

Update prizetap according to last contract changes #165

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1f5b86f
change global setting field names
Oct 24, 2023
20bf3bf
Fix prizetap auto tasks according to new contracts
ShayanShiravani Oct 26, 2023
a089d51
Remove linea contract auto tasks
ShayanShiravani Oct 26, 2023
432f0f9
price_url in TokenPrice can be blank
PooyaFekri Oct 27, 2023
da0a3c7
Merge pull request #157 from UnitapApp/refactor/token_price_price_url…
Oct 27, 2023
f8e8752
change gastap claimstrategy | change global settings fields
Oct 27, 2023
01e4cd4
Merge pull request #158 from UnitapApp/feature/gastap_dynamic_round_d…
Oct 27, 2023
2381792
db error permissions
Oct 27, 2023
9149c88
db error permissions
Oct 27, 2023
8b76d1e
Merge pull request #159 from UnitapApp/feature/gastap_dynamic_round_d…
Oct 27, 2023
82346ec
db error permissions
Oct 27, 2023
7a6cd8f
Merge pull request #160 from UnitapApp/feature/gastap_dynamic_round_d…
Oct 27, 2023
e6289d4
db error permissions
Oct 27, 2023
4dffb96
Merge pull request #161 from UnitapApp/feature/gastap_dynamic_round_d…
Oct 27, 2023
7a0de66
fix user info error
Oct 27, 2023
605cfed
Merge pull request #162 from UnitapApp/feature/gastap_dynamic_round_d…
Oct 27, 2023
8654374
fix remaining claims error
Oct 27, 2023
190b2f7
Merge pull request #163 from UnitapApp/feature/gastap_dynamic_round_d…
Oct 27, 2023
d75610c
fix remaining claims error
Oct 27, 2023
85cb263
Merge pull request #164 from UnitapApp/feature/gastap_dynamic_round_d…
Oct 27, 2023
ea00242
Prizetap: fix tests
ShayanShiravani Oct 28, 2023
931dde2
Merge branch 'develop' of github.com:UnitapApp/unitap-backend into fi…
ShayanShiravani Oct 28, 2023
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
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
29 changes: 8 additions & 21 deletions brightIDfaucet/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,21 @@
"task": "faucet.tasks.update_tokentap_claim_for_verified_lightning_claims",
"schedule": 9,
},
'update-tokens-price': {
"update-tokens-price": {
"task": "faucet.tasks.update_tokens_price",
"schedule": 600,
},
'update-donation-receipt-status': {
"update-donation-receipt-status": {
"task": "faucet.tasks.update_donation_receipt_pending_status",
"schedule": 180,
},
"draw-prizetap-raffles": {
"task": "prizetap.tasks.draw_the_expired_raffles",
"schedule": 300
},
"set-raffle-winner": {
"task": "prizetap.tasks.set_the_winner_of_raffles",
"schedule": 300
},
"request-random-words-for-linea-raffles": {
"task": "prizetap.tasks.request_random_words_for_expired_linea_raffles",
"schedule": 150
},
"draw-linea-raffles": {
"task": "prizetap.tasks.draw_expired_linea_raffles",
"schedule": 60
"request-random-words-for-raffles": {
"task": "prizetap.tasks.request_random_words_for_expired_raffles",
"schedule": 120,
},
"set-linea-raffle-winners": {
"task": "prizetap.tasks.set_the_winner_of_linea_raffles",
"schedule": 60
}
"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},
}

# Load task modules from all registered Django apps.
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