Skip to content

Commit

Permalink
Merge pull request #3945 from rafalkowalski/issue/3913
Browse files Browse the repository at this point in the history
[Issue #3913] Add functionality to become a matching partner
  • Loading branch information
SaptakS authored Mar 21, 2019
2 parents 73253d8 + 2601898 commit cdf9d05
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 16 deletions.
64 changes: 64 additions & 0 deletions app/assets/v2/js/grants/newMatchingPartner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const GITCOIN_ADDRESS = '0x00De4B13153673BCAE2616b67bf822500d325Fc3';

function createMatchingPartner(transactionID, transactionAmount) {
let newMatchPledgeUrl = '/grants/matching-partners/new';

$.post(newMatchPledgeUrl, {hash: transactionID, amount: transactionAmount}).then(function(result) {
_alert(
result.message,
'success'
);
}).fail(function(data) {
_alert(
data.responseJSON['message'],
'error'
);
});
}

function saveTransactionDetails(transactionID) {

web3.eth.getTransaction(transactionID).then(function(transactionDetails) {
let network = document.web3network || 'mainnet';
let data = {
'txid': transactionID,
'amount': transactionDetails.value,
'network': network,
'from_address': transactionDetails.from,
'to_address': transactionDetails.to,
'type': 'matchpledge'
};

let newAttestationsUrl = '/revenue/attestations/new';

$.post(newAttestationsUrl, data).then(function(result) {
createMatchingPartner(transactionID, transactionDetails.value);
});

});

}

function processPayment() {
var ethAmount = prompt('How many ETH would you like to transfer?', '1ETH');

if (ethAmount && ethAmount.length > 0) {
ethAmount = ethAmount.replace('ETH', '');
var transactionParams = {
to: GITCOIN_ADDRESS,
from: web3.currentProvider.selectedAddress,
value: web3.utils.toWei(ethAmount)
};

web3.eth.sendTransaction(
transactionParams,
function(error, hash) {
if (error) {
_alert(error.message, 'error');
} else {
saveTransactionDetails(hash);
}
}
);
}
}
18 changes: 18 additions & 0 deletions app/grants/migrations/0018_matchpledge_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.7 on 2019-03-14 22:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('grants', '0018_grant_hidden'),
]

operations = [
migrations.AddField(
model_name='matchpledge',
name='data',
field=models.TextField(blank=True),
),
]
1 change: 1 addition & 0 deletions app/grants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ class MatchPledge(SuperModel):
)
comments = models.TextField(default='', blank=True, help_text=_('The comments.'))
end_date = models.DateTimeField(null=False, default=next_month)
data = models.TextField(blank=True)

