-
-
Notifications
You must be signed in to change notification settings - Fork 775
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let users ingest missing contributions (#8274)
* Setup new page for ingesting missing contributions * Setup form and add input validation * Add ingestion endpoint + UI status alerts * Remove unnecessary authentication check in ingest_contributions_view * Move missing-contributions JS file into grants folder
- Loading branch information
Showing
5 changed files
with
550 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
app/assets/v2/js/grants/ingest-missing-contributions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/** | ||
* @notice Vue component for ingesting contributions that were missed during checkout | ||
* @dev See more at: https://github.com/gitcoinco/web/issues/7744 | ||
*/ | ||
|
||
let appIngestContributions; | ||
|
||
Vue.component('grants-ingest-contributions', { | ||
delimiters: [ '[[', ']]' ], | ||
|
||
data: function() { | ||
return { | ||
form: { | ||
txHash: undefined, // user transaction hash, used to ingest L1 donations | ||
userAddress: undefined // user address, used to ingest zkSync (L2) donations | ||
}, | ||
errors: {}, // keys are errors that occurred | ||
submitted: false // true if form has been submitted and we are waiting on response | ||
}; | ||
}, | ||
|
||
methods: { | ||
checkForm() { | ||
this.submitted = true; | ||
this.errors = {}; | ||
let isValidTxHash; | ||
let isValidAddress; | ||
|
||
// Validate that at least one of txHash and userAddress is provided | ||
const { txHash, userAddress } = this.form; | ||
const isFormComplete = txHash || userAddress; | ||
|
||
if (!isFormComplete) { | ||
// Form was not filled out | ||
this.$set(this.errors, 'invalidForm', 'Please enter a valid transaction hash or a valid wallet address'); | ||
} else { | ||
// Form was filled out, so validate the inputs | ||
isValidTxHash = txHash && txHash.length === 66 && txHash.startsWith('0x'); | ||
isValidAddress = ethers.utils.isAddress(userAddress); | ||
|
||
if (txHash && !isValidTxHash) { | ||
this.$set(this.errors, 'txHash', 'Please enter a valid transaction hash'); | ||
} | ||
if (userAddress && !isValidAddress) { | ||
this.$set(this.errors, 'address', 'Please enter a valid address'); | ||
} | ||
} | ||
|
||
if (Object.keys(this.errors).length) { | ||
return false; // there are errors the user must correct | ||
} | ||
|
||
return { | ||
txHash: isValidTxHash ? txHash : '', | ||
// getAddress returns checksum address required by web3py, and throws if address is invalid | ||
userAddress: isValidAddress ? ethers.utils.getAddress(userAddress) : '' | ||
}; | ||
}, | ||
|
||
async ingest(event) { | ||
try { | ||
event.preventDefault(); | ||
|
||
// Return if form is not valid | ||
const formParams = this.checkForm(); | ||
|
||
if (!formParams) { | ||
return; | ||
} | ||
|
||
// Send POST requests to ingest contributions | ||
const { txHash, userAddress } = formParams; | ||
const csrfmiddlewaretoken = document.querySelector('[name=csrfmiddlewaretoken]').value; | ||
const url = '/grants/ingest'; | ||
const headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }; | ||
const payload = { | ||
csrfmiddlewaretoken, | ||
txHash, | ||
userAddress, | ||
network: document.web3network || 'mainnet' | ||
}; | ||
const postParams = { | ||
method: 'POST', | ||
headers, | ||
body: new URLSearchParams(payload) | ||
}; | ||
|
||
// Send saveSubscription request | ||
const res = await fetch(url, postParams); | ||
const json = await res.json(); | ||
|
||
// Notify user of success status, and clear form if successful | ||
console.log('ingestion response: ', json); | ||
if (!json.success) { | ||
console.log('ingestion failed'); | ||
this.submitted = false; | ||
throw new Error('Your transactions could not be processed, please try again'); | ||
} else { | ||
console.log('ingestion successful'); | ||
_alert('Your contributions have been added successfully!', 'success'); | ||
this.resetForm(); | ||
} | ||
} catch (e) { | ||
this.handleError(e); | ||
} | ||
}, | ||
|
||
resetForm() { | ||
this.form.txHash = undefined; | ||
this.form.userAddress = undefined; | ||
this.errors = {}; | ||
this.submitted = false; | ||
}, | ||
|
||
handleError(err) { | ||
console.error(err); // eslint-disable-line no-console | ||
let message = 'There was an error'; | ||
|
||
if (err.message) | ||
message = err.message; | ||
else if (err.msg) | ||
message = err.msg; | ||
else if (typeof err === 'string') | ||
message = err; | ||
|
||
_alert(message, 'error'); | ||
this.submitted = false; | ||
} | ||
}, | ||
|
||
watch: { | ||
deep: true, | ||
form: { | ||
deep: true, | ||
handler(newVal, oldVal) { | ||
this.checkForm(); | ||
this.submitted = false; | ||
this.errors = {}; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
if (document.getElementById('gc-grants-ingest-contributions')) { | ||
|
||
appIngestContributions = new Vue({ | ||
delimiters: [ '[[', ']]' ], | ||
el: '#gc-grants-ingest-contributions' | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
{% comment %} | ||
Copyright (C) 2020 Gitcoin Core | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
|
||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
{% endcomment %} | ||
{% load i18n static email_obfuscator add_url_schema avatar_tags %} | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
{% include 'shared/head.html' with slim=1 %} | ||
{% include 'shared/cards.html' %} | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vue-select@latest/dist/vue-select.css"> | ||
<link rel="stylesheet" href="{% static "v2/css/grants/new.css" %}"> | ||
<link rel="stylesheet" href={% static "v2/css/tabs.css" %}> | ||
</head> | ||
|
||
<body class="interior {{ active }} grant g-font-muli"> | ||
|
||
{% include 'shared/tag_manager_2.html' %} | ||
<div class="container-fluid header dash px-0"> | ||
{% include 'shared/top_nav.html' with class='d-md-flex' %} | ||
{% include 'grants/nav.html' %} | ||
</div> | ||
|
||
<grants-ingest-contributions class="container-fluid bg-lightblue pb-5 pt-5" v-cloak id="gc-grants-ingest-contributions" inline-template> | ||
<form action="" @submit="checkForm"> | ||
<div class="text-center"> | ||
<img style="width:6rem;" src="{% static "v2/images/grants/torchbearer.svg" %}"> | ||
</div> | ||
|
||
<div class="container mt-3 mb-3 bg-white position-relative rounded col-lg-6 mx-auto"> | ||
<div class="row p-4 p-md-5"> | ||
|
||
<div class="col-12 text-center mb-4"> | ||
<h1 class="text-center font-title-xl">Add Missing Contributions</h1> | ||
<p class="text-center font-smaller-1 text-black-60"> | ||
If you completed a Gitcoin Grants checkout, but don't see evidence of this in your | ||
email or the Gitcoin interface, you can use this form to fix that! | ||
</p> | ||
</div> | ||
|
||
{% csrf_token %} | ||
|
||
<!-- Instructions --> | ||
<div class="col-12 mb-2"> | ||
<h5 class="mt-4">Instructions</h5> | ||
<hr> | ||
</div> | ||
|
||
<!-- Amount --> | ||
<div class="col-12 mb-3"> | ||
<p class="font-body mb-1"> | ||
<ul> | ||
<li>If you donated using L1 (Standard Checkout), please enter the transaction hash</li> | ||
<li>If you donated using L2 (zkSync Checkout), please enter your wallet address</li> | ||
<li>At least one of these two is required</li> | ||
</ul> | ||
</p> | ||
</div> | ||
|
||
<!-- Collect Information --> | ||
<div class="col-12 mb-2"> | ||
<h5 class="mt-4">Contribution Data</h5> | ||
<hr> | ||
</div> | ||
|
||
<!-- Transaction hash --> | ||
<div class="col-12 mb-3"> | ||
<label class="font-caption letter-spacing text-black-60 text-uppercase">Transaction Hash</label> | ||
<input id="amount" v-model="form.txHash" name="amount" class="form__input form__input-lg" /> | ||
</div> | ||
<div class="col-12 text-danger" v-if="errors.txHash"> | ||
[[errors.txHash]] | ||
</div> | ||
|
||
<!-- User address --> | ||
<div class="col-12 mb-3"> | ||
<label class="font-caption letter-spacing text-black-60 text-uppercase">Wallet Address</label> | ||
<input id="amount" v-model="form.userAddress" name="amount" class="form__input form__input-lg" /> | ||
</div> | ||
|
||
<div class="col-12 text-danger" v-if="errors.address"> | ||
[[errors.address]] | ||
</div> | ||
|
||
<div class="col-12 text-danger" v-if="errors.invalidForm"> | ||
[[errors.invalidForm]] | ||
</div> | ||
|
||
</div> | ||
</div> | ||
|
||
<div class="container mt-5"> | ||
<div class="row"> | ||
<div class="col-12 text-center"> | ||
<button class="btn btn-gc-blue btn-lg mb-3 px-5 btn-lg-padding" :disabled="submitted" type="submit" @click="ingest($event)">Add Contributions</button> | ||
</div> | ||
<div class="col-12 text-center" v-if="Object.keys(errors).length > 0"> | ||
Please verify forms errors and try again | ||
</div> | ||
<div class="col-12 text-center" v-else-if="submitted"> | ||
Processing your contributions. This may take a minute or two... | ||
</div> | ||
</div> | ||
</div> | ||
|
||
</form> | ||
</grants-ingest-contributions> | ||
|
||
{% include 'shared/bottom_notification.html' %} | ||
{% include 'shared/footer.html' %} | ||
{% include 'shared/current_profile.html' %} | ||
{% include 'shared/analytics.html' %} | ||
{% include 'grants/shared/shared_scripts.html' %} | ||
{% include 'shared/footer_scripts.html' with vue=True ignore_inject_web3=1 %} | ||
|
||
<script type="text/javascript" src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js"></script> | ||
<script src="{% static "v2/js/grants/ingest-missing-contributions.js" %}"></script> | ||
|
||
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | ||
|
||
<script src="{% static "v2/js/lib/ipfs-api.js" %}"></script> | ||
<script src="{% static "v2/js/ipfs.js" %}"></script> | ||
<script src="{% static "v2/js/abi.js" %}"></script> | ||
|
||
<script src="{% static "v2/js/tokens.js" %}"></script> | ||
<script src="{% static "v2/js/grants/shared.js" %}"></script> | ||
|
||
<script src="{% static "v2/js/grants/new_match.js" %}"></script> | ||
|
||
</body> | ||
|
||
<html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.