Skip to content

Commit

Permalink
Merge pull request #7718 from ScopeLift/zcash-eth-fixes
Browse files Browse the repository at this point in the history
ZCash-Ethereum cart fixes
  • Loading branch information
octavioamu authored Oct 21, 2020
2 parents 0f2d428 + 4b8afd7 commit d2afd1d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
37 changes: 21 additions & 16 deletions app/assets/v2/js/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ Vue.component('grants-cart', {

// Array of arrays, item i lists supported tokens for donating to grant given by grantData[i]
currencies() {
if (!this.grantData || !this.tokenList)
if (!this.grantsByTenant || !this.tokenList)
return undefined;

// Get supported tokens for each grant
const currencies = this.grantData.map(grant => {
const currencies = this.grantsByTenant.map(grant => {
// Return full list if grant accepts all tokens
if (grant.grant_token_address === '0x0000000000000000000000000000000000000000') {
return this.tokenList.map(token => token.name);
Expand Down Expand Up @@ -242,12 +242,12 @@ Vue.component('grants-cart', {

// Array of objects containing all donations and associated data
donationInputs() {
if (!this.grantData)
if (!this.grantsByTenant)
return undefined;

// Generate array of objects containing donation info from cart
let gitcoinFactor = 100 * this.gitcoinFactor;
const donations = this.grantData.map((grant, index) => {
const donations = this.grantsByTenant.map((grant, index) => {
const tokenDetails = this.getTokenByName(grant.grant_donation_currency);
const amount = this.toWeiString(
Number(grant.grant_donation_amount),
Expand Down Expand Up @@ -781,7 +781,7 @@ Vue.component('grants-cart', {
donationSummaryTotals(scaleFactor = 1) {
const totals = {};

this.grantData.forEach(grant => {
this.grantsByTenant.forEach(grant => {
if (!totals[grant.grant_donation_currency]) {
// First time seeing this token, set the field and initial value
totals[grant.grant_donation_currency] = Number(grant.grant_donation_amount) * scaleFactor;
Expand All @@ -805,6 +805,10 @@ Vue.component('grants-cart', {
let string = '';

Object.keys(this[propertyName]).forEach(key => {
// key is our token symbol, so for now let's skip this if key is ZEC
if (key === 'ZEC')
return;

// Round to 2 digits
const amount = this[propertyName][key];
const formattedAmount = amount.toLocaleString(undefined, {
Expand Down Expand Up @@ -888,10 +892,16 @@ Vue.component('grants-cart', {
const preferredAmount = grant.grant_donation_amount;
const preferredTokenName = grant.grant_donation_currency;
const fallbackAmount = await this.valueToEth(preferredAmount, preferredTokenName);
const tenant = grant.tenant[0];

this.grantData.forEach((grant, index) => {
const acceptedCurrencies = this.currencies[index]; // tokens accepted by this grant


// Skip this loop if this grant is not the same tenant as the clicked grant
if (this.grantData[index].tenant[0] !== tenant)
return;

// Update the values
if (!acceptedCurrencies.includes(preferredTokenName)) {
// If the selected token is not available, fallback to ETH
this.grantData[index].grant_donation_amount = fallbackAmount;
Expand Down Expand Up @@ -1173,7 +1183,7 @@ Vue.component('grants-cart', {
},

/**
* @notice POSTs donation data to database. Intended to be called from finalizeCheckout()
* @notice POSTs donation data to database
*/
async postToDatabase(txHash, contractAddress, userAddress) {
// this.donationInputs is the array used for bulk donations
Expand Down Expand Up @@ -1278,18 +1288,13 @@ Vue.component('grants-cart', {
async finalizeCheckout() {
// Clear cart, redirect back to grants page, and show success alert
localStorage.setItem('contributions_were_successful', 'true');
localStorage.setItem('contributions_count', String(this.grantData.length));
var network = document.web3network;
localStorage.setItem('contributions_count', String(this.grantsByTenant.length));
let timeout_amount = 1500 + (CartData.loadCart().length * 500);

setTimeout(function() {
_alert('Contributions saved', 'success', 1000);
setTimeout(function() {
if (network === 'rinkeby') {
window.location.href = `${window.location.origin}/grants/?network=rinkeby&category=`;
} else {
window.location.href = `${window.location.origin}/grants`;
}
window.location.href = `${window.location.origin}/grants`;
}, 500);
}, timeout_amount);
},
Expand Down Expand Up @@ -1902,7 +1907,7 @@ Vue.component('grants-cart', {
this.zkSyncFeeTotals[tokenSymbol] = await this.getMaxFee(tokenSymbol);
this.setZkSyncFeesString();

// Note: Don't `break` out of the if statements if insufficient balance, because we
// NOTE: Don't `break` out of the if statements if insufficient balance, because we
// also use this function to set the fee string shown to the user on the checkout modal

// Balance will be undefined if the user does not have that token, so we can break
Expand All @@ -1911,7 +1916,7 @@ Vue.component('grants-cart', {
}

// Otherwise, we compare their balance against the required amount
if (ethers.BigNumber.from(balance).lt(totalRequiredAmount)) {
if (balance && ethers.BigNumber.from(balance).lt(totalRequiredAmount)) {
this.hasSufficientZkSyncBalance = false;
}
}
Expand Down
17 changes: 11 additions & 6 deletions app/assets/v2/js/grants/funding.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ $(document).ready(function() {
const shouldShowAlert = Boolean(localStorage.getItem('contributions_were_successful'));

if (shouldShowAlert) {
// This alert currently shows after Ethereum checkouts only. As a result, we make sure to
// only clear ETH grants from local storage
const numberOfContributions = Number(localStorage.getItem('contributions_count'));
const grantWord = numberOfContributions === 1 ? 'grant' : 'grants';
const message = `You have successfully funded ${numberOfContributions} ${grantWord}. Thank you for your contribution!`;
Expand All @@ -21,23 +23,26 @@ $(document).ready(function() {
localStorage.removeItem('contributions_were_successful');
localStorage.removeItem('contributions_count');
$('#tweetModal').modal('show');
let donations = CartData.loadCart();

if (donations.length) {
const allDonations = CartData.loadCart();
const ethereumDonations = allDonations.filter((grant) => grant.tenants[0] === 'ETH');
const otherDonations = allDonations.filter((grant) => grant.tenants[0] !== 'ETH');

if (allDonations.length) {
let cart_html = 'You just funded: ';
let bulk_add_cart = CartData.share_url();

for (let i = 0; i < donations.length; i += 1) {
const donation = donations[i];
for (let i = 0; i < allDonations.length; i += 1) {
const donation = allDonations[i];

cart_html += '<li><a href=' + donation.grant_url + ' target=_blank>' + donation['grant_title'] + '</a> for ' + donation['grant_donation_amount'] + ' ' + donation['grant_donation_currency'] + ' (+' + donation['grant_donation_clr_match'] + ' DAI match)</li>';
}
cart_html += '<HR><a href=' + bulk_add_cart + ' target=_blank>Here is a handy link</a> for sharing this collection with others.';
$("<span class='mt-2 mb-2 w-100'>" + cart_html + '</span>').insertBefore($('#tweetModal span.copy'));
$('#tweetModal a.button').attr('href', 'https://twitter.com/intent/tweet?text=I%20just%20funded%20these%20' + donations.length + '%20grants%20on%20@gitcoin%20=%3E%20' + bulk_add_cart);
$('#tweetModal a.button').attr('href', 'https://twitter.com/intent/tweet?text=I%20just%20funded%20these%20' + allDonations.length + '%20grants%20on%20@gitcoin%20=%3E%20' + bulk_add_cart);
$('#tweetModal a.button').text('Tweet about it');
}
CartData.setCart([]);
CartData.setCart(otherDonations);
}

$('#js-addToCart-form').submit(function(event) {
Expand Down

0 comments on commit d2afd1d

Please sign in to comment.