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.

466 lines
21 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
char *LP_numutxos()
{
cJSON *retjson = cJSON_CreateObject();
if ( LP_mypeer != 0 )
{
8 years ago
jaddstr(retjson,"ipaddr",LP_mypeer->ipaddr);
jaddnum(retjson,"port",LP_mypeer->port);
8 years ago
jaddnum(retjson,"numutxos",LP_mypeer->numutxos);
jaddnum(retjson,"numpeers",LP_mypeer->numpeers);
jaddnum(retjson,"session",LP_sessionid);
8 years ago
} else jaddstr(retjson,"error","client node");
8 years ago
return(jprint(retjson,1));
}
8 years ago
8 years ago
char *stats_JSON(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port
8 years ago
{
8 years ago
char *method,*ipaddr,*userpass,*base,*rel,*coin,*retstr = 0; uint16_t argport=0,pushport,subport; int32_t changed,otherpeers,othernumutxos,flag = 0; struct LP_peerinfo *peer; cJSON *retjson,*reqjson = 0; struct iguana_info *ptr;
8 years ago
//printf("stats_JSON(%s)\n",jprint(argjson,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 && argport >= 1000 )
8 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;
}
8 years ago
if ( peer->sessionid == 0 )
peer->sessionid = juint(argjson,"session");
8 years ago
//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(LP_mypeer,LP_mypubsock,ipaddr,argport,pushport,subport,jint(argjson,"numpeers"),jint(argjson,"numutxos"),juint(argjson,"session"));
8 years ago
}
}
8 years ago
if ( (method= jstr(argjson,"method")) == 0 )
8 years ago
{
8 years ago
if ( flag == 0 || jobj(argjson,"result") != 0 )
8 years ago
printf("stats_JSON no method: (%s) (%s:%u)\n",jprint(argjson,0),ipaddr,argport);
8 years ago
return(0);
8 years ago
}
8 years ago
/*if ( strcmp(method,"hello") == 0 )
8 years ago
{
8 years ago
//printf("got hello from %s:%u\n",ipaddr!=0?ipaddr:"",argport);
8 years ago
return(0);
8 years ago
}
8 years ago
else*/ if ( strcmp(method,"sendmessage") == 0 && jobj(argjson,"userpass") == 0 )
8 years ago
{
8 years ago
static char *laststr;
char *newstr; bits256 pubkey = jbits256(argjson,"pubkey");
8 years ago
if ( bits256_nonz(pubkey) == 0 || bits256_cmp(pubkey,LP_mypub25519) == 0 )
8 years ago
{
newstr = jprint(argjson,0);
if ( laststr == 0 || strcmp(laststr,newstr) != 0 )
{
printf("got message.(%s) from %s:%u\n",newstr,ipaddr!=0?ipaddr:"",argport);
if ( laststr != 0 )
free(laststr);
laststr = newstr;
8 years ago
LP_gotmessage(argjson);
8 years ago
retstr = clonestr(laststr);
8 years ago
}
8 years ago
} else retstr = clonestr("{\"error\":\"duplicate message\"}");
8 years ago
}
8 years ago
//else if ( strcmp(method,"nn_tests") == 0 )
// return(clonestr("{\"result\":\"success\"}"));
8 years ago
else if ( strcmp(method,"help") == 0 )
8 years ago
return(clonestr("{\"result\":\" \
available localhost RPC commands:\n \
8 years ago
pricearray(base, rel, firsttime=0, lasttime=-1, timescale=60) -> [timestamp, avebid, aveask, highbid, lowask]\n\
8 years ago
setprice(base, rel, price)\n\
8 years ago
autoprice(base, rel, price, margin, type)\n\
8 years ago
goal(coin=*, val=<autocalc>)\n\
8 years ago
myprice(base, rel)\n\
enable(coin)\n\
disable(coin)\n\
inventory(coin)\n\
8 years ago
bestfit(rel, relvolume)\n\
8 years ago
ordermatch(base, txfee=0, rel, desttxfee=0, price, relvolume=0, txid, vout, feetxid, feevout, duration=3600)\n\
8 years ago
trade(price, timeout=10, duration=3600, <quotejson returned from ordermatch>)\n\
8 years ago
autotrade(base, rel, price, relvolume, timeout=10, duration=3600)\n\
8 years ago
swapstatus()\n\
swapstatus(requestid, quoteid)\n\
public API:\n \
getcoins()\n\
8 years ago
getcoin(coin)\n\
8 years ago
portfolio()\n\
8 years ago
getpeers()\n\
getutxos()\n\
getutxos(coin, lastn)\n\
8 years ago
orderbook(base, rel, duration=3600)\n\
8 years ago
getprices(base, rel)\n\
8 years ago
sendmessage(base=coin, rel="", pubkey=zero, <argjson method2>)\n\
8 years ago
getmessages(firsti=0, num=100)\n\
clearmessages(firsti=0, num=100)\n\
7 years ago
secretaddresses(passphrase, num=10, pubtype=60, taddr=0)\n\
7 years ago
electrum(coin, ipaddr, port)\n\
7 years ago
snapshot(coin, height)\n\
7 years ago
snapshot_balance(coin, height, addresses[])\n\
7 years ago
dividends(coin, height, <args>)\n\
8 years ago
\"}"));
8 years ago
8 years ago
base = jstr(argjson,"base");
rel = jstr(argjson,"rel");
8 years ago
if ( USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 )
8 years ago
{
8 years ago
if ( USERPASS_COUNTER == 0 )
8 years ago
{
8 years ago
USERPASS_COUNTER = 1;
retjson = cJSON_CreateObject();
jaddstr(retjson,"userpass",USERPASS);
8 years ago
jaddbits256(retjson,"mypubkey",LP_mypub25519);
8 years ago
jadd(retjson,"coins",LP_coinsjson(LP_showwif));
8 years ago
return(jprint(retjson,1));
8 years ago
}
8 years ago
if ( (userpass= jstr(argjson,"userpass")) == 0 || strcmp(userpass,USERPASS) != 0 )
return(clonestr("{\"error\":\"authentication error\"}"));
8 years ago
jdelete(argjson,"userpass");
8 years ago
if ( strcmp(method,"sendmessage") == 0 )
{
8 years ago
if ( jobj(argjson,"method2") == 0 )
{
printf("broadcast message\n");
LP_broadcast_message(LP_mypubsock,base!=0?base:jstr(argjson,"coin"),rel,jbits256(argjson,"pubkey"),jprint(argjson,0));
}
return(clonestr("{\"result\":\"success\"}"));
}
else if ( strcmp(method,"getmessages") == 0 )
{
if ( (retjson= LP_getmessages(jint(argjson,"firsti"),jint(argjson,"num"))) != 0 )
return(jprint(retjson,1));
else return(clonestr("{\"error\":\"null messages\"}"));
}
else if ( strcmp(method,"deletemessages") == 0 )
{
LP_deletemessages(jint(argjson,"firsti"),jint(argjson,"num"));
8 years ago
return(clonestr("{\"result\":\"success\"}"));
8 years ago
}
8 years ago
else if ( strcmp(method,"portfolio") == 0 )
{
return(LP_portfolio());
}
7 years ago
else if ( strcmp(method,"secretaddresses") == 0 )
{
uint8_t taddr,pubtype;
pubtype = (jobj(argjson,"pubtype") == 0) ? 60 : juint(argjson,"pubtype");
taddr = (jobj(argjson,"taddr") == 0) ? 0 : juint(argjson,"taddr");
return(LP_secretaddresses(ctx,jstr(argjson,"passphrase"),juint(argjson,"num"),taddr,pubtype));
}
8 years ago
if ( base != 0 && rel != 0 )
8 years ago
{
8 years ago
double price,bid,ask;
8 years ago
if ( IAMLP == 0 && LP_isdisabled(base,rel) != 0 )
8 years ago
return(clonestr("{\"error\":\"at least one of coins disabled\"}"));
8 years ago
price = jdouble(argjson,"price");
8 years ago
if ( strcmp(method,"setprice") == 0 )
{
8 years ago
if ( price > SMALLVAL )
8 years ago
{
8 years ago
if ( LP_mypriceset(&changed,base,rel,price) < 0 )
return(clonestr("{\"error\":\"couldnt set price\"}"));
8 years ago
//else if ( LP_mypriceset(&changed,rel,base,1./price) < 0 )
// return(clonestr("{\"error\":\"couldnt set price\"}"));
8 years ago
else return(LP_pricepings(ctx,myipaddr,LP_mypubsock,base,rel,price * LP_profitratio));
8 years ago
} else return(clonestr("{\"error\":\"no price\"}"));
8 years ago
}
8 years ago
else if ( strcmp(method,"autoprice") == 0 )
{
8 years ago
if ( LP_autoprice(base,rel,price,jdouble(argjson,"margin"),jstr(argjson,"type")) < 0 )
8 years ago
return(clonestr("{\"error\":\"couldnt set autoprice\"}"));
8 years ago
else return(clonestr("{\"result\":\"success\"}"));
8 years ago
}
8 years ago
else if ( strcmp(method,"pricearray") == 0 )
8 years ago
{
8 years ago
return(jprint(LP_pricearray(base,rel,juint(argjson,"firsttime"),juint(argjson,"lasttime"),jint(argjson,"timescale")),1));
8 years ago
}
8 years ago
else if ( strcmp(method,"myprice") == 0 )
{
8 years ago
if ( LP_myprice(&bid,&ask,base,rel) > SMALLVAL )
8 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\"}"));
}
8 years ago
else if ( strcmp(method,"ordermatch") == 0 )
{
if ( price > SMALLVAL )
8 years ago
return(LP_ordermatch(base,j64bits(argjson,"txfee"),price,jdouble(argjson,"relvolume"),rel,jbits256(argjson,"txid"),jint(argjson,"vout"),jbits256(argjson,"feetxid"),jint(argjson,"feevout"),j64bits(argjson,"desttxfee"),jint(argjson,"duration")));
8 years ago
else return(clonestr("{\"error\":\"no price set\"}"));
}
8 years ago
else if ( strcmp(method,"trade") == 0 )
{
struct LP_quoteinfo Q;
if ( price > SMALLVAL || jobj(argjson,"quote") != 0 )
{
LP_quoteparse(&Q,jobj(argjson,"quote"));
8 years ago
return(LP_trade(ctx,myipaddr,pubsock,&Q,price,jint(argjson,"timeout"),jint(argjson,"duration")));
8 years ago
} else return(clonestr("{\"error\":\"no price set or no quote object\"}"));
}
8 years ago
else if ( strcmp(method,"autotrade") == 0 )
8 years ago
{
if ( price > SMALLVAL )
{
8 years ago
return(LP_autotrade(ctx,myipaddr,pubsock,base,rel,price,jdouble(argjson,"relvolume"),jint(argjson,"timeout"),jint(argjson,"duration")));
8 years ago
} else return(clonestr("{\"error\":\"no price set\"}"));
}
8 years ago
}
else if ( rel != 0 && strcmp(method,"bestfit") == 0 )
{
double relvolume;
if ( (relvolume= jdouble(argjson,"relvolume")) > SMALLVAL )
return(LP_bestfit(rel,relvolume));
else return(clonestr("{\"error\":\"no relvolume set\"}"));
8 years ago
}
8 years ago
else if ( (coin= jstr(argjson,"coin")) != 0 )
8 years ago
{
8 years ago
if ( strcmp(method,"enable") == 0 )
8 years ago
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
ptr->inactive = 0;
8 years ago
return(jprint(LP_coinsjson(0),1));
8 years ago
}
else if ( strcmp(method,"disable") == 0 )
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
ptr->inactive = (uint32_t)time(NULL);
8 years ago
return(jprint(LP_coinsjson(0),1));
8 years ago
}
7 years ago
else if ( strcmp(method,"electrum") == 0 )
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
{
ptr->inactive = 0;
7 years ago
return(jprint(LP_electrumserver(ptr,jstr(argjson,"ipaddr"),juint(argjson,"port")),1));
7 years ago
} else return(clonestr("{\"error\":\"cant find coind\"}"));
}
7 years ago
else if ( strcmp(method,"snapshot") == 0 )
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
7 years ago
return(jprint(LP_snapshot(ptr,juint(argjson,"height")),1));
else return(clonestr("{\"error\":\"cant find coind\"}"));
7 years ago
}
7 years ago
else if ( strcmp(method,"dividends") == 0 )
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
return(LP_dividends(ptr,juint(argjson,"height"),argjson));
else return(clonestr("{\"error\":\"cant find coind\"}"));
}
7 years ago
else if ( strcmp(method,"snapshot_balance") == 0 )
7 years ago
{
if ( (ptr= LP_coinsearch(coin)) != 0 )
return(LP_snapshot_balance(ptr,juint(argjson,"height"),argjson));
else return(clonestr("{\"error\":\"cant find coind\"}"));
}
8 years ago
if ( LP_isdisabled(coin,0) != 0 )
return(clonestr("{\"error\":\"coin is disabled\"}"));
if ( strcmp(method,"inventory") == 0 )
{
8 years ago
struct iguana_info *ptr;
8 years ago
if ( (ptr= LP_coinfind(coin)) != 0 )
{
8 years ago
//privkey = LP_privkeycalc(ctx,pubkey33,&pubkey,ptr,"",USERPASS_WIFSTR);
8 years ago
//LP_utxopurge(0);
8 years ago
if ( bits256_nonz(LP_mypriv25519) != 0 )
LP_privkey_init(-1,ptr,LP_mypriv25519,LP_mypub25519);
8 years ago
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
8 years ago
jaddstr(retjson,"coin",coin);
jaddnum(retjson,"timestamp",time(NULL));
8 years ago
jadd(retjson,"alice",LP_inventory(coin,0));
jadd(retjson,"bob",LP_inventory(coin,1));
return(jprint(retjson,1));
}
8 years ago
}
8 years ago
else if ( strcmp(method,"goal") == 0 )
8 years ago
return(LP_portfolio_goal(coin,jdouble(argjson,"val")));
8 years ago
else if ( strcmp(method,"getcoin") == 0 )
return(LP_getcoin(coin));
8 years ago
}
8 years ago
else if ( strcmp(method,"goal") == 0 )
return(LP_portfolio_goal("*",100.));
8 years ago
else if ( strcmp(method,"swapstatus") == 0 )
{
uint32_t requestid,quoteid;
if ( (requestid= juint(argjson,"requestid")) != 0 && (quoteid= juint(argjson,"quoteid")) != 0 )
8 years ago
return(basilisk_swapentry(requestid,quoteid));
else return(basilisk_swaplist());
8 years ago
}
8 years ago
else if ( strcmp(method,"myprices") == 0 )
return(LP_myprices());
8 years ago
else if ( strcmp(method,"trust") == 0 )
return(LP_pubkey_trustset(jbits256(argjson,"pubkey"),jint(argjson,"trust")));
8 years ago
}
8 years ago
if ( IAMLP == 0 )
8 years ago
{
if ( (reqjson= LP_dereference(argjson,"broadcast")) != 0 )
{
8 years ago
if ( jobj(reqjson,"method2") != 0 )
8 years ago
{
8 years ago
jdelete(reqjson,"method");
method = jstr(reqjson,"method2");
jaddstr(reqjson,"method",method);
8 years ago
}
8 years ago
argjson = reqjson;
8 years ago
}
}
8 years ago
if ( IAMLP == 0 && LP_isdisabled(base,rel) != 0 )
8 years ago
return(clonestr("{\"result\":\"at least one of coins disabled\"}"));
8 years ago
else if ( IAMLP == 0 && LP_isdisabled(jstr(argjson,"coin"),0) != 0 )
8 years ago
retstr = clonestr("{\"result\":\"coin is disabled\"}");
8 years ago
else if ( strcmp(method,"reserved") == 0 )
8 years ago
retstr = LP_quotereceived(argjson);
8 years ago
else if ( strcmp(method,"connected") == 0 )
8 years ago
retstr = LP_connectedalice(argjson);
8 years ago
else if ( strcmp(method,"checktxid") == 0 )
retstr = LP_spentcheck(argjson);
8 years ago
else if ( strcmp(method,"getcoins") == 0 )
8 years ago
return(jprint(LP_coinsjson(0),1));
8 years ago
else if ( strcmp(method,"numutxos") == 0 )
7 years ago
{
printf("deprecated numutxos received\n");
retstr = clonestr("{\"result\":\"couldnt add utxo\"}");
//return(LP_numutxos());
}
8 years ago
else if ( strcmp(method,"postprice") == 0 )
retstr = LP_postedprice(argjson);
7 years ago
else if ( strcmp(method,"postutxos") == 0 )
retstr = LP_postedutxos(argjson);
8 years ago
else if ( strcmp(method,"encrypted") == 0 )
retstr = clonestr("{\"result\":\"success\"}");
8 years ago
else if ( strcmp(method,"getprices") == 0 )
8 years ago
return(LP_prices());
8 years ago
else if ( strcmp(method,"orderbook") == 0 )
8 years ago
return(LP_orderbook(base,rel,jint(argjson,"duration")));
8 years ago
else if ( strcmp(method,"registerall") == 0 )
return(clonestr("{\"error\":\"you are running an obsolete version, update\"}"));
else if ( strcmp(method,"forward") == 0 )
return(clonestr("{\"error\":\"you are running an obsolete version, update\"}"));
8 years ago
else if ( strcmp(method,"keepalive") == 0 )
8 years ago
return(clonestr("{\"error\":\"you are running an obsolete version, update\"}"));
8 years ago
else if ( strcmp(method,"getpeers") == 0 )
8 years ago
return(LP_peers());
8 years ago
else if ( strcmp(method,"getutxos") == 0 )
7 years ago
{
printf("deprecated getutxos received\n");
retstr = clonestr("{\"result\":\"couldnt add utxo\"}");
//return(LP_utxos(1,LP_mypeer,jstr(argjson,"coin"),jint(argjson,"lastn")));
}
8 years ago
else if ( strcmp(method,"utxo") == 0 )
8 years ago
{
7 years ago
printf("deprecated utxo received\n");
//if ( LP_utxoaddjson(1,LP_mypubsock,argjson) != 0 )
// retstr = clonestr("{\"result\":\"success\",\"utxo\":\"received\"}");
//else
retstr = clonestr("{\"result\":\"couldnt add utxo\"}");
8 years ago
}
8 years ago
else
8 years ago
{
7 years ago
if ( base != 0 && rel != 0 && strcmp(method,"pricearray") == 0 )
{
uint32_t firsttime;
if ( (firsttime= juint(argjson,"firsttime")) < time(NULL)-30*24*3600 )
firsttime = (uint32_t)(time(NULL)-30*24*3600);
return(jprint(LP_pricearray(base,rel,firsttime,juint(argjson,"lasttime"),jint(argjson,"timescale")),1));
}
8 years ago
if ( IAMLP != 0 )
8 years ago
{
8 years ago
if ( strcmp(method,"register") == 0 )
return(clonestr("{\"error\":\"you are running an obsolete version, update\"}"));
8 years ago
else if ( strcmp(method,"lookup") == 0 )
8 years ago
return(clonestr("{\"error\":\"you are running an obsolete version, update\"}"));
7 years ago
else if ( strcmp(method,"listunspent") == 0 )
{
if ( (ptr= LP_coinsearch(jstr(argjson,"coin"))) != 0 )
return(jprint(LP_address_utxos(ptr,jstr(argjson,"address"),1),1));
else return(clonestr("{\"error\":\"cant find coind\"}"));
}
8 years ago
if ( strcmp(method,"broadcast") == 0 )
{
8 years ago
bits256 zero; char *cipherstr; int32_t cipherlen; uint8_t cipher[LP_ENCRYPTED_MAXSIZE];
8 years ago
if ( (reqjson= LP_dereference(argjson,"broadcast")) != 0 )
{
8 years ago
if ( (cipherstr= jstr(reqjson,"cipher")) != 0 )
8 years ago
{
cipherlen = (int32_t)strlen(cipherstr) >> 1;
if ( cipherlen <= sizeof(cipher) )
{
decode_hex(cipher,cipherlen,cipherstr);
8 years ago
LP_queuesend(calc_crc32(0,&cipher[2],cipherlen-2),LP_mypubsock,base,rel,cipher,cipherlen);
8 years ago
} else retstr = clonestr("{\"error\":\"cipher too big\"}");
8 years ago
}
else
{
memset(zero.bytes,0,sizeof(zero));
8 years ago
LP_broadcast_message(LP_mypubsock,base!=0?base:jstr(argjson,"coin"),rel,zero,jprint(reqjson,0));
8 years ago
}
retstr = clonestr("{\"result\":\"success\"}");
} else retstr = clonestr("{\"error\":\"couldnt dereference sendmessage\"}");
}
8 years ago
else if ( strcmp(method,"psock") == 0 )
{
if ( myipaddr == 0 || myipaddr[0] == 0 || strcmp(myipaddr,"127.0.0.1") == 0 )
{
if ( LP_mypeer != 0 )
myipaddr = LP_mypeer->ipaddr;
else printf("LP_psock dont have actual ipaddr?\n");
}
8 years ago
if ( jint(argjson,"ispaired") != 0 )
return(LP_psock(myipaddr,jint(argjson,"ispaired")));
else return(clonestr("{\"error\":\"you are running an obsolete version, update\"}"));
8 years ago
}
else if ( strcmp(method,"notify") == 0 )
retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}");
8 years ago
}
8 years ago
else
8 years ago
{
8 years ago
if ( strcmp(method,"psock") == 0 )
8 years ago
{
8 years ago
//printf("nonLP got (%s)\n",jprint(argjson,0));
8 years ago
retstr = clonestr("{\"result\":\"success\"}");
}
8 years ago
}
8 years ago
}
8 years ago
if ( retstr == 0 )
printf("ERROR.(%s)\n",jprint(argjson,0));
8 years ago
if ( reqjson != 0 )
free_json(reqjson);
8 years ago
if ( retstr != 0 )
8 years ago
{
free(retstr);
return(0);
}
return(0);
8 years ago
}