From dc273062f7fc8c874ae4784a8d292f3dd16d7286 Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Mon, 3 Dec 2018 14:41:38 -0800 Subject: [PATCH 1/4] Alert user if insufficient tokens for tip --- app/assets/onepager/js/send.js | 30 ++++++++++++--------- app/assets/v2/js/pages/new_bounty.js | 26 +++--------------- app/assets/v2/js/shared.js | 26 ++++++++++++++++++ app/dashboard/templates/onepager/send1.html | 2 +- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/app/assets/onepager/js/send.js b/app/assets/onepager/js/send.js index e5402134a06..095cfea2903 100644 --- a/app/assets/onepager/js/send.js +++ b/app/assets/onepager/js/send.js @@ -82,7 +82,7 @@ $(document).ready(function() { var github_url = $('#issueURL').val(); var from_name = $('#fromName').val(); var username = $('.username-search').select2('data')[0].text; - var amountInEth = parseFloat($('#amount').val()); + var amount = parseFloat($('#amount').val()); var comments_priv = $('#comments_priv').val(); var comments_public = $('#comments_public').val(); var from_email = $('#fromEmail').val(); @@ -117,7 +117,7 @@ $(document).ready(function() { unloading_button($('#send')); }; - return sendTip(email, github_url, from_name, username, amountInEth, comments_public, comments_priv, from_email, accept_tos, tokenAddress, expires, success_callback, failure_callback, false); + return sendTip(email, github_url, from_name, username, amount, comments_public, comments_priv, from_email, accept_tos, tokenAddress, expires, success_callback, failure_callback, false); }); @@ -151,7 +151,7 @@ function isNumeric(n) { } -function sendTip(email, github_url, from_name, username, amountInEth, comments_public, comments_priv, from_email, accept_tos, tokenAddress, expires, success_callback, failure_callback, is_for_bounty_fulfiller) { +function sendTip(email, github_url, from_name, username, amount, comments_public, comments_priv, from_email, accept_tos, tokenAddress, expires, success_callback, failure_callback, is_for_bounty_fulfiller) { if (typeof web3 == 'undefined') { _alert({ message: gettext('You must have a web3 enabled browser to do this. Please download Metamask.') }, 'warning'); failure_callback(); @@ -168,15 +168,20 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p var isSendingETH = (tokenAddress == '0x0' || tokenAddress == '0x0000000000000000000000000000000000000000'); var tokenDetails = tokenAddressToDetails(tokenAddress); var tokenName = 'ETH'; - var weiConvert = Math.pow(10, 18); + var denomFactor = Math.pow(10, 18); var creation_time = Math.round((new Date()).getTime() / 1000); var salt = parseInt((Math.random() * 1000000)); if (!isSendingETH) { tokenName = tokenDetails.name; - weiConvert = Math.pow(10, tokenDetails.decimals); + denomFactor = Math.pow(10, tokenDetails.decimals); + + check_balance_and_alert_user_if_not_enough( + tokenAddress, + amount, + 'You do not have enough tokens to send this tip.'); } - var amountInWei = amountInEth * 1.0 * weiConvert; + var amountInDenom = amount * 1.0 * denomFactor; // validation var hasEmail = email != ''; @@ -191,7 +196,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p failure_callback(); return; } - if (!isNumeric(amountInWei) || amountInWei == 0) { + if (!isNumeric(amountInDenom) || amountInDenom == 0) { _alert({ message: gettext('You must enter an number for the amount!') }, 'warning'); failure_callback(); return; @@ -207,7 +212,6 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p return; } - var got_metadata_callback = function(metadata) { const url = '/tip/send/3'; @@ -222,7 +226,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p username: username, email: email, tokenName: tokenName, - amount: amountInEth, + amount: amount, comments_priv: comments_priv, comments_public: comments_public, expires_date: expires, @@ -285,14 +289,14 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p if (isSendingETH) { web3.eth.sendTransaction({ to: destinationAccount, - value: amountInWei, + value: amountInDenom, gasPrice: web3.toHex(get_gas_price()) }, post_send_callback); } else { var send_erc20 = function() { var token_contract = web3.eth.contract(token_abi).at(tokenAddress); - token_contract.transfer(destinationAccount, amountInWei, {gasPrice: web3.toHex(get_gas_price())}, post_send_callback); + token_contract.transfer(destinationAccount, amountInDenom, {gasPrice: web3.toHex(get_gas_price())}, post_send_callback); }; var send_gas_money_and_erc20 = function() { _alert({ message: gettext('You will now be asked to confirm two transactions. The first is gas money, so your receipient doesnt have to pay it. The second is the actual token transfer. (note: check Metamask extension, sometimes the 2nd confirmation window doesnt popup)') }, 'info'); @@ -308,7 +312,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p } else { send_gas_money_and_erc20(); } - + } } }); @@ -361,4 +365,4 @@ var etherscanDomain = function() { // mainnet } return etherscanDomain; -}; \ No newline at end of file +}; diff --git a/app/assets/v2/js/pages/new_bounty.js b/app/assets/v2/js/pages/new_bounty.js index fcbfea672ed..2767b393a99 100644 --- a/app/assets/v2/js/pages/new_bounty.js +++ b/app/assets/v2/js/pages/new_bounty.js @@ -374,7 +374,10 @@ $(document).ready(function() { var account = web3.eth.coinbase; if (!isETH) { - check_balance_and_alert_user_if_not_enough(tokenAddress, amount); + check_balance_and_alert_user_if_not_enough( + tokenAddress, + amount, + 'You do not have enough tokens to fund this bounty.'); } amount = amount * decimalDivisor; @@ -621,27 +624,6 @@ $(window).on('load', function() { }); }); -var check_balance_and_alert_user_if_not_enough = function(tokenAddress, amount) { - var token_contract = web3.eth.contract(token_abi).at(tokenAddress); - var from = web3.eth.coinbase; - var token_details = tokenAddressToDetails(tokenAddress); - var token_decimals = token_details['decimals']; - var token_name = token_details['name']; - - token_contract.balanceOf.call(from, function(error, result) { - if (error) return; - var balance = result.toNumber() / Math.pow(10, token_decimals); - var balance_rounded = Math.round(balance * 10) / 10; - const total = parseFloat(amount) + parseFloat((parseFloat(amount) / FEE_PERCENTAGE).toFixed(4)); - - if (parseFloat(total) > balance) { - var msg = gettext('You do not have enough tokens to fund this bounty. You have ') + balance_rounded + ' ' + token_name + ' ' + gettext(' but you need ') + amount + ' ' + token_name; - - _alert(msg, 'warning'); - } - }); -}; - let usdFeaturedPrice = $('.featured-price-usd').text(); let ethFeaturedPrice; let bountyFee; diff --git a/app/assets/v2/js/shared.js b/app/assets/v2/js/shared.js index bebea749ab2..506b606862b 100644 --- a/app/assets/v2/js/shared.js +++ b/app/assets/v2/js/shared.js @@ -1532,3 +1532,29 @@ $(document).ready(function() { }); }); }); + +function check_balance_and_alert_user_if_not_enough( + tokenAddress, + amount, + msg = 'You do not have enough tokens to perform this action.') { + + var token_contract = web3.eth.contract(token_abi).at(tokenAddress); + var from = web3.eth.coinbase; + var token_details = tokenAddressToDetails(tokenAddress); + var token_decimals = token_details['decimals']; + var token_name = token_details['name']; + + token_contract.balanceOf.call(from, function(error, result) { + if (error) return; + var balance = result.toNumber() / Math.pow(10, token_decimals); + var balance_rounded = Math.round(balance * 10) / 10; + + if (parseFloat(amount) > balance) { + var msg1 = gettext(msg); + var msg2 = gettext(' You have ') + balance_rounded + ' ' + token_name + ' ' + gettext(' but you need ') + amount + ' ' + token_name; + + _alert(msg1 + msg2, 'warning'); + } + }); + +} diff --git a/app/dashboard/templates/onepager/send1.html b/app/dashboard/templates/onepager/send1.html index 4f47667647f..27059a911e4 100644 --- a/app/dashboard/templates/onepager/send1.html +++ b/app/dashboard/templates/onepager/send1.html @@ -41,6 +41,6 @@

{% trans "It's Fast. It's Easy. It's Free.️" %}

- {% trans "Send Ether" %} 💰 + {% trans "Send Tip" %} 💰 {% endblock %} From d9f495be2241adb01dcc19e1042c99f550d60935 Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Mon, 25 Feb 2019 22:09:00 +0900 Subject: [PATCH 2/4] Change var declarations to --- app/assets/v2/js/shared.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/assets/v2/js/shared.js b/app/assets/v2/js/shared.js index 506b606862b..ec7052c1ae8 100644 --- a/app/assets/v2/js/shared.js +++ b/app/assets/v2/js/shared.js @@ -1538,20 +1538,20 @@ function check_balance_and_alert_user_if_not_enough( amount, msg = 'You do not have enough tokens to perform this action.') { - var token_contract = web3.eth.contract(token_abi).at(tokenAddress); - var from = web3.eth.coinbase; - var token_details = tokenAddressToDetails(tokenAddress); - var token_decimals = token_details['decimals']; - var token_name = token_details['name']; + let token_contract = web3.eth.contract(token_abi).at(tokenAddress); + let from = web3.eth.coinbase; + let token_details = tokenAddressToDetails(tokenAddress); + let token_decimals = token_details['decimals']; + let token_name = token_details['name']; token_contract.balanceOf.call(from, function(error, result) { if (error) return; - var balance = result.toNumber() / Math.pow(10, token_decimals); - var balance_rounded = Math.round(balance * 10) / 10; + let balance = result.toNumber() / Math.pow(10, token_decimals); + let balance_rounded = Math.round(balance * 10) / 10; if (parseFloat(amount) > balance) { - var msg1 = gettext(msg); - var msg2 = gettext(' You have ') + balance_rounded + ' ' + token_name + ' ' + gettext(' but you need ') + amount + ' ' + token_name; + let msg1 = gettext(msg); + let msg2 = gettext(' You have ') + balance_rounded + ' ' + token_name + ' ' + gettext(' but you need ') + amount + ' ' + token_name; _alert(msg1 + msg2, 'warning'); } From 391684d414022a936e782b1de5fab72eac7be1db Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Tue, 5 Mar 2019 14:44:07 +0900 Subject: [PATCH 3/4] Run balance check on Eth when tipping Also, normalize 0x0 to full legnth --- app/assets/onepager/js/send.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/assets/onepager/js/send.js b/app/assets/onepager/js/send.js index 095cfea2903..aa2d01fff3e 100644 --- a/app/assets/onepager/js/send.js +++ b/app/assets/onepager/js/send.js @@ -87,7 +87,10 @@ $(document).ready(function() { var comments_public = $('#comments_public').val(); var from_email = $('#fromEmail').val(); var accept_tos = $('#tos').is(':checked'); - var tokenAddress = $('#token').val(); + var tokenAddress = ( + ($('#token').val()=='0x0') ? + '0x0000000000000000000000000000000000000000' + :$('#token').val()); var expires = parseInt($('#expires').val()); // derived info @@ -175,12 +178,13 @@ function sendTip(email, github_url, from_name, username, amount, comments_public if (!isSendingETH) { tokenName = tokenDetails.name; denomFactor = Math.pow(10, tokenDetails.decimals); - - check_balance_and_alert_user_if_not_enough( - tokenAddress, - amount, - 'You do not have enough tokens to send this tip.'); } + + check_balance_and_alert_user_if_not_enough( + tokenAddress, + amount, + 'You do not have enough ' + tokenName +' to send this tip.'); + var amountInDenom = amount * 1.0 * denomFactor; // validation var hasEmail = email != ''; From 978207a9132cc52d815b267553a072dd2b712b19 Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Wed, 6 Mar 2019 16:24:05 +0900 Subject: [PATCH 4/4] Fix js lint errors --- app/assets/onepager/js/send.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/assets/onepager/js/send.js b/app/assets/onepager/js/send.js index aa2d01fff3e..e53e4391a4e 100644 --- a/app/assets/onepager/js/send.js +++ b/app/assets/onepager/js/send.js @@ -88,9 +88,9 @@ $(document).ready(function() { var from_email = $('#fromEmail').val(); var accept_tos = $('#tos').is(':checked'); var tokenAddress = ( - ($('#token').val()=='0x0') ? - '0x0000000000000000000000000000000000000000' - :$('#token').val()); + ($('#token').val() == '0x0') ? + '0x0000000000000000000000000000000000000000' + : $('#token').val()); var expires = parseInt($('#expires').val()); // derived info @@ -183,7 +183,7 @@ function sendTip(email, github_url, from_name, username, amount, comments_public check_balance_and_alert_user_if_not_enough( tokenAddress, amount, - 'You do not have enough ' + tokenName +' to send this tip.'); + 'You do not have enough ' + tokenName + ' to send this tip.'); var amountInDenom = amount * 1.0 * denomFactor; // validation