diff --git a/faucet/constraints.py b/faucet/constraints.py index 457ddba6..c6a1a452 100644 --- a/faucet/constraints.py +++ b/faucet/constraints.py @@ -1,21 +1,23 @@ import requests from core.constraints import * from core.utils import Web3Utils +from faucet.faucet_manager.credit_strategy import WeeklyCreditStrategy from .models import DonationReceipt, Chain, ClaimReceipt + class DonationConstraint(ConstraintVerification): - _param_keys = [ - ConstraintParam.CHAIN - ] + _param_keys = [ConstraintParam.CHAIN] def is_observed(self, *args, **kwargs): chain_pk = self._param_values[ConstraintParam.CHAIN] - return DonationReceipt.objects\ - .filter(chain__pk=chain_pk)\ - .filter(user_profile = self.user_profile)\ - .filter(status=ClaimReceipt.PROCESSED_FOR_TOKENTAP)\ - .exists() - + return ( + DonationReceipt.objects.filter(chain__pk=chain_pk) + .filter(user_profile=self.user_profile) + .filter(status=ClaimReceipt.PROCESSED_FOR_TOKENTAP) + .exists() + ) + + class OptimismDonationConstraint(DonationConstraint): _param_keys = [] @@ -27,39 +29,46 @@ def is_observed(self, *args, **kwargs): self._param_values[ConstraintParam.CHAIN] = chain.pk return super().is_observed(*args, **kwargs) + class EvmClaimingGasConstraint(ConstraintVerification): - _param_keys = [ - ConstraintParam.CHAIN - ] + _param_keys = [ConstraintParam.CHAIN] def is_observed(self, *args, **kwargs): chain_pk = self._param_values[ConstraintParam.CHAIN] chain = Chain.objects.get(pk=chain_pk) w3 = Web3Utils(chain.rpc_url_private, chain.poa) current_block = w3.current_block() - user_address = self.user_profile.wallets.get(wallet_type=chain.chain_type).address + user_address = self.user_profile.wallets.get( + wallet_type=chain.chain_type + ).address first_internal_tx = requests.get( f"{chain.explorer_api_url}/api?module=account&action=txlistinternal&address={user_address}&startblock=0&endblock={current_block}&page=1&offset=1&sort=asc&apikey={chain.explorer_api_key}" ) first_internal_tx = first_internal_tx.json() - if first_internal_tx and first_internal_tx['status'] == '1': - first_internal_tx = first_internal_tx['result'][0] - if first_internal_tx and first_internal_tx['from'] == chain.fund_manager_address.lower()\ - and first_internal_tx['isError'] == '0': + if first_internal_tx and first_internal_tx["status"] == "1": + first_internal_tx = first_internal_tx["result"][0] + if ( + first_internal_tx + and first_internal_tx["from"] == chain.fund_manager_address.lower() + and first_internal_tx["isError"] == "0" + ): first_tx = requests.get( f"{chain.explorer_api_url}/api?module=account&action=txlist&address={user_address}&startblock=0&endblock={current_block}&page=1&offset=1&sort=asc&apikey={chain.explorer_api_key}" ) first_tx = first_tx.json() if first_tx: - if not first_tx['result']: + if not first_tx["result"]: return True - first_tx = first_tx['result'][0] - claiming_gas_tx = w3.get_transaction_by_hash(first_internal_tx['hash']) - web3_first_tx = w3.get_transaction_by_hash(first_tx['hash']) - return web3_first_tx['blockNumber'] > claiming_gas_tx['blockNumber'] + first_tx = first_tx["result"][0] + claiming_gas_tx = w3.get_transaction_by_hash( + first_internal_tx["hash"] + ) + web3_first_tx = w3.get_transaction_by_hash(first_tx["hash"]) + return web3_first_tx["blockNumber"] > claiming_gas_tx["blockNumber"] return False + class OptimismClaimingGasConstraint(EvmClaimingGasConstraint): _param_keys = [] @@ -69,4 +78,30 @@ def is_observed(self, *args, **kwargs): except: return False self._param_values[ConstraintParam.CHAIN] = chain.pk - return super().is_observed(*args, **kwargs) \ No newline at end of file + return super().is_observed(*args, **kwargs) + + +class HasClaimedGasInThisRound(ConstraintVerification): + _param_keys = [ConstraintParam.CHAIN] + + def is_observed(self, *args, **kwargs): + chain_pk = self._param_values[ConstraintParam.CHAIN] + chain = Chain.objects.get(pk=chain_pk) + return ClaimReceipt.objects.filter( + user_profile=self.user_profile, + chain=chain, + status=ClaimReceipt.VERIFIED, + datetime__gte=WeeklyCreditStrategy.get_last_monday(), + ).exists() + + +class OptimismHasClaimedGasInThisRound(HasClaimedGasInThisRound): + _param_keys = [] + + def is_observed(self, *args, **kwargs): + try: + chain = Chain.objects.get(chain_id=10) + except: + return False + self._param_values[ConstraintParam.CHAIN] = chain.pk + return super().is_observed(*args, **kwargs) diff --git a/tokenTap/models.py b/tokenTap/models.py index e0db7e00..57b3dbd5 100644 --- a/tokenTap/models.py +++ b/tokenTap/models.py @@ -2,6 +2,7 @@ from django.db import models from authentication.models import NetworkTypes, UserProfile from faucet.models import Chain, ClaimReceipt +from faucet.constraints import OptimismHasClaimedGasInThisRound from core.models import UserConstraint from .constraints import * from django.core.cache import cache @@ -12,6 +13,7 @@ class Constraint(UserConstraint): OncePerWeekVerification, OncePerMonthVerification, OnceInALifeTimeVerification, + OptimismHasClaimedGasInThisRound, ] name = UserConstraint.create_name_field(constraints)