Skip to content

Commit

Permalink
change global setting field names
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad Bastin committed Oct 24, 2023
1 parent d9cb85d commit 1f5b86f
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 82 deletions.
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
Original file line number Diff line number Diff line change
@@ -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',
),
]
Original file line number Diff line number Diff line change
@@ -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',
),
]
Original file line number Diff line number Diff line change
@@ -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',
),
]
Original file line number Diff line number Diff line change
@@ -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',
),
]
104 changes: 37 additions & 67 deletions faucet/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -222,19 +210,15 @@ 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)
enough_fee_multiplier = models.BigIntegerField(default=200000)

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)
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions tokenTap/migrations/0019_alter_constraint_name.py
Original file line number Diff line number Diff line change
@@ -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),
),
]

0 comments on commit 1f5b86f

Please sign in to comment.