Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sybil data input endpoint for round #9371

Merged
merged 4 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion app/grants/router.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta

from django.core.paginator import Paginator
from django.core.paginator import EmptyPage, Paginator

import django_filters.rest_framework
from ratelimit.decorators import ratelimit
Expand Down
10 changes: 6 additions & 4 deletions app/grants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
collection_thumbnail, contribute_to_grants_v1, contribution_addr_from_all_as_json,
contribution_addr_from_grant_as_json, contribution_addr_from_grant_during_round_as_json,
contribution_addr_from_round_as_json, contribution_info_from_grant_during_round_as_json, create_matching_pledge_v1,
flag, get_collection, get_collections_list, get_ethereum_cart_data, get_grant_payload, get_grants,
get_interrupted_contributions, get_replaced_tx, grant_activity, grant_categories, grant_details, grant_details_api,
grant_details_contributions, grant_details_contributors, grant_edit, grant_fund, grant_new, grants,
grants_addr_as_json, grants_bulk_add, grants_by_grant_type, grants_cart_view, grants_info, grants_landing,
flag, get_clr_sybil_input, get_collection, get_collections_list, get_ethereum_cart_data, get_grant_payload,
get_grants, get_interrupted_contributions, get_replaced_tx, grant_activity, grant_categories, grant_details,
grant_details_api, grant_details_contributions, grant_details_contributors, grant_edit, grant_fund, grant_new,
grants, grants_addr_as_json, grants_bulk_add, grants_by_grant_type, grants_cart_view, grants_info, grants_landing,
grants_type_redirect, ingest_contributions, ingest_contributions_view, invoice, leaderboard,
manage_ethereum_cart_data, new_matching_partner, profile, quickstart, remove_grant_from_collection, save_collection,
toggle_grant_favorite, verify_grant, get_trust_bonus
Expand Down Expand Up @@ -114,5 +114,7 @@
path('v1/api/export_addresses/grant<int:grant_id>_round<int:round_id>.json', contribution_addr_from_grant_during_round_as_json, name='contribution_addr_from_grant_during_round_as_json'),
path('v1/api/export_info/grant<int:grant_id>_round<int:round_id>.json', contribution_info_from_grant_during_round_as_json, name='contribution_addr_from_grant_during_round_as_json'),

# custom API
path('v1/api/get-clr-data/<int:round_id>', get_clr_sybil_input, name='get_clr_sybil_input')

]
74 changes: 71 additions & 3 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import logging
import math
import re
import time
import uuid
from datetime import datetime
from urllib.parse import urlencode
Expand All @@ -38,6 +37,7 @@
from django.db import connection, transaction
from django.db.models import Q, Subquery
from django.http import Http404, HttpResponse, JsonResponse
from django.http.response import HttpResponseBadRequest, HttpResponseServerError
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.templatetags.static import static
Expand Down Expand Up @@ -67,6 +67,7 @@
from economy.models import Token as FTokens
from economy.utils import convert_token_to_usdt
from eth_account.messages import defunct_hash_message
from grants.clr import fetch_data
from grants.models import (
CartActivity, Contribution, Flag, Grant, GrantAPIKey, GrantBrandingRoutingPolicy, GrantCategory, GrantCLR,
GrantCollection, GrantType, MatchPledge, Subscription,
Expand All @@ -81,7 +82,7 @@
from kudos.models import BulkTransferCoupon, Token
from marketing.mails import grant_cancellation, new_grant_flag_admin
from marketing.models import Keyword, Stat
from perftools.models import JSONStore
from perftools.models import JSONStore, StaticJsonEnv
from ratelimit.decorators import ratelimit
from retail.helpers import get_ip
from townsquare.models import Announcement, Favorite, PinnedPost
Expand Down Expand Up @@ -3496,6 +3497,73 @@ def handle_ingestion(profile, network, identifier, do_write):
return JsonResponse({ 'success': True, 'ingestion_types': ingestion_types })



def get_clr_sybil_input(request, round_id):
'''
This returns a paginated JSON response to return contributions
which are considered while calculating the QF match for a given CLR
'''
token = request.headers['token']
page = request.GET.get('page', 1)

data = StaticJsonEnv.objects.get(key='BSCI_SYBIL_TOKEN').data

if not round_id or not token or not data['token']:
return HttpResponseBadRequest("error: missing arguments")

if token != data['token']:
return HttpResponseBadRequest("error: invalid token")

clr = GrantCLR.objects.filter(pk=round_id).first()
if not clr:
return HttpResponseBadRequest("error: round not found")

try:
limit = data['limit'] if data['limit'] else 100

# fetch grant contributions needed for round
__, all_clr_contributions = fetch_data(clr)
total_count = all_clr_contributions.count()

# extract only needed fields
all_clr_contributions = list(all_clr_contributions.values(
'created_on', 'profile_for_clr__handle', 'profile_for_clr_id',
'match', 'normalized_data'
))

# paginate contributions
contributions = Paginator(all_clr_contributions, limit)
try:
contributions_queryset = contributions.page(page)
except EmptyPage:
response = {
'metadata': {
'count': 0,
'current_page': 0,
'num_pages': 0,
'has_next': False
},
'contributions': []
}
return HttpResponse(response)

response = {
'metadata': {
'count': total_count,
'current_page': page,
'total_pages': contributions.num_pages,
'has_next': contributions_queryset.has_next()
},
'contributions': contributions_queryset.object_list
}

except Exception as e:
print(e)
return HttpResponseServerError()

return JsonResponse(response)


@csrf_exempt
def get_trust_bonus(request):
'''
Expand Down Expand Up @@ -3525,4 +3593,4 @@ def get_trust_bonus(request):
})
_addrs.append(subscription.contributor_address)

return JsonResponse(response, safe=False)
return JsonResponse(response, safe=False)