Skip to content

Commit

Permalink
feat(cgrants): only return contributions greater than 1 dollar (#351)
Browse files Browse the repository at this point in the history
* feat(cgrants): only return contributions greater than 1 dollar

* fix(cgrants): round total amount
  • Loading branch information
tim-schultz authored Aug 14, 2023
1 parent 1e09db5 commit 5fcbab0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
21 changes: 11 additions & 10 deletions api/cgrants/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,21 @@ def _get_contributor_statistics_for_cgrants(


def _get_contributor_statistics_for_protocol(address: str) -> dict:
total_amount_usd = ProtocolContributions.objects.filter(
contributor=address
).aggregate(Sum("amount"))["amount__sum"]
num_rounds = ProtocolContributions.objects.filter(contributor=address).aggregate(
Count("round", distinct=True)
)["round__count"]
num_projects = ProtocolContributions.objects.filter(contributor=address).aggregate(
Count("project", distinct=True)
)["project__count"]
protocol_filter = ProtocolContributions.objects.filter(
contributor=address, amount__gte=1
)
total_amount_usd = protocol_filter.aggregate(Sum("amount"))["amount__sum"]
num_rounds = protocol_filter.aggregate(Count("round", distinct=True))[
"round__count"
]
num_projects = protocol_filter.aggregate(Count("project", distinct=True))[
"project__count"
]

return {
"num_grants_contribute_to": num_projects if num_projects is not None else 0,
"num_rounds_contribute_to": num_rounds if num_rounds is not None else 0,
"total_contribution_amount": total_amount_usd
"total_valid_contribution_amount": round(total_amount_usd, 3)
if total_amount_usd is not None
else 0,
"num_gr14_contributions": 0,
Expand Down
29 changes: 28 additions & 1 deletion api/cgrants/test/test_cgrants_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,34 @@ def test_contributor_statistics_with_only_protocol_contributions(self):
{
"num_grants_contribute_to": 4,
"num_rounds_contribute_to": 2,
"total_contribution_amount": "10",
"total_valid_contribution_amount": "10.000",
"num_gr14_contributions": 0,
},
)

def test_contributor_statistics_with_below_threshold_contributions_contributions(
self,
):
existing_contributions = ProtocolContributions.objects.filter(
contributor=self.address
).all()
for contribution in existing_contributions:
contribution.amount = 0.5
contribution.save()

# Edge case: User has made no contributions
response = self.client.get(
reverse("cgrants:allo_contributor_statistics"),
{"address": self.address},
**self.headers,
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json(),
{
"num_grants_contribute_to": 0,
"num_rounds_contribute_to": 0,
"total_valid_contribution_amount": 0,
"num_gr14_contributions": 0,
},
)
Expand Down

0 comments on commit 5fcbab0

Please sign in to comment.