Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pots/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def get_bulk_account_data(self, voter_ids):
"""Get account data for multiple voters in one query"""
accounts = {
account.id: AccountSerializer(account).data
for account in Account.objects.select_related().filter(id__in=voter_ids)
for account in Account.objects.filter(id__in=voter_ids)
}
return accounts

Expand Down Expand Up @@ -418,7 +418,7 @@ class MpdaoVotersListAPI(MpdaoVoterMixin, APIView):
500: OpenApiResponse(description="Error fetching voters"),
},
)
@method_decorator(cache_page(14400))
@method_decorator(cache_page(86400))
def get(self, request: Request, *args, **kwargs):
try:
return self.get_all_voters(request.query_params)
Expand Down Expand Up @@ -515,13 +515,13 @@ class MpdaoVoterDetailAPI(MpdaoVoterMixin, APIView):
500: OpenApiResponse(description="Error fetching voter data"),
},
)
@method_decorator(cache_page(14400))
@method_decorator(cache_page(86400))
def get(self, request: Request, voter_id: str, *args, **kwargs):
try:
voter_data = self.get_voter_data(voter_id)

accounts = self.get_bulk_account_data([voter_id])
account_data = accounts.get(voter_id)
# accounts = self.get_bulk_account_data([voter_id])
account_data = accounts = AccountSerializer(voter_id).data

response_data = {
"voter_id": voter_id,
Expand Down
17 changes: 15 additions & 2 deletions pots/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rest_framework.serializers import ModelSerializer

from django.conf import settings
from django.core.cache import cache

from accounts.serializers import SIMPLE_ACCOUNT_EXAMPLE, AccountSerializer, NearSocialProfileDataSerializer
from base.serializers import TwoDecimalPlacesField
Expand Down Expand Up @@ -355,19 +356,31 @@ class MpdaoSnapshotSerializer(serializers.Serializer):

def get_is_human(self, obj) -> bool:
voter_id = obj.get('voter_id')
url = f"https://rpc.web4.near.page/account/v1.nadabot.near/view/is_human?account_id={voter_id}"
cache_key = f'{voter_id}_is_human'
cached_res = cache.get(cache_key)

if cached_res is not None:
return cached_res
url = f"https://rpc.web4.near.page/account/v1.nadabot.near/view/is_human?account_id={voter_id}&near_block_height=137346724"
response = requests.get(url)
if response.status_code == 200:
is_human = response.json()
cache.set(cache_key, is_human, 8640000)
return is_human
return False

def get_staking_token_balance(self, obj):
voter_id = obj.get('voter_id')
url = f"https://rpc.web4.near.page/account/meta-pool.near/view/ft_balance_of?account_id={voter_id}"
cache_key = f'{voter_id}_token_balance'
cached_res = cache.get(cache_key)

if cached_res is not None:
return cached_res
url = f"https://rpc.web4.near.page/account/meta-pool.near/view/ft_balance_of?account_id={voter_id}&near_block_height=137346724"
response = requests.get(url)
if response.status_code == 200:
balance = response.json()
cache.set(cache_key, balance, 8640000)
return balance
return "0"

Expand Down
Loading