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.

247 lines
11 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
//
7 years ago
char *stats_JSON(char *myipaddr,int32_t pubsock,double profitmargin,cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port
8 years ago
{
7 years ago
char *method,*ipaddr,*userpass,*base,*rel,*coin,*retstr = 0; uint16_t argport=0,pushport,subport; int32_t otherpeers,othernumutxos,flag = 0; struct LP_peerinfo *peer; cJSON *retjson; struct iguana_info *ptr;
7 years ago
if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 )
{
7 years ago
if ( strcmp(ipaddr,"127.0.0.1") != 0 && argport >= 1000 )
7 years ago
{
flag = 1;
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 )
{
if ( 0 && (otherpeers= jint(argjson,"numpeers")) > peer->numpeers )
peer->numpeers = otherpeers;
if ( 0 && (othernumutxos= jint(argjson,"numutxos")) > peer->numutxos )
{
printf("change.(%s) numutxos.%d -> %d mynumutxos.%d\n",peer->ipaddr,peer->numutxos,othernumutxos,LP_mypeer != 0 ? LP_mypeer->numutxos:0);
peer->numutxos = othernumutxos;
}
//printf("peer.(%s) found (%d %d) (%d %d) (%s)\n",peer->ipaddr,peer->numpeers,peer->numutxos,otherpeers,othernumutxos,jprint(argjson,0));
} else LP_addpeer(LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jdouble(argjson,"profit"),jint(argjson,"numpeers"),jint(argjson,"numutxos"));
}
}
8 years ago
if ( (method= jstr(argjson,"method")) == 0 )
7 years ago
{
7 years ago
if ( flag == 0 || jobj(argjson,"result") != 0 )
7 years ago
printf("stats_JSON no method: (%s) (%s:%u)\n",jprint(argjson,0),ipaddr,argport);
7 years ago
return(0);
7 years ago
}
7 years ago
if ( strcmp(method,"hello") == 0 )
return(0);
7 years ago
else if ( 0 && strcmp(method,"nn_tests") == 0 )
7 years ago
{
LP_send(pubsock,"nn_test return",0);
7 years ago
printf("send back nn_test return -> %d\n",pubsock);
7 years ago
return(0);
}
7 years ago
else if ( strcmp(method,"help") == 0 )
7 years ago
return(clonestr("{\"result\":\" \
available localhost RPC commands:\n \
setprice(base, rel, price)\n\
myprice(base, rel)\n\
enable(coin)\n\
disable(coin)\n\
inventory(coin)\n\
7 years ago
autotrade(base, rel, price, volume, timeout)\n\
7 years ago
swapstatus()\n\
swapstatus(requestid, quoteid)\n\
public API:\n \
getcoins()\n\
getpeers()\n\
getutxos()\n\
getutxos(coin, lastn)\n\
orderbook(base, rel)\n\
7 years ago
getprices(base, rel)\n\
7 years ago
trust(pubkey, trust)\n\
7 years ago
register(pubkey,pushaddr)\n\
lookup(pubkey)\n\
7 years ago
forward(pubkey,method2,<argjson>)\n\
forward(pubkey,method2=publish,<argjson>)\n\
forwardhex(pubkey,hex)\n\
7 years ago
\"}"));
7 years ago
base = jstr(argjson,"base");
rel = jstr(argjson,"rel");
7 years ago
if ( USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 )
8 years ago
{
7 years ago
if ( USERPASS_COUNTER == 0 )
8 years ago
{
7 years ago
USERPASS_COUNTER = 1;
retjson = cJSON_CreateObject();
jaddstr(retjson,"userpass",USERPASS);
7 years ago
jaddbits256(retjson,"mypubkey",LP_mypubkey);
7 years ago
jadd(retjson,"coins",LP_coinsjson());
7 years ago
return(jprint(retjson,1));
8 years ago
}
7 years ago
if ( (userpass= jstr(argjson,"userpass")) == 0 || strcmp(userpass,USERPASS) != 0 )
return(clonestr("{\"error\":\"authentication error\"}"));
7 years ago
if ( base != 0 && rel != 0 )
7 years ago
{
7 years ago
double price;
7 years ago
if ( LP_isdisabled(base,rel) != 0 )
return(clonestr("{\"error\":\"at least one of coins disabled\"}"));
7 years ago
price = jdouble(argjson,"price");
7 years ago
if ( strcmp(method,"setprice") == 0 )
{
7 years ago
if ( price > SMALLVAL )
7 years ago
{
7 years ago
if ( LP_mypriceset(base,rel,price) < 0 )
return(clonestr("{\"error\":\"couldnt set price\"}"));
7 years ago
else return(LP_pricepings(myipaddr,LP_mypubsock,profitmargin,base,rel,price * LP_profitratio));
7 years ago
} else return(clonestr("{\"error\":\"no price\"}"));
7 years ago
}
else if ( strcmp(method,"myprice") == 0 )
{
double bid,ask;
7 years ago
if ( LP_myprice(&bid,&ask,base,rel) > SMALLVAL )
7 years ago
{
retjson = cJSON_CreateObject();
jaddstr(retjson,"base",base);
jaddstr(retjson,"rel",rel);
jaddnum(retjson,"bid",bid);
jaddnum(retjson,"ask",ask);
return(jprint(retjson,1));
} else return(clonestr("{\"error\":\"no price set\"}"));
}
7 years ago
else if ( strcmp(method,"autotrade") == 0 )
7 years ago
{
if ( price > SMALLVAL )
{
7 years ago
printf("price set (%s/%s) <- %.8f\n",rel,base,1./price);
7 years ago
LP_mypriceset(rel,base,1./price);
7 years ago
return(LP_autotrade(myipaddr,pubsock,profitmargin,base,rel,price,jdouble(argjson,"volume"),jint(argjson,"timeout")));
} else return(clonestr("{\"error\":\"no price set\"}"));
}
7 years ago
}
7 years ago
else if ( (coin= jstr(argjson,"coin")) != 0 )
8 years ago
{
7 years ago
if ( strcmp(method,"enable") == 0 )
7 years ago
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
ptr->inactive = 0;
7 years ago
return(jprint(LP_coinsjson(),1));
7 years ago
}
else if ( strcmp(method,"disable") == 0 )
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
ptr->inactive = (uint32_t)time(NULL);
7 years ago
return(jprint(LP_coinsjson(),1));
7 years ago
}
7 years ago
if ( LP_isdisabled(coin,0) != 0 )
return(clonestr("{\"error\":\"coin is disabled\"}"));
if ( strcmp(method,"inventory") == 0 )
{
7 years ago
struct iguana_info *ptr; bits256 privkey,pubkey; uint8_t pubkey33[33];
if ( (ptr= LP_coinfind(coin)) != 0 )
{
privkey = LP_privkeycalc(pubkey33,&pubkey,ptr,"",USERPASS_WIFSTR);
7 years ago
//LP_utxopurge(0);
7 years ago
LP_privkey_init(-1,ptr,privkey,pubkey,pubkey33);
7 years ago
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
jadd(retjson,"alice",LP_inventory(coin,0));
jadd(retjson,"bob",LP_inventory(coin,1));
return(jprint(retjson,1));
}
7 years ago
}
7 years ago
}
else if ( strcmp(method,"swapstatus") == 0 )
{
uint32_t requestid,quoteid;
if ( (requestid= juint(argjson,"requestid")) != 0 && (quoteid= juint(argjson,"quoteid")) != 0 )
7 years ago
return(basilisk_swapentry(requestid,quoteid));
else return(basilisk_swaplist());
7 years ago
}
7 years ago
else if ( strcmp(method,"myprices") == 0 )
return(LP_myprices());
7 years ago
else if ( strcmp(method,"trust") == 0 )
return(LP_pubkey_trustset(jbits256(argjson,"pubkey"),jint(argjson,"trust")));
7 years ago
}
7 years ago
if ( LP_isdisabled(base,rel) != 0 )
7 years ago
retstr = clonestr("{\"error\":\"at least one of coins disabled\"}");
else if ( LP_isdisabled(jstr(argjson,"coin"),0) != 0 )
retstr = clonestr("{\"error\":\"coin is disabled\"}");
else if ( strcmp(method,"reserved") == 0 )
7 years ago
retstr = LP_quotereceived(argjson);
7 years ago
else if ( strcmp(method,"connected") == 0 )
7 years ago
retstr = LP_connectedalice(argjson);
7 years ago
else if ( strcmp(method,"checktxid") == 0 )
retstr = LP_spentcheck(argjson);
7 years ago
else if ( strcmp(method,"getcoins") == 0 )
7 years ago
return(jprint(LP_coinsjson(),1));
7 years ago
else if ( strcmp(method,"postprice") == 0 )
retstr = LP_postedprice(argjson);
else if ( strcmp(method,"broadcast") == 0 )
retstr = LP_broadcasted(argjson);
7 years ago
else if ( strcmp(method,"getprices") == 0 )
7 years ago
return(LP_prices());
7 years ago
else if ( strcmp(method,"orderbook") == 0 )
7 years ago
return(LP_orderbook(base,rel));
7 years ago
else if ( strcmp(method,"forward") == 0 )
{
7 years ago
cJSON *reqjson;
if ( (reqjson= LP_dereference(argjson,"forward")) != 0 )
7 years ago
{
7 years ago
//printf("FORWARDED.(%s)\n",jprint(argjson,0));
7 years ago
if ( LP_forward(myipaddr,pubsock,profitmargin,jbits256(argjson,"pubkey"),jprint(reqjson,1),1) > 0 )
7 years ago
retstr = clonestr("{\"result\":\"success\"}");
else retstr = clonestr("{\"error\":\"error forwarding\"}");
} else retstr = clonestr("{\"error\":\"cant recurse forwards\"}");
7 years ago
7 years ago
}
7 years ago
else if ( strcmp(method,"getpeers") == 0 )
7 years ago
return(LP_peers());
7 years ago
else if ( strcmp(method,"getutxos") == 0 )
7 years ago
return(LP_utxos(1,LP_mypeer,jstr(argjson,"coin"),jint(argjson,"lastn")));
7 years ago
else if ( strcmp(method,"notified") == 0 )
{
7 years ago
LP_utxoaddjson(1,LP_mypubsock,argjson);
7 years ago
return(clonestr("{\"result\":\"success\",\"notifyutxo\":\"received\"}"));
7 years ago
}
7 years ago
else if ( IAMLP != 0 )
7 years ago
{
7 years ago
if ( strcmp(method,"register") == 0 )
7 years ago
{
retstr = LP_register(jbits256(argjson,"client"),jstr(argjson,"pushaddr"));
7 years ago
//printf("got (%s) from register\n",retstr!=0?retstr:"");
7 years ago
return(retstr);
}
7 years ago
else if ( strcmp(method,"lookup") == 0 )
7 years ago
return(LP_lookup(jbits256(argjson,"client")));
7 years ago
else if ( strcmp(method,"forwardhex") == 0 )
7 years ago
retstr = LP_forwardhex(pubsock,jbits256(argjson,"pubkey"),jstr(argjson,"hex"));
7 years ago
else if ( strcmp(method,"notify") == 0 )
retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}");
7 years ago
}
8 years ago
if ( retstr != 0 )
7 years ago
{
free(retstr);
return(0);
}
8 years ago
printf("ERROR.(%s)\n",jprint(argjson,0));
7 years ago
return(0);
8 years ago
}