From 7a589eff02265f83260ecb42e3b4b3caa59428c2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 23 May 2017 17:19:47 +0300 Subject: [PATCH] test --- crypto777/bitcoind_RPC.c | 2 +- iguana/exchanges/DEXstats.h | 2 + iguana/exchanges/LP_unspents.c | 268 ++++++++++++++++++++++++++++++++- iguana/exchanges/mm.c | 74 ++++++++- 4 files changed, 337 insertions(+), 9 deletions(-) diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index a4a9499b1..37ef4be0a 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -207,7 +207,7 @@ try_again: free(s.ptr); return(0); } - else if ( numretries >= 4 ) + else if ( numretries >= 2 ) { printf( "curl_easy_perform() failed: %s %s.(%s %s), retries: %d\n",curl_easy_strerror(res),debugstr,url,command,numretries); //printf("Maximum number of retries exceeded!\n"); diff --git a/iguana/exchanges/DEXstats.h b/iguana/exchanges/DEXstats.h index 16889c2e1..5738ab91e 100644 --- a/iguana/exchanges/DEXstats.h +++ b/iguana/exchanges/DEXstats.h @@ -915,6 +915,7 @@ char *stats_prices(char *symbol,char *dest,struct DEXstats_disp *prices,int32_t return(jprint(retjson,1)); } +#ifndef FROM_MARKETMAKER char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) { char *method,*agent,*retstr,*source,*dest; struct tai T; uint32_t endtimestamp; struct DEXstats_disp prices[365]; int32_t leftdatenum,seconds,numdates; @@ -940,5 +941,6 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) } return(clonestr(jprint(argjson,0))); } +#endif #endif /* DEXstats_h */ diff --git a/iguana/exchanges/LP_unspents.c b/iguana/exchanges/LP_unspents.c index aab4d8283..b522309c6 100644 --- a/iguana/exchanges/LP_unspents.c +++ b/iguana/exchanges/LP_unspents.c @@ -8,14 +8,270 @@ #include -void LPinit() +char *default_LPnodes[] = { "5.9.253.195", "5.9.253.196", "5.9.253.197", "5.9.253.198", "5.9.253.199", "5.9.253.200", "5.9.253.201", "5.9.253.202", "5.9.253.203", "5.9.253.204" }; + +struct LP_peerinfo { - char *retstr; - retstr = iguana_listunspent("KMD","RRyBxbrAPRUBCUpiJgJZYrkxqrh8x5ta9Z"); - if ( retstr != 0 ) + double profitmargin; + uint32_t ipbits,gotintro,sentintro; + char ipaddr[64]; + uint16_t port; +} LP_peerinfos[1024]; +int32_t LP_numpeers; + +void LP_addutxo(char *coin,bits256 txid,int32_t vout,uint64_t satoshis,bits256 deposittxid,int32_t depositvout,uint64_t depositsatoshis,char *spendscript,char *coinaddr) +{ + printf("LP_addutxo.(%.8f %.8f)\n",dstr(satoshis),dstr(depositsatoshis)); +} + +void _LP_addpeer(int32_t i,uint32_t ipbits,char *ipaddr,uint16_t port,uint32_t gotintro,uint32_t sentintro,double profitmargin) +{ + struct LP_peerinfo *peer; + if ( i == sizeof(LP_peerinfos)/sizeof(*LP_peerinfos) ) + i = (rand() % (sizeof(LP_peerinfos)/sizeof(*LP_peerinfos))); + peer = &LP_peerinfos[i]; + memset(peer,0,sizeof(*peer)); + peer->profitmargin = profitmargin; + peer->ipbits = ipbits; + peer->gotintro = gotintro; + peer->sentintro = sentintro; + strcpy(peer->ipaddr,ipaddr); + peer->port = port; + if ( i == LP_numpeers ) + LP_numpeers++; +} + +void LP_notify(struct LP_peerinfo *peer,char *ipaddr,uint16_t port) +{ + char buf[1024],*retstr,*argipaddr; uint32_t ipbits; cJSON *array,*item; int32_t i,j,n; uint16_t argport; double profit; + sprintf(buf,"http://%s:%u/api/stats/intro?ipaddr=%s&port=%u",peer->ipaddr,peer->port,ipaddr,port); + if ( (retstr= issue_curl(buf)) != 0 ) { - printf("listunspent.(%s)\n",retstr); + printf("got (%s) from (%s)\n",retstr,buf); + if ( (array= cJSON_Parse(retstr)) != 0 ) + { + if ( (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; i 0. ) + { + argport = juint(item,"port"); + ipbits = (uint32_t)calc_ipbits(argipaddr); + for (j=0; jipaddr); + jaddnum(item,"port",peer->port); + return(item); +} + +char *LP_peers() +{ + int32_t i; cJSON *peersjson = cJSON_CreateArray(); + for (i=0; i 0 ) + { + lastj = -1; + for (iter=0; iter<2; iter++) + { + j = (rand() % LP_numpeers); + if ( j != lastj ) + { + peer = &LP_peerinfos[j]; + if ( peer->sentintro == 0 ) + LP_notify(peer,ipaddr,port); + lastj = j; + } + } + } + _LP_addpeer(i,ipbits,ipaddr,port,gotintro,sentintro,profitmargin); + } + } + return(LP_peers()); +} + +int32_t LP_maxvalue(uint64_t *values,int32_t n) +{ + int32_t i,maxi = -1; uint64_t maxval = 0; + for (i=0; i maxval ) + { + maxi = i; + maxval = values[i]; + } + return(maxi); +} + +int32_t LP_nearestvalue(uint64_t *values,int32_t n,uint64_t targetval) +{ + int32_t i,mini = -1; int64_t dist; uint64_t mindist = (1 << 31); + for (i=0; i= 0 && dist < mindist ) + { + mini = i; + mindist = dist; + } + } + return(mini); +} + +uint64_t LP_privkey_init(char *coin,uint8_t addrtype,char *passphrase,char *wifstr) +{ + char *retstr,coinaddr[64],*script; cJSON *array,*item; bits256 txid,deposittxid; int32_t used,i,n,vout,depositvout; uint64_t *values,satoshis,depositval,targetval,value,total = 0; bits256 privkey,pubkey; uint8_t pubkey33[33]; + if ( passphrase != 0 ) + conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); + else privkey = iguana_wif2privkey(wifstr); + iguana_priv2pub(pubkey33,coinaddr,privkey,addrtype); + retstr = iguana_listunspent(coin,coinaddr); + if ( retstr != 0 && retstr[0] == '[' && retstr[1] == ']' ) + free(retstr), retstr = 0; + if ( retstr == 0 ) + { + if ( (retstr= DEX_listunspent(coin,coinaddr)) == 0 ) + { + printf("null listunspent\n"); + return(0); + } + } + printf("LP_privkey_init.(%s)\n",retstr); + if ( (array= cJSON_Parse(retstr)) != 0 ) + { + if ( (n= cJSON_GetArraySize(array)) > 0 ) + { + values = calloc(n,sizeof(*values)); + for (i=0; i= 0 ) + { + item = jitem(array,i); + deposittxid = jbits256(item,"txid"); + depositvout = juint(item,"vout"); + script = jstr(item,"scriptPubKey"); + depositval = values[i]; + values[i] = 0, used++; + targetval = (depositval / 9) * 8; + printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); + if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) + { + item = jitem(array,i); + txid = jbits256(item,"txid"); + vout = juint(item,"vout"); + if ( jstr(item,"scriptPubKey") != 0 && strcmp(script,jstr(item,"scriptPubKey")) == 0 ) + { + value = values[i]; + values[i] = 0, used++; + LP_addutxo(coin,txid,vout,value,deposittxid,depositvout,depositval,script,coinaddr); + total += value; + } + } + } else break; + } + free(values); + } + free_json(array); + } + free(retstr); + return(total); +} + +void LPinit(uint16_t port,double profitmargin) +{ + char *retstr,*ipaddr; long filesize,n; int32_t i; + if ( system("curl -s4 checkip.amazonaws.com > /tmp/myipaddr") == 0 ) + { + if ( (ipaddr= OS_filestr(&filesize,"/tmp/myipaddr")) != 0 && ipaddr[0] != 0 ) + { + n = strlen(ipaddr); + if ( ipaddr[n-1] == '\n' ) + ipaddr[--n] = 0; + retstr = LP_addpeer(ipaddr,port,0,(uint32_t)time(NULL),profitmargin); + printf("my ipaddr.(%s) peers.(%s)\n",ipaddr,retstr!=0?retstr:""); + if ( retstr != 0 ) + free(retstr); + for (i=0; i %s\n",default_LPnodes[i],retstr); + free(retstr); + } + } + } + } + LP_privkey_init("KMD",60,"test",""); + if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)stats_rpcloop,(void *)&port) != 0 ) + { + printf("error launching stats rpcloop for port.%u\n",port); + exit(-1); + } getchar(); } + +char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) +{ + char *method,*ipaddr,*coin,*dest,*retstr = 0; uint16_t argport; double profitmargin; + if ( (method= jstr(argjson,"method")) == 0 ) + return(clonestr("{\"error\":\"need method in request\"}")); + else + { + if ( strcmp(method,"intro") == 0 ) + { + if ( (ipaddr= jstr(argjson,"ipaddr")) != 0 && (argport= juint(argjson,"port")) != 0 && (profitmargin= jdouble(argjson,"profit")) != 0. ) + retstr = LP_addpeer(ipaddr,argport,(uint32_t)time(NULL),0,profitmargin); + } + else if ( strcmp(method,"getpeers") == 0 ) + retstr = LP_peers(); + else if ( strcmp(method,"getutxos") == 0 && (coin= jstr(argjson,"coin")) != 0 && (dest= jstr(argjson,"dest")) != 0 ) + { + //retstr = LP_getutxos(coin,dest); + } + } + if ( retstr != 0 ) + return(retstr); + return(clonestr(jprint(argjson,0))); +} diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index 92356d660..26eedc0ab 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -23,8 +23,9 @@ #include #include #include "OS_portable.h" -#include "stats.c" #define MAX(a,b) ((a) > (b) ? (a) : (b)) +char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port); +#include "stats.c" char DEX_baseaddr[64],DEX_reladdr[64]; struct mmpending_order @@ -158,6 +159,75 @@ char *iguana_listunspent(char *coin,char *coinaddr) return(bitcoind_RPC(0,"",url,0,"listunspent",postdata)); } +char *issue_LP_intro(char *destip,uint16_t destport,char *ipaddr,uint16_t port) +{ + char url[512]; + sprintf(url,"http://%s:%u/api/stats/intro?ipaddr=%s&port=%u",destip,destport,ipaddr,port); + return(issue_curl(url)); +} + +char *issue_LP_getpeers(char *destip,uint16_t destport) +{ + char url[512]; + sprintf(url,"http://%s:%u/api/stats/getpeers",destip,destport); + return(issue_curl(url)); +} + +// +// http://127.0.0.1:7779/api/stats/getpeers + +char *DEX_listunspent(char *coin,char *coinaddr) +{ + char url[512],postdata[1024]; + sprintf(url,"%s/?",IGUANA_URL); + sprintf(postdata,"{\"agent\":\"dex\",\"method\":\"listunspent\",\"address\":\"%s\",\"symbol\":\"%s\"}",coinaddr,coin); + return(bitcoind_RPC(0,"dex",url,0,"listunspent",postdata)); +} + +bits256 iguana_wif2privkey(char *wifstr) +{ + char url[512],postdata[1024],*retstr,*privstr; bits256 privkey; cJSON *retjson; + memset(privkey.bytes,0,sizeof(privkey)); + sprintf(url,"%s/?",IGUANA_URL); + sprintf(postdata,"{\"agent\":\"SuperNET\",\"method\":\"wif2priv\",\"wif\":\"%s\"}",wifstr); + if ( (retstr= bitcoind_RPC(0,"SuperNET",url,0,"wif2priv",postdata)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( (privstr= jstr(retjson,"privkey")) != 0 ) + { + if ( strlen(privstr) == 64 ) + decode_hex(privkey.bytes,32,privstr); + } + free_json(retjson); + } + free(retstr); + } + return(privkey); +} + +void iguana_priv2pub(uint8_t *pubkey33,char *coinaddr,bits256 privkey,uint8_t addrtype) +{ + char privstr[65],url[512],postdata[1024],*retstr,*pubstr,*addr; cJSON *retjson; + memset(pubkey33,0,33); + coinaddr[0] = 0; + bits256_str(privstr,privkey); + sprintf(url,"%s/?",IGUANA_URL); + sprintf(postdata,"{\"agent\":\"SuperNET\",\"method\":\"priv2pub\",\"privkey\":\"%s\",\"addrtype\":%u}",privstr,addrtype); + if ( (retstr= bitcoind_RPC(0,"SuperNET",url,0,"priv2pub",postdata)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( (pubstr= jstr(retjson,"secp256k1")) != 0 && strlen(pubstr) == 66 ) + decode_hex(pubkey33,33,pubstr); + if ( (addr= jstr(retjson,"result")) != 0 && strlen(addr) < 64 ) + strcpy(coinaddr,addr); + free_json(retjson); + } + free(retstr); + } +} + double bittrex_balance(char *base,char *coinaddr) { char *retstr; cJSON *retjson; double balance = 0.; @@ -717,12 +787,12 @@ int main(int argc, const char * argv[]) char *base,*rel,*name,*exchange,*apikey,*apisecret,*blocktrail,*retstr,*baseaddr,*reladdr,*passphrase; double profitmargin,maxexposure,incrratio,start_rel,start_base,minask,maxbid; cJSON *retjson,*loginjson; int32_t i; - LPinit(); if ( argc > 1 && (retjson= cJSON_Parse(argv[1])) != 0 ) { minask = jdouble(retjson,"minask"); maxbid = jdouble(retjson,"maxbid"); profitmargin = jdouble(retjson,"profitmargin"); + LPinit(7779,profitmargin); maxexposure = jdouble(retjson,"maxexposure"); incrratio = jdouble(retjson,"lotratio"); start_base = jdouble(retjson,"start_base");