Skip to content

Commit

Permalink
Merge branch 'master' into issue/6948
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfred authored Jun 25, 2020
2 parents 073fbf8 + 24f50e2 commit 151cd8c
Show file tree
Hide file tree
Showing 51 changed files with 493 additions and 212 deletions.
1 change: 1 addition & 0 deletions app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@
path('_administration/email/wallpost', retail.emails.wallpost, name='wallpost_email'),
path('_administration/email/grant_update', retail.emails.grant_update, name='grant_update_email'),
path('_administration/email/grant_recontribute', retail.emails.grant_recontribute, name='grant_recontribute_email'),
path('_administration/email/grant_txn_failed', retail.emails.grant_txn_failed, name='grant_txn_failed_email'),
path(
'_administration/email/new_bounty_acceptance',
retail.emails.new_bounty_acceptance,
Expand Down
4 changes: 0 additions & 4 deletions app/assets/v2/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,6 @@ $(document).ready(function() {
_alert('Please login first.', 'error');
return;
}
if (!web3) {
_alert('Please enable and unlock your web3 wallet.', 'error');
return;
}

if (!provider) {
return onConnect();
Expand Down
75 changes: 58 additions & 17 deletions app/assets/v2/js/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
* @dev If you need to interact with the Rinkeby Dai contract (e.g. to reset allowances for
* testing), use this one click dapp: https://oneclickdapp.com/drink-leopard/
*/
let BN;

needWalletConnection();
window.addEventListener('dataWalletReady', function(e) {
BN = web3.utils.BN;
}, false);
// Constants
const BN = web3.utils.BN;
const ETH_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
const gitcoinAddress = '0x00De4B13153673BCAE2616b67bf822500d325Fc3'; // Gitcoin donation address for mainnet and rinkeby

Expand Down Expand Up @@ -199,16 +203,47 @@ Vue.component('grants-cart', {

// Estimated gas limit for the transaction
donationInputsGasLimit() {
// Based on contract tests, we use the heuristic below to get gas estimate. This is done
// instead of `estimateGas()` so we can send the donation transaction before the approval txs
// are confirmed, because if the approval txs are not confirmed then estimateGas will fail.
// The estimates used here are based on single donations (i.e. one item in the cart). Because
// gas prices go down with batched transactions, whereas this assumes they're constant,
// this gives us a conservative estimate
// The below heuristics are used instead of `estimateGas()` so we can send the donation
// transaction before the approval txs are confirmed, because if the approval txs
// are not confirmed then estimateGas will fail.

// If we have a cart where all donations are in Dai, we use a linear regression to
// estimate gas costs based on real checkout transaction data, and add a 50% margin
const donationCurrencies = this.donationInputs.map(donation => donation.token);
const isAllDai = donationCurrencies.every((addr, index, array) => addr === array[0]);

if (isAllDai) {
if (donationCurrencies.length === 1) {
// Special case since we overestimate here otherwise
return 80000;
}
// Below curve found by running script at the repo below around 9AM PT on 2020-Jun-19
// then generating a conservative best-fit line
// https://github.com/mds1/Gitcoin-Checkout-Gas-Analysis
return 25000 * donationCurrencies.length + 125000;
}

// Otherwise, based on contract tests, we use the more conservative heuristic below to get
// a gas estimate. The estimates used here are based on testing the cost of a single
// donation (i.e. one item in the cart). Because gas prices go down with batched
// transactions, whereas this assumes they're constant, this gives us a conservative estimate
const gasLimit = this.donationInputs.reduce((accumulator, currentValue) => {
return currentValue.token === ETH_ADDRESS
? accumulator + 50000// ETH donation gas estimate
: accumulator + 100000; // token donation gas estimate
const tokenAddr = currentValue.token.toLowerCase();

if (currentValue.token === ETH_ADDRESS) {
return accumulator + 50000; // ETH donation gas estimate

} else if (tokenAddr === '0x960b236A07cf122663c4303350609A66A7B288C0'.toLowerCase()) {
return accumulator + 170000; // ANT donation gas estimate

} else if (tokenAddr === '0xfC1E690f61EFd961294b3e1Ce3313fBD8aa4f85d'.toLowerCase()) {
return accumulator + 500000; // aDAI donation gas estimate

} else if (tokenAddr === '0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643'.toLowerCase()) {
return accumulator + 450000; // cDAI donation gas estimate

}
return accumulator + 100000; // generic token donation gas estimate
}, 0);

return gasLimit;
Expand Down Expand Up @@ -486,7 +521,7 @@ Vue.component('grants-cart', {
// Setup -----------------------------------------------------------------------------------
// Prompt web3 login if not connected
if (!provider) {
await onConnect();
return await onConnect();
}

// Throw if invalid Gitcoin contribution percentage
Expand All @@ -511,6 +546,16 @@ Vue.component('grants-cart', {
// For each token, check if an approval is needed, and if so save off the data
let allowanceData = [];

const calcTotalAllowance = (tokenDetails) => {
const initialValue = new BN('0');

return this.donationInputs.reduce((accumulator, currentValue) => {
return currentValue.token === tokenDetails.addr
? accumulator.add(new BN(currentValue.amount)) // correct token donation
: accumulator.add(new BN('0')); // ETH donation
}, initialValue);
};

for (let i = 0; i < selectedTokens.length; i += 1) {
const tokenName = selectedTokens[i];
const tokenDetails = this.getTokenByName(tokenName);
Expand All @@ -535,12 +580,8 @@ Vue.component('grants-cart', {
// Get required allowance based on donation amounts
// We use reduce instead of this.donationsTotal because this.donationsTotal will
// not have floating point errors, but the actual amounts used will
const initialValue = new BN('0');
const requiredAllowance = this.donationInputs.reduce((accumulator, currentValue) => {
return currentValue.token === tokenDetails.addr
? accumulator.add(new BN(currentValue.amount)) // correct token donation
: accumulator.add(new BN('0')); // ETH donation
}, initialValue);

const requiredAllowance = calcTotalAllowance(tokenDetails);

// Check user token balance against requiredAllowance
const userTokenBalance = await tokenContract.methods.balanceOf(userAddress).call({ from: userAddress });
Expand Down
9 changes: 4 additions & 5 deletions app/assets/v2/js/grants/fund.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,7 @@ $(document).ready(function() {
$('#transaction_url').attr('href', linkURL);
enableWaitState('#grants_form');
set_form_disabled(false);
$('#tweetModal').css('display', 'block');

$('#tweetModal').modal('show');
};

if (!gitcoin_amount) {
Expand Down Expand Up @@ -558,7 +557,7 @@ const signSubscriptionHash = (subscriptionHash) => {
web3.eth.personal.sign('' + subscriptionHash, accounts[0], function(err, signature) {
indicateMetamaskPopup(true);
set_form_disabled(false);
$('#tweetModal').css('display', 'block');
$('#tweetModal').modal('show');

if (signature) {
$('#signature').val(signature);
Expand Down Expand Up @@ -669,7 +668,7 @@ const splitPayment = (account, toFirst, toSecond, valueFirst, valueSecond) => {
}).on('transactionHash', function(transactionHash) {
indicateMetamaskPopup(1);
set_form_disabled(false);
$('#tweetModal').css('display', 'block');
$('#tweetModal').modal('show');
data = {
'subscription_hash': 'onetime',
'signature': 'onetime',
Expand All @@ -690,7 +689,7 @@ const splitPayment = (account, toFirst, toSecond, valueFirst, valueSecond) => {
$('#transaction_url').attr('href', linkURL);
enableWaitState('#grants_form');
set_form_disabled(false);
$('#tweetModal').css('display', 'block');
$('#tweetModal').modal('show');
}).on('confirmation', function(confirmationNumber, receipt) {
data = {
'subscription_hash': 'onetime',
Expand Down
9 changes: 1 addition & 8 deletions app/assets/v2/js/grants/funding.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $(document).ready(function() {
_alert(message, 'success');
localStorage.removeItem('contributions_were_successful');
localStorage.removeItem('contributions_count');
$('#tweetModal').css('display', 'block');
$('#tweetModal').modal('show');
let donations = CartData.loadCart();

if (donations.length) {
Expand All @@ -31,13 +31,6 @@ $(document).ready(function() {
$('#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);
}
CartData.setCart([]);

$(document).keydown(function(e) {
if (e.keyCode == 27) {
$('#tweetModal').remove();
}
});

}

$('#js-addToCart-form').submit(function(event) {
Expand Down
112 changes: 45 additions & 67 deletions app/assets/v2/js/grants/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,8 @@ function changeTokens() {
window.addEventListener('tokensReady', function(e) {
changeTokens();
}, false);
needWalletConnection();

$(document).ready(function() {

$('.select2-selection__choice').removeAttr('title');

if (web3 && web3.eth) {
web3.eth.net.isListening((error, connectionStatus) => {
if (connectionStatus)
init();
document.init = true;
});
}
// fix for triage bug https://gitcoincore.slack.com/archives/CAXQ7PT60/p1551220641086800
setTimeout(function() {
if (!document.init) {
show_error_banner();
}
}, 1000);
});

function saveGrant(grantData, isFinal) {
let csrftoken = $("#create-grant input[name='csrfmiddlewaretoken']").val();

$.ajax({
type: 'post',
url: '/grants/new',
processData: false,
contentType: false,
data: grantData,
headers: {'X-CSRFToken': csrftoken},
success: json => {
if (isFinal) {
if (json.url) {
document.suppress_loading_leave_code = true;
window.location = json.url;
} else {
console.error('Grant failed to save');
}
}
},
error: () => {
console.error('Grant failed to save');
_alert({ message: gettext('Your grant failed to save. Please try again.') }, 'error');
}
});
}


$('#new_button').on('click', function(e) {
if (!provider) {
e.preventDefault();
return onConnect().then(() => init());
}
});

const init = () => {
/*
Expand All @@ -79,11 +27,8 @@ const init = () => {
window.location = document.location.origin + '/grants/quickstart';
}

web3.eth.getAccounts(function(err, accounts) {
$('#input-admin_address').val(accounts[0]);
$('#contract_owner_address').val(accounts[0]);
});

$('#input-admin_address').val(selectedAccount);
$('#contract_owner_address').val(selectedAccount);
userSearch('.team_members', false, undefined, false, false, true);

addGrantLogo();
Expand Down Expand Up @@ -206,14 +151,47 @@ const init = () => {
});
};

window.addEventListener('load', async() => {
if (!provider && !web3Modal.cachedProvider || provider === 'undefined') {
onConnect().then(() => {
init();
});
} else {
web3Modal.on('connect', async(data) => {
init();
});
window.addEventListener('dataWalletReady', function(e) {
init();
}, false);

$(document).ready(function() {

$('.select2-selection__choice').removeAttr('title');

});

function saveGrant(grantData, isFinal) {
let csrftoken = $("#create-grant input[name='csrfmiddlewaretoken']").val();

$.ajax({
type: 'post',
url: '/grants/new',
processData: false,
contentType: false,
data: grantData,
headers: {'X-CSRFToken': csrftoken},
success: json => {
if (isFinal) {
if (json.url) {
document.suppress_loading_leave_code = true;
window.location = json.url;
} else {
console.error('Grant failed to save');
}
}
},
error: () => {
console.error('Grant failed to save');
_alert({ message: gettext('Your grant failed to save. Please try again.') }, 'error');
}
});
}


$('#new_button').on('click', function(e) {
if (!provider) {
e.preventDefault();
return onConnect().then(() => init());
}
});
12 changes: 11 additions & 1 deletion app/assets/v2/js/pages/bulk_payout.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ const normalizeUsername = function(username) {
return username;
};

needWalletConnection();

window.addEventListener('dataWalletReady', function(e) {
update_registry(selectedAccount);
}, false);

$(document).ready(function($) {

$(document).on('blur', '#amount', function(event) {
Expand Down Expand Up @@ -167,6 +173,11 @@ $(document).ready(function($) {
getFulfillers();
update_registry(selectedAccount);

if (!provider) {
onConnect();
return false;
}

if (!$('#terms').is(':checked')) {
_alert('Please accept the TOS.', 'error');
return;
Expand All @@ -190,7 +201,6 @@ $(document).ready(function($) {

$('document').ready(function() {
add_row();
update_registry(selectedAccount);

$('.add_another').on('click', function() {
add_row();
Expand Down
Loading

0 comments on commit 151cd8c

Please sign in to comment.