|
|
@ -464,9 +464,15 @@ char *LP_connectedalice(cJSON *argjson) // alice |
|
|
|
{ |
|
|
|
cJSON *retjson; double bid,ask,price,qprice; int32_t pairsock = -1; char *pairstr; int32_t DEXselector = 0; struct LP_utxoinfo *autxo,B,*butxo; struct LP_quoteinfo Q; struct basilisk_swap *swap; struct iguana_info *coin; //uint64_t value,value2;
|
|
|
|
if ( LP_quoteparse(&Q,argjson) < 0 ) |
|
|
|
{ |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error0",0,0); |
|
|
|
clonestr("{\"error\":\"cant parse quote\"}"); |
|
|
|
} |
|
|
|
if ( bits256_cmp(Q.desthash,G.LP_mypub25519) != 0 ) |
|
|
|
{ |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error1",0,0); |
|
|
|
return(clonestr("{\"result\",\"update stats\"}")); |
|
|
|
} |
|
|
|
printf("CONNECTED.(%s) numpending.%d tradeid.%u\n",jprint(argjson,0),G.LP_pendingswaps,Q.tradeid); |
|
|
|
/*if ( LP_alice_eligible() == 0 || LP_quotecmp(&Q,&LP_Alicequery) != 0 )
|
|
|
|
{ |
|
|
@ -479,16 +485,22 @@ char *LP_connectedalice(cJSON *argjson) // alice |
|
|
|
if ( (autxo= LP_utxopairfind(0,Q.desttxid,Q.destvout,Q.feetxid,Q.feevout)) == 0 ) |
|
|
|
{ |
|
|
|
printf("cant find autxo\n"); |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error2",0,0); |
|
|
|
return(clonestr("{\"error\":\"cant find autxo\"}")); |
|
|
|
} |
|
|
|
if ( autxo->S.swap != 0 ) |
|
|
|
{ |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error3",0,0); |
|
|
|
return(clonestr("{\"error\":\"ignore duplicate swap\"}")); |
|
|
|
} |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"connected",Q.R.requestid,Q.R.quoteid); |
|
|
|
butxo = &B; |
|
|
|
memset(butxo,0,sizeof(*butxo)); |
|
|
|
LP_abutxo_set(0,butxo,&Q); |
|
|
|
if ( (qprice= LP_quote_validate(autxo,butxo,&Q,0)) <= SMALLVAL ) |
|
|
|
{ |
|
|
|
LP_availableset(autxo); |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error4",0,0); |
|
|
|
printf("quote validate error %.0f\n",qprice); |
|
|
|
return(clonestr("{\"error\":\"quote validation error\"}")); |
|
|
|
} |
|
|
@ -496,12 +508,16 @@ char *LP_connectedalice(cJSON *argjson) // alice |
|
|
|
{ |
|
|
|
printf("this node has no price for %s/%s (%.8f %.8f)\n",Q.destcoin,Q.srccoin,bid,ask); |
|
|
|
LP_availableset(autxo); |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error5",0,0); |
|
|
|
return(clonestr("{\"error\":\"no price set\"}")); |
|
|
|
} |
|
|
|
printf("%s/%s bid %.8f ask %.8f values %.8f %.8f\n",Q.srccoin,Q.destcoin,bid,ask,dstr(butxo->payment.value),dstr(butxo->deposit.value)); |
|
|
|
price = bid; |
|
|
|
if ( (coin= LP_coinfind(Q.destcoin)) == 0 ) |
|
|
|
{ |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error6",0,0); |
|
|
|
return(clonestr("{\"error\":\"cant get alicecoin\"}")); |
|
|
|
} |
|
|
|
Q.privkey = LP_privkey(Q.destaddr,coin->taddr); |
|
|
|
if ( bits256_nonz(Q.privkey) != 0 )//&& Q.quotetime >= Q.timestamp-3 )
|
|
|
|
{ |
|
|
@ -511,10 +527,14 @@ char *LP_connectedalice(cJSON *argjson) // alice |
|
|
|
{ |
|
|
|
jaddstr(retjson,"error","couldnt swapinit"); |
|
|
|
LP_availableset(autxo); |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error7",Q.R.requestid,Q.R.quoteid); |
|
|
|
return(jprint(retjson,1)); |
|
|
|
} |
|
|
|
if ( (pairstr= jstr(argjson,"pair")) == 0 || (pairsock= nn_socket(AF_SP,NN_PAIR)) < 0 ) |
|
|
|
{ |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error8",Q.R.requestid,Q.R.quoteid); |
|
|
|
jaddstr(retjson,"error","couldnt create pairsock"); |
|
|
|
} |
|
|
|
else if ( nn_connect(pairsock,pairstr) >= 0 ) |
|
|
|
{ |
|
|
|
//timeout = 1;
|
|
|
@ -524,6 +544,7 @@ char *LP_connectedalice(cJSON *argjson) // alice |
|
|
|
swap->N.pair = pairsock; |
|
|
|
autxo->S.swap = swap; |
|
|
|
swap->utxo = autxo; |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"started",Q.R.requestid,Q.R.quoteid); |
|
|
|
printf("alice pairstr.(%s) pairsock.%d\n",pairstr,pairsock); |
|
|
|
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_aliceloop,(void *)swap) == 0 ) |
|
|
|
{ |
|
|
@ -531,8 +552,18 @@ char *LP_connectedalice(cJSON *argjson) // alice |
|
|
|
jaddstr(retjson,"result","success"); |
|
|
|
//jaddnum(retjson,"requestid",Q.R.requestid);
|
|
|
|
//jaddnum(retjson,"quoteid",Q.R.quoteid);
|
|
|
|
} else jaddstr(retjson,"error","couldnt aliceloop"); |
|
|
|
} else printf("connect error %s\n",nn_strerror(nn_errno())); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error9",Q.R.requestid,Q.R.quoteid); |
|
|
|
jaddstr(retjson,"error","couldnt aliceloop"); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error10",Q.R.requestid,Q.R.quoteid); |
|
|
|
printf("connect error %s\n",nn_strerror(nn_errno())); |
|
|
|
} |
|
|
|
printf("connected result.(%s)\n",jprint(retjson,0)); |
|
|
|
if ( jobj(retjson,"error") != 0 ) |
|
|
|
LP_availableset(autxo); |
|
|
@ -541,6 +572,7 @@ char *LP_connectedalice(cJSON *argjson) // alice |
|
|
|
else |
|
|
|
{ |
|
|
|
LP_availableset(autxo); |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"error11",0,0); |
|
|
|
printf("no privkey found coin.%s %s taddr.%u\n",Q.destcoin,Q.destaddr,coin->taddr); |
|
|
|
return(clonestr("{\"error\",\"no privkey\"}")); |
|
|
|
} |
|
|
@ -658,6 +690,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson, |
|
|
|
} |
|
|
|
if ( bits256_cmp(G.LP_mypub25519,Q.desthash) == 0 && bits256_cmp(G.LP_mypub25519,Q.srchash) != 0 && LP_alice_eligible() > 0 ) |
|
|
|
{ |
|
|
|
LP_aliceid(Q.tradeid,Q.aliceid,"reserved",0,0); |
|
|
|
printf("alice %s received RESERVED.(%s)\n",bits256_str(str,G.LP_mypub25519),jprint(argjson,0)); |
|
|
|
if ( (retstr= LP_quotereceived(argjson)) != 0 ) |
|
|
|
free(retstr); |
|
|
|