diff --git a/faucet/admin.py b/faucet/admin.py index ccb83759..e95f9b50 100644 --- a/faucet/admin.py +++ b/faucet/admin.py @@ -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): @@ -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 ( @@ -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): @@ -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) diff --git a/faucet/migrations/0060_rename_weekly_chain_claim_limit_globalsettings_gastap_claim_limit.py b/faucet/migrations/0060_rename_weekly_chain_claim_limit_globalsettings_gastap_claim_limit.py new file mode 100644 index 00000000..553c8071 --- /dev/null +++ b/faucet/migrations/0060_rename_weekly_chain_claim_limit_globalsettings_gastap_claim_limit.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2023-10-24 13:33 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('faucet', '0059_chain_explorer_api_key_chain_explorer_api_url'), + ] + + operations = [ + migrations.RenameField( + model_name='globalsettings', + old_name='weekly_chain_claim_limit', + new_name='gastap_claim_limit', + ), + ] diff --git a/faucet/migrations/0061_rename_gastap_claim_limit_globalsettings_gastap_round_claim_limit.py b/faucet/migrations/0061_rename_gastap_claim_limit_globalsettings_gastap_round_claim_limit.py new file mode 100644 index 00000000..ab52234f --- /dev/null +++ b/faucet/migrations/0061_rename_gastap_claim_limit_globalsettings_gastap_round_claim_limit.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2023-10-24 13:34 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('faucet', '0060_rename_weekly_chain_claim_limit_globalsettings_gastap_claim_limit'), + ] + + operations = [ + migrations.RenameField( + model_name='globalsettings', + old_name='gastap_claim_limit', + new_name='gastap_round_claim_limit', + ), + ] diff --git a/faucet/migrations/0062_rename_tokentap_weekly_claim_limit_globalsettings_tokentap_round_claim_limit.py b/faucet/migrations/0062_rename_tokentap_weekly_claim_limit_globalsettings_tokentap_round_claim_limit.py new file mode 100644 index 00000000..fe8c1d4a --- /dev/null +++ b/faucet/migrations/0062_rename_tokentap_weekly_claim_limit_globalsettings_tokentap_round_claim_limit.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2023-10-24 13:35 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('faucet', '0061_rename_gastap_claim_limit_globalsettings_gastap_round_claim_limit'), + ] + + operations = [ + migrations.RenameField( + model_name='globalsettings', + old_name='tokentap_weekly_claim_limit', + new_name='tokentap_round_claim_limit', + ), + ] diff --git a/faucet/migrations/0063_rename_prizetap_weekly_claim_limit_globalsettings_prizetap_round_claim_limit.py b/faucet/migrations/0063_rename_prizetap_weekly_claim_limit_globalsettings_prizetap_round_claim_limit.py new file mode 100644 index 00000000..f4ffffbe --- /dev/null +++ b/faucet/migrations/0063_rename_prizetap_weekly_claim_limit_globalsettings_prizetap_round_claim_limit.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2023-10-24 13:35 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('faucet', '0062_rename_tokentap_weekly_claim_limit_globalsettings_tokentap_round_claim_limit'), + ] + + operations = [ + migrations.RenameField( + model_name='globalsettings', + old_name='prizetap_weekly_claim_limit', + new_name='prizetap_round_claim_limit', + ), + ] diff --git a/faucet/models.py b/faucet/models.py index f0fd8d17..6e6a8e6e 100644 --- a/faucet/models.py +++ b/faucet/models.py @@ -1,25 +1,21 @@ -from decimal import Decimal -from datetime import datetime, timedelta +import binascii import logging -from django.db import models import uuid +from datetime import datetime, timedelta + +from bip_utils import Bip44, Bip44Coins +from django.conf import settings +from django.core.cache import cache +from django.db import models from django.utils import timezone from encrypted_model_fields.fields import EncryptedCharField -import binascii -from bip_utils import Bip44Coins, Bip44 -from web3.exceptions import TimeExhausted -from django.conf import settings -from authentication.models import NetworkTypes, UserProfile, Wallet -from solders.pubkey import Pubkey from solders.keypair import Keypair -from faucet.faucet_manager.lnpay_client import LNPayClient -from django.core.cache import cache -from core.models import BigNumField +from solders.pubkey import Pubkey +from authentication.models import NetworkTypes, UserProfile from brightIDfaucet.settings import BRIGHT_ID_INTERFACE - -# import django transaction -from django.db import transaction +from core.models import BigNumField +from faucet.faucet_manager.lnpay_client import LNPayClient def get_cache_time(id): @@ -29,22 +25,20 @@ def get_cache_time(id): class WalletAccount(models.Model): name = models.CharField(max_length=255, blank=True, null=True) private_key = EncryptedCharField(max_length=100) - network_type = models.CharField( - choices=NetworkTypes.networks, max_length=10, default=NetworkTypes.EVM - ) + network_type = models.CharField(choices=NetworkTypes.networks, max_length=10, default=NetworkTypes.EVM) @property def address(self): try: - node = Bip44.FromPrivateKey( - binascii.unhexlify(self.private_key), Bip44Coins.ETHEREUM - ) + node = Bip44.FromPrivateKey(binascii.unhexlify(self.private_key), Bip44Coins.ETHEREUM) return node.PublicKey().ToAddress() - except: + except Exception as e: + logging.exception(f"Error getting address for {self.name} error is {e}") try: keypair = Keypair.from_base58_string(self.private_key) return str(keypair.pubkey()) - except: + except Exception as e2: + logging.exception(f"Error getting address for {self.name} error is {e2}") pass def __str__(self) -> str: @@ -83,12 +77,8 @@ class BrightUser(models.Model): address = models.CharField(max_length=45, unique=True) context_id = models.UUIDField(default=uuid.uuid4, unique=True) - _verification_status = models.CharField( - max_length=1, choices=states, default=PENDING - ) - _last_verified_datetime = models.DateTimeField( - default=timezone.make_aware(datetime.utcfromtimestamp(0)) - ) + _verification_status = models.CharField(max_length=1, choices=states, default=PENDING) + _last_verified_datetime = models.DateTimeField(default=timezone.make_aware(datetime.utcfromtimestamp(0))) _sponsored = models.BooleanField(default=False) objects = BrightUserManager() @@ -191,9 +181,7 @@ def claims_count(): cached_count = cache.get("gastap_claims_count") if cached_count: return cached_count - count = ClaimReceipt.objects.filter( - _status__in=[ClaimReceipt.VERIFIED, BrightUser.VERIFIED] - ).count() + count = ClaimReceipt.objects.filter(_status__in=[ClaimReceipt.VERIFIED, BrightUser.VERIFIED]).count() cache.set("gastap_claims_count", count, 600) return count @@ -222,9 +210,7 @@ class Chain(models.Model): fund_manager_address = models.CharField(max_length=255) tokentap_contract_address = models.CharField(max_length=255, null=True, blank=True) - wallet = models.ForeignKey( - WalletAccount, related_name="chains", on_delete=models.PROTECT - ) + wallet = models.ForeignKey(WalletAccount, related_name="chains", on_delete=models.PROTECT) max_gas_price = models.BigIntegerField(default=250000000000) gas_multiplier = models.FloatField(default=1) @@ -232,9 +218,7 @@ class Chain(models.Model): needs_funding = models.BooleanField(default=False) is_testnet = models.BooleanField(default=False) - chain_type = models.CharField( - max_length=10, choices=NetworkTypes.networks, default=NetworkTypes.EVM - ) + chain_type = models.CharField(max_length=10, choices=NetworkTypes.networks, default=NetworkTypes.EVM) order = models.IntegerField(default=0) is_active = models.BooleanField(default=True) @@ -276,9 +260,7 @@ def get_manager_balance(self): if self.chain_type == NetworkTypes.EVM or int(self.chain_id) == 500: if self.chain_id == 500: logging.debug("chain XDC NONEVM is checking its balances") - funds = EVMFundManager(self).w3.eth.get_balance( - self.fund_manager_address - ) + funds = EVMFundManager(self).w3.eth.get_balance(self.fund_manager_address) return funds elif self.chain_type == NetworkTypes.SOLANA: @@ -295,9 +277,7 @@ def get_manager_balance(self): raise Exception("Invalid chain type") except Exception as e: - logging.exception( - f"Error getting manager balance for {self.chain_name} error is {e}" - ) + logging.exception(f"Error getting manager balance for {self.chain_name} error is {e}") return 0 @property @@ -318,9 +298,7 @@ def get_wallet_balance(self): return EVMFundManager(self).w3.eth.get_balance(self.wallet.address) elif self.chain_type == NetworkTypes.SOLANA: fund_manager = SolanaFundManager(self) - v = fund_manager.w3.get_balance( - Pubkey.from_string(self.wallet.address) - ).value + v = fund_manager.w3.get_balance(Pubkey.from_string(self.wallet.address)).value return v elif self.chain_type == NetworkTypes.LIGHTNING: lnpay_client = LNPayClient( @@ -331,9 +309,7 @@ def get_wallet_balance(self): return lnpay_client.get_balance() raise Exception("Invalid chain type") except Exception as e: - logging.exception( - f"Error getting wallet balance for {self.chain_name} error is {e}" - ) + logging.exception(f"Error getting wallet balance for {self.chain_name} error is {e}") return 0 @property @@ -352,7 +328,8 @@ def gas_price(self): from faucet.faucet_manager.fund_manager import EVMFundManager return EVMFundManager(self).w3.eth.gas_price - except: + except Exception as e: + logging.exception(f"Error getting gas price for {self.chain_name} error is {e}") return self.max_gas_price + 1 @property @@ -364,7 +341,8 @@ def is_gas_price_too_high(self): from faucet.faucet_manager.fund_manager import EVMFundManager return EVMFundManager(self).is_gas_price_too_high - except: + except Exception as e: + logging.exception(f"Error getting gas price for {self.chain_name} error is {e}") return True @property @@ -384,9 +362,7 @@ def total_claims(self): @property def total_claims_since_last_monday(self): - cached_total_claims_since_last_monday = cache.get( - f"gas_tap_chain_total_claims_since_last_monday_{self.pk}" - ) + cached_total_claims_since_last_monday = cache.get(f"gas_tap_chain_total_claims_since_last_monday_{self.pk}") if cached_total_claims_since_last_monday: return cached_total_claims_since_last_monday from faucet.faucet_manager.claim_manager import WeeklyCreditStrategy @@ -405,9 +381,7 @@ def total_claims_since_last_monday(self): @property def total_claims_for_last_round(self): - cached_total_claims_for_last_round = cache.get( - f"gas_tap_chain_total_claims_for_last_round_{self.pk}" - ) + cached_total_claims_for_last_round = cache.get(f"gas_tap_chain_total_claims_for_last_round_{self.pk}") if cached_total_claims_for_last_round: return cached_total_claims_for_last_round from faucet.faucet_manager.claim_manager import WeeklyCreditStrategy @@ -427,9 +401,7 @@ def total_claims_for_last_round(self): @property def total_claims_since_last_round(self): - cached_total_claims_since_last_round = cache.get( - f"gas_tap_chain_total_claims_since_last_round_{self.pk}" - ) + cached_total_claims_since_last_round = cache.get(f"gas_tap_chain_total_claims_since_last_round_{self.pk}") if cached_total_claims_since_last_round: return cached_total_claims_since_last_round from faucet.faucet_manager.claim_manager import WeeklyCreditStrategy @@ -448,16 +420,14 @@ def total_claims_since_last_round(self): class GlobalSettings(models.Model): - weekly_chain_claim_limit = models.IntegerField(default=5) - tokentap_weekly_claim_limit = models.IntegerField(default=3) - prizetap_weekly_claim_limit = models.IntegerField(default=3) + gastap_round_claim_limit = models.IntegerField(default=5) + tokentap_round_claim_limit = models.IntegerField(default=3) + prizetap_round_claim_limit = models.IntegerField(default=3) is_gas_tap_available = models.BooleanField(default=True) class TransactionBatch(models.Model): - chain = models.ForeignKey( - Chain, related_name="batches", on_delete=models.PROTECT, db_index=True - ) + chain = models.ForeignKey(Chain, related_name="batches", on_delete=models.PROTECT, db_index=True) datetime = models.DateTimeField(auto_now_add=True) tx_hash = models.CharField(max_length=255, blank=True, null=True, db_index=True) diff --git a/tokenTap/migrations/0019_alter_constraint_name.py b/tokenTap/migrations/0019_alter_constraint_name.py new file mode 100644 index 00000000..6092aee2 --- /dev/null +++ b/tokenTap/migrations/0019_alter_constraint_name.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2023-10-24 13:33 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tokenTap', '0018_constraint_icon_url'), + ] + + operations = [ + migrations.AlterField( + model_name='constraint', + name='name', + field=models.CharField(choices=[('BrightIDMeetVerification', 'BrightIDMeetVerification'), ('BrightIDAuraVerification', 'BrightIDAuraVerification'), ('OncePerWeekVerification', 'OncePerWeekVerification'), ('OncePerMonthVerification', 'OncePerMonthVerification'), ('OnceInALifeTimeVerification', 'OnceInALifeTimeVerification'), ('OptimismHasClaimedGasInThisRound', 'OptimismHasClaimedGasInThisRound')], max_length=255, unique=True), + ), + ]