Skip to content

Instantly share code, notes, and snippets.

@mandb
Last active August 29, 2015 14:02
Show Gist options
  • Save mandb/a885607137a844635289 to your computer and use it in GitHub Desktop.
Save mandb/a885607137a844635289 to your computer and use it in GitHub Desktop.
mintpall sell-to click
$('#buyTable, #sellTable').on('click', '.clickableOrder', function(event) {
event.preventDefault();
var myClass=this.className;
var sumPairSource = parseFloat($(this).find('.amount').text());
var sumPairTarget = parseFloat($(this).find('.amount').text()) * parseFloat($(this).find('.price').text());
var orderQty = 0; // coin order tally
var orderPrice = 0; // order price avg per coin
var orderValue = 0; // total exchange value of coin order
var pairTargetBalance = 0;
var pairSourceBalance = 0;
var isBuying = 0;
var contLoop = 1;
var isLogged = false;
if ($(this).parent().parent()[0].id == 'sellTable'){
isBuying = 1;
}
if ($('.exchangeBalance').length){
pairTargetBalance = parseFloat($('.exchangeBalance').text()) - (parseFloat($('.exchangeBalance').text())*(buyerFee)); // accomodate for buying fees
pairSourceBalance = parseFloat($('.coinBalance').text());
console.log('coinbal: ' + pairSourceBalance + ', exchangebal: ' + pairTargetBalance);
isLogged = true;
}
var my_id = this.id;
var sumQty = 0;
$(this).parent().children('.'+myClass).each(function(i,e){
if (isBuying){
console.log('buying...');
if (!isLogged){
orderValue += (parseFloat($(e).find('.amount').text())) * parseFloat($(e).find('.price').text());
orderQty += parseFloat($(e).find('.amount').text());
}else{
if ((pairTargetBalance - orderValue) <= 0){
return false;
}else if((pairTargetBalance - orderValue) < (parseFloat($(e).find('.amount').text()) * parseFloat($(e).find('.price').text()))){
// user can only buy a fraction fo this order.
sumQty = (pairTargetBalance - orderValue)/parseFloat($(e).find('.price').text());
orderValue += sumQty * parseFloat($(e).find('.price').text());
orderQty += sumQty;
return false;
}else{
orderValue += (parseFloat($(e).find('.amount').text())) * parseFloat($(e).find('.price').text());
orderQty += parseFloat($(e).find('.amount').text());
}
}
}else{
// is selling
console.log('selling...');
if (!isLogged){
orderValue += (parseFloat($(e).find('.amount').text())) * parseFloat($(e).find('.price').text());
orderQty += parseFloat($(e).find('.amount').text());
}else{
if ((pairSourceBalance - orderQty) <= 0){
return false;
}else if( (pairSourceBalance - orderQty) < (parseFloat($(e).find('.amount').text()))){
// user can only sell into a fraction of this order.
sumQty = (pairSourceBalance - orderQty);
orderValue += sumQty * parseFloat($(e).find('.price').text());
orderQty += sumQty;
return false;
}else{
orderValue += (parseFloat($(e).find('.amount').text())) * parseFloat($(e).find('.price').text());
orderQty += parseFloat($(e).find('.amount').text());
}
}
}
if (e.id == my_id){
// at target
return false;
}
});
console.log('orderQty: ' + orderQty + ', orderValue: ' + orderValue);
if (isBuying){
$('.buyForm input[name="amount"]').val(orderQty);
if (orderQty == 0){
$('.buyForm input[name="price"]').val(0);
}else{
$('.buyForm input[name="price"]').val(orderValue/orderQty);
}
$('.sellForm input[name="amount"]').val(0);
$('.sellForm input[name="price"]').val(0);
}else{
$('.sellForm input[name="amount"]').val(orderQty);
if (orderQty == 0){
$('.sellForm input[name="price"]').val(0);
}else{
$('.sellForm input[name="price"]').val(orderValue/orderQty);
}
$('.buyForm input[name="amount"]').val(0);
$('.buyForm input[name="price"]').val(0);
}
$('.buyForm input[name="amount"], .sellForm input[name="amount"]').trigger('change');
$('.buyForm input[name="amount"], .sellForm input[name="amount"]').keyup();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment