Skip to content

Commit

Permalink
optimism has claimed this round constraint in faucet
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad Bastin committed Oct 21, 2023
1 parent 36cfe7c commit e1734f7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
81 changes: 58 additions & 23 deletions faucet/constraints.py
Original file line number Diff line number Diff line change
@@ -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 = []

Expand All @@ -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 = []

Expand All @@ -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)
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)
2 changes: 2 additions & 0 deletions tokenTap/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,6 +13,7 @@ class Constraint(UserConstraint):
OncePerWeekVerification,
OncePerMonthVerification,
OnceInALifeTimeVerification,
OptimismHasClaimedGasInThisRound,
]
name = UserConstraint.create_name_field(constraints)

Expand Down

0 comments on commit e1734f7

Please sign in to comment.