From 88c981797cde3d0776cb1aced2f6de66ae55de58 Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Mon, 3 Dec 2018 14:41:38 -0800 Subject: [PATCH] Alert user if insufficient tokens for tip --- app/assets/onepager/js/send.js | 30 ++++++++++++--------- app/assets/v2/js/pages/new_bounty.js | 28 +++---------------- app/assets/v2/js/shared.js | 27 +++++++++++++++++++ app/dashboard/templates/onepager/send1.html | 2 +- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/app/assets/onepager/js/send.js b/app/assets/onepager/js/send.js index 1b623ba7d13..be12b980b85 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) { mixpanel.track('Tip Step 2 Click', {}); if (typeof web3 == 'undefined') { _alert({ message: gettext('You must have a web3 enabled browser to do this. Please download Metamask.') }, 'warning'); @@ -169,15 +169,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 != ''; @@ -192,7 +197,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; @@ -209,6 +214,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p } + var got_metadata_callback = function(metadata) { const url = '/tip/send/3'; @@ -223,7 +229,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, @@ -284,14 +290,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'); @@ -307,7 +313,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p } else { send_gas_money_and_erc20(); } - + } } }); @@ -360,4 +366,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 4df741b3583..63f3bfc78f9 100644 --- a/app/assets/v2/js/pages/new_bounty.js +++ b/app/assets/v2/js/pages/new_bounty.js @@ -306,9 +306,11 @@ $(document).ready(function() { var isETH = tokenAddress == '0x0000000000000000000000000000000000000000'; var token_contract = web3.eth.contract(token_abi).at(tokenAddress); 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; @@ -439,25 +441,3 @@ $(document).ready(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; - - if (parseFloat(amount) > 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'); - } - }); - - -}; diff --git a/app/assets/v2/js/shared.js b/app/assets/v2/js/shared.js index 8e0466401ac..74f06bbfe11 100644 --- a/app/assets/v2/js/shared.js +++ b/app/assets/v2/js/shared.js @@ -1336,3 +1336,30 @@ function shuffleArray(array) { } return array; } + +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']; + var ret_balances; + + 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 %}