Skip to content

Commit

Permalink
Alert user if insufficient tokens for tip
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjw committed Dec 3, 2018
1 parent 25d19ef commit 88c9817
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
30 changes: 18 additions & 12 deletions app/assets/onepager/js/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

});

Expand Down Expand Up @@ -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');
Expand All @@ -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 != '';

Expand All @@ -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;
Expand All @@ -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';

Expand All @@ -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,
Expand Down Expand Up @@ -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');
Expand All @@ -307,7 +313,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p
} else {
send_gas_money_and_erc20();
}

}
}
});
Expand Down Expand Up @@ -360,4 +366,4 @@ var etherscanDomain = function() {
// mainnet
}
return etherscanDomain;
};
};
28 changes: 4 additions & 24 deletions app/assets/v2/js/pages/new_bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
});


};
27 changes: 27 additions & 0 deletions app/assets/v2/js/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});

}
2 changes: 1 addition & 1 deletion app/dashboard/templates/onepager/send1.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ <h3>{% trans "It's Fast. It's Easy. It's Free.️" %}</h3>
<option value="50">50 {% trans "receipients" %}</option>
</select>
</header>
<a href="{% url "send_tip_2" %}" id="neweth" class="button button--primary">{% trans "Send Ether" %} <span class="emoji">💰</span></a>
<a href="{% url "send_tip_2" %}" id="neweth" class="button button--primary">{% trans "Send Tip" %} <span class="emoji">💰</span></a>
</section>
{% endblock %}

0 comments on commit 88c9817

Please sign in to comment.