Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alert user if insufficient tokens for tip #3058

Merged
merged 6 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions app/assets/onepager/js/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ $(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();
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
Expand Down Expand Up @@ -117,7 +120,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 +154,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();
Expand All @@ -168,15 +171,21 @@ 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);
}
var amountInWei = amountInEth * 1.0 * weiConvert;

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 != '';

Expand All @@ -191,7 +200,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 @@ -207,7 +216,6 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p
return;
}


var got_metadata_callback = function(metadata) {
const url = '/tip/send/3';

Expand All @@ -222,7 +230,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 @@ -285,14 +293,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 @@ -308,7 +316,7 @@ function sendTip(email, github_url, from_name, username, amountInEth, comments_p
} else {
send_gas_money_and_erc20();
}

}
}
});
Expand Down Expand Up @@ -361,4 +369,4 @@ var etherscanDomain = function() {
// mainnet
}
return etherscanDomain;
};
};
26 changes: 4 additions & 22 deletions app/assets/v2/js/pages/new_bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions app/assets/v2/js/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -1550,3 +1550,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.') {

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;
let balance = result.toNumber() / Math.pow(10, token_decimals);
let balance_rounded = Math.round(balance * 10) / 10;

if (parseFloat(amount) > balance) {
let msg1 = gettext(msg);
let 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 %}