From 007a4ff69bcaee5ec1475d5976c34148fa6065d5 Mon Sep 17 00:00:00 2001 From: Mohamad Bastin Date: Wed, 6 Dec 2023 20:32:53 +0100 Subject: [PATCH] fix no return for web3 utils get balance --- core/utils.py | 10 +++++--- faucet/models.py | 60 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/core/utils.py b/core/utils.py index 1c1b3f2d..6a31543a 100644 --- a/core/utils.py +++ b/core/utils.py @@ -54,7 +54,9 @@ 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) + first_day_of_last_month = last_month.replace( + day=1, hour=0, minute=0, second=0, microsecond=0 + ) return first_day_of_last_month @@ -122,7 +124,9 @@ def get_gas_estimate(self, func: Type[ContractFunction]): def build_contract_txn(self, func: Type[ContractFunction], **kwargs): nonce = self.w3.eth.get_transaction_count(self.account.address) - tx_data = func.build_transaction({"from": self.account.address, "nonce": nonce, **kwargs}) + tx_data = func.build_transaction( + {"from": self.account.address, "nonce": nonce, **kwargs} + ) return self.sign_tx(tx_data) def sign_tx(self, tx_data: TxParams): @@ -154,4 +158,4 @@ def get_transaction_receipt(self, hash): return self.w3.eth.get_transaction_receipt(hash) def get_balance(self, address): - self.w3.eth.get_balance(address) + return self.w3.eth.get_balance(address) diff --git a/faucet/models.py b/faucet/models.py index 870af2ee..2831a87a 100644 --- a/faucet/models.py +++ b/faucet/models.py @@ -25,18 +25,24 @@ 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: # noqa: E722 #dont change this, somehow it creates a bug if changed to Exception + except: # noqa: E722 #dont change this, somehow it creates a bug + # if changed to Exception try: keypair = Keypair.from_base58_string(self.private_key) return str(keypair.pubkey()) - except: # noqa: E722 #dont change this, somehow it creates a bug if changed to Exception + except: # noqa: E722 #dont change this, somehow it creates a + # bug if changed to Exception pass def __str__(self) -> str: @@ -75,8 +81,12 @@ 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() @@ -179,7 +189,9 @@ 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 @@ -208,7 +220,9 @@ 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) @@ -216,7 +230,9 @@ 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_one_time_claim = models.BooleanField(default=False) @@ -237,6 +253,8 @@ def has_enough_funds(self): @property def block_scan_address(self): address = "" + if not self.explorer_url: + return None if self.explorer_url[-1] == "/": address = self.explorer_url + f"address/{self.fund_manager_address}" else: @@ -277,7 +295,9 @@ 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 @@ -298,7 +318,9 @@ def get_wallet_balance(self): return EVMFundManager(self).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( @@ -309,7 +331,9 @@ 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 @@ -362,7 +386,9 @@ def total_claims(self): @property def total_claims_this_round(self): - cached_total_claims_this_round = cache.get(f"gas_tap_chain_total_claims_this_round_{self.pk}") + cached_total_claims_this_round = cache.get( + f"gas_tap_chain_total_claims_this_round_{self.pk}" + ) if cached_total_claims_this_round: return cached_total_claims_this_round from faucet.faucet_manager.claim_manager import RoundCreditStrategy @@ -381,7 +407,9 @@ def total_claims_this_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 RoundCreditStrategy @@ -407,7 +435,9 @@ class GlobalSettings(models.Model): 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)