|
|
@ -399,11 +399,37 @@ char *LP_tradebot_list(void *ctx,int32_t pubsock,cJSON *argjson) |
|
|
|
|
|
|
|
char *LP_tradebot_buy(int32_t dispdir,char *base,char *rel,double maxprice,double relvolume) |
|
|
|
{ |
|
|
|
struct LP_tradebot *bot; struct iguana_info *basecoin,*relcoin; |
|
|
|
struct LP_tradebot *bot; int32_t i,n; cJSON *array,*item,*retjson; uint64_t txfees,balance=0; struct iguana_info *basecoin,*relcoin; |
|
|
|
basecoin = LP_coinfind(base); |
|
|
|
relcoin = LP_coinfind(rel); |
|
|
|
if ( basecoin == 0 || relcoin == 0 || basecoin->inactive != 0 || relcoin->inactive != 0 ) |
|
|
|
return(clonestr("{\"error\":\"one or more coins inactive\"}")); |
|
|
|
if ( (array= LP_inventory(rel)) != 0 ) |
|
|
|
{ |
|
|
|
if ( (n= cJSON_GetArraySize(array)) > 0 && is_cJSON_Array(array) != 0 ) |
|
|
|
{ |
|
|
|
for (i=0; i<n; i++) |
|
|
|
{ |
|
|
|
item = jitem(array,i); |
|
|
|
//valuesum += j64bits(item,"value") + j64bits(item,"value2");
|
|
|
|
balance += j64bits(item,"satoshis"); |
|
|
|
} |
|
|
|
} |
|
|
|
free_json(array); |
|
|
|
} |
|
|
|
txfees = 10 * relcoin->txfee; |
|
|
|
if ( dstr(balance) < relvolume + dstr(txfees) ) |
|
|
|
{ |
|
|
|
printf("%s inventory balance only %.8f, less than relvolume %.8f + txfees %.8f\n",rel,dstr(balance),relvolume,dstr(txfees)); |
|
|
|
retjson = cJSON_CreateObject(); |
|
|
|
jaddstr(retjson,"error","not enough funds"); |
|
|
|
jaddstr(retjson,"coin",rel); |
|
|
|
jaddnum(retjson,"balance",dstr(balance)); |
|
|
|
jaddnum(retjson,"relvolume",relvolume); |
|
|
|
jaddnum(retjson,"txfees",dstr(txfees)); |
|
|
|
jaddnum(retjson,"shortfall",(relvolume + dstr(txfees)) - dstr(balance)); |
|
|
|
return(jprint(retjson,1)); |
|
|
|
} |
|
|
|
printf("disp.%d tradebot_buy(%s / %s) maxprice %.8f relvolume %.8f\n",dispdir,base,rel,maxprice,relvolume); |
|
|
|
if ( (bot= calloc(1,sizeof(*bot))) != 0 ) |
|
|
|
{ |
|
|
|