diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index eb9abe808..9b3345ab6 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -187,6 +187,24 @@ static char *assetids[][4] = { "10524562908394749924", "MGW", "1", "100000000" }, }; +void LP_sendtoaddress_line(char *validaddress,char *assetname,uint64_t satoshis,uint64_t txnum) +{ + char line[1024],lowerstr[64]; + if ( strcmp(assetname,"SUPERNETx2") == 0 ) + { + sprintf(line,"fiat/supernet sendtoaddress %s %.8f # txnum.%llu",validaddress,dstr(satoshis),(long long)txnum); + printf("%s\n",line); + sprintf(line,"fiat/revs sendtoaddress %s %.8f # txnum.%llu",validaddress,dstr(satoshis),(long long)txnum); + } + else + { + strcpy(lowerstr,assetname); + tolowercase(lowerstr); + sprintf(line,"fiat/%s sendtoaddress %s %.8f # txnum.%llu",lowerstr,validaddress,dstr(satoshis),(long long)txnum); + } + printf("%s\n",line); +} + uint64_t LP_assetid_mult(int32_t *assetindp,char *name,uint64_t assetid) { int32_t i; uint64_t mult = 0; @@ -292,9 +310,9 @@ char *account = "NXT-MRBN-8DFH-PFMK-A4DBM"; if ( (decjson= LP_NXT_decrypt(txnum,account,jstr(encjson,"data"),jstr(encjson,"nonce"),passphrase)) != 0 ) { //printf("%s\n",jprint(decjson,0)); - msgstr = jstr(decjson,"decryptedMessage"); + if ( jstr(decjson,"decryptedMessage") != 0 ) + msgstr = jstr(decjson,"decryptedMessage"); } - } } } @@ -315,12 +333,12 @@ char *account = "NXT-MRBN-8DFH-PFMK-A4DBM"; strncpy(validaddress,&msgstr[z],34); if ( strlen(validaddress) == 34 || strlen(validaddress) == 33 ) { - printf("%-4d: (%34s) <- %13.5f %10s tx.%llu past_marker.%d\n",i,validaddress,dstr(qty * mult),assetname,(long long)txnum,past_marker); + //printf("%-4d: (%34s) <- %13.5f %10s tx.%llu past_marker.%d\n",i,validaddress,dstr(qty * mult),assetname,(long long)txnum,past_marker); + if ( past_marker == 0 ) + { + LP_sendtoaddress_line(validaddress,assetname,(qty * mult),txnum); + } } else printf("%-4d: (%34s) <- %13.5f %10s tx.%llu\n",i,msgstr!=0?msgstr:jprint(item,0),dstr(qty * mult),assetname,(long long)txnum); - if ( past_marker == 0 ) - { - - } } if ( msgjson != 0 ) free_json(msgjson); @@ -802,14 +820,22 @@ int32_t LP_importaddress(char *symbol,char *address) double _LP_getestimatedrate(struct iguana_info *coin) { - char buf[512],*retstr; cJSON *errjson; double rate = 0.00000020; + char buf[512],*retstr=0; int32_t numblocks; cJSON *errjson,*retjson; double rate = 0.00000020; if ( coin->rate < 0. || time(NULL) > coin->ratetime+30 ) { - sprintf(buf,"[%d]",strcmp(coin->symbol,"BTC") == 0 ? 6 : 2); - if ( (retstr= LP_apicall(coin,coin->electrum==0?"estimatefee" : "blockchain.estimatefee",buf)) != 0 ) + numblocks = strcmp(coin->symbol,"BTC") == 0 ? 6 : 2; + if ( coin->electrum == 0 ) + { + sprintf(buf,"[%d]",numblocks); + retstr = LP_apicall(coin,"estimatefee",buf); + } + else + { + if ( (retjson= electrum_estimatefee(coin->symbol,coin->electrum,&retjson,numblocks)) != 0 ) + retstr = jprint(retjson,1); + } + if ( retstr != 0 ) { - if ( coin->electrum != 0 ) - printf("estimatefee.(%s)\n",retstr); if ( retstr[0] == '{' && (errjson= cJSON_Parse(retstr)) != 0 ) { if ( jobj(errjson,"error") != 0 ) diff --git a/iguana/exchanges/LP_socket.c b/iguana/exchanges/LP_socket.c index 8907be909..dafac13b2 100644 --- a/iguana/exchanges/LP_socket.c +++ b/iguana/exchanges/LP_socket.c @@ -360,6 +360,35 @@ int32_t electrum_process_array(struct iguana_info *coin,struct electrum_info *ep return(flag); } +cJSON *electrum_version(char *symbol,struct electrum_info *ep,cJSON **retjsonp); +cJSON *electrum_headers_subscribe(char *symbol,struct electrum_info *ep,cJSON **retjsonp); + +struct stritem *electrum_sitem(struct electrum_info *ep,char *stratumreq,int32_t timeout,cJSON **retjsonp) +{ + struct stritem *sitem = (struct stritem *)queueitem(stratumreq); + sitem->expiration = timeout; + sitem->DL.type = ep->stratumid++; + sitem->retptrp = (void **)retjsonp; + queue_enqueue("sendQ",&ep->sendQ,&sitem->DL); + return(sitem); +} + +void electrum_initial_requests(struct electrum_info *ep) +{ + cJSON *retjson; char stratumreq[1024]; + retjson = 0; + sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,"blockchain.headers.subscribe","[]"); + electrum_sitem(ep,stratumreq,3,&retjson); + + retjson = 0; + sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,"server.version","[\"barterDEX\", [\"1.1\", \"1.1\"]]"); + electrum_sitem(ep,stratumreq,3,&retjson); + + retjson = 0; + sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,"blockchain.estimatefee","[2]"); + electrum_sitem(ep,stratumreq,3,&retjson); +} + cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,char *params,int32_t timeout) { // queue id and string and callback @@ -374,12 +403,13 @@ cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,method,params); //printf("%s %s",symbol,stratumreq); memset(ep->buf,0,ep->bufsize); - sitem = (struct stritem *)queueitem(stratumreq); + sitem = electrum_sitem(ep,stratumreq,timeout,retjsonp); + /*sitem = (struct stritem *)queueitem(stratumreq); sitem->expiration = timeout; sitem->DL.type = ep->stratumid++; - sitem->retptrp = (void **)retjsonp; + sitem->retptrp = (void **)retjsonp;*/ portable_mutex_lock(&ep->mutex); - queue_enqueue("sendQ",&ep->sendQ,&sitem->DL); + //queue_enqueue("sendQ",&ep->sendQ,&sitem->DL); expiration = (uint32_t)time(NULL) + timeout + 1; while ( *retjsonp == 0 && time(NULL) <= expiration ) usleep(5000); @@ -393,6 +423,8 @@ cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch printf("error RE-connecting to %s:%u\n",ep->ipaddr,ep->port); else { + ep->stratumid = 0; + electrum_initial_requests(ep); printf("ep.%p %s numerrors.%d too big -> new %s:%u sock.%d\n",ep,ep->symbol,ep->numerrors,ep->ipaddr,ep->port,ep->sock); ep->numerrors = 0; } @@ -591,7 +623,10 @@ cJSON *electrum_address_getbalance(char *symbol,struct electrum_info *ep,cJSON * cJSON *electrum_addpeer(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *endpoint) { return(electrum_strarg(symbol,ep,retjsonp,"server.add_peer",endpoint,ELECTRUM_TIMEOUT)); } cJSON *electrum_sendrawtransaction(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *rawtx) { return(electrum_strarg(symbol,ep,retjsonp,"blockchain.transaction.broadcast",rawtx,ELECTRUM_TIMEOUT)); } -cJSON *electrum_estimatefee(char *symbol,struct electrum_info *ep,cJSON **retjsonp,int32_t numblocks) { return(electrum_intarg(symbol,ep,retjsonp,"blockchain.estimatefee",numblocks,ELECTRUM_TIMEOUT)); } +cJSON *electrum_estimatefee(char *symbol,struct electrum_info *ep,cJSON **retjsonp,int32_t numblocks) +{ + return(electrum_intarg(symbol,ep,retjsonp,"blockchain.estimatefee",numblocks,ELECTRUM_TIMEOUT)); +} cJSON *electrum_getchunk(char *symbol,struct electrum_info *ep,cJSON **retjsonp,int32_t n) { return(electrum_intarg(symbol,ep,retjsonp,"blockchain.block.get_chunk",n,ELECTRUM_TIMEOUT)); } @@ -833,7 +868,17 @@ struct electrum_info *LP_electrum_info(int32_t *alreadyp,char *symbol,char *ipad int32_t LP_recvfunc(struct electrum_info *ep,char *str,int32_t len) { cJSON *strjson,*errjson,*resultjson,*paramsjson; char *method; int32_t i,n,height; uint32_t idnum=0; struct stritem *stritem; struct iguana_info *coin; struct queueitem *tmp,*item = 0; + if ( str == 0 || len == 0 ) + return(-1); ep->lasttime = (uint32_t)time(NULL); + /*if ( (strjson= cJSON_Parse(str)) == 0 ) + { + strjson = cJSON_CreateObject(); + resitem = cJSON_CreateObject(); + jaddstr(resitem,"string",str); + jadd(strjson,"result",resitem); + printf("mapped.(%s) -> %s\n",str,jprint(strjson,0)); + }*/ if ( (strjson= cJSON_Parse(str)) != 0 ) { resultjson = jobj(strjson,"result"); @@ -876,9 +921,9 @@ int32_t LP_recvfunc(struct electrum_info *ep,char *str,int32_t len) stritem = (struct stritem *)item; if ( item->type == idnum ) { - //printf("matched idnum.%d result.%p\n",idnum,resultjson); DL_DELETE(ep->pendingQ.list,item); - *((cJSON **)stritem->retptrp) = (resultjson != 0 ? jduplicate(resultjson) : strjson); + *((cJSON **)stritem->retptrp) = (resultjson != 0 ? jduplicate(resultjson) : jduplicate(strjson)); + //printf("matched idnum.%d result.(%s)\n",idnum,jprint(*((cJSON **)stritem->retptrp),0)); resultjson = strjson = 0; free(item); break; @@ -907,14 +952,10 @@ int32_t LP_recvfunc(struct electrum_info *ep,char *str,int32_t len) void LP_dedicatedloop(void *arg) { - struct pollfd fds; int32_t i,len,flag,timeout = 10; struct iguana_info *coin; cJSON *retjson; struct stritem *sitem; struct electrum_info *ep = arg; + struct pollfd fds; int32_t i,len,flag,timeout = 10; struct iguana_info *coin; struct stritem *sitem; struct electrum_info *ep = arg; if ( (coin= LP_coinfind(ep->symbol)) != 0 ) ep->heightp = &coin->height, ep->heighttimep = &coin->heighttime; - sleep(2); - if ( (retjson= electrum_version(ep->symbol,ep,&retjson)) != 0 ) - printf("electrum_version %s\n",jprint(retjson,1)); - if ( (retjson= electrum_headers_subscribe(ep->symbol,ep,&retjson)) != 0 ) - free_json(retjson); + electrum_initial_requests(ep); printf("LP_dedicatedloop ep.%p sock.%d for %s:%u num.%d %p %s ht.%d\n",ep,ep->sock,ep->ipaddr,ep->port,Num_electrums,&Num_electrums,ep->symbol,*ep->heightp); while ( ep->sock >= 0 ) {