def __str__(self):
"""Return the string representation of this object."""
Expand Down
41 changes: 26 additions & 15 deletions app/grants/templates/grants/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ <h1 class="font-title-xl">{% trans 'Provide sustainable funding for Open Source
</div>
</div>

{% if current_partners or past_partners %}

<div class="container-fluid py-5 grants-matching">
<div class="container">
<div class="row">
Expand All @@ -63,19 +61,31 @@ <h2>Round 2 of Matching is Live Now!</h2>
<h5>Contribute from 3/6 to 4/19 and have up to $50k in contributions matched.</h5>
<p>A special thanks to the following partners who have offered to match contributions to Gitcoin Grants. <a href="https://medium.com/gitcoin/gitcoin-grants-50k-oss-fund-e20e09dc2110">(Read Announcement Here)</a></p>

<ul class="nav nav-tabs my-5" role="tablist">
<li class="nav-item">
<a class="nav-link nav-line active" id="currentpartners-tab" data-toggle="tab" href="#currentpartners" role="tab" aria-controls="currentpartners" aria-selected="true">Current Partners</a>
</li>
<li class="nav-item">
<a class="nav-link nav-line" id="pastpartners-tab" data-toggle="tab" href="#pastpartners" role="tab" aria-controls="pastpartners" aria-selected="false">Past Partners</a>
</li>
<li class="align-self-center ml-auto font-smaller-2">
</li>
</ul>
</div>
{% if not user.is_authenticated %}
<a class="btn btn-gc-blue" href="{% url 'social:begin' 'github' %}?next={{ request.get_full_path }}">{% trans 'Become a Matching Partner' %}</a>
{% else %}
<div class="btn btn-gc-blue" onclick="processPayment()">
{% trans 'Become a Matching Partner' %}
</div>
{% endif %}


</div>
<div class="col-md-6"><img src="{% static 'v2/images/grants/robots.png' %}" class="rounded mx-auto d-block"/></div>
</div>
{% if current_partners or past_partners %}
<ul class="nav nav-tabs my-5" role="tablist">
<li class="nav-item">
<a class="nav-link nav-line active" id="currentpartners-tab" data-toggle="tab" href="#currentpartners" role="tab" aria-controls="currentpartners" aria-selected="true">Current Partners</a>
</li>
<li class="nav-item">
<a class="nav-link nav-line" id="pastpartners-tab" data-toggle="tab" href="#pastpartners" role="tab" aria-controls="pastpartners" aria-selected="false">Past Partners</a>
</li>
<li class="align-self-center ml-auto font-smaller-2">
</li>
</ul>


<div class="tab-content">
<div class="tab-pane fade show active" id="currentpartners" role="tabpanel" aria-labelledby="currentpartners-tab">
{% for current_partner in current_partners %}
Expand Down Expand Up @@ -103,10 +113,10 @@ <h5>@{{past_partner.profile.handle}}</h5>
{% endfor %}
</div>
</div>

{% endif %}
</div>
</div>
{% endif %}


<div class="container-fluid py-5 grants-search">
<div class="container">
Expand Down Expand Up @@ -205,6 +215,7 @@ <h1 class="font-title-lg font-weight-semibold mt-4 mb-2">{% trans "This is not t
<script src="{% static "v2/js/grants/index.js" %}"></script>
<script src="{% static "v2/js/popper.min.js" %}"></script>
<script src="{% static "v2/js/bootstrap.min.js" %}"></script>
<script src="{% static "v2/js/grants/newMatchingPartner.js" %}"></script>
<script>
var infinite = new Waypoint.Infinite({
element: $('.infinite-container')[0]
Expand Down
12 changes: 12 additions & 0 deletions app/grants/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import reverse

from test_plus.test import TestCase


class GrantsViewResponsesTests(TestCase):
def test_not_authorized(self):
response = self.client.post(reverse('grants:new_matching_partner'))

expected_response = {'message': 'Not Authorized', 'status': 403}
self.assertEqual(response.status_code, expected_response['status'])
self.assertEqual(response.json(), expected_response)
4 changes: 3 additions & 1 deletion app/grants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from django.urls import path, re_path

from grants.views import (
grant_details, grant_fund, grant_new, grants, leaderboard, milestones, profile, quickstart, subscription_cancel,
grant_details, grant_fund, grant_new, grants, leaderboard, milestones, new_matching_partner, profile, quickstart,
subscription_cancel,
)

app_name = 'grants'
Expand All @@ -38,4 +39,5 @@
re_path(r'^profile', profile, name='profile'),
re_path(r'^quickstart', quickstart, name='quickstart'),
re_path(r'^leaderboard', leaderboard, name='leaderboard'),
re_path(r'^matching-partners/new', new_matching_partner, name='new_matching_partner'),
]
51 changes: 51 additions & 0 deletions app/grants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from app.utils import get_profile
from cacheops import cached_view
from dashboard.models import Profile
from dashboard.utils import get_web3, has_tx_mined
from gas.utils import conf_time_spread, eth_usd_conv_rate, gas_advisories, recommend_min_gas_price_to_confirm_in_time
from grants.forms import MilestoneForm
from grants.models import Contribution, Grant, MatchPledge, Milestone, Subscription, Update
Expand Down Expand Up @@ -634,3 +635,53 @@ def leaderboard(request):
params['items'].append(item)
counter += 1
return TemplateResponse(request, 'grants/leaderboard.html', params)


@csrf_exempt
def new_matching_partner(request):

tx_hash = request.POST.get('hash')
tx_amount = request.POST.get('amount')
profile = get_profile(request)

def get_json_response(message, status):
return JsonResponse(
{'status': status, 'message': message},
status=status
)

def is_verified(tx_details, tx_hash, tx_amount, network):
gitcoin_account = '0x00De4B13153673BCAE2616b67bf822500d325Fc3'
return has_tx_mined(tx_hash, network) and\
tx_details.to == gitcoin_account and\
str(tx_details.value) == str(tx_amount)

if not request.user.is_authenticated:
return get_json_response("Not Authorized", 403)

if profile:
return get_json_response("Profile not found.", 404)

if request.POST and tx_hash:
network = 'mainnet'
web3 = get_web3(network)
tx = web3.eth.getTransaction(tx_hash)
if is_verified(tx, tx_hash, tx_amount, network):
MatchPledge(
profile=profile,
amount=tx.value,
data=json.dumps({
'tx_hash': tx_hash,
'network': network,
'from': tx['from'],
'to': tx.to,
'tx_amount': tx.value
})
).save()

return get_json_response(
"""Thank you for volunteering to match on Gitcoin Grants.
You are supporting open source, and we thank you""", 201
)
return get_json_response("Transaction wasn't verified.", 400)
return get_json_response("Wrong request.", 400)

0 comments on commit cdf9d05

Please sign in to comment.