Skip to content

Commit

Permalink
Merge pull request #211 from UnitapApp/fix/get_balance
Browse files Browse the repository at this point in the history
fix no return for web3 utils get balance
  • Loading branch information
Mohamad Bastin authored Dec 6, 2023
2 parents dfb919f + 007a4ff commit 6a42e87
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
10 changes: 7 additions & 3 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
60 changes: 45 additions & 15 deletions faucet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -208,15 +220,19 @@ 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_one_time_claim = models.BooleanField(default=False)
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 6a42e87

Please sign in to comment.