diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 947a34131..aa074832b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -281,11 +281,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port { uint32_t requestid,quoteid; if ( (requestid= juint(argjson,"requestid")) != 0 && (quoteid= juint(argjson,"quoteid")) != 0 ) - { - if ( (retstr= basilisk_swapfinished(requestid,quoteid)) == 0 ) - return(clonestr("{\"result\":\"swap pending\"}")); - else return(retstr); - } else return(basilisk_swaplist()); + return(basilisk_swapentry(requestid,quoteid)); + else return(basilisk_swaplist()); } } amclient = (LP_mypeer == 0); diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index a830bfe2e..1756b86a7 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -888,7 +888,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti char *basilisk_swaplist() { - char fname[512],*status; FILE *fp; cJSON *item,*retjson,*array,*totalsobj; uint32_t quoteid,requestid; int64_t KMDtotals[16],BTCtotals[16],Btotal,Ktotal; int32_t i; + char fname[512]; FILE *fp; cJSON *item,*retjson,*array,*totalsobj; uint32_t quoteid,requestid; int64_t KMDtotals[16],BTCtotals[16],Btotal,Ktotal; int32_t i; memset(KMDtotals,0,sizeof(KMDtotals)); memset(BTCtotals,0,sizeof(BTCtotals)); //,statebits; int32_t optionduration; struct basilisk_request R; bits256 privkey; @@ -914,8 +914,8 @@ char *basilisk_swaplist() if ( (item= basilisk_remember(KMDtotals,BTCtotals,requestid,quoteid)) != 0 ) { jaddi(array,item); - if ( (status= jstr(item,"status")) != 0 && strcmp(status,"pending") == 0 ) - break; + //if ( (status= jstr(item,"status")) != 0 && strcmp(status,"pending") == 0 ) + // break; } } } @@ -952,7 +952,7 @@ char *basilisk_swaplist() return(jprint(retjson,1)); } -char *basilisk_swapfinished(uint32_t requestid,uint32_t quoteid) +char *basilisk_swapentry(uint32_t requestid,uint32_t quoteid) { char *liststr,*retstr = 0; cJSON *retjson,*array,*item; int32_t i,n; if ( (liststr= basilisk_swaplist()) != 0 ) @@ -967,8 +967,7 @@ char *basilisk_swapfinished(uint32_t requestid,uint32_t quoteid) //printf("(%s) check r%u/q%u\n",jprint(item,0),juint(item,"requestid"),juint(item,"quoteid")); if ( juint(item,"requestid") == requestid && juint(item,"quoteid") == quoteid ) { - if ( jstr(item,"status") != 0 && strcmp(jstr(item,"status"),"finished") == 0 ) - retstr = jprint(item,0); + retstr = jprint(item,0); break; } } diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 21ad1fa48..f512b294e 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -617,9 +617,37 @@ uint32_t LP_swapdata_rawtxsend(int32_t pairsock,struct basilisk_swap *swap,uint3 return(0); } +int32_t LP_swapwait(uint32_t requestid,uint32_t quoteid,int32_t duration,int32_t sleeptime) +{ + char *retstr; cJSON *retjson=0; uint32_t expiration = (uint32_t)(time(NULL) + duration); + printf("wait %d:%d for SWAP.(r%u/q%u) to complete\n",duration,sleeptime,requestid,quoteid); + while ( time(NULL) < expiration ) + { + sleep(3); + if ( (retstr= basilisk_swapentry(requestid,quoteid)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( jstr(retjson,"status") != 0 && strcmp(jstr(retjson,"status"),"finished") == 0 ) + break; + free_json(retjson); + retjson = 0; + } + free(retstr); + } + sleep(sleeptime); + } + if ( retjson != 0 ) + { + printf("SWAP completed! %u-%u %s\n",requestid,quoteid,jprint(retjson,0)); + free_json(retjson); + return(0); + } else return(-1); +} + void LP_bobloop(void *_utxo) { - uint8_t *data; int32_t maxlen; char *retstr; uint32_t expiration; struct basilisk_swap *swap; struct LP_utxoinfo *utxo = _utxo; + uint8_t *data; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap; struct LP_utxoinfo *utxo = _utxo; fprintf(stderr,"start swap iambob\n"); maxlen = 1024*1024 + sizeof(*swap); data = malloc(maxlen); @@ -658,13 +686,7 @@ void LP_bobloop(void *_utxo) swap->bobreclaim.utxovout = 0; swap->bobreclaim.utxotxid = swap->bobpayment.I.signedtxid; basilisk_bobpayment_reclaim(swap,swap->I.callduration); - printf("wait for SWAP to complete\n"); - while ( (retstr= basilisk_swapfinished(swap->I.req.requestid,swap->I.req.quoteid)) == 0 ) - { - sleep(100); - } - printf("SWAP completed! %u-%u %s\n",swap->I.req.requestid,swap->I.req.quoteid,retstr); - free(retstr); + LP_swapwait(swap->I.req.requestid,swap->I.req.quoteid,4*3600,600); } } basilisk_swap_finished(swap); @@ -677,7 +699,7 @@ void LP_bobloop(void *_utxo) void LP_aliceloop(void *_qp) { - uint8_t *data; char *retstr; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap = 0; struct LP_quoteinfo *qp = _qp; + uint8_t *data; int32_t maxlen; uint32_t expiration; struct basilisk_swap *swap = 0; struct LP_quoteinfo *qp = _qp; fprintf(stderr,"start swap iamalice pair.%d\n",qp->pair); maxlen = 1024*1024 + sizeof(*swap); data = malloc(maxlen); @@ -706,13 +728,7 @@ void LP_aliceloop(void *_qp) printf("error waiting for bobpayment\n"); else { - printf("wait for SWAP to complete\n"); - while ( (retstr= basilisk_swapfinished(swap->I.req.requestid,swap->I.req.quoteid)) == 0 ) - { - sleep(100); - } - printf("SWAP completed! %u-%u %s\n",swap->I.req.requestid,swap->I.req.quoteid,retstr); - free(retstr); + LP_swapwait(swap->I.req.requestid,swap->I.req.quoteid,4*3600,600); } } basilisk_swap_finished(swap);