You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

509 lines
24 KiB

8 years ago
/******************************************************************************
* Copyright © 2014-2017 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
//
// LP_commands.c
// marketmaker
//
8 years ago
struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srchash,bits256 desthash,char *src,uint64_t srcsatoshis,char *dest,uint64_t destsatoshis,uint32_t timestamp,uint32_t quotetime,int32_t DEXselector)
{
8 years ago
struct basilisk_request R;
8 years ago
memset(rp,0,sizeof(*rp));
rp->srchash = srchash;
rp->desthash = desthash;
rp->srcamount = srcsatoshis;
rp->destamount = destsatoshis;
rp->timestamp = timestamp;
rp->quotetime = quotetime;
rp->DEXselector = DEXselector;
safecopy(rp->src,src,sizeof(rp->src));
safecopy(rp->dest,dest,sizeof(rp->dest));
8 years ago
R = *rp;
8 years ago
rp->requestid = basilisk_requestid(rp);
8 years ago
*rp = R;
8 years ago
rp->quoteid = basilisk_quoteid(rp);
8 years ago
return(rp);
}
8 years ago
cJSON *LP_quotejson(struct LP_quoteinfo *qp)
{
double price; cJSON *retjson = cJSON_CreateObject();
jaddstr(retjson,"base",qp->srccoin);
jaddstr(retjson,"rel",qp->destcoin);
8 years ago
if ( qp->coinaddr[0] != 0 )
jaddstr(retjson,"address",qp->coinaddr);
8 years ago
if ( qp->timestamp != 0 )
jaddnum(retjson,"timestamp",qp->timestamp);
8 years ago
if ( bits256_nonz(qp->txid) != 0 )
{
jaddbits256(retjson,"txid",qp->txid);
jaddnum(retjson,"vout",qp->vout);
}
if ( bits256_nonz(qp->srchash) != 0 )
jaddbits256(retjson,"srchash",qp->srchash);
if ( qp->txfee != 0 )
jadd64bits(retjson,"txfee",qp->txfee);
8 years ago
if ( qp->quotetime != 0 )
jaddnum(retjson,"quotetime",qp->quotetime);
8 years ago
if ( qp->satoshis != 0 )
{
jadd64bits(retjson,"satoshis",qp->satoshis);
price = (double)(qp->destsatoshis+qp->desttxfee) / qp->satoshis;
jaddnum(retjson,"price",price);
}
8 years ago
if ( bits256_nonz(qp->desthash) != 0 )
jaddbits256(retjson,"desthash",qp->desthash);
if ( bits256_nonz(qp->txid) != 0 )
{
jaddbits256(retjson,"txid",qp->txid);
jaddnum(retjson,"vout",qp->vout);
}
if ( bits256_nonz(qp->txid2) != 0 )
{
jaddbits256(retjson,"txid2",qp->txid2);
jaddnum(retjson,"vout2",qp->vout2);
8 years ago
if ( qp->satoshis2 != 0 )
jadd64bits(retjson,"satoshis2",qp->satoshis2);
8 years ago
}
if ( bits256_nonz(qp->desttxid) != 0 )
{
8 years ago
if ( qp->destaddr[0] != 0 )
jaddstr(retjson,"destaddr",qp->destaddr);
8 years ago
jaddbits256(retjson,"desttxid",qp->desttxid);
jaddnum(retjson,"destvout",qp->destvout);
}
8 years ago
if ( qp->destsatoshis != 0 )
jadd64bits(retjson,"destsatoshis",qp->destsatoshis);
if ( qp->desttxfee != 0 )
jadd64bits(retjson,"desttxfee",qp->desttxfee);
8 years ago
if ( qp->change != 0 )
jaddnum(retjson,"change",dstr(qp->change));
return(retjson);
}
int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson)
{
safecopy(qp->srccoin,jstr(argjson,"base"),sizeof(qp->srccoin));
safecopy(qp->coinaddr,jstr(argjson,"address"),sizeof(qp->coinaddr));
safecopy(qp->destcoin,jstr(argjson,"rel"),sizeof(qp->destcoin));
safecopy(qp->destaddr,jstr(argjson,"destaddr"),sizeof(qp->destaddr));
qp->timestamp = juint(argjson,"timestamp");
qp->quotetime = juint(argjson,"quotetime");
qp->txid = jbits256(argjson,"txid");
qp->txid2 = jbits256(argjson,"txid2");
qp->vout = jint(argjson,"vout");
qp->vout2 = jint(argjson,"vout2");
qp->srchash = jbits256(argjson,"srchash");
qp->desttxid = jbits256(argjson,"desttxid");
qp->destvout = jint(argjson,"destvout");
qp->desthash = jbits256(argjson,"desthash");
8 years ago
if ( (qp->satoshis= j64bits(argjson,"satoshis")) == 0 )
qp->satoshis = j64bits(argjson,"value");
if ( (qp->satoshis2= j64bits(argjson,"satoshis2")) == 0 )
qp->satoshis2 = j64bits(argjson,"value2");
8 years ago
qp->destsatoshis = j64bits(argjson,"destsatoshis");
qp->change = SATOSHIDEN * jdouble(argjson,"change");
qp->txfee = j64bits(argjson,"txfee");
qp->desttxfee = j64bits(argjson,"desttxfee");
return(0);
}
8 years ago
double LP_query(char *method,struct LP_quoteinfo *qp,char *ipaddr,uint16_t port,char *base,char *rel,bits256 mypub)
8 years ago
{
8 years ago
cJSON *reqjson; struct LP_peerinfo *peer; int32_t i,flag = 0,pushsock = -1; double price = 0.;
8 years ago
if ( ipaddr != 0 && port >= 1000 )
{
if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),port)) == 0 )
peer = LP_addpeer(1,0,-1,ipaddr,port,port+1,port+2,0,0,0);
if ( peer != 0 )
{
if ( (pushsock= peer->pushsock) >= 0 )
{
8 years ago
qp->desthash = mypub;
strcpy(qp->srccoin,base);
strcpy(qp->destcoin,rel);
reqjson = LP_quotejson(qp);
if ( bits256_nonz(qp->desthash) != 0 )
8 years ago
flag = 1;
8 years ago
jaddstr(reqjson,"method",method);
8 years ago
LP_send(pushsock,jprint(reqjson,1),1);
8 years ago
for (i=0; i<30; i++)
8 years ago
{
8 years ago
if ( (price= LP_pricecache(qp,base,rel,qp->txid,qp->vout)) != 0. )
8 years ago
{
8 years ago
if ( flag == 0 || bits256_nonz(qp->desthash) != 0 )
8 years ago
{
printf("break out of loop.%d price %.8f\n",i,price);
8 years ago
break;
8 years ago
}
8 years ago
}
8 years ago
usleep(250000);
8 years ago
}
} else printf("no pushsock for peer.%s:%u\n",ipaddr,port);
} else printf("cant find/create peer.%s:%u\n",ipaddr,port);
}
return(price);
}
int32_t LP_sizematch(uint64_t mysatoshis,uint64_t othersatoshis)
{
if ( mysatoshis >= othersatoshis )
return(0);
else return(-1);
}
cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base)
{
8 years ago
struct LP_peerinfo *peer,*tmp; struct LP_quoteinfo Q; char *utxostr,coinstr[16]; cJSON *array,*icopy,*retarray=0,*item; int32_t i,n; double price; int64_t estimatedbase;
8 years ago
if ( (price= LP_price(base,myutxo->coin)) == .0 )
return(0);
estimatedbase = myutxo->satoshis / price;
if ( estimatedbase <= 0 )
return(0);
8 years ago
//printf("%s -> %s price %.8f mysatoshis %llu estimated base %llu\n",base,myutxo->coin,price,(long long)myutxo->satoshis,(long long)estimatedbase);
8 years ago
HASH_ITER(hh,LP_peerinfos,peer,tmp)
{
8 years ago
if ( (utxostr= issue_LP_clientgetutxos(peer->ipaddr,peer->port,base,100)) != 0 )
8 years ago
{
8 years ago
//printf("%s:%u %s\n",peer->ipaddr,peer->port,utxostr);
8 years ago
if ( (array= cJSON_Parse(utxostr)) != 0 )
8 years ago
{
if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 )
{
8 years ago
retarray = cJSON_CreateArray();
for (i=0; i<n; i++)
8 years ago
{
item = jitem(array,i);
8 years ago
safecopy(coinstr,jstr(item,"base"),sizeof(coinstr));
8 years ago
if ( strcmp(coinstr,base) == 0 && LP_sizematch(estimatedbase,j64bits(item,"satoshis")) == 0 )
{
icopy = 0;
8 years ago
if ( (price= LP_pricecache(&Q,base,myutxo->coin,jbits256(item,"txid"),jint(item,"vout"))) != 0. )
8 years ago
{
8 years ago
if ( LP_sizematch(myutxo->satoshis,Q.destsatoshis) == 0 )
8 years ago
icopy = jduplicate(item);
} else icopy = jduplicate(item);
if ( icopy != 0 )
{
8 years ago
if ( price != 0. )
jaddnum(icopy,"price",price);
8 years ago
jaddi(retarray,icopy);
}
}
8 years ago
}
}
free_json(array);
}
free(utxostr);
}
8 years ago
if ( retarray != 0 )
break;
}
return(retarray);
}
cJSON *LP_bestprice(struct LP_utxoinfo *utxo,char *base)
{
8 years ago
static bits256 zero;
8 years ago
int32_t i,n,besti; cJSON *array,*item,*bestitem=0; double bestmetric,metric,bestprice=0.,price,prices[100]; struct LP_quoteinfo Q[sizeof(prices)/sizeof(*prices)];
8 years ago
bestprice = 0.;
if ( (array= LP_tradecandidates(utxo,base)) != 0 )
{
if ( (n= cJSON_GetArraySize(array)) > 0 )
{
memset(prices,0,sizeof(prices));
8 years ago
memset(Q,0,sizeof(Q));
8 years ago
for (i=0; i<n && i<sizeof(prices)/sizeof(*prices); i++)
{
item = jitem(array,i);
if ( (price= jdouble(item,"price")) == 0. )
{
8 years ago
LP_quoteparse(&Q[i],item);
8 years ago
char str[65]; printf("i.%d of %d: (%s) -> txid.%s\n",i,n,jprint(item,0),bits256_str(str,Q[i].txid));
8 years ago
price = LP_query("price",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,zero);
8 years ago
printf("price %.8f\n",price);
8 years ago
if ( Q[i].destsatoshis != 0 && (double)j64bits(item,"satoshis")/Q[i].destsatoshis > price )
8 years ago
{
printf("adjustprice %.8f -> %.8f\n",price,(double)j64bits(item,"satoshis")/Q[i].destsatoshis);
8 years ago
price = (double)j64bits(item,"satoshis")/Q[i].destsatoshis;
8 years ago
}
8 years ago
}
if ( (prices[i]= price) != 0. && (bestprice == 0. || price < bestprice) )
bestprice = price;
8 years ago
printf("i.%d price %.8f bestprice %.8f: (%s)\n",i,price,bestprice,jprint(item,0));
8 years ago
}
if ( bestprice != 0. )
{
bestmetric = 0.;
besti = -1;
for (i=0; i<n && i<sizeof(prices)/sizeof(*prices); i++)
{
8 years ago
if ( (price= prices[i]) != 0. && Q[i].destsatoshis != 0 )
8 years ago
{
metric = price / bestprice;
if ( metric > 0.9 )
{
8 years ago
metric = Q[i].destsatoshis / metric * metric * metric;
8 years ago
if ( metric > bestmetric )
{
besti = i;
bestmetric = metric;
}
}
}
}
8 years ago
if ( besti >= 0 )//&& bits256_cmp(utxo->mypub,otherpubs[besti]) == 0 )
8 years ago
{
8 years ago
item = jitem(array,besti);
bestitem = LP_quotejson(&Q[i]);
8 years ago
i = besti;
8 years ago
price = LP_query("request",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,utxo->mypub);
8 years ago
if ( jobj(bestitem,"price") != 0 )
jdelete(bestitem,"price");
jaddnum(bestitem,"price",prices[besti]);
8 years ago
if ( LP_price(base,utxo->coin) > 0.975*price )
{
8 years ago
// the same, cleanup
8 years ago
price = LP_query("connect",&Q[i],jstr(item,"ipaddr"),jint(item,"port"),base,utxo->coin,utxo->mypub);
8 years ago
}
8 years ago
}
8 years ago
}
free_json(array);
}
8 years ago
}
8 years ago
return(bestitem);
}
8 years ago
int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char *destcoin,double price)
{
memset(qp,0,sizeof(*qp));
qp->timestamp = (uint32_t)time(NULL);
8 years ago
safecopy(qp->destcoin,destcoin,sizeof(qp->destcoin));
8 years ago
if ( (qp->txfee= LP_getestimatedrate(utxo->coin)*LP_AVETXSIZE) < 10000 )
qp->txfee = 10000;
if ( qp->txfee >= utxo->satoshis || qp->txfee >= utxo->satoshis2 )
return(-1);
qp->txid = utxo->txid;
qp->vout = utxo->vout;
qp->txid2 = utxo->txid2;
qp->vout2 = utxo->vout2;
qp->satoshis2 = utxo->satoshis2 - qp->txfee;
qp->satoshis = utxo->satoshis - qp->txfee;
qp->destsatoshis = qp->satoshis * price;
if ( (qp->desttxfee= LP_getestimatedrate(qp->destcoin) * LP_AVETXSIZE) < 10000 )
qp->desttxfee = 10000;
if ( qp->desttxfee >= qp->destsatoshis )
return(-2);
qp->destsatoshis -= qp->desttxfee;
safecopy(qp->srccoin,utxo->coin,sizeof(qp->srccoin));
safecopy(qp->coinaddr,utxo->coinaddr,sizeof(qp->coinaddr));
qp->srchash = LP_pubkey(LP_privkey(utxo->coinaddr));
return(0);
}
int32_t LP_quoteinfoset(struct LP_quoteinfo *qp,uint32_t timestamp,uint32_t quotetime,uint64_t value,uint64_t txfee,uint64_t destsatoshis,uint64_t desttxfee,bits256 desttxid,int32_t destvout,bits256 desthash,char *destaddr)
{
if ( txfee != qp->txfee )
{
if ( txfee >= value )
return(-1);
qp->txfee = txfee;
qp->satoshis = value - txfee;
}
qp->timestamp = timestamp;
qp->quotetime = quotetime;
qp->destsatoshis = destsatoshis;
qp->desttxfee = desttxfee;
qp->desttxid = desttxid;
qp->destvout = destvout;
qp->desthash = desthash;
safecopy(qp->destaddr,destaddr,sizeof(qp->destaddr));
return(0);
}
char *LP_quote(cJSON *argjson)
{
struct LP_cacheinfo *ptr; double price; struct LP_quoteinfo Q;
LP_quoteparse(&Q,argjson);
price = (double)(Q.destsatoshis + Q.desttxfee) / (Q.satoshis + Q.txfee);
if ( (ptr= LP_cacheadd(Q.srccoin,Q.destcoin,Q.txid,Q.vout,price,&Q)) != 0 )
8 years ago
{
//SENT.({"base":"KMD","rel":"BTC","address":"RFQn4gNG555woQWQV1wPseR47spCduiJP5","timestamp":1496216835,"price":0.00021141,"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","srchash":"0bcabd875bfa724e26de5f35035ca3767c50b30960e23cbfcbd478cac9147412","txfee":"100000","desttxfee":"10000","value":"10000000000","satoshis":"9999900000","destsatoshis":"2124104","method":"quote"})
8 years ago
ptr->Q = Q;
8 years ago
return(clonestr("{\"result\":\"updated\"}"));
8 years ago
}
8 years ago
else return(clonestr("{\"error\":\"nullptr\"}"));
8 years ago
}
8 years ago
int32_t LP_command(struct LP_peerinfo *mypeer,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen,double profitmargin)
8 years ago
{
8 years ago
char *method,*base,*rel,*retstr,pairstr[512]; cJSON *retjson; double price; bits256 privkey,txid; struct LP_utxoinfo *utxo; int32_t retval = -1,DEXselector = 0; uint64_t destvalue; struct basilisk_request R; struct LP_quoteinfo Q;
8 years ago
if ( (method= jstr(argjson,"method")) != 0 )
{
8 years ago
txid = jbits256(argjson,"txid");
if ( (utxo= LP_utxofind(txid,jint(argjson,"vout"))) != 0 && strcmp(utxo->ipaddr,mypeer->ipaddr) == 0 && utxo->port == mypeer->port && (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 && strcmp(base,utxo->coin) == 0 )
8 years ago
{
8 years ago
printf("LP_command.(%s)\n",jprint(argjson,0));
8 years ago
if ( time(NULL) > utxo->swappending )
utxo->swappending = 0;
if ( strcmp(method,"price") == 0 || strcmp(method,"request") == 0 )
8 years ago
{
8 years ago
retval = 1;
8 years ago
if ( utxo->swappending == 0 )
8 years ago
{
8 years ago
if ( strcmp(method,"request") == 0 && utxo->pair >= 0 )
8 years ago
nn_close(utxo->pair), utxo->pair = -1;
8 years ago
if ( (price= LP_price(base,rel)) != 0. )
{
price *= (1. + profitmargin);
8 years ago
if ( LP_quoteinfoinit(&Q,utxo,rel,price) < 0 )
return(-1);
retjson = LP_quotejson(&Q);
8 years ago
if ( strcmp(method,"request") == 0 )
{
8 years ago
retval |= 2;
8 years ago
utxo->swappending = (uint32_t)(time(NULL) + LP_RESERVETIME);
8 years ago
utxo->otherpubkey = jbits256(argjson,"desthash");
jaddnum(retjson,"quotetime",juint(argjson,"quotetime"));
8 years ago
jaddnum(retjson,"pending",utxo->swappending);
8 years ago
jaddbits256(retjson,"desthash",utxo->otherpubkey);
jaddstr(retjson,"method","reserved");
8 years ago
} else jaddstr(retjson,"method","quote");
8 years ago
retstr = jprint(retjson,1);
LP_send(pubsock,retstr,1);
8 years ago
} else printf("null price\n");
} else printf("swappending.%u pair.%d\n",utxo->swappending,utxo->pair);
8 years ago
}
8 years ago
else if ( strcmp(method,"connect") == 0 )
{
8 years ago
retval = 4;
8 years ago
if ( utxo->pair < 0 )
{
8 years ago
//LP_command.({"txid":"f5d5e2eb4ef85c78f95076d0d2d99af9e1b85968e57b3c7bdb282bd005f7c341","vout":1,"base":"KMD","rel":"BTC","mypub":"3b8f71015d644aaa4c9cceeee289b9a50dc9ec7fafab861c4d5872a8e3844466","satoshis":"0","txfee":"100000","desttxfee":"10000","destsatoshis":"2163363","method":"connect"})
8 years ago
if ( (price= LP_price(base,rel)) != 0. )
{
price *= (1. + profitmargin);
8 years ago
if ( LP_quoteinfoinit(&Q,utxo,rel,price) < 0 )
return(-1);
if ( LP_quoteparse(&Q,argjson) < 0 )
return(-2);
8 years ago
privkey = LP_privkey(utxo->coinaddr);
8 years ago
if ( bits256_nonz(privkey) != 0 && Q.timestamp == utxo->swappending-LP_RESERVETIME && Q.quotetime >= Q.timestamp && Q.quotetime < utxo->swappending && bits256_cmp(utxo->mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee )
8 years ago
{
8 years ago
Q.change = destvalue - (Q.destsatoshis+Q.desttxfee);
8 years ago
nanomsg_tcpname(pairstr,mypeer->ipaddr,10000+(rand() % 10000));
8 years ago
if ( (utxo->pair= nn_socket(AF_SP,NN_PAIR)) < 0 )
printf("error creating utxo->pair\n");
8 years ago
else if ( nn_connect(utxo->pair,pairstr) >= 0 )
8 years ago
{
8 years ago
LP_requestinit(&R,Q.srchash,Q.desthash,base,Q.satoshis,rel,Q.destsatoshis,Q.timestamp,Q.quotetime,DEXselector);
8 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_bobloop,(void *)utxo) == 0 )
8 years ago
{
8 years ago
retjson = LP_quotejson(&Q);
8 years ago
jaddstr(retjson,"method","connected");
8 years ago
jaddstr(retjson,"pair",pairstr);
8 years ago
jaddnum(retjson,"requestid",R.requestid);
jaddnum(retjson,"quoteid",R.quoteid);
retstr = jprint(retjson,1);
LP_send(pubsock,retstr,1);
utxo->swap = LP_swapinit(1,0,privkey,&R);
}
else
{
printf("error launching swaploop\n");
free(utxo->swap);
utxo->swap = 0;
nn_close(utxo->pair);
utxo->pair = -1;
}
}
else
{
8 years ago
printf("printf error nn_connect to %s\n",pairstr);
8 years ago
nn_close(utxo->pair);
utxo->pair = -1;
}
8 years ago
} else printf("dest %.8f < required %.8f (%d %d %d %d %d %d)\n",dstr(Q.satoshis),dstr(price*(utxo->satoshis-Q.txfee)),bits256_nonz(privkey) != 0 ,Q.timestamp == utxo->swappending-LP_RESERVETIME ,Q.quotetime >= Q.timestamp ,Q.quotetime < utxo->swappending ,bits256_cmp(utxo->mypub,Q.srchash) == 0 , LP_txvalue(rel,Q.desttxid,Q.destvout) >= price*Q.satoshis+Q.desttxfee);
8 years ago
} else printf("no price for %s/%s\n",base,rel);
} else printf("utxo->pair.%d when connect came in (%s)\n",utxo->pair,jprint(argjson,0));
}
8 years ago
}
}
8 years ago
return(retval);
8 years ago
}
8 years ago
// add orderbook api
8 years ago
char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port
{
8 years ago
char *method,*ipaddr,*coin,*retstr = 0; uint16_t argport,pushport,subport; int32_t amclient,otherpeers,othernumutxos; struct LP_peerinfo *peer; cJSON *retjson;
8 years ago
if ( (method= jstr(argjson,"method")) == 0 )
return(clonestr("{\"error\":\"need method in request\"}"));
else
{
8 years ago
amclient = (LP_mypeer == 0);
8 years ago
if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 )
{
8 years ago
if ( strcmp(ipaddr,"127.0.0.1") != 0 && port >= 1000 )
8 years ago
{
8 years ago
if ( (pushport= juint(argjson,"push")) == 0 )
pushport = argport + 1;
if ( (subport= juint(argjson,"sub")) == 0 )
subport = argport + 2;
if ( (peer= LP_peerfind((uint32_t)calc_ipbits(ipaddr),argport)) != 0 )
8 years ago
{
8 years ago
if ( (otherpeers= jint(argjson,"numpeers")) > peer->numpeers )
peer->numpeers = otherpeers;
if ( (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos )
{
8 years ago
printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer != 0 ? LP_mypeer->numutxos:0);
8 years ago
peer->numutxos = othernumutxos;
}
//printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0));
8 years ago
} else LP_addpeer(amclient,LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos"));
8 years ago
}
8 years ago
}
8 years ago
printf("CMD.(%s)\n",jprint(argjson,0));
8 years ago
if ( strcmp(method,"quote") == 0 || strcmp(method,"reserved") == 0 )
8 years ago
retstr = LP_quote(argjson);
8 years ago
else if ( IAMCLIENT != 0 && strcmp(method,"connected") == 0 )
8 years ago
{
8 years ago
retstr = jprint(argjson,0);
8 years ago
printf("got connected! (%s)\n",retstr);
}
8 years ago
else if ( IAMCLIENT == 0 && strcmp(method,"getprice") == 0 )
retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel"));
8 years ago
else if ( IAMCLIENT == 0 && strcmp(method,"getpeers") == 0 )
8 years ago
retstr = LP_peers();
8 years ago
else if ( IAMCLIENT == 0 && strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 )
8 years ago
retstr = LP_utxos(LP_mypeer,coin,jint(argjson,"lastn"));
8 years ago
else if ( IAMCLIENT == 0 && strcmp(method,"notify") == 0 )
8 years ago
retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}");
8 years ago
else if ( IAMCLIENT == 0 && strcmp(method,"notifyutxo") == 0 )
8 years ago
{
printf("utxonotify.(%s)\n",jprint(argjson,0));
8 years ago
LP_addutxo(amclient,LP_mypeer,LP_mypubsock,jstr(argjson,"coin"),jbits256(argjson,"txid"),jint(argjson,"vout"),SATOSHIDEN * jdouble(argjson,"value"),jbits256(argjson,"txid2"),jint(argjson,"vout2"),SATOSHIDEN * jdouble(argjson,"value2"),jstr(argjson,"script"),jstr(argjson,"address"),jstr(argjson,"ipaddr"),juint(argjson,"port"),jdouble(argjson,"profit"));
8 years ago
retstr = clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}");
}
8 years ago
}
if ( retstr != 0 )
return(retstr);
retjson = cJSON_CreateObject();
jaddstr(retjson,"error","unrecognized command");
8 years ago
printf("ERROR.(%s)\n",jprint(argjson,0));
8 years ago
return(clonestr(jprint(retjson,1)));
}