From c2277f48705a139154d6e5ea7377658c02767f2a Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 03:35:39 -1100 Subject: [PATCH 01/60] Reset utxo --- iguana/exchanges/LP_ordermatch.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 858aca8d6..13136c3ea 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -1135,26 +1135,29 @@ printf("bob %s received REQUEST.(%s) fill.%d gtc.%d\n",bits256_str(str,G.LP_mypu else if ( qp->fill != 0 || i == priceiters ) { printf("i.%d cant find utxopair aliceid.%llu %s/%s %.8f -> relvol %.8f txfee %.8f\n",i,(long long)qp->aliceid,qp->srccoin,qp->destcoin,dstr(LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee)),dstr(qp->destsatoshis),dstr(qp->txfee)); - if ( qp->gtc != 0 && qp->fill != 0 && coin != 0 && LP_getheight(¬arized,coin) > coin->bobfillheight+3 ) + if ( qp->gtc != 0 && qp->fill != 0 && coin != 0 ) { - satoshis = LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee) + 3*qp->txfee; LP_address_utxo_reset(&num,coin); - if ( (retstr= LP_autofillbob(coin,satoshis*1.02)) != 0 ) + if (LP_getheight(¬arized,coin) > coin->bobfillheight+3 ) { - if ( (retjson= cJSON_Parse(retstr)) != 0 ) + satoshis = LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee) + 3*qp->txfee; + if ( (retstr= LP_autofillbob(coin,satoshis*1.02)) != 0 ) { - if ( (hexstr= jstr(retjson,"hex")) != 0 ) + if ( (retjson= cJSON_Parse(retstr)) != 0 ) { - if ( (txidstr= LP_sendrawtransaction(coin->symbol,hexstr,0)) != 0 ) + if ( (hexstr= jstr(retjson,"hex")) != 0 ) { - printf("autofill created %s\n",txidstr); - free(txidstr); - coin->bobfillheight = LP_getheight(¬arized,coin); + if ( (txidstr= LP_sendrawtransaction(coin->symbol,hexstr,0)) != 0 ) + { + printf("autofill created %s\n",txidstr); + free(txidstr); + coin->bobfillheight = LP_getheight(¬arized,coin); + } } + free_json(retjson); } - free_json(retjson); + free(retstr); } - free(retstr); } } return(0); From 32d572ac4300a53e19b48aadd7c2e5fdda5e91b3 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 04:35:58 -1100 Subject: [PATCH 02/60] Prevent starvation of gtc --- iguana/exchanges/LP_ordermatch.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 13136c3ea..6206a99f8 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -578,10 +578,15 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock) { - struct LP_gtcorder *gtc,*tmp; struct LP_quoteinfo *qp; uint64_t destvalue,destvalue2; + struct LP_gtcorder *gtc,*tmp; struct LP_quoteinfo *qp; uint64_t destvalue,destvalue2; uint32_t oldest = 0; if ( Alice_expiration != 0 ) return; DL_FOREACH_SAFE(GTCorders,gtc,tmp) + { + if ( gtc->cancelled == 0 && (oldest == 0 || gtc->pending < oldest) ) + oldest = gtc->pending; + } + DL_FOREACH_SAFE(GTCorders,gtc,tmp) { qp = >c->Q; if ( gtc->cancelled == 0 && LP_iseligible(&destvalue,&destvalue2,0,qp->destcoin,qp->desttxid,qp->destvout,qp->destsatoshis,qp->feetxid,qp->feevout) == 0 ) @@ -598,9 +603,9 @@ void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock) } else { - if ( time(NULL) > gtc->pending+LP_AUTOTRADE_TIMEOUT*10 ) + if ( gtc->pending <= oldest+60 && time(NULL) > gtc->pending+LP_AUTOTRADE_TIMEOUT*10 ) { - qp->timestamp = (uint32_t)time(NULL); + gtc->pending = qp->timestamp = (uint32_t)time(NULL); LP_query(ctx,myipaddr,mypubsock,"request",qp); LP_Alicequery = *qp, LP_Alicemaxprice = gtc->maxprice, Alice_expiration = qp->timestamp + 2*LP_AUTOTRADE_TIMEOUT, LP_Alicedestpubkey = qp->srchash; char str[65]; printf("LP_gtc fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),gtc->maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); From c5bf9bee56b9231899b1589df6e24c5af4d06be8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 06:39:11 -1100 Subject: [PATCH 03/60] mpnet stubs --- iguana/exchanges/LP_include.h | 2 +- iguana/exchanges/LP_ordermatch.c | 80 +++++++++++++++++++++----------- iguana/exchanges/LP_signatures.c | 26 +++++++---- 3 files changed, 71 insertions(+), 37 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 1dd67762f..e820e8e42 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -380,7 +380,7 @@ struct LP_quoteinfo bits256 srchash,desthash,txid,txid2,desttxid,feetxid,privkey; int64_t othercredits; uint64_t satoshis,txfee,destsatoshis,desttxfee,aliceid; - uint32_t timestamp,quotetime,tradeid,gtc,fill; + uint32_t timestamp,quotetime,tradeid,gtc,fill,mpnet; int32_t vout,vout2,destvout,feevout,pair; char srccoin[65],coinaddr[64],destcoin[65],destaddr[64],gui[64],etomicsrc[65],etomicdest[65],uuidstr[65]; }; diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 6206a99f8..8e715b858 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -526,27 +526,35 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double LP_importaddress(qp->destcoin,qp->destaddr); LP_otheraddress(qp->srccoin,otheraddr,qp->destcoin,qp->destaddr); LP_importaddress(qp->srccoin,otheraddr); - bits256 zero; - memset(zero.bytes,0,sizeof(zero)); - for (i=0; i<1; i++) + if ( qp->mpnet == 0 || qp->fill == 0 || qp->gtc == 0 ) { - LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0)); - break; - sleep(10); - if ( swap->received != 0 ) + bits256 zero; + memset(zero.bytes,0,sizeof(zero)); + for (i=0; i<1; i++) { - printf("swap %u-%u has started t%u\n",swap->I.req.requestid,swap->I.req.quoteid,swap->received); + LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0)); break; + sleep(10); + if ( swap->received != 0 ) + { + printf("swap %u-%u has started t%u\n",swap->I.req.requestid,swap->I.req.quoteid,swap->received); + break; + } + printf("bob tries %u-%u again i.%d\n",swap->I.req.requestid,swap->I.req.quoteid,i); + LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); } - printf("bob tries %u-%u again i.%d\n",swap->I.req.requestid,swap->I.req.quoteid,i); + sleep(1); + printf("send CONNECT for %u-%u\n",qp->R.requestid,qp->R.quoteid); LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); + if ( IPC_ENDPOINT >= 0 ) + LP_queuecommand(0,jprint(reqjson,1),IPC_ENDPOINT,-1,0); + else free_json(reqjson); + } + else + { + // send to mpnet + free_json(reqjson); } - sleep(1); - printf("send CONNECT for %u-%u\n",qp->R.requestid,qp->R.quoteid); - LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); - if ( IPC_ENDPOINT >= 0 ) - LP_queuecommand(0,jprint(reqjson,1),IPC_ENDPOINT,-1,0); - else free_json(reqjson); retval = 0; } else @@ -583,7 +591,8 @@ void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock) return; DL_FOREACH_SAFE(GTCorders,gtc,tmp) { - if ( gtc->cancelled == 0 && (oldest == 0 || gtc->pending < oldest) ) + qp = >c->Q; + if ( (qp->mpnet == 0 || qp->fill == 0) && gtc->cancelled == 0 && (oldest == 0 || gtc->pending < oldest) ) oldest = gtc->pending; } DL_FOREACH_SAFE(GTCorders,gtc,tmp) @@ -603,7 +612,7 @@ void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock) } else { - if ( gtc->pending <= oldest+60 && time(NULL) > gtc->pending+LP_AUTOTRADE_TIMEOUT*10 ) + if ( (qp->mpnet == 0 || qp->fill == 0) && gtc->pending <= oldest+60 && time(NULL) > gtc->pending+LP_AUTOTRADE_TIMEOUT*10 ) { gtc->pending = qp->timestamp = (uint32_t)time(NULL); LP_query(ctx,myipaddr,mypubsock,"request",qp); @@ -637,9 +646,16 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q DL_APPEND(GTCorders,gtc); portable_mutex_unlock(&LP_gtcmutex); } - LP_query(ctx,myipaddr,mypubsock,"request",qp); - LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash; - char str[65]; printf("LP_trade fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); + if ( qp->mpnet == 0 || qp->gtc == 0 || qp->fill == 0 ) + { + LP_query(ctx,myipaddr,mypubsock,"request",qp); + LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash; + } + else + { + // send to mpnet + } + char str[65]; printf("LP_trade mpnet.%d fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->mpnet,qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); return(LP_recent_swaps(0,uuidstr)); } @@ -740,7 +756,7 @@ char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice LP_failedmsg(qp->R.requestid,qp->R.quoteid,-4000,qp->uuidstr); return(clonestr("{\"result\",\"update stats\"}")); } - printf("CONNECTED fill.%d gtc.%d numpending.%d tradeid.%u requestid.%u quoteid.%u pairstr.%s\n",qp->fill,qp->gtc,G.LP_pendingswaps,qp->tradeid,qp->R.requestid,qp->R.quoteid,pairstr!=0?pairstr:""); + printf("CONNECTED mpnet.%d fill.%d gtc.%d numpending.%d tradeid.%u requestid.%u quoteid.%u pairstr.%s\n",qp->mpnet,qp->fill,qp->gtc,G.LP_pendingswaps,qp->tradeid,qp->R.requestid,qp->R.quoteid,pairstr!=0?pairstr:""); LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis-qp->txfee,qp->destcoin,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector,qp->fill,qp->gtc); //printf("calculated requestid.%u quoteid.%u\n",qp->R.requestid,qp->R.quoteid); memset(&LP_Alicereserved,0,sizeof(LP_Alicereserved)); @@ -1008,7 +1024,7 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru double price=0.,p=0.,qprice,myprice,bestprice,range,bid,ask; uint64_t satoshis; struct iguana_info *coin,*othercoin; struct LP_utxoinfo A,B,*autxo,*butxo; cJSON *reqjson,*retjson; char str[65],*retstr,*txidstr,*hexstr; struct LP_address_utxo *utxos[4096]; int32_t i,j,notarized,r,num,counter,max = (int32_t)(sizeof(utxos)/sizeof(*utxos)); *newqp = *qp; qp = newqp; -printf("bob %s received REQUEST.(%s) fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32,qp->fill,qp->gtc); +printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32,qp->mpnet,qp->fill,qp->gtc); if ( (coin= LP_coinfind(qp->srccoin)) == 0 || (othercoin= LP_coinfind(qp->destcoin)) == 0 ) return(0); if ( (myprice= LP_trades_bobprice(&bid,&ask,qp)) == 0. ) @@ -1185,10 +1201,17 @@ printf("bob %s received REQUEST.(%s) fill.%d gtc.%d\n",bits256_str(str,G.LP_mypu jaddnum(reqjson,"quotetime",qp->quotetime); jaddnum(reqjson,"pending",qp->timestamp + LP_RESERVETIME); jaddstr(reqjson,"method","reserved"); - LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0)); - bits256 zero; - memset(zero.bytes,0,sizeof(zero)); - LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); + if ( qp->mpnet == 0 || qp->fill == 0 || qp->gtc == 0 ) + { + LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0)); + bits256 zero; + memset(zero.bytes,0,sizeof(zero)); + LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); + } + else + { + // send to mpnet + } free_json(reqjson); //printf("Send RESERVED id.%llu\n",(long long)qp->aliceid); return(qp); @@ -1199,7 +1222,7 @@ printf("bob %s received REQUEST.(%s) fill.%d gtc.%d\n",bits256_str(str,G.LP_mypu struct LP_quoteinfo *LP_trades_gotreserved(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp) { char *retstr; double qprice; - char str[65]; printf("alice %s received RESERVED.(%s) %.8f fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32,(double)qp->destsatoshis/(qp->satoshis+1),qp->fill,qp->gtc); + char str[65]; printf("alice %s received RESERVED.(%s) %.8f mpnet.%d fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32,(double)qp->destsatoshis/(qp->satoshis+1),qp->mpnet,qp->fill,qp->gtc); *newqp = *qp; qp = newqp; if ( (qprice= LP_trades_alicevalidate(ctx,qp)) > 0. ) @@ -1241,7 +1264,7 @@ struct LP_quoteinfo *LP_trades_gotconnect(void *ctx,struct LP_quoteinfo *qp,stru struct LP_quoteinfo *LP_trades_gotconnected(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp,char *pairstr) { char *retstr; int32_t changed; double val; - char str[65]; printf("alice %s received CONNECTED.(%llu) fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid,qp->fill,qp->gtc); + char str[65]; printf("alice %s received CONNECTED.(%llu) mpnet.%d fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid,qp->mpnet,qp->fill,qp->gtc); *newqp = *qp; qp = newqp; if ( (val= LP_trades_alicevalidate(ctx,qp)) > 0. ) @@ -1783,6 +1806,7 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha } } int32_t changed; + Q.mpnet = 1; Q.fill = fillflag; Q.gtc = gtcflag; LP_mypriceset(&changed,rel,base,1. / maxprice); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index 3e3055ee0..8242d39a3 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -45,6 +45,8 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp) if ( jobj(retjson,"gui") == 0 ) jaddstr(retjson,"gui",qp->gui[0] != 0 ? qp->gui : LP_gui); jaddstr(retjson,"uuid",qp->uuidstr); + if ( qp->mpnet != 0 ) + jaddnum(retjson,"mpnet",qp->mpnet); if ( qp->gtc != 0 ) jaddnum(retjson,"gtc",qp->gtc); if ( qp->fill != 0 ) @@ -117,6 +119,7 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) { uint32_t rid,qid; char etomic[64],activesymbol[65],*etomicstr; memset(qp,0,sizeof(*qp)); + qp->mpnet = juint(argjson,"mpnet"); qp->gtc = juint(argjson,"gtc"); qp->fill = juint(argjson,"fill"); safecopy(qp->gui,LP_gui,sizeof(qp->gui)); @@ -723,15 +726,22 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ jadd(reqjson,"proof",LP_instantdex_txids(0,coin->smartaddr)); } msg = jprint(reqjson,1); - //printf("QUERY.(%s)\n",msg); - if ( IPC_ENDPOINT >= 0 ) - LP_queuecommand(0,msg,IPC_ENDPOINT,-1,0); - memset(&zero,0,sizeof(zero)); - LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg)); - //if ( bits256_nonz(qp->srchash) != 0 ) + if ( qp->mpnet == 0 || qp->fill == 0 || qp->gtc == 0 ) { - sleep(1); - LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg)); + //printf("QUERY.(%s)\n",msg); + if ( IPC_ENDPOINT >= 0 ) + LP_queuecommand(0,msg,IPC_ENDPOINT,-1,0); + memset(&zero,0,sizeof(zero)); + LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg)); + //if ( bits256_nonz(qp->srchash) != 0 ) + { + sleep(1); + LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg)); + } + } + else + { + // send to mpnet } free(msg); } From 87e3235b6b6d3985bc3cb7e501f2dae96a292c8b Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 07:42:34 -1100 Subject: [PATCH 04/60] Clear my price on Alice query clear --- iguana/exchanges/LP_ordermatch.c | 23 ++++++++++++----------- iguana/exchanges/LP_signatures.c | 3 +-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 8e715b858..0f77f4386 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -526,7 +526,6 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double LP_importaddress(qp->destcoin,qp->destaddr); LP_otheraddress(qp->srccoin,otheraddr,qp->destcoin,qp->destaddr); LP_importaddress(qp->srccoin,otheraddr); - if ( qp->mpnet == 0 || qp->fill == 0 || qp->gtc == 0 ) { bits256 zero; memset(zero.bytes,0,sizeof(zero)); @@ -547,14 +546,13 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double printf("send CONNECT for %u-%u\n",qp->R.requestid,qp->R.quoteid); LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); if ( IPC_ENDPOINT >= 0 ) - LP_queuecommand(0,jprint(reqjson,1),IPC_ENDPOINT,-1,0); - else free_json(reqjson); + LP_queuecommand(0,jprint(reqjson,0),IPC_ENDPOINT,-1,0); } - else + if ( qp->mpnet != 0 && qp->fill != 0 && qp->gtc != 0 ) { // send to mpnet - free_json(reqjson); } + free_json(reqjson); retval = 0; } else @@ -592,7 +590,7 @@ void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock) DL_FOREACH_SAFE(GTCorders,gtc,tmp) { qp = >c->Q; - if ( (qp->mpnet == 0 || qp->fill == 0) && gtc->cancelled == 0 && (oldest == 0 || gtc->pending < oldest) ) + if ( gtc->cancelled == 0 && (oldest == 0 || gtc->pending < oldest) ) oldest = gtc->pending; } DL_FOREACH_SAFE(GTCorders,gtc,tmp) @@ -612,7 +610,7 @@ void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock) } else { - if ( (qp->mpnet == 0 || qp->fill == 0) && gtc->pending <= oldest+60 && time(NULL) > gtc->pending+LP_AUTOTRADE_TIMEOUT*10 ) + if ( gtc->pending <= oldest+60 && time(NULL) > gtc->pending+LP_AUTOTRADE_TIMEOUT*10 ) { gtc->pending = qp->timestamp = (uint32_t)time(NULL); LP_query(ctx,myipaddr,mypubsock,"request",qp); @@ -646,12 +644,11 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q DL_APPEND(GTCorders,gtc); portable_mutex_unlock(&LP_gtcmutex); } - if ( qp->mpnet == 0 || qp->gtc == 0 || qp->fill == 0 ) { LP_query(ctx,myipaddr,mypubsock,"request",qp); LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash; } - else + if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) { // send to mpnet } @@ -680,6 +677,11 @@ int32_t LP_quotecmp(int32_t strictflag,struct LP_quoteinfo *qp,struct LP_quotein void LP_alicequery_clear() { + if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 ) + { + LP_mypriceset(&changed,LP_Alicequery.destcoin,LP_Alicequery.srccoin,0.); + LP_mypriceset(&changed,LP_Alicequery.srccoin,LP_Alicequery.destcoin,0.); + } memset(&LP_Alicequery,0,sizeof(LP_Alicequery)); memset(&LP_Alicedestpubkey,0,sizeof(LP_Alicedestpubkey)); LP_Alicemaxprice = 0.; @@ -1201,14 +1203,13 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, jaddnum(reqjson,"quotetime",qp->quotetime); jaddnum(reqjson,"pending",qp->timestamp + LP_RESERVETIME); jaddstr(reqjson,"method","reserved"); - if ( qp->mpnet == 0 || qp->fill == 0 || qp->gtc == 0 ) { LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0)); bits256 zero; memset(zero.bytes,0,sizeof(zero)); LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); } - else + if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) { // send to mpnet } diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index 8242d39a3..15fb2b694 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -726,7 +726,6 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ jadd(reqjson,"proof",LP_instantdex_txids(0,coin->smartaddr)); } msg = jprint(reqjson,1); - if ( qp->mpnet == 0 || qp->fill == 0 || qp->gtc == 0 ) { //printf("QUERY.(%s)\n",msg); if ( IPC_ENDPOINT >= 0 ) @@ -739,7 +738,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg)); } } - else + if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) { // send to mpnet } From 4dd75d915a8a30a4159a9cbdcb2b62ebfc60d84c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 07:43:24 -1100 Subject: [PATCH 05/60] Clear mpnet for now --- iguana/exchanges/LP_ordermatch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 0f77f4386..f319f7aa2 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -1807,7 +1807,7 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha } } int32_t changed; - Q.mpnet = 1; + Q.mpnet = 0; Q.fill = fillflag; Q.gtc = gtcflag; LP_mypriceset(&changed,rel,base,1. / maxprice); From 2886dc9867e76d411599fb04a1daf5875ae96af4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 07:43:53 -1100 Subject: [PATCH 06/60] Fix --- iguana/exchanges/LP_ordermatch.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index f319f7aa2..eceb7e58e 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -677,6 +677,7 @@ int32_t LP_quotecmp(int32_t strictflag,struct LP_quoteinfo *qp,struct LP_quotein void LP_alicequery_clear() { + int32_t changed; if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 ) { LP_mypriceset(&changed,LP_Alicequery.destcoin,LP_Alicequery.srccoin,0.); From dd730c5bf5b745d1063306eda8bc078f46bfd742 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 08:05:12 -1100 Subject: [PATCH 07/60] Test --- iguana/exchanges/LP_ordermatch.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index eceb7e58e..9033696f7 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -677,12 +677,6 @@ int32_t LP_quotecmp(int32_t strictflag,struct LP_quoteinfo *qp,struct LP_quotein void LP_alicequery_clear() { - int32_t changed; - if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 ) - { - LP_mypriceset(&changed,LP_Alicequery.destcoin,LP_Alicequery.srccoin,0.); - LP_mypriceset(&changed,LP_Alicequery.srccoin,LP_Alicequery.destcoin,0.); - } memset(&LP_Alicequery,0,sizeof(LP_Alicequery)); memset(&LP_Alicedestpubkey,0,sizeof(LP_Alicedestpubkey)); LP_Alicemaxprice = 0.; @@ -691,11 +685,17 @@ void LP_alicequery_clear() int32_t LP_alice_eligible(uint32_t quotetime) { + int32_t changed; if ( Alice_expiration != 0 && quotetime > Alice_expiration ) { if ( LP_Alicequery.uuidstr[0] != 0 ) LP_failedmsg(LP_Alicequery.R.requestid,LP_Alicequery.R.quoteid,-9999,LP_Alicequery.uuidstr); printf("time expired for Alice_request\n"); + if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 ) + { + LP_mypriceset(&changed,LP_Alicequery.destcoin,LP_Alicequery.srccoin,0.); + LP_mypriceset(&changed,LP_Alicequery.srccoin,LP_Alicequery.destcoin,0.); + } LP_alicequery_clear(); } return(Alice_expiration == 0 || time(NULL) < Alice_expiration); From 79b30cc997b178d0fac62d46e54c1e718e572438 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 08:11:31 -1100 Subject: [PATCH 08/60] Restruct autofill tux-merge --- iguana/exchanges/LP_ordermatch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 9033696f7..bb454c167 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -1162,9 +1162,9 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, if ( qp->gtc != 0 && qp->fill != 0 && coin != 0 ) { LP_address_utxo_reset(&num,coin); - if (LP_getheight(¬arized,coin) > coin->bobfillheight+3 ) + satoshis = LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee) + 3*qp->txfee; + if ( LP_getheight(¬arized,coin) > coin->bobfillheight+3 && fabs(coin->fillsatoshis - satoshis)/satoshis > 0.1 ) { - satoshis = LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee) + 3*qp->txfee; if ( (retstr= LP_autofillbob(coin,satoshis*1.02)) != 0 ) { if ( (retjson= cJSON_Parse(retstr)) != 0 ) From a6fd1efddd3dbb3da7f39739694437954d579abc Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 08:13:03 -1100 Subject: [PATCH 09/60] Fix --- iguana/exchanges/LP_include.h | 2 +- iguana/exchanges/LP_ordermatch.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index e820e8e42..c0eafa3de 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -311,7 +311,7 @@ struct iguana_info // portfolio double price_kmd,force,perc,goal,goalperc,relvolume,rate; void *electrum; void *ctx; - uint64_t maxamount,kmd_equiv,balanceA,balanceB,valuesumA,valuesumB; + uint64_t maxamount,kmd_equiv,balanceA,balanceB,valuesumA,valuesumB,fillsatoshis; uint8_t pubkey33[33],zcash; int32_t privkeydepth,bobfillheight; void *curl_handle; portable_mutex_t curl_mutex; diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index bb454c167..faa9e98b1 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -1175,6 +1175,7 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, { printf("autofill created %s\n",txidstr); free(txidstr); + coin->fillsatoshis = satoshis; coin->bobfillheight = LP_getheight(¬arized,coin); } } From f211c162d0be664a0625be8076682ae7f52a61a4 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 08:18:37 -1100 Subject: [PATCH 10/60] Revert --- iguana/exchanges/LP_ordermatch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index faa9e98b1..b210f182b 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -691,11 +691,11 @@ int32_t LP_alice_eligible(uint32_t quotetime) if ( LP_Alicequery.uuidstr[0] != 0 ) LP_failedmsg(LP_Alicequery.R.requestid,LP_Alicequery.R.quoteid,-9999,LP_Alicequery.uuidstr); printf("time expired for Alice_request\n"); - if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 ) + /*if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 ) cant do this! myprice is used to validate the order { LP_mypriceset(&changed,LP_Alicequery.destcoin,LP_Alicequery.srccoin,0.); LP_mypriceset(&changed,LP_Alicequery.srccoin,LP_Alicequery.destcoin,0.); - } + }*/ LP_alicequery_clear(); } return(Alice_expiration == 0 || time(NULL) < Alice_expiration); From b90399c8fbcfde074993c2a22513c3f24245f64c Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 09:30:42 -1100 Subject: [PATCH 11/60] do_autofill_merge queued --- iguana/exchanges/LP_include.h | 2 +- iguana/exchanges/LP_nativeDEX.c | 24 +++++++++++++++++++++++- iguana/exchanges/LP_ordermatch.c | 22 +++------------------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index c0eafa3de..4cbf9a89f 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -303,7 +303,7 @@ struct iguana_info { UT_hash_handle hh; portable_mutex_t txmutex,addrmutex,addressutxo_mutex; struct LP_transaction *transactions; struct LP_address *addresses; - uint64_t txfee; + uint64_t txfee,do_autofill_merge; int32_t numutxos,notarized,longestchain,firstrefht,firstscanht,lastscanht,height; uint16_t busport,did_addrutxo_reset; uint32_t txversion,dPoWtime,lastautosplit,lastresetutxo,loadedcache,electrumlist,lastunspent,importedprivkey,lastpushtime,lastutxosync,addr_listunspent_requested,lastutxos,updaterate,counter,inactive,lastmempool,lastgetinfo,ratetime,heighttime,lastmonitor,obooktime; uint8_t pubtype,p2shtype,isPoS,wiftype,wiftaddr,taddr,noimportprivkey_flag,userconfirms,isassetchain,maxconfirms; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index bde3532cc..5e321e562 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -513,7 +513,7 @@ void command_rpcloop(void *ctx) void LP_coinsloop(void *_coins) { static int32_t didfilescreate; - struct LP_address *ap=0; struct LP_transaction *tx; cJSON *retjson; struct LP_address_utxo *up,*tmp; struct iguana_info *coin,*ctmp; char str[65]; struct electrum_info *ep,*backupep=0; bits256 zero; int32_t notarized,oldht,j,nonz; char *coins = _coins; + struct LP_address *ap=0; struct LP_transaction *tx; cJSON *retjson; struct LP_address_utxo *up,*tmp; struct iguana_info *coin,*ctmp; char str[65],*retstr,*hexstr,*txidstr; struct electrum_info *ep,*backupep=0; bits256 zero; int32_t notarized,oldht,j,nonz; char *coins = _coins; if ( strcmp("BTC",coins) == 0 ) { strcpy(LP_coinsloopBTC_stats.name,"BTC coin loop"); @@ -576,6 +576,28 @@ void LP_coinsloop(void *_coins) LP_address_utxo_reset(&num,coin); coin->did_addrutxo_reset = 1; } + if ( coin->do_autofill_merge != 0 ) + { + if ( (retstr= LP_autofillbob(coin,coin->do_autofill_merge*1.02)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( (hexstr= jstr(retjson,"hex")) != 0 ) + { + if ( (txidstr= LP_sendrawtransaction(coin->symbol,hexstr,0)) != 0 ) + { + printf("autofill created %s\n",txidstr); + free(txidstr); + coin->do_autofill_merge = 0; + coin->fillsatoshis = satoshis; + coin->bobfillheight = LP_getheight(¬arized,coin); + } + } + free_json(retjson); + } + free(retstr); + } + } if ( coin->longestchain == 1 ) // special init value coin->longestchain = LP_getheight(¬arized,coin); if ( (ep= coin->electrum) != 0 ) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index b210f182b..b7be970c5 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -1163,26 +1163,10 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, { LP_address_utxo_reset(&num,coin); satoshis = LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee) + 3*qp->txfee; - if ( LP_getheight(¬arized,coin) > coin->bobfillheight+3 && fabs(coin->fillsatoshis - satoshis)/satoshis > 0.1 ) + if ( LP_getheight(¬arized,coin) > coin->bobfillheight+3 && fabs((double)coin->fillsatoshis - satoshis)/satoshis > 0.1 ) { - if ( (retstr= LP_autofillbob(coin,satoshis*1.02)) != 0 ) - { - if ( (retjson= cJSON_Parse(retstr)) != 0 ) - { - if ( (hexstr= jstr(retjson,"hex")) != 0 ) - { - if ( (txidstr= LP_sendrawtransaction(coin->symbol,hexstr,0)) != 0 ) - { - printf("autofill created %s\n",txidstr); - free(txidstr); - coin->fillsatoshis = satoshis; - coin->bobfillheight = LP_getheight(¬arized,coin); - } - } - free_json(retjson); - } - free(retstr); - } + printf("queue up do_autofill_merge %.8f\n",dstr(satoshis)); + coin->do_autofill_merge = satoshis; } } return(0); From 858c6bdf50a99ed35fb1157ae4e86edf4e05e210 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 09:34:30 -1100 Subject: [PATCH 12/60] Fix --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 5e321e562..7c5a02037 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -588,8 +588,8 @@ void LP_coinsloop(void *_coins) { printf("autofill created %s\n",txidstr); free(txidstr); + coin->fillsatoshis = coin->do_autofill_merge; coin->do_autofill_merge = 0; - coin->fillsatoshis = satoshis; coin->bobfillheight = LP_getheight(¬arized,coin); } } From 42cc2b83224763ca9ee5b1561b79ff09de6ca6d5 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 22:13:06 -1100 Subject: [PATCH 13/60] Separate my prices into bob and Alice --- iguana/exchanges/LP_commands.c | 20 ++++---- iguana/exchanges/LP_nativeDEX.c | 1 + iguana/exchanges/LP_ordermatch.c | 28 +++++----- iguana/exchanges/LP_portfolio.c | 18 +++---- iguana/exchanges/LP_prices.c | 87 +++++++++++++++++--------------- 5 files changed, 80 insertions(+), 74 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index baab34c10..265827d58 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -486,14 +486,14 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ double price,bid,ask; if ( strcmp(method,"getprice") == 0 ) { - ask = LP_price(base,rel); - if ( (bid= LP_price(rel,base)) > SMALLVAL ) + ask = LP_price(1,base,rel); + if ( (bid= LP_price(1,rel,base)) > SMALLVAL ) bid = 1./bid; } else { - ask = LP_getmyprice(base,rel); - if ( (bid= LP_getmyprice(rel,base)) > SMALLVAL ) + ask = LP_getmyprice(1,base,rel); + if ( (bid= LP_getmyprice(1,rel,base)) > SMALLVAL ) bid = 1./bid; } price = _pairaved(bid,ask); @@ -512,9 +512,9 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ price = jdouble(argjson,"price"); if ( strcmp(method,"setprice") == 0 ) { - if ( LP_mypriceset(&changed,base,rel,price) < 0 ) + if ( LP_mypriceset(1,&changed,base,rel,price) < 0 ) return(clonestr("{\"error\":\"couldnt set price\"}")); - //else if ( LP_mypriceset(&changed,rel,base,1./price) < 0 ) + //else if ( LP_mypriceset(1,&changed,rel,base,1./price) < 0 ) // return(clonestr("{\"error\":\"couldnt set price\"}")); else if ( price == 0. || jobj(argjson,"broadcast") == 0 || jint(argjson,"broadcast") != 0 ) return(LP_pricepings(ctx,myipaddr,LP_mypubsock,base,rel,price * LP_profitratio)); @@ -524,7 +524,7 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ return(LP_orderbook(base,rel,jint(argjson,"duration"))); else if ( strcmp(method,"myprice") == 0 ) { - if ( LP_myprice(&bid,&ask,base,rel) > SMALLVAL ) + if ( LP_myprice(1,&bid,&ask,base,rel) > SMALLVAL ) { retjson = cJSON_CreateObject(); jaddstr(retjson,"base",base); @@ -801,7 +801,7 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ return(jprint(retjson,1)); } else if ( strcmp(method,"myprices") == 0 ) - return(LP_myprices()); + return(LP_myprices(1)); else if ( strcmp(method,"trust") == 0 ) return(LP_pubkey_trustset(jbits256(argjson,"pubkey"),jint(argjson,"trust"))); else if ( strcmp(method,"trusted") == 0 ) @@ -863,12 +863,12 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ if ( strcmp(method,"getprice") == 0 ) { ask = LP_price(base,rel); - if ( (bid= LP_price(rel,base)) > SMALLVAL ) + if ( (bid= LP_price(1,rel,base)) > SMALLVAL ) bid = 1./bid; } else { - ask = LP_getmyprice(base,rel); + ask = LP_getmyprice(1,base,rel); if ( (bid= LP_getmyprice(rel,base)) > SMALLVAL ) bid = 1./bid; } diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 7c5a02037..6765de92b 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -576,6 +576,7 @@ void LP_coinsloop(void *_coins) LP_address_utxo_reset(&num,coin); coin->did_addrutxo_reset = 1; } + free_json(LP_address_balance(coin,coin->smartaddr,1)); if ( coin->do_autofill_merge != 0 ) { if ( (retstr= LP_autofillbob(coin,coin->do_autofill_merge*1.02)) != 0 ) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index b7be970c5..cac436955 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -685,17 +685,11 @@ void LP_alicequery_clear() int32_t LP_alice_eligible(uint32_t quotetime) { - int32_t changed; if ( Alice_expiration != 0 && quotetime > Alice_expiration ) { if ( LP_Alicequery.uuidstr[0] != 0 ) LP_failedmsg(LP_Alicequery.R.requestid,LP_Alicequery.R.quoteid,-9999,LP_Alicequery.uuidstr); printf("time expired for Alice_request\n"); - /*if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 ) cant do this! myprice is used to validate the order - { - LP_mypriceset(&changed,LP_Alicequery.destcoin,LP_Alicequery.srccoin,0.); - LP_mypriceset(&changed,LP_Alicequery.srccoin,LP_Alicequery.destcoin,0.); - }*/ LP_alicequery_clear(); } return(Alice_expiration == 0 || time(NULL) < Alice_expiration); @@ -752,7 +746,7 @@ char *LP_cancel_order(char *uuidstr) char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice { - cJSON *retjson; char otheraddr[64],*msg; double bid,ask,price,qprice; int32_t pairsock = -1; int32_t DEXselector = 0; struct LP_utxoinfo *autxo,A,B,*butxo; struct basilisk_swap *swap; struct iguana_info *coin; + cJSON *retjson; char otheraddr[64],*msg; double bid,ask,price,qprice; int32_t connected,pairsock = -1; int32_t DEXselector = 0; struct LP_utxoinfo *autxo,A,B,*butxo; struct basilisk_swap *swap; struct iguana_info *coin; if ( bits256_cmp(qp->desthash,G.LP_mypub25519) != 0 ) { LP_aliceid(qp->tradeid,qp->aliceid,"error1",0,0); @@ -762,6 +756,12 @@ char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice printf("CONNECTED mpnet.%d fill.%d gtc.%d numpending.%d tradeid.%u requestid.%u quoteid.%u pairstr.%s\n",qp->mpnet,qp->fill,qp->gtc,G.LP_pendingswaps,qp->tradeid,qp->R.requestid,qp->R.quoteid,pairstr!=0?pairstr:""); LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis-qp->txfee,qp->destcoin,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector,qp->fill,qp->gtc); //printf("calculated requestid.%u quoteid.%u\n",qp->R.requestid,qp->R.quoteid); + if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 ) + { + LP_mypriceset(0,&changed,LP_Alicequery.destcoin,LP_Alicequery.srccoin,0.); + LP_mypriceset(0,&changed,LP_Alicequery.srccoin,LP_Alicequery.destcoin,0.); + } + LP_alicequery_clear(); memset(&LP_Alicereserved,0,sizeof(LP_Alicereserved)); LP_aliceid(qp->tradeid,qp->aliceid,"connected",qp->R.requestid,qp->R.quoteid); autxo = &A; @@ -778,7 +778,7 @@ char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice printf("quote %s/%s validate error %.0f\n",qp->srccoin,qp->destcoin,qprice); return(clonestr("{\"error\":\"quote validation error\"}")); } - if ( LP_myprice(&bid,&ask,qp->srccoin,qp->destcoin) <= SMALLVAL || bid <= SMALLVAL ) + if ( LP_myprice(0,&bid,&ask,qp->srccoin,qp->destcoin) <= SMALLVAL || bid <= SMALLVAL ) { printf("this node has no price for %s/%s (%.8f %.8f)\n",qp->destcoin,qp->srccoin,bid,ask); LP_availableset(qp->desttxid,qp->vout); @@ -969,7 +969,7 @@ void LP_reserved(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo double LP_trades_bobprice(double *bidp,double *askp,struct LP_quoteinfo *qp) { double price; struct iguana_info *coin; char str[65]; - price = LP_myprice(bidp,askp,qp->srccoin,qp->destcoin); + price = LP_myprice(1,bidp,askp,qp->srccoin,qp->destcoin); if ( (coin= LP_coinfind(qp->srccoin)) == 0 || price <= SMALLVAL || *askp <= SMALLVAL ) { //printf("this node has no price for %s/%s\n",qp->srccoin,qp->destcoin); @@ -1260,7 +1260,7 @@ struct LP_quoteinfo *LP_trades_gotconnected(void *ctx,struct LP_quoteinfo *qp,st LP_aliceid(qp->tradeid,qp->aliceid,"connected",0,0); if ( (retstr= LP_connectedalice(qp,pairstr)) != 0 ) free(retstr); - LP_mypriceset(&changed,qp->destcoin,qp->srccoin,0.); + LP_mypriceset(0,&changed,qp->destcoin,qp->srccoin,0.); LP_alicequery_clear(); return(qp); } else LP_failedmsg(qp->R.requestid,qp->R.quoteid,val,qp->uuidstr); @@ -1594,7 +1594,9 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson, else LP_tradecommandQ(&Q,jstr(argjson,"pair"),LP_CONNECTED); } } - price = LP_myprice(&bid,&ask,Q.srccoin,Q.destcoin); + if ( strcmp(method,"request") == 0 || strcmp(method,"connect") == 0 ) // bob + price = LP_myprice(1,&bid,&ask,Q.srccoin,Q.destcoin); + else price = LP_myprice(0,&bid,&ask,Q.srccoin,Q.destcoin); // alice if ( (coin= LP_coinfind(Q.srccoin)) == 0 || coin->inactive != 0 ) { //printf("%s is not active\n",Q.srccoin); @@ -1796,8 +1798,8 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha Q.mpnet = 0; Q.fill = fillflag; Q.gtc = gtcflag; - LP_mypriceset(&changed,rel,base,1. / maxprice); - LP_mypriceset(&changed,base,rel,0.); + LP_mypriceset(0,&changed,rel,base,1. / maxprice); + LP_mypriceset(0,&changed,base,rel,0.); if ( uuidstr == 0 || uuidstr[0] == 0 ) { uint8_t uuidhash[256]; bits256 hash; uint64_t millis; int32_t len = 0; diff --git a/iguana/exchanges/LP_portfolio.c b/iguana/exchanges/LP_portfolio.c index 6f6c6c0a7..dea9bce00 100644 --- a/iguana/exchanges/LP_portfolio.c +++ b/iguana/exchanges/LP_portfolio.c @@ -152,7 +152,7 @@ char *LP_portfolio() coin->balanceA = LP_balance(&coin->valuesumA,0,coin->symbol,coin->smartaddr); coin->balanceB = LP_balance(&coin->valuesumB,1,coin->symbol,coin->smartaddr); if ( strcmp(coin->symbol,"KMD") != 0 ) - coin->price_kmd = LP_price(coin->symbol,"KMD"); + coin->price_kmd = LP_price(1,coin->symbol,"KMD"); else coin->price_kmd = 1.; coin->maxamount = coin->valuesumA; if ( coin->valuesumB > coin->maxamount ) @@ -274,7 +274,7 @@ void LP_autopriceset(int32_t ind,void *ctx,int32_t dir,struct LP_priceinfo *base oppomargin = basepp->buymargins[relpp->ind]; if ( (fixedprice= basepp->fixedprices[relpp->ind]) > SMALLVAL ) { - LP_mypriceset(&changed,relpp->symbol,basepp->symbol,fixedprice); + LP_mypriceset(1,&changed,relpp->symbol,basepp->symbol,fixedprice); //printf("autoprice FIXED %s/%s <- %.8f\n",basepp->symbol,relpp->symbol,fixedprice); LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,relpp->symbol,basepp->symbol,fixedprice); return; @@ -285,7 +285,7 @@ void LP_autopriceset(int32_t ind,void *ctx,int32_t dir,struct LP_priceinfo *base factor = basepp->factors[relpp->ind]; if ( fabs(price) < SMALLVAL && refbase != 0 && refrel != 0 ) { - price = LP_myprice(&bid,&ask,refbase,refrel); + price = LP_myprice(1,&bid,&ask,refbase,refrel); //printf("%s/%s USE ref %s/%s %.8f factor %.8f offset %.8f margin %.8f/%.8f\n",basepp->symbol,relpp->symbol,refbase,refrel,price,factor,offset,oppomargin,margin); } if ( LP_pricevalid(price) > 0 ) @@ -312,7 +312,7 @@ void LP_autopriceset(int32_t ind,void *ctx,int32_t dir,struct LP_priceinfo *base newprice = LP_autorefs[ind].lastask; //printf("autopriceset %s/%s <- %.8f %.8f (%.8f %.8f)\n",basepp->symbol,relpp->symbol,price,newprice,LP_autorefs[ind].lastbid,LP_autorefs[ind].lastask); } - LP_mypriceset(&changed,relpp->symbol,basepp->symbol,newprice); + LP_mypriceset(1,&changed,relpp->symbol,basepp->symbol,newprice); if ( changed != 0 || time(NULL) > lasttime+LP_ORDERBOOK_DURATION*.777) { lasttime = (uint32_t)time(NULL); @@ -582,9 +582,9 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp) LP_autorefs[i].lastask = askprice; else LP_autorefs[i].lastask = (LP_autorefs[i].lastask * 0.9) + (0.1 * askprice); askprice = LP_autorefs[i].lastask; - LP_mypriceset(&changed,rel,base,bidprice); + LP_mypriceset(1,&changed,rel,base,bidprice); LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,rel,base,bidprice); - LP_mypriceset(&changed,base,rel,askprice); + LP_mypriceset(1,&changed,base,rel,askprice); LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,base,rel,askprice); //printf("price %.8f -> %.8f %.8f\n",price,bidprice,askprice); } @@ -626,7 +626,7 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp) LP_autorefs[i].lastbid = newprice; else LP_autorefs[i].lastbid = (LP_autorefs[i].lastbid * 0.99) + (0.01 * newprice); newprice = LP_autorefs[i].lastbid; - LP_mypriceset(&changed,rel,base,newprice); + LP_mypriceset(1,&changed,rel,base,newprice); LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,rel,base,newprice); //printf("%s/%s price %.8f margin %.8f/%.8f newprice %.8f %.8f\n",base,rel,price,buymargin,sellmargin,newprice,(1. / newprice) * (1. + sellmargin)); newprice = (1. / price) * (1. + sellmargin); @@ -634,7 +634,7 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp) LP_autorefs[i].lastask = newprice; else LP_autorefs[i].lastask = (LP_autorefs[i].lastask * 0.99) + (0.01 * newprice); newprice = LP_autorefs[i].lastask; - LP_mypriceset(&changed,base,rel,newprice); + LP_mypriceset(1,&changed,base,rel,newprice); LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,base,rel,newprice); } //else printf("null return from CMC\n"); } @@ -782,7 +782,7 @@ int32_t LP_portfolio_trade(void *ctx,uint32_t *requestidp,uint32_t *quoteidp,str char *retstr2; uint64_t txfee,desttxfee; double bid,ask,maxprice; bits256 zero; uint32_t requestid,quoteid,iter,i; cJSON *retjson2; struct LP_utxoinfo A; struct LP_address_utxo *utxos[1000]; int32_t max=(int32_t)(sizeof(utxos)/sizeof(*utxos)); LP_txfees(&txfee,&desttxfee,buy->symbol,sell->symbol); requestid = quoteid = 0; - LP_myprice(&bid,&ask,buy->symbol,sell->symbol); + LP_myprice(1,&bid,&ask,buy->symbol,sell->symbol); maxprice = ask; if ( setbaserel != 0 ) { diff --git a/iguana/exchanges/LP_prices.c b/iguana/exchanges/LP_prices.c index 0669b2c35..38d37c626 100644 --- a/iguana/exchanges/LP_prices.c +++ b/iguana/exchanges/LP_prices.c @@ -35,7 +35,7 @@ struct LP_priceinfo int32_t ind,pad; double diagval,high[2],low[2],last[2],bid[2],ask[2]; double relvals[LP_MAXPRICEINFOS]; - double myprices[LP_MAXPRICEINFOS]; + double myprices[2][LP_MAXPRICEINFOS]; double minprices[LP_MAXPRICEINFOS]; // autoprice double fixedprices[LP_MAXPRICEINFOS]; // fixedprices double buymargins[LP_MAXPRICEINFOS]; @@ -451,16 +451,16 @@ void LP_priceinfoupdate(char *base,char *rel,double price) } } -double LP_myprice(double *bidp,double *askp,char *base,char *rel) +double LP_myprice(int32_t iambob,double *bidp,double *askp,char *base,char *rel) { struct LP_priceinfo *basepp,*relpp; double val; *bidp = *askp = 0.; if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) { - *askp = basepp->myprices[relpp->ind]; + *askp = basepp->myprices[iambob][relpp->ind]; if ( LP_pricevalid(*askp) > 0 ) { - val = relpp->myprices[basepp->ind]; + val = relpp->myprices[iambob][basepp->ind]; if ( LP_pricevalid(val) > 0 ) { *bidp = 1. / val; @@ -474,7 +474,7 @@ double LP_myprice(double *bidp,double *askp,char *base,char *rel) } else { - val = relpp->myprices[basepp->ind]; + val = relpp->myprices[iambob][basepp->ind]; if ( LP_pricevalid(val) > 0 ) { *bidp = 1. / val; @@ -486,7 +486,7 @@ double LP_myprice(double *bidp,double *askp,char *base,char *rel) return(0.); } -char *LP_myprices() +char *LP_myprices(int32_t iambob) { int32_t baseid,relid; double bid,ask; char *base,*rel; cJSON *item,*array; array = cJSON_CreateArray(); @@ -496,7 +496,7 @@ char *LP_myprices() for (relid=0; relid SMALLVAL ) + if ( LP_myprice(iambob,&bid,&ask,base,rel) > SMALLVAL ) { item = cJSON_CreateObject(); jaddstr(item,"base",base); @@ -510,7 +510,7 @@ char *LP_myprices() return(jprint(array,1)); } -int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price) +int32_t LP_mypriceset(int32_t iambob,int32_t *changedp,char *base,char *rel,double price) { struct LP_priceinfo *basepp=0,*relpp=0; struct LP_pubkey_info *pubp; double minprice,maxprice,margin,buymargin,sellmargin; *changedp = 0; @@ -519,43 +519,46 @@ int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price) if ( base != 0 && rel != 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) { - if ( price == 0. || fabs(basepp->myprices[relpp->ind] - price)/price > 0.001 ) + if ( price == 0. || fabs(basepp->myprices[iambob][relpp->ind] - price)/price > 0.001 ) *changedp = 1; - sellmargin = relpp->sellmargins[basepp->ind]; - buymargin = relpp->buymargins[basepp->ind]; - margin = (sellmargin + buymargin) * 0.5; - if ( price == 0. ) + if ( iambob != 0 ) { - relpp->minprices[basepp->ind] = 0.; - relpp->fixedprices[basepp->ind] = 0.; - relpp->buymargins[basepp->ind] = 0.; - relpp->sellmargins[basepp->ind] = 0.; - relpp->offsets[basepp->ind] = 0.; - relpp->factors[basepp->ind] = 0.; - LP_autoref_clear(base,rel); - margin = 0.; - } - else if ( (minprice= basepp->minprices[relpp->ind]) > SMALLVAL && price < minprice ) - { - //printf("%s/%s price %.8f less than minprice %.8f\n",base,rel,price,minprice); - price = minprice * (1. - margin); - } - else if ( (maxprice= relpp->minprices[basepp->ind]) > SMALLVAL ) - { - if ( price > (1. / maxprice) ) + sellmargin = relpp->sellmargins[basepp->ind]; + buymargin = relpp->buymargins[basepp->ind]; + margin = (sellmargin + buymargin) * 0.5; + if ( price == 0. ) + { + relpp->minprices[basepp->ind] = 0.; + relpp->fixedprices[basepp->ind] = 0.; + relpp->buymargins[basepp->ind] = 0.; + relpp->sellmargins[basepp->ind] = 0.; + relpp->offsets[basepp->ind] = 0.; + relpp->factors[basepp->ind] = 0.; + LP_autoref_clear(base,rel); + margin = 0.; + } + else if ( (minprice= basepp->minprices[relpp->ind]) > SMALLVAL && price < minprice ) + { + //printf("%s/%s price %.8f less than minprice %.8f\n",base,rel,price,minprice); + price = minprice * (1. - margin); + } + else if ( (maxprice= relpp->minprices[basepp->ind]) > SMALLVAL ) { - //printf("%s/%s price %.8f less than maxprice %.8f, more than %.8f\n",base,rel,price,maxprice,1./maxprice); - price = (1. / maxprice) * (1. + margin); + if ( price > (1. / maxprice) ) + { + //printf("%s/%s price %.8f less than maxprice %.8f, more than %.8f\n",base,rel,price,maxprice,1./maxprice); + price = (1. / maxprice) * (1. + margin); + } } } /*else if ( basepp->myprices[relpp->ind] > SMALLVAL ) { price = (basepp->myprices[relpp->ind] * 0.9) + (0.1 * price); }*/ - basepp->myprices[relpp->ind] = price; // ask + basepp->myprices[iambob][relpp->ind] = price; // ask //printf("LP_mypriceset base.%s rel.%s <- price %.8f\n",base,rel,price); //relpp->myprices[basepp->ind] = (1. / price); // bid, but best to do one dir at a time - if ( (pubp= LP_pubkeyadd(G.LP_mypub25519)) != 0 ) + if ( iambob != 0 && (pubp= LP_pubkeyadd(G.LP_mypub25519)) != 0 ) { pubp->timestamp = (uint32_t)time(NULL); LP_pubkey_update(pubp,basepp->ind,relpp->ind,price,0,0,0,0,0); @@ -569,12 +572,12 @@ int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price) return(-1); } -double LP_price(char *base,char *rel) +double LP_price(int32_t iambob,char *base,char *rel) { struct LP_priceinfo *basepp; int32_t relind; double price = 0.; if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 ) { - if ( (price= basepp->myprices[relind]) == 0. ) + if ( (price= basepp->myprices[iambob][relind]) == 0. ) { price = basepp->relvals[relind]; } @@ -582,19 +585,19 @@ double LP_price(char *base,char *rel) return(price); } -double LP_getmyprice(char *base,char *rel) +double LP_getmyprice(int32_t iambob,char *base,char *rel) { struct LP_priceinfo *basepp; int32_t relind; double price = 0.; if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 ) { - if ( (price= basepp->myprices[relind]) == 0. ) + if ( (price= basepp->myprices[iambob][relind]) == 0. ) { } } return(price); } -cJSON *LP_priceinfomatrix(int32_t usemyprices) +cJSON *LP_priceinfomatrix(int32_t iambob,int32_t usemyprices) { int32_t i,j,n,m; double total,sum,val; struct LP_priceinfo *pp; uint32_t now; struct LP_cacheinfo *ptr,*tmp; cJSON *vectorjson = cJSON_CreateObject(); now = (uint32_t)time(NULL); @@ -611,7 +614,7 @@ cJSON *LP_priceinfomatrix(int32_t usemyprices) pp->diagval = sum = n = 0; for (j=0; jmyprices[j]) == 0. ) + if ( usemyprices == 0 || (val= pp->myprices[iambob][j]) == 0. ) val = pp->relvals[j]; if ( val > SMALLVAL ) { @@ -661,7 +664,7 @@ struct LP_priceinfo *LP_priceinfoadd(char *symbol) pp->coinbits = stringbits(symbol); pp->ind = LP_numpriceinfos++; //LP_numpriceinfos++; - if ( (retjson= LP_priceinfomatrix(0)) != 0 ) + if ( (retjson= LP_priceinfomatrix(1,0)) != 0 ) free_json(retjson); return(pp); } @@ -1016,7 +1019,7 @@ int64_t LP_KMDvalue(struct iguana_info *coin,int64_t balance) KMDvalue = balance; else { - if ( (price= LP_price(coin->symbol,"KMD")) > SMALLVAL ) + if ( (price= LP_price(1,coin->symbol,"KMD")) > SMALLVAL ) KMDvalue = price * balance; } } From b2d980a76811c4fe9319cce811daae1035689d63 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 22:14:28 -1100 Subject: [PATCH 14/60] Fix --- iguana/exchanges/LP_commands.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 265827d58..38b95aa25 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -862,14 +862,14 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ double price,bid,ask; if ( strcmp(method,"getprice") == 0 ) { - ask = LP_price(base,rel); + ask = LP_price(1,base,rel); if ( (bid= LP_price(1,rel,base)) > SMALLVAL ) bid = 1./bid; } else { ask = LP_getmyprice(1,base,rel); - if ( (bid= LP_getmyprice(rel,base)) > SMALLVAL ) + if ( (bid= LP_getmyprice(1,rel,base)) > SMALLVAL ) bid = 1./bid; } price = _pairaved(bid,ask); From 9959b8187057869fdb6345c8db1736806a37ae83 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 22:15:03 -1100 Subject: [PATCH 15/60] Fix --- iguana/exchanges/LP_ordermatch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index cac436955..9163604bd 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -746,7 +746,7 @@ char *LP_cancel_order(char *uuidstr) char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice { - cJSON *retjson; char otheraddr[64],*msg; double bid,ask,price,qprice; int32_t connected,pairsock = -1; int32_t DEXselector = 0; struct LP_utxoinfo *autxo,A,B,*butxo; struct basilisk_swap *swap; struct iguana_info *coin; + cJSON *retjson; char otheraddr[64],*msg; double bid,ask,price,qprice; int32_t changed,pairsock = -1; int32_t DEXselector = 0; struct LP_utxoinfo *autxo,A,B,*butxo; struct basilisk_swap *swap; struct iguana_info *coin; if ( bits256_cmp(qp->desthash,G.LP_mypub25519) != 0 ) { LP_aliceid(qp->tradeid,qp->aliceid,"error1",0,0); From b45cf46dd7bb2b3cff4766e189b2f213af5ca0dc Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 22:43:03 -1100 Subject: [PATCH 16/60] mpnet stubs --- iguana/exchanges/LP_mpnet.c | 1327 ++++++++++++++++++++++++++++++ iguana/exchanges/LP_nativeDEX.c | 5 +- iguana/exchanges/LP_ordermatch.c | 15 +- iguana/exchanges/LP_signatures.c | 4 +- 4 files changed, 1343 insertions(+), 8 deletions(-) create mode 100644 iguana/exchanges/LP_mpnet.c diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c new file mode 100644 index 000000000..38d37c626 --- /dev/null +++ b/iguana/exchanges/LP_mpnet.c @@ -0,0 +1,1327 @@ + +/****************************************************************************** + * Copyright © 2014-2018 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_prices.c +// marketmaker +// + +struct LP_orderbookentry +{ + bits256 pubkey; + double price; + int64_t avesatoshis,maxsatoshis,depth,dynamictrust; + uint32_t timestamp; + int32_t numutxos; + char coinaddr[64]; +}; + +struct LP_priceinfo +{ + char symbol[68]; + uint64_t coinbits; + int32_t ind,pad; + double diagval,high[2],low[2],last[2],bid[2],ask[2]; + double relvals[LP_MAXPRICEINFOS]; + double myprices[2][LP_MAXPRICEINFOS]; + double minprices[LP_MAXPRICEINFOS]; // autoprice + double fixedprices[LP_MAXPRICEINFOS]; // fixedprices + double buymargins[LP_MAXPRICEINFOS]; + double sellmargins[LP_MAXPRICEINFOS]; + double offsets[LP_MAXPRICEINFOS]; + double factors[LP_MAXPRICEINFOS]; +} LP_priceinfos[LP_MAXPRICEINFOS]; +int32_t LP_numpriceinfos; + +struct LP_cacheinfo +{ + UT_hash_handle hh; + struct LP_quoteinfo Q; + uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(int32_t)]; + double price; + uint32_t timestamp; +} *LP_cacheinfos; + +void LP_priceinfos_clear() +{ + int32_t i; struct LP_priceinfo *pp; + for (i=0; imyprices,0,sizeof(pp->myprices)); + memset(pp->minprices,0,sizeof(pp->minprices)); + memset(pp->fixedprices,0,sizeof(pp->fixedprices)); + memset(pp->buymargins,0,sizeof(pp->buymargins)); + memset(pp->sellmargins,0,sizeof(pp->sellmargins)); + memset(pp->offsets,0,sizeof(pp->offsets)); + memset(pp->factors,0,sizeof(pp->factors)); + } +} + +float LP_pubkey_price(int32_t *numutxosp,int64_t *avesatoshisp,int64_t *maxsatoshisp,struct LP_pubkey_info *pubp,uint32_t baseind,uint32_t relind) +{ + struct LP_pubkey_quote *pq,*tmp; int32_t scale; int64_t scale64; + *numutxosp = 0; + *avesatoshisp = *maxsatoshisp = 0; + DL_FOREACH_SAFE(pubp->quotes,pq,tmp) + { + if ( baseind == pq->baseind && relind == pq->relind ) + { + if ( (scale= pq->scale) == 0 ) + pq->scale = scale = 6; + scale64 = 1; + while ( scale > 0 ) + { + scale64 *= 10; + scale--; + } + *numutxosp = pq->numutxos; + *avesatoshisp = pq->aveutxo * scale64; + *maxsatoshisp = pq->maxutxo * scale64; + return(pq->price); + } + } + return(0); +} + +void LP_pubkey_update(struct LP_pubkey_info *pubp,uint32_t baseind,uint32_t relind,float price,int64_t balance,char *utxocoin,int32_t numutxos,int64_t minutxo,int64_t maxutxo) +{ + struct LP_pubkey_quote *pq,*tmp; int64_t aveutxo,scale64,ave64,max64; int32_t scale; + DL_FOREACH_SAFE(pubp->quotes,pq,tmp) + { + if ( baseind == pq->baseind && relind == pq->relind ) + break; + pq = 0; + } + if ( pq == 0 ) + { + pq = calloc(1,sizeof(*pq)); + pq->baseind = baseind; + pq->relind = relind; + pq->scale = 6; // millions of SATOSHIS, ie. 0.01 + DL_APPEND(pubp->quotes,pq); // already serialized as only path is via stats_JSON() + //printf("create pubp quotes %d/%d\n",baseind,relind); + } +//printf("%d/%d price %.8f balance %.8f %s num.%d min %.8f max %.8f\n",baseind,relind,price,dstr(balance),utxocoin,numutxos,dstr(minutxo),dstr(maxutxo)); + pq->price = price; + if ( utxocoin != 0 && utxocoin[0] != 0 ) + { + if ( (scale= pq->scale) == 0 ) + pq->scale = scale = 6; + scale64 = 1; + while ( scale > 0 ) + { + scale64 *= 10; + scale--; + } + if ( numutxos >= 256 ) + pq->numutxos = 255; + else pq->numutxos = numutxos; + aveutxo = (balance + (scale64>>1)) / numutxos; + if ( (ave64= (aveutxo / scale64)) >= (1LL << 32) ) + ave64 = (1LL << 32) - 1; + max64 = ((maxutxo + (scale64>>1)) / scale64); + if ( max64 >= (1LL << 32) ) + max64 = (1LL << 32) - 1; + pq->aveutxo = (uint32_t)ave64; + pq->maxutxo = (uint32_t)max64; + if ( 0 ) + { + printf("price %.8f base.%s rel.%s utxocoin.%s balance %.8f numutxos.%u %u scale64 = %llu, ave %llu, ave32 %u (%llu) max32 %u (%llu)\n",price,LP_priceinfos[baseind].symbol,LP_priceinfos[relind].symbol,utxocoin,dstr(balance),numutxos,pq->numutxos,(long long)scale64,(long long)aveutxo,pq->aveutxo,(long long)pq->aveutxo * scale64,pq->maxutxo,(long long)pq->maxutxo * scale64); + int64_t avesatoshis,maxsatoshis; + price = LP_pubkey_price(&numutxos,&avesatoshis,&maxsatoshis,pubp,baseind,relind); + printf("checkprice %.8f numutxos.%d ave %.8f max %.8f\n",price,numutxos,dstr(avesatoshis),dstr(maxsatoshis)); + } + } +} + +struct LP_priceinfo *LP_priceinfo(int32_t ind) +{ + if ( ind < 0 || ind >= LP_MAXPRICEINFOS ) + return(0); + else return(&LP_priceinfos[ind]); +} + +char *LP_priceinfostr(int32_t ind) +{ + if ( ind < 0 || ind >= LP_MAXPRICEINFOS ) + return("UNKNOWN"); + else return(LP_priceinfos[ind].symbol); +} + +int32_t LP_pricevalid(double price) +{ + if ( price > SMALLVAL && isnan(price) == 0 && price < SATOSHIDEN ) + return(1); + else return(0); +} + +struct LP_priceinfo *LP_priceinfofind(char *symbol) +{ + int32_t i; struct LP_priceinfo *pp; uint64_t coinbits; + if ( symbol == 0 || symbol[0] == 0 ) + return(0); + if ( LP_numpriceinfos > 0 ) + { + coinbits = stringbits(symbol); + pp = LP_priceinfos; + for (i=0; icoinbits == coinbits ) + return(pp); + } + return(0); +} + +int32_t LP_priceinfoind(char *symbol) +{ + struct LP_priceinfo *pp; + if ( (pp= LP_priceinfofind(symbol)) != 0 ) + return(pp->ind); + else return(-1); +} + +struct LP_priceinfo *LP_priceinfoptr(int32_t *indp,char *base,char *rel) +{ + struct LP_priceinfo *basepp,*relpp; + if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + *indp = relpp->ind; + return(basepp); + } + else + { + *indp = -1; + return(0); + } +} + +int32_t LP_cachekey(uint8_t *key,char *base,char *rel,bits256 txid,int32_t vout) +{ + uint64_t basebits,relbits; int32_t offset = 0; + basebits = stringbits(base); + relbits = stringbits(rel); + memcpy(&key[offset],&basebits,sizeof(basebits)), offset += sizeof(basebits); + memcpy(&key[offset],&relbits,sizeof(relbits)), offset += sizeof(relbits); + memcpy(&key[offset],&txid,sizeof(txid)), offset += sizeof(txid); + memcpy(&key[offset],&vout,sizeof(vout)), offset += sizeof(vout); + return(offset); +} + +struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout) +{ + struct LP_cacheinfo *ptr=0; uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(vout)]; + if ( base == 0 || rel == 0 ) + return(0); + if ( LP_cachekey(key,base,rel,txid,vout) == sizeof(key) ) + { + portable_mutex_lock(&LP_cachemutex); + HASH_FIND(hh,LP_cacheinfos,key,sizeof(key),ptr); + portable_mutex_unlock(&LP_cachemutex); + } else printf("LP_cachefind keysize mismatch?\n"); + if ( 0 && ptr != 0 && ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) + { + printf("expire price %.8f\n",ptr->price); + ptr->price = 0.; + ptr->timestamp = 0; + memset(&ptr->Q,0,sizeof(ptr->Q)); + } + return(ptr); +} + +struct LP_pubkey_info *LP_pubkey_rmd160find(uint8_t rmd160[20]) +{ + struct LP_pubkey_info *pubp=0,*tmp; + portable_mutex_lock(&LP_pubkeymutex); + HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) + { + if ( memcmp(rmd160,pubp->rmd160,sizeof(pubp->rmd160)) == 0 ) + break; + pubp = 0; + } + portable_mutex_unlock(&LP_pubkeymutex); + return(pubp); +} + +struct LP_address *_LP_addressfind(struct iguana_info *coin,char *coinaddr) +{ + uint8_t rmd160[20],addrtype; struct LP_address *ap; struct LP_pubkey_info *pubp; + HASH_FIND(hh,coin->addresses,coinaddr,strlen(coinaddr),ap); + if ( ap != 0 && bits256_nonz(ap->pubkey) == 0 ) + { + bitcoin_addr2rmd160(coin->symbol,coin->taddr,&addrtype,rmd160,coinaddr); + if ( (pubp= LP_pubkey_rmd160find(rmd160)) != 0 ) + { + ap->pubkey = pubp->pubkey; + memcpy(ap->pubsecp,pubp->pubsecp,sizeof(ap->pubsecp)); + } + } + return(ap); +} + +struct LP_address *_LP_addressadd(struct iguana_info *coin,char *coinaddr) +{ + uint8_t rmd160[20],addrtype; struct LP_address *ap; struct LP_pubkey_info *pubp; + ap = calloc(1,sizeof(*ap)); + safecopy(ap->coinaddr,coinaddr,sizeof(ap->coinaddr)); + bitcoin_addr2rmd160(coin->symbol,coin->taddr,&addrtype,rmd160,coinaddr); + if ( (pubp= LP_pubkey_rmd160find(rmd160)) != 0 ) + { + ap->pubkey = pubp->pubkey; + memcpy(ap->pubsecp,pubp->pubsecp,sizeof(ap->pubsecp)); + } + //printf("LP_ADDRESS %s ADD.(%s)\n",coin->symbol,coinaddr); + HASH_ADD_KEYPTR(hh,coin->addresses,ap->coinaddr,strlen(ap->coinaddr),ap); + return(ap); +} + +struct LP_pubkey_info *LP_pubkeyfind(bits256 pubkey) +{ + struct LP_pubkey_info *pubp=0; + portable_mutex_lock(&LP_pubkeymutex); + HASH_FIND(hh,LP_pubkeyinfos,&pubkey,sizeof(pubkey),pubp); + portable_mutex_unlock(&LP_pubkeymutex); + return(pubp); +} + +struct LP_pubkey_info *LP_pubkeyadd(bits256 pubkey) +{ + char str[65]; struct LP_pubkey_info *pubp=0; + portable_mutex_lock(&LP_pubkeymutex); + HASH_FIND(hh,LP_pubkeyinfos,&pubkey,sizeof(pubkey),pubp); + if ( pubp == 0 ) + { + pubp = calloc(1,sizeof(*pubp)); + pubp->pubkey = pubkey; + pubp->pairsock = -1; + if ( bits256_cmp(G.LP_mypub25519,pubkey) == 0 ) + { + memcpy(pubp->rmd160,G.LP_myrmd160,sizeof(pubp->rmd160)); + memcpy(pubp->pubsecp,G.LP_pubsecp,sizeof(pubp->pubsecp)); + } + HASH_ADD_KEYPTR(hh,LP_pubkeyinfos,&pubp->pubkey,sizeof(pubp->pubkey),pubp); + HASH_FIND(hh,LP_pubkeyinfos,&pubkey,sizeof(pubkey),pubp); + if ( pubp == 0 ) + printf("pubkeyadd find %s error after add\n",bits256_str(str,pubp->pubkey)); + } + portable_mutex_unlock(&LP_pubkeymutex); + return(pubp); +} + +int32_t LP_pubkey_istrusted(bits256 pubkey) +{ + struct LP_pubkey_info *pubp; + if ( (pubp= LP_pubkeyadd(pubkey)) != 0 ) + return(pubp->istrusted != 0); + return(0); +} + +char *LP_pubkey_trustset(bits256 pubkey,uint32_t trustval) +{ + struct LP_pubkey_info *pubp; + if ( (pubp= LP_pubkeyadd(pubkey)) != 0 ) + { + pubp->istrusted = trustval; + return(clonestr("{\"result\":\"success\"}")); + } + return(clonestr("{\"error\":\"pubkey not found\"}")); +} + +char *LP_pubkey_trusted() +{ + struct LP_pubkey_info *pubp,*tmp; cJSON *array = cJSON_CreateArray(); + HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) + { + if ( pubp->istrusted != 0 ) + jaddibits256(array,pubp->pubkey); + } + return(jprint(array,1)); +} + +int64_t LP_unspents_metric(struct iguana_info *coin,char *coinaddr) +{ + cJSON *array,*item; int32_t i,n; int64_t metric=0,total; + //LP_listunspent_both(coin->symbol,coinaddr,0); + if ( (array= LP_address_utxos(coin,coinaddr,1)) != 0 ) + { + total = 0; + if ( (n= cJSON_GetArraySize(array)) > 0 ) + { + for (i=0; imatrix[baseid][relid]; + if ( LP_pricevalid(price) > 0 ) + { + item = cJSON_CreateArray(); + jaddistr(item,base); + jaddistr(item,LP_priceinfos[relid].symbol); + jaddinum(item,price); + jaddi(array,item); + } + } + } + jaddbits256(obj,"pubkey",pubp->pubkey); + init_hexbytes_noT(hexstr,pubp->rmd160,sizeof(pubp->rmd160)); + jaddstr(obj,"rmd160",hexstr); + init_hexbytes_noT(hexstr2,pubp->pubsecp,sizeof(pubp->pubsecp)); + jaddstr(obj,"pubsecp",hexstr2); + init_hexbytes_noT(sigstr,pubp->sig,pubp->siglen); + jaddstr(obj,"sig",sigstr); + jaddnum(obj,"timestamp",pubp->timestamp); + jadd(obj,"asks",array); + if ( pubp->istrusted != 0 ) + jaddnum(obj,"istrusted",pubp->istrusted); + return(obj); +} + +char *LP_prices() +{ + struct LP_pubkey_info *pubp,*tmp; cJSON *array = cJSON_CreateArray(); + HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) + { + jaddi(array,LP_pubkeyjson(pubp)); + } + return(jprint(array,1)); +} + +double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,int32_t vout) +{ + struct LP_cacheinfo *ptr; + if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) + { + if ( qp != 0 ) + (*qp) = ptr->Q; + if ( ptr->price == 0. && ptr->Q.satoshis > ptr->Q.txfee ) + { + ptr->price = (double)ptr->Q.destsatoshis / (ptr->Q.satoshis - ptr->Q.txfee); + if ( LP_pricevalid(ptr->price) <= 0 ) + ptr->price = 0.; + printf("LP_pricecache: set %s/%s ptr->price %.8f\n",base,rel,ptr->price); + } + //printf(">>>>>>>>>> found %s/%s %.8f\n",base,rel,ptr->price); + return(ptr->price); + } + //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); + return(0.); +} + +void LP_priceinfoupdate(char *base,char *rel,double price) +{ + struct LP_priceinfo *basepp,*relpp; + if ( LP_pricevalid(price) > 0 ) + { + if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + dxblend(&basepp->relvals[relpp->ind],price,0.9); + dxblend(&relpp->relvals[basepp->ind],1. / price,0.9); + //basepp->relvals[relpp->ind] = price; + //relpp->relvals[basepp->ind] = 1. / price; + } + } +} + +double LP_myprice(int32_t iambob,double *bidp,double *askp,char *base,char *rel) +{ + struct LP_priceinfo *basepp,*relpp; double val; + *bidp = *askp = 0.; + if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + *askp = basepp->myprices[iambob][relpp->ind]; + if ( LP_pricevalid(*askp) > 0 ) + { + val = relpp->myprices[iambob][basepp->ind]; + if ( LP_pricevalid(val) > 0 ) + { + *bidp = 1. / val; + return((*askp + *bidp) * 0.5); + } + else + { + *bidp = 0.; + return(*askp); + } + } + else + { + val = relpp->myprices[iambob][basepp->ind]; + if ( LP_pricevalid(val) > 0 ) + { + *bidp = 1. / val; + *askp = 0.; + return(*bidp); + } + } + } + return(0.); +} + +char *LP_myprices(int32_t iambob) +{ + int32_t baseid,relid; double bid,ask; char *base,*rel; cJSON *item,*array; + array = cJSON_CreateArray(); + for (baseid=0; baseid SMALLVAL ) + { + item = cJSON_CreateObject(); + jaddstr(item,"base",base); + jaddstr(item,"rel",rel); + jaddnum(item,"bid",bid); + jaddnum(item,"ask",ask); + jaddi(array,item); + } + } + } + return(jprint(array,1)); +} + +int32_t LP_mypriceset(int32_t iambob,int32_t *changedp,char *base,char *rel,double price) +{ + struct LP_priceinfo *basepp=0,*relpp=0; struct LP_pubkey_info *pubp; double minprice,maxprice,margin,buymargin,sellmargin; + *changedp = 0; + //if ( strcmp("DEX",base) == 0 || strcmp("DEX",rel) == 0 ) + // printf("%s/%s setprice %.8f\n",base,rel,price); + if ( base != 0 && rel != 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + + if ( price == 0. || fabs(basepp->myprices[iambob][relpp->ind] - price)/price > 0.001 ) + *changedp = 1; + if ( iambob != 0 ) + { + sellmargin = relpp->sellmargins[basepp->ind]; + buymargin = relpp->buymargins[basepp->ind]; + margin = (sellmargin + buymargin) * 0.5; + if ( price == 0. ) + { + relpp->minprices[basepp->ind] = 0.; + relpp->fixedprices[basepp->ind] = 0.; + relpp->buymargins[basepp->ind] = 0.; + relpp->sellmargins[basepp->ind] = 0.; + relpp->offsets[basepp->ind] = 0.; + relpp->factors[basepp->ind] = 0.; + LP_autoref_clear(base,rel); + margin = 0.; + } + else if ( (minprice= basepp->minprices[relpp->ind]) > SMALLVAL && price < minprice ) + { + //printf("%s/%s price %.8f less than minprice %.8f\n",base,rel,price,minprice); + price = minprice * (1. - margin); + } + else if ( (maxprice= relpp->minprices[basepp->ind]) > SMALLVAL ) + { + if ( price > (1. / maxprice) ) + { + //printf("%s/%s price %.8f less than maxprice %.8f, more than %.8f\n",base,rel,price,maxprice,1./maxprice); + price = (1. / maxprice) * (1. + margin); + } + } + } + /*else if ( basepp->myprices[relpp->ind] > SMALLVAL ) + { + price = (basepp->myprices[relpp->ind] * 0.9) + (0.1 * price); + }*/ + basepp->myprices[iambob][relpp->ind] = price; // ask + //printf("LP_mypriceset base.%s rel.%s <- price %.8f\n",base,rel,price); + //relpp->myprices[basepp->ind] = (1. / price); // bid, but best to do one dir at a time + if ( iambob != 0 && (pubp= LP_pubkeyadd(G.LP_mypub25519)) != 0 ) + { + pubp->timestamp = (uint32_t)time(NULL); + LP_pubkey_update(pubp,basepp->ind,relpp->ind,price,0,0,0,0,0); + //pubp->matrix[basepp->ind][relpp->ind] = price; + //pubp->timestamps[basepp->ind][relpp->ind] = pubp->timestamp; + //pubp->matrix[relpp->ind][basepp->ind] = (1. / price); + } + return(0); + } + printf("base.%s rel.%s %p %p price %.8f error case\n",base!=0?base:"",rel!=0?rel:"",basepp,relpp,price); + return(-1); +} + +double LP_price(int32_t iambob,char *base,char *rel) +{ + struct LP_priceinfo *basepp; int32_t relind; double price = 0.; + if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 ) + { + if ( (price= basepp->myprices[iambob][relind]) == 0. ) + { + price = basepp->relvals[relind]; + } + } + return(price); +} + +double LP_getmyprice(int32_t iambob,char *base,char *rel) +{ + struct LP_priceinfo *basepp; int32_t relind; double price = 0.; + if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 ) + { + if ( (price= basepp->myprices[iambob][relind]) == 0. ) + { + } + } + return(price); +} + +cJSON *LP_priceinfomatrix(int32_t iambob,int32_t usemyprices) +{ + int32_t i,j,n,m; double total,sum,val; struct LP_priceinfo *pp; uint32_t now; struct LP_cacheinfo *ptr,*tmp; cJSON *vectorjson = cJSON_CreateObject(); + now = (uint32_t)time(NULL); + HASH_ITER(hh,LP_cacheinfos,ptr,tmp) + { + if ( ptr->timestamp < now-3600*2 || ptr->price == 0. ) + continue; + LP_priceinfoupdate(ptr->Q.srccoin,ptr->Q.destcoin,ptr->price); + } + pp = LP_priceinfos; + total = m = 0; + for (i=0; idiagval = sum = n = 0; + for (j=0; jmyprices[iambob][j]) == 0. ) + val = pp->relvals[j]; + if ( val > SMALLVAL ) + { + sum += val; + n++; + } + } + if ( n > 0 ) + { + pp->diagval = sum / n; + total += pp->diagval, m++; + } + } + if ( m > 0 ) + { + pp = LP_priceinfos; + for (i=0; idiagval > SMALLVAL ) + { + pp->diagval /= total; + jaddnum(vectorjson,pp->symbol,pp->diagval); + } + } + } + return(vectorjson); +} + +struct LP_priceinfo *LP_priceinfoadd(char *symbol) +{ + struct LP_priceinfo *pp; cJSON *retjson; + if ( symbol == 0 ) + return(0); + if ( (pp= LP_priceinfofind(symbol)) != 0 ) + { + printf("%s already there\n",symbol); + return(pp); + } + if ( LP_numpriceinfos >= sizeof(LP_priceinfos)/sizeof(*LP_priceinfos) ) + { + printf("cant add any more priceinfos than %d\n",LP_numpriceinfos); + return(0); + } + pp = &LP_priceinfos[LP_numpriceinfos]; + memset(pp,0,sizeof(*pp)); + safecopy(pp->symbol,symbol,sizeof(pp->symbol)); + pp->coinbits = stringbits(symbol); + pp->ind = LP_numpriceinfos++; + //LP_numpriceinfos++; + if ( (retjson= LP_priceinfomatrix(1,0)) != 0 ) + free_json(retjson); + return(pp); +} + +struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout,double price,struct LP_quoteinfo *qp) +{ + char str[65]; struct LP_cacheinfo *ptr=0; + if ( base == 0 || rel == 0 ) + return(0); + if ( LP_pricevalid(price) > 0 ) + { + if ( (ptr= LP_cachefind(base,rel,txid,vout)) == 0 ) + { + ptr = calloc(1,sizeof(*ptr)); + if ( LP_cachekey(ptr->key,base,rel,txid,vout) == sizeof(ptr->key) ) + { + portable_mutex_lock(&LP_cachemutex); + HASH_ADD(hh,LP_cacheinfos,key,sizeof(ptr->key),ptr); + portable_mutex_unlock(&LP_cachemutex); + } else printf("LP_cacheadd keysize mismatch?\n"); + } + ptr->Q = *qp; + ptr->timestamp = (uint32_t)time(NULL); + if ( price != ptr->price ) + { + ptr->price = price; + LP_priceinfoupdate(base,rel,price); + printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)qp->satoshis,price); + } else ptr->price = price; + } + return(ptr); +} + +static int _cmp_orderbook(const void *a,const void *b) +{ + int32_t retval = 0; +#define ptr_a (*(struct LP_orderbookentry **)a)->price +#define ptr_b (*(struct LP_orderbookentry **)b)->price + if ( ptr_b > ptr_a ) + retval = -1; + else if ( ptr_b < ptr_a ) + retval = 1; + else + { +#undef ptr_a +#undef ptr_b +#define ptr_a ((struct LP_orderbookentry *)a)->maxsatoshis +#define ptr_b ((struct LP_orderbookentry *)b)->maxsatoshis + if ( ptr_b > ptr_a ) + return(-1); + else if ( ptr_b < ptr_a ) + return(1); + } + // printf("%.8f vs %.8f -> %d\n",ptr_a,ptr_b,retval); + return(retval); +#undef ptr_a +#undef ptr_b +} + +static int _revcmp_orderbook(const void *a,const void *b) +{ + int32_t retval = 0; +#define ptr_a (*(struct LP_orderbookentry **)a)->price +#define ptr_b (*(struct LP_orderbookentry **)b)->price + if ( ptr_b > ptr_a ) + retval = 1; + else if ( ptr_b < ptr_a ) + retval = -1; + else + { +#undef ptr_a +#undef ptr_b +#define ptr_a ((struct LP_orderbookentry *)a)->maxsatoshis +#define ptr_b ((struct LP_orderbookentry *)b)->maxsatoshis + if ( ptr_b > ptr_a ) + return(-1); + else if ( ptr_b < ptr_a ) + return(1); + } + // printf("%.8f vs %.8f -> %d\n",ptr_a,ptr_b,retval); + return(retval); +#undef ptr_a +#undef ptr_b +} + +cJSON *LP_orderbookjson(char *symbol,struct LP_orderbookentry *op) +{ + cJSON *item = cJSON_CreateObject(); + if ( LP_pricevalid(op->price) > 0 ) + { + jaddstr(item,"coin",symbol); + jaddstr(item,"address",op->coinaddr); + jaddnum(item,"price",op->price); + jaddnum(item,"numutxos",op->numutxos); + jaddnum(item,"avevolume",dstr(op->avesatoshis)); + jaddnum(item,"maxvolume",dstr(op->maxsatoshis)); + jaddnum(item,"depth",dstr(op->depth)); + jaddbits256(item,"pubkey",op->pubkey); + jaddnum(item,"age",time(NULL)-op->timestamp); + jaddnum(item,"zcredits",dstr(op->dynamictrust)); + } + return(item); +} + +struct LP_orderbookentry *LP_orderbookentry(char *address,char *base,char *rel,double price,int32_t numutxos,int64_t avesatoshis,int64_t maxsatoshis,bits256 pubkey,uint32_t timestamp,int64_t balance,int64_t dynamictrust) +{ + struct LP_orderbookentry *op; + if ( (op= calloc(1,sizeof(*op))) != 0 ) + { + safecopy(op->coinaddr,address,sizeof(op->coinaddr)); + op->price = price; + op->numutxos = numutxos; + op->avesatoshis = avesatoshis; + op->maxsatoshis = maxsatoshis; + op->pubkey = pubkey; + op->timestamp = timestamp; + op->depth = balance; + op->dynamictrust = dynamictrust; + } + return(op); +} + +void LP_pubkeys_query() +{ + uint8_t zeroes[20]; bits256 zero; cJSON *reqjson; struct LP_pubkey_info *pubp=0,*tmp; + memset(zero.bytes,0,sizeof(zero)); + memset(zeroes,0,sizeof(zeroes)); + HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) + { + if ( memcmp(zeroes,pubp->rmd160,sizeof(pubp->rmd160)) == 0 && time(NULL) > pubp->lasttime+60 ) + { + pubp->lasttime = (uint32_t)time(NULL); + reqjson = cJSON_CreateObject(); + jaddstr(reqjson,"method","wantnotify"); + jaddbits256(reqjson,"pub",pubp->pubkey); + //printf("LP_pubkeys_query %s\n",jprint(reqjson,0)); + LP_reserved_msg(0,"","",zero,jprint(reqjson,1)); + } + } +} + +int32_t LP_orderbook_utxoentries(uint32_t now,int32_t polarity,char *base,char *rel,struct LP_orderbookentry *(**arrayp),int32_t num,int32_t cachednum,int32_t duration) +{ + char coinaddr[64]; uint8_t zeroes[20]; struct LP_pubkey_info *pubp=0,*tmp; struct LP_priceinfo *basepp; struct LP_orderbookentry *op; struct LP_address *ap; struct iguana_info *basecoin; uint32_t oldest; double price; int32_t baseid,relid,n; int64_t maxsatoshis,balance,avesatoshis; + if ( (basepp= LP_priceinfoptr(&relid,base,rel)) != 0 ) + baseid = basepp->ind; + else return(num); + if ( (basecoin= LP_coinfind(base)) == 0 ) + return(-1); + now = (uint32_t)time(NULL); + oldest = now - duration; + memset(zeroes,0,sizeof(zeroes)); + HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) + { + if ( memcmp(zeroes,pubp->rmd160,sizeof(pubp->rmd160)) == 0 ) + { + //printf("skip pubp since no rmd160\n"); + continue; + } + if ( pubp->timestamp < oldest ) + continue; + bitcoin_address(base,coinaddr,basecoin->taddr,basecoin->pubtype,pubp->pubsecp,33); + avesatoshis = maxsatoshis = n = 0; + ap = 0; + if ( (price= LP_pubkey_price(&n,&avesatoshis,&maxsatoshis,pubp,baseid,relid)) > SMALLVAL ) //pubp->matrix[baseid][relid]) > SMALLVAL )//&& pubp->timestamps[baseid][relid] >= oldest ) + { + balance = avesatoshis * n; + //if ( (ap= LP_addressfind(basecoin,coinaddr)) != 0 ) + { + //n = LP_address_minmax(&balance,&minsatoshis,&maxsatoshis,ap); + if ( polarity > 0 ) + { + balance *= price; + avesatoshis *= price; + maxsatoshis *= price; + } + //printf("%s/%s %s n.%d ap->n.%d %.8f\n",base,rel,coinaddr,n,ap->n,dstr(ap->total)); + } + if ( (op= LP_orderbookentry(coinaddr,base,rel,polarity > 0 ? price : 1./price,n,avesatoshis,maxsatoshis,pubp->pubkey,pubp->timestamp,balance,pubp->dynamictrust)) != 0 ) + { + *arrayp = realloc(*arrayp,sizeof(*(*arrayp)) * (num+1)); + (*arrayp)[num++] = op; + } + } + //printf("pubp.(%s) %.8f %p\n",coinaddr,price,ap); + } + return(num); +} + +char *LP_orderbook(char *base,char *rel,int32_t duration) +{ + uint32_t now,i; int64_t depth,askdepth=0,biddepth=0; struct LP_priceinfo *basepp=0,*relpp=0; struct LP_orderbookentry **bids = 0,**asks = 0; cJSON *retjson,*array; struct iguana_info *basecoin,*relcoin; int32_t n,numbids=0,numasks=0,cachenumbids,cachenumasks,baseid,relid,suppress_prefetch=0; + basecoin = LP_coinfind(base); + relcoin = LP_coinfind(rel); + if ( basecoin == 0 || relcoin == 0 ) + return(clonestr("{\"error\":\"base or rel not added\"}")); + if ( (basepp= LP_priceinfofind(base)) == 0 || (relpp= LP_priceinfofind(rel)) == 0 ) + return(clonestr("{\"error\":\"base or rel not added\"}")); + if ( duration <= 0 ) + { + if ( duration < 0 ) + suppress_prefetch = 1; + duration = LP_ORDERBOOK_DURATION; + } + //LP_pubkeys_query(); + baseid = basepp->ind; + relid = relpp->ind; + now = (uint32_t)time(NULL); + basecoin->obooktime = now; + relcoin->obooktime = now; + cachenumbids = numbids, cachenumasks = numasks; + //printf("start cache.(%d %d) numbids.%d numasks.%d\n",cachenumbids,cachenumasks,numbids,numasks); + numasks = LP_orderbook_utxoentries(now,1,base,rel,&asks,numasks,cachenumasks,duration); + numbids = LP_orderbook_utxoentries(now,-1,rel,base,&bids,numbids,cachenumbids,duration); + retjson = cJSON_CreateObject(); + array = cJSON_CreateArray(); + if ( numbids > 1 ) + { + qsort(bids,numbids,sizeof(*bids),_revcmp_orderbook); + depth = 0; + for (i=0; idepth; + bids[i]->depth = depth; + } + } + if ( numasks > 1 ) + { + qsort(asks,numasks,sizeof(*asks),_cmp_orderbook); + depth = 0; + for (i=0; idepth; + asks[i]->depth = depth; + } + } + for (i=n=0; idepth; + jaddi(array,LP_orderbookjson(rel,bids[i])); + if ( suppress_prefetch == 0 && n < 3 && bids[i]->numutxos == 0 ) + { + //printf("bid ping %s %s\n",rel,bids[i]->coinaddr); + LP_address(relcoin,bids[i]->coinaddr); + /*if ( 0 && relcoin->electrum == 0 ) + { + LP_listunspent_issue(rel,bids[i]->coinaddr,0); + //else if ( (tmpjson= LP_listunspent(rel,bids[i]->coinaddr)) != 0 ) + // free_json(tmpjson); + LP_listunspent_query(rel,bids[i]->coinaddr); + }*/ + n++; + } + if ( i == 0 ) + { + LP_priceinfoupdate(rel,base,1. / bids[i]->price); + //printf("update %s/%s %.8f [%.8f]\n",rel,base,1./bids[i]->price,bids[i]->price); + } + free(bids[i]); + bids[i] = 0; + } + if ( n > 0 && relcoin->lastmonitor > 3600 ) + relcoin->lastmonitor -= 3600; + jadd(retjson,"bids",array); + jaddnum(retjson,"numbids",numbids); + jaddnum(retjson,"biddepth",dstr(biddepth)); + array = cJSON_CreateArray(); + for (i=n=0; idepth; + jaddi(array,LP_orderbookjson(base,asks[i])); + if ( suppress_prefetch == 0 && n < 3 && asks[i]->numutxos == 0 ) + { + //printf("ask ping %s %s\n",base,asks[i]->coinaddr); + LP_address(basecoin,asks[i]->coinaddr); + /*if ( 0 && basecoin->electrum == 0 ) + { + LP_listunspent_issue(base,asks[i]->coinaddr,0); + //else if ( (tmpjson= LP_listunspent(base,asks[i]->coinaddr)) != 0 ) + // free_json(tmpjson); + LP_listunspent_query(base,asks[i]->coinaddr); + }*/ + n++; + } + if ( i == 0 ) + { + LP_priceinfoupdate(base,rel,asks[i]->price); + //printf("update %s/%s %.8f [%.8f]\n",base,rel,asks[i]->price,1./asks[i]->price); + } + free(asks[i]); + asks[i] = 0; + } + if ( n > 0 && basecoin->lastmonitor > 3600 ) + basecoin->lastmonitor -= 3600; + jadd(retjson,"asks",array); + jaddnum(retjson,"numasks",numasks); + jaddnum(retjson,"askdepth",dstr(askdepth)); + jaddstr(retjson,"base",base); + jaddstr(retjson,"rel",rel); + jaddnum(retjson,"timestamp",now); + jaddnum(retjson,"netid",G.netid); + if ( bids != 0 ) + free(bids); + if ( asks != 0 ) + free(asks); + return(jprint(retjson,1)); +} + +double LP_fomoprice(char *base,char *rel,double *relvolumep) +{ + char *retstr; cJSON *retjson,*asks,*item; int32_t i,numasks; double maxvol=0.,relvolume,biggest,price,fomoprice = 0.; + relvolume = *relvolumep; + if ( (retstr= LP_orderbook(base,rel,0)) != 0 ) + { + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( (asks= jarray(&numasks,retjson,"asks")) != 0 && numasks > 0 ) + { + for (i=0; i maxvol ) + { + maxvol = biggest; + fomoprice = price; + } + printf("fomoprice (%.8f) i.%d %.8f vol %.8f [max %.8f @ %.8f]\n",relvolume,i,price,biggest,maxvol,fomoprice); + } + } + free_json(retjson); + } + free(retstr); + } + if ( maxvol > 0. && fomoprice > 0. ) + { + if ( maxvol < relvolume ) + relvolume = maxvol * 0.98; + fomoprice /= 0.95; + } else fomoprice = 0.; + *relvolumep = relvolume; + return(fomoprice); +} + +int64_t LP_KMDvalue(struct iguana_info *coin,int64_t balance) +{ + double price = 0.; int64_t KMDvalue=0; + if ( balance != 0 ) + { + if ( strcmp(coin->symbol,"KMD") == 0 ) + KMDvalue = balance; + else + { + if ( (price= LP_price(1,coin->symbol,"KMD")) > SMALLVAL ) + KMDvalue = price * balance; + } + } + return(KMDvalue); +} + +int64_t LP_kmdvalue(char *symbol,int64_t satoshis) +{ + struct iguana_info *coin; int64_t kmdvalue = 0; + if ( (coin= LP_coinfind(symbol)) != 0 ) + kmdvalue = LP_KMDvalue(coin,satoshis); + if ( kmdvalue == 0 ) + kmdvalue = satoshis; + return(kmdvalue); +} + +void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32]) +{ + LP_priceinfoupdate(base,rel,price); +} + +void LP_pricefname(char *fname,char *base,char *rel) +{ + sprintf(fname,"%s/PRICES/%s_%s",GLOBAL_DBDIR,base,rel); + OS_compatible_path(fname); +} + +void LP_priceitemadd(cJSON *retarray,uint32_t timestamp,double avebid,double aveask,double highbid,double lowask) +{ + cJSON *item = cJSON_CreateArray(); + jaddinum(item,timestamp); + jaddinum(item,avebid); + jaddinum(item,aveask); + jaddinum(item,highbid); + jaddinum(item,lowask); + jaddi(retarray,item); +} + +cJSON *LP_pricearray(char *base,char *rel,uint32_t firsttime,uint32_t lasttime,int32_t timescale) +{ + cJSON *retarray; char askfname[1024],bidfname[1024]; int64_t bidprice64,askprice64; uint32_t bidnow,asknow,bidi,aski,lastbidi,lastaski; int32_t numbids,numasks; double bidemit,askemit,bidsum,asksum,bid,ask,highbid,lowbid,highask,lowask,bidemit2,askemit2; FILE *askfp=0,*bidfp=0; + if ( timescale <= 0 ) + timescale = 60; + if ( lasttime == 0 ) + lasttime = (uint32_t)-1; + LP_pricefname(askfname,base,rel); + LP_pricefname(bidfname,rel,base); + retarray = cJSON_CreateArray(); + lastbidi = lastaski = 0; + numbids = numasks = 0; + bidsum = asksum = askemit = bidemit = highbid = lowbid = highask = lowask = 0.; + if ( (bidfp= fopen(bidfname,"rb")) != 0 && (askfp= fopen(askfname,"rb")) != 0 ) + { + while ( bidfp != 0 || askfp != 0 ) + { + bidi = aski = 0; + bidemit = askemit = bidemit2 = askemit2 = 0.; + if ( bidfp != 0 && fread(&bidnow,1,sizeof(bidnow),bidfp) == sizeof(bidnow) && fread(&bidprice64,1,sizeof(bidprice64),bidfp) == sizeof(bidprice64) ) + { + //printf("bidnow.%u %.8f\n",bidnow,dstr(bidprice64)); + if ( bidnow != 0 && bidprice64 != 0 && bidnow >= firsttime && bidnow <= lasttime ) + { + bidi = bidnow / timescale; + if ( bidi != lastbidi ) + { + if ( bidsum != 0. && numbids != 0 ) + { + bidemit = bidsum / numbids; + bidemit2 = highbid; + } + bidsum = highbid = lowbid = 0.; + numbids = 0; + } + if ( (bid= 1. / dstr(bidprice64)) != 0. ) + { + if ( bid > highbid ) + highbid = bid; + if ( lowbid == 0. || bid < lowbid ) + lowbid = bid; + bidsum += bid; + numbids++; + //printf("bidi.%u num.%d %.8f [%.8f %.8f]\n",bidi,numbids,bid,lowbid,highbid); + } + } + } else fclose(bidfp), bidfp = 0; + if ( askfp != 0 && fread(&asknow,1,sizeof(asknow),askfp) == sizeof(asknow) && fread(&askprice64,1,sizeof(askprice64),askfp) == sizeof(askprice64) ) + { + //printf("asknow.%u %.8f\n",asknow,dstr(askprice64)); + if ( asknow != 0 && askprice64 != 0 && asknow >= firsttime && asknow <= lasttime ) + { + aski = asknow / timescale; + if ( aski != lastaski ) + { + if ( asksum != 0. && numasks != 0 ) + { + askemit = asksum / numasks; + askemit2 = lowask; + } + asksum = highask = lowask = 0.; + numasks = 0; + } + if ( (ask= dstr(askprice64)) != 0. ) + { + if ( ask > highask ) + highask = ask; + if ( lowask == 0. || ask < lowask ) + lowask = ask; + asksum += ask; + numasks++; + //printf("aski.%u num.%d %.8f [%.8f %.8f]\n",aski,numasks,ask,lowask,highask); + } + } + } else fclose(askfp), askfp = 0; + if ( bidemit != 0. || askemit != 0. ) + { + if ( bidemit != 0. && askemit != 0. && lastbidi == lastaski ) + { + LP_priceitemadd(retarray,lastbidi * timescale,bidemit,askemit,bidemit2,askemit2); + highbid = lowbid = highask = lowask = 0.; + } + else + { + if ( bidemit != 0. ) + { + printf("bidonly %.8f %.8f\n",bidemit,highbid); + LP_priceitemadd(retarray,lastbidi * timescale,bidemit,0.,bidemit2,0.); + highbid = lowbid = 0.; + } + if ( askemit != 0. ) + { + printf("askonly %.8f %.8f\n",askemit,lowask); + LP_priceitemadd(retarray,lastaski * timescale,0.,askemit,0.,askemit2); + highask = lowask = 0.; + } + } + } + if ( bidi != 0 ) + lastbidi = bidi; + if ( aski != 0 ) + lastaski = aski; + } + } else printf("couldnt open either %s %p or %s %p\n",bidfname,bidfp,askfname,askfp); + if ( bidfp != 0 ) + fclose(bidfp); + if ( askfp != 0 ) + fclose(askfp); + return(retarray); +} + +void LP_pricefeedupdate(bits256 pubkey,char *base,char *rel,double price,char *utxocoin,int32_t numrelutxos,int64_t balance,int64_t minutxo,int64_t maxutxo,int64_t unconfcredits) +{ + struct LP_priceinfo *basepp,*relpp; uint32_t now; int64_t price64; struct LP_pubkey_info *pubp; char str[65],fname[512]; FILE *fp; +//printf("check PRICEFEED UPDATE.(%s/%s) %.8f %s balance %.8f min %.8f max %.8f\n",base,rel,price,bits256_str(str,pubkey),dstr(balance),dstr(minutxo),dstr(maxutxo)); + if ( LP_pricevalid(price) > 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) + { + //if ( (fp= basepp->fps[relpp->ind]) == 0 ) + { + LP_pricefname(fname,base,rel); + fp = OS_appendfile(fname); //basepp->fps[relpp->ind] = + } + if ( fp != 0 ) + { + now = (uint32_t)time(NULL); + price64 = price * SATOSHIDEN; + fwrite(&now,1,sizeof(now),fp); + fwrite(&price64,1,sizeof(price64),fp); + fclose(fp); + } + //if ( (fp= relpp->fps[basepp->ind]) == 0 ) + { + sprintf(fname,"%s/PRICES/%s_%s",GLOBAL_DBDIR,rel,base); + fp = OS_appendfile(fname); //relpp->fps[basepp->ind] = + } + if ( fp != 0 ) + { + now = (uint32_t)time(NULL); + price64 = (1. / price) * SATOSHIDEN; + fwrite(&now,1,sizeof(now),fp); + fwrite(&price64,1,sizeof(price64),fp); + fclose(fp); + } + if ( (pubp= LP_pubkeyadd(pubkey)) != 0 ) + { + if ( (LP_rand() % 1000) == 0 ) + printf("PRICEFEED UPDATE.(%-6s/%6s) %12.8f %s %12.8f\n",base,rel,price,bits256_str(str,pubkey),1./price); + if ( unconfcredits > pubp->unconfcredits ) + pubp->unconfcredits = unconfcredits; + pubp->timestamp = (uint32_t)time(NULL); + LP_pubkey_update(pubp,basepp->ind,relpp->ind,price,balance,utxocoin,numrelutxos,minutxo,maxutxo); + //pubp->depthinfo[basepp->ind][relpp->ind] = LP_depthinfo_compact(); + //if ( fabs(pubp->matrix[basepp->ind][relpp->ind] - price) > SMALLVAL ) + { + //pubp->matrix[basepp->ind][relpp->ind] = price; + //pubp->timestamps[basepp->ind][relpp->ind] = pubp->timestamp; + dxblend(&basepp->relvals[relpp->ind],price,0.9); + dxblend(&relpp->relvals[basepp->ind],1. / price,0.9); + } + } else printf("error finding pubkey entry %s, ok if rare\n",bits256_str(str,pubkey)); + } + //else if ( (rand() % 100) == 0 ) + // printf("error finding %s/%s %.8f\n",base,rel,price); +} + +double LP_CMCbtcprice(double *price_usdp,char *symbol) +{ + char *retstr; cJSON *ticker,*item; double price_btc = 0.; + *price_usdp = 0.; + if ( (retstr= cmc_ticker(symbol)) != 0 ) + { + if ( (ticker= cJSON_Parse(retstr)) != 0 ) + { + item = jitem(ticker,0); + price_btc = jdouble(item,"price_btc"); + *price_usdp = jdouble(item,"price_usd"); +//printf("%.8f item.(%s)\n",price_btc,jprint(item,0)); + free_json(ticker); + } + free(retstr); + } + return(price_btc); +} + +cJSON *LP_fundvalue(cJSON *argjson) +{ + cJSON *holdings,*item,*newitem,*array,*retjson; int32_t i,iter,n,missing=0; double usdprice,divisor,btcprice,balance,btcsum,KMDholdings,numKMD; struct iguana_info *coin; char *symbol,*coinaddr; int64_t fundvalue,KMDvalue = 0; + fundvalue = 0; + KMDholdings = btcsum = 0.; + array = cJSON_CreateArray(); + for (iter=0; iter<2; iter++) + { + if ( iter == 0 ) + holdings = jarray(&n,argjson,"holdings"); + else + { + if ( (coinaddr= jstr(argjson,"address")) != 0 ) + { + holdings = LP_balances(coinaddr); + n = cJSON_GetArraySize(holdings); + } else break; + } + if ( holdings != 0 ) + { + for (i=0; i SMALLVAL ) + { + newitem = cJSON_CreateObject(); + jaddstr(newitem,"coin",symbol); + jaddnum(newitem,"balance",balance); + if ( (coin= LP_coinfind(symbol)) != 0 && (KMDvalue= LP_KMDvalue(coin,SATOSHIDEN * balance)) > 0 ) + { + jaddnum(newitem,"KMD",dstr(KMDvalue)); + fundvalue += KMDvalue; + if ( strcmp(symbol,"KMD") == 0 ) + KMDholdings += dstr(KMDvalue); + } + else if ( iter == 0 && (btcprice= LP_CMCbtcprice(&usdprice,symbol)) > SMALLVAL ) + { + btcsum += btcprice * balance; + jaddnum(newitem,"BTC",btcprice * balance); + } + else jaddstr(newitem,"error","no price source"); + jaddi(array,newitem); + } else missing++; + } + } + } + retjson = cJSON_CreateObject(); + jaddstr(retjson,"result","success"); + jaddnum(retjson,"missing",missing); + jadd(retjson,"holdings",array); + btcprice = LP_CMCbtcprice(&usdprice,"komodo"); + divisor = jdouble(argjson,"divisor"); + jaddnum(retjson,"KMDholdings",KMDholdings); + if ( btcsum != 0 ) + { + if ( btcprice > SMALLVAL ) + { + numKMD = (btcsum / btcprice); + fundvalue += numKMD * SATOSHIDEN; + jaddnum(retjson,"KMD_BTC",btcprice); + jaddnum(retjson,"btcsum",btcsum); + numKMD += KMDholdings; + jaddnum(retjson,"btc2kmd",numKMD); + if ( divisor != 0 ) + { + jaddnum(retjson,"NAV_KMD",numKMD/divisor); + jaddnum(retjson,"NAV_BTC",(btcsum + (KMDholdings * btcprice))/divisor); + jaddnum(retjson,"NAV_USD",(usdprice * numKMD)/divisor); + } + } + } + jaddnum(retjson,"fundvalue",dstr(fundvalue)); + if ( divisor != 0 ) + { + jaddnum(retjson,"divisor",divisor); + numKMD = dstr(fundvalue); + jaddnum(retjson,"assetNAV_KMD",numKMD/divisor); + jaddnum(retjson,"assetNAV_BTC",(btcprice * numKMD)/divisor); + jaddnum(retjson,"assetNAV_USD",(usdprice * numKMD)/divisor); + } + return(retjson); +} + diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 6765de92b..5ddc56f45 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -120,7 +120,7 @@ struct LP_globals uint64_t LP_skipstatus[10000]; uint16_t netid; uint8_t LP_myrmd160[20],LP_pubsecp[33]; - uint32_t LP_sessionid,counter; + uint32_t LP_sessionid,counter,mpnet; int32_t LP_IAMLP,LP_pendingswaps,USERPASS_COUNTER,LP_numprivkeys,initializing,waiting,LP_numskips; char seednode[64],USERPASS[65],USERPASS_WIFSTR[64],LP_myrmd160str[41],gui[65],LP_NXTaddr[64]; struct LP_privkey LP_privkeys[100]; @@ -172,6 +172,7 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ #include "LP_mmjson.c" #include "LP_socket.c" +#include "LP_mpnet.c" #include "LP_secp.c" #include "LP_bitcoin.c" #include "LP_coins.c" @@ -480,6 +481,8 @@ int32_t LP_nanomsg_recvs(void *ctx) nonz += LP_sock_check("PULL",ctx,origipaddr,-1,LP_mypullsock,"127.0.0.1",1); } portable_mutex_unlock(&LP_nanorecvsmutex); + if ( G.mpnet != 0 ) + LP_mpnet_check(ctx,origipaddr,LP_mypubsock); return(nonz); } diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 9163604bd..6ce1b93d9 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -550,7 +550,9 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double } if ( qp->mpnet != 0 && qp->fill != 0 && qp->gtc != 0 ) { - // send to mpnet + char *msg = jprint(reqjson,0); + LP_mpnet_send(msg); + free(msg); } free_json(reqjson); retval = 0; @@ -650,7 +652,10 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q } if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) { - // send to mpnet + cJSON *reqjson = LP_quotejson(qp); + char *msg = jprint(reqjson,1); + LP_mpnet_send(msg); + free(msg); } char str[65]; printf("LP_trade mpnet.%d fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->mpnet,qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); return(LP_recent_swaps(0,uuidstr)); @@ -1197,7 +1202,9 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, } if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) { - // send to mpnet + char *msg = jprint(reqjson,0); + LP_mpnet_send(msg); + free(msg); } free_json(reqjson); //printf("Send RESERVED id.%llu\n",(long long)qp->aliceid); @@ -1795,7 +1802,7 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha } } int32_t changed; - Q.mpnet = 0; + Q.mpnet = G.mpnet; Q.fill = fillflag; Q.gtc = gtcflag; LP_mypriceset(0,&changed,rel,base,1. / maxprice); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index 15fb2b694..079dd43fe 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -739,9 +739,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ } } if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) - { - // send to mpnet - } + LP_mpnet_send(msg); free(msg); } From b15bc8addab6b255a55c7cc6befe88d3f4ec31d6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 22:45:53 -1100 Subject: [PATCH 17/60] mpnet stub code --- iguana/exchanges/LP_mpnet.c | 1305 +---------------------------------- 1 file changed, 9 insertions(+), 1296 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 38d37c626..457a3f30b 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -14,1314 +14,27 @@ * * ******************************************************************************/ // -// LP_prices.c +// LP_mpnet.c // marketmaker // -struct LP_orderbookentry -{ - bits256 pubkey; - double price; - int64_t avesatoshis,maxsatoshis,depth,dynamictrust; - uint32_t timestamp; - int32_t numutxos; - char coinaddr[64]; -}; - -struct LP_priceinfo -{ - char symbol[68]; - uint64_t coinbits; - int32_t ind,pad; - double diagval,high[2],low[2],last[2],bid[2],ask[2]; - double relvals[LP_MAXPRICEINFOS]; - double myprices[2][LP_MAXPRICEINFOS]; - double minprices[LP_MAXPRICEINFOS]; // autoprice - double fixedprices[LP_MAXPRICEINFOS]; // fixedprices - double buymargins[LP_MAXPRICEINFOS]; - double sellmargins[LP_MAXPRICEINFOS]; - double offsets[LP_MAXPRICEINFOS]; - double factors[LP_MAXPRICEINFOS]; -} LP_priceinfos[LP_MAXPRICEINFOS]; -int32_t LP_numpriceinfos; - -struct LP_cacheinfo -{ - UT_hash_handle hh; - struct LP_quoteinfo Q; - uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(int32_t)]; - double price; - uint32_t timestamp; -} *LP_cacheinfos; - -void LP_priceinfos_clear() -{ - int32_t i; struct LP_priceinfo *pp; - for (i=0; imyprices,0,sizeof(pp->myprices)); - memset(pp->minprices,0,sizeof(pp->minprices)); - memset(pp->fixedprices,0,sizeof(pp->fixedprices)); - memset(pp->buymargins,0,sizeof(pp->buymargins)); - memset(pp->sellmargins,0,sizeof(pp->sellmargins)); - memset(pp->offsets,0,sizeof(pp->offsets)); - memset(pp->factors,0,sizeof(pp->factors)); - } -} - -float LP_pubkey_price(int32_t *numutxosp,int64_t *avesatoshisp,int64_t *maxsatoshisp,struct LP_pubkey_info *pubp,uint32_t baseind,uint32_t relind) -{ - struct LP_pubkey_quote *pq,*tmp; int32_t scale; int64_t scale64; - *numutxosp = 0; - *avesatoshisp = *maxsatoshisp = 0; - DL_FOREACH_SAFE(pubp->quotes,pq,tmp) - { - if ( baseind == pq->baseind && relind == pq->relind ) - { - if ( (scale= pq->scale) == 0 ) - pq->scale = scale = 6; - scale64 = 1; - while ( scale > 0 ) - { - scale64 *= 10; - scale--; - } - *numutxosp = pq->numutxos; - *avesatoshisp = pq->aveutxo * scale64; - *maxsatoshisp = pq->maxutxo * scale64; - return(pq->price); - } - } - return(0); -} - -void LP_pubkey_update(struct LP_pubkey_info *pubp,uint32_t baseind,uint32_t relind,float price,int64_t balance,char *utxocoin,int32_t numutxos,int64_t minutxo,int64_t maxutxo) -{ - struct LP_pubkey_quote *pq,*tmp; int64_t aveutxo,scale64,ave64,max64; int32_t scale; - DL_FOREACH_SAFE(pubp->quotes,pq,tmp) - { - if ( baseind == pq->baseind && relind == pq->relind ) - break; - pq = 0; - } - if ( pq == 0 ) - { - pq = calloc(1,sizeof(*pq)); - pq->baseind = baseind; - pq->relind = relind; - pq->scale = 6; // millions of SATOSHIS, ie. 0.01 - DL_APPEND(pubp->quotes,pq); // already serialized as only path is via stats_JSON() - //printf("create pubp quotes %d/%d\n",baseind,relind); - } -//printf("%d/%d price %.8f balance %.8f %s num.%d min %.8f max %.8f\n",baseind,relind,price,dstr(balance),utxocoin,numutxos,dstr(minutxo),dstr(maxutxo)); - pq->price = price; - if ( utxocoin != 0 && utxocoin[0] != 0 ) - { - if ( (scale= pq->scale) == 0 ) - pq->scale = scale = 6; - scale64 = 1; - while ( scale > 0 ) - { - scale64 *= 10; - scale--; - } - if ( numutxos >= 256 ) - pq->numutxos = 255; - else pq->numutxos = numutxos; - aveutxo = (balance + (scale64>>1)) / numutxos; - if ( (ave64= (aveutxo / scale64)) >= (1LL << 32) ) - ave64 = (1LL << 32) - 1; - max64 = ((maxutxo + (scale64>>1)) / scale64); - if ( max64 >= (1LL << 32) ) - max64 = (1LL << 32) - 1; - pq->aveutxo = (uint32_t)ave64; - pq->maxutxo = (uint32_t)max64; - if ( 0 ) - { - printf("price %.8f base.%s rel.%s utxocoin.%s balance %.8f numutxos.%u %u scale64 = %llu, ave %llu, ave32 %u (%llu) max32 %u (%llu)\n",price,LP_priceinfos[baseind].symbol,LP_priceinfos[relind].symbol,utxocoin,dstr(balance),numutxos,pq->numutxos,(long long)scale64,(long long)aveutxo,pq->aveutxo,(long long)pq->aveutxo * scale64,pq->maxutxo,(long long)pq->maxutxo * scale64); - int64_t avesatoshis,maxsatoshis; - price = LP_pubkey_price(&numutxos,&avesatoshis,&maxsatoshis,pubp,baseind,relind); - printf("checkprice %.8f numutxos.%d ave %.8f max %.8f\n",price,numutxos,dstr(avesatoshis),dstr(maxsatoshis)); - } - } -} - -struct LP_priceinfo *LP_priceinfo(int32_t ind) -{ - if ( ind < 0 || ind >= LP_MAXPRICEINFOS ) - return(0); - else return(&LP_priceinfos[ind]); -} - -char *LP_priceinfostr(int32_t ind) -{ - if ( ind < 0 || ind >= LP_MAXPRICEINFOS ) - return("UNKNOWN"); - else return(LP_priceinfos[ind].symbol); -} +int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); -int32_t LP_pricevalid(double price) +void LP_mpnet_send(char *msg) { - if ( price > SMALLVAL && isnan(price) == 0 && price < SATOSHIDEN ) - return(1); - else return(0); + } -struct LP_priceinfo *LP_priceinfofind(char *symbol) +cJSON *LP_mpnet_get() { - int32_t i; struct LP_priceinfo *pp; uint64_t coinbits; - if ( symbol == 0 || symbol[0] == 0 ) - return(0); - if ( LP_numpriceinfos > 0 ) - { - coinbits = stringbits(symbol); - pp = LP_priceinfos; - for (i=0; icoinbits == coinbits ) - return(pp); - } return(0); } -int32_t LP_priceinfoind(char *symbol) -{ - struct LP_priceinfo *pp; - if ( (pp= LP_priceinfofind(symbol)) != 0 ) - return(pp->ind); - else return(-1); -} - -struct LP_priceinfo *LP_priceinfoptr(int32_t *indp,char *base,char *rel) -{ - struct LP_priceinfo *basepp,*relpp; - if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) - { - *indp = relpp->ind; - return(basepp); - } - else - { - *indp = -1; - return(0); - } -} - -int32_t LP_cachekey(uint8_t *key,char *base,char *rel,bits256 txid,int32_t vout) -{ - uint64_t basebits,relbits; int32_t offset = 0; - basebits = stringbits(base); - relbits = stringbits(rel); - memcpy(&key[offset],&basebits,sizeof(basebits)), offset += sizeof(basebits); - memcpy(&key[offset],&relbits,sizeof(relbits)), offset += sizeof(relbits); - memcpy(&key[offset],&txid,sizeof(txid)), offset += sizeof(txid); - memcpy(&key[offset],&vout,sizeof(vout)), offset += sizeof(vout); - return(offset); -} - -struct LP_cacheinfo *LP_cachefind(char *base,char *rel,bits256 txid,int32_t vout) -{ - struct LP_cacheinfo *ptr=0; uint8_t key[sizeof(bits256)+sizeof(uint64_t)*2+sizeof(vout)]; - if ( base == 0 || rel == 0 ) - return(0); - if ( LP_cachekey(key,base,rel,txid,vout) == sizeof(key) ) - { - portable_mutex_lock(&LP_cachemutex); - HASH_FIND(hh,LP_cacheinfos,key,sizeof(key),ptr); - portable_mutex_unlock(&LP_cachemutex); - } else printf("LP_cachefind keysize mismatch?\n"); - if ( 0 && ptr != 0 && ptr->timestamp != 0 && ptr->timestamp < time(NULL)-LP_CACHEDURATION ) - { - printf("expire price %.8f\n",ptr->price); - ptr->price = 0.; - ptr->timestamp = 0; - memset(&ptr->Q,0,sizeof(ptr->Q)); - } - return(ptr); -} - -struct LP_pubkey_info *LP_pubkey_rmd160find(uint8_t rmd160[20]) -{ - struct LP_pubkey_info *pubp=0,*tmp; - portable_mutex_lock(&LP_pubkeymutex); - HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) - { - if ( memcmp(rmd160,pubp->rmd160,sizeof(pubp->rmd160)) == 0 ) - break; - pubp = 0; - } - portable_mutex_unlock(&LP_pubkeymutex); - return(pubp); -} - -struct LP_address *_LP_addressfind(struct iguana_info *coin,char *coinaddr) -{ - uint8_t rmd160[20],addrtype; struct LP_address *ap; struct LP_pubkey_info *pubp; - HASH_FIND(hh,coin->addresses,coinaddr,strlen(coinaddr),ap); - if ( ap != 0 && bits256_nonz(ap->pubkey) == 0 ) - { - bitcoin_addr2rmd160(coin->symbol,coin->taddr,&addrtype,rmd160,coinaddr); - if ( (pubp= LP_pubkey_rmd160find(rmd160)) != 0 ) - { - ap->pubkey = pubp->pubkey; - memcpy(ap->pubsecp,pubp->pubsecp,sizeof(ap->pubsecp)); - } - } - return(ap); -} - -struct LP_address *_LP_addressadd(struct iguana_info *coin,char *coinaddr) +void LP_mpnet_check(void *ctx,char *myipaddr,int32_t pubsock) { - uint8_t rmd160[20],addrtype; struct LP_address *ap; struct LP_pubkey_info *pubp; - ap = calloc(1,sizeof(*ap)); - safecopy(ap->coinaddr,coinaddr,sizeof(ap->coinaddr)); - bitcoin_addr2rmd160(coin->symbol,coin->taddr,&addrtype,rmd160,coinaddr); - if ( (pubp= LP_pubkey_rmd160find(rmd160)) != 0 ) + while ( (argjson= LP_mpnet_get()) != 0 ) { - ap->pubkey = pubp->pubkey; - memcpy(ap->pubsecp,pubp->pubsecp,sizeof(ap->pubsecp)); + LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0); + free_json(argjson); } - //printf("LP_ADDRESS %s ADD.(%s)\n",coin->symbol,coinaddr); - HASH_ADD_KEYPTR(hh,coin->addresses,ap->coinaddr,strlen(ap->coinaddr),ap); - return(ap); } - -struct LP_pubkey_info *LP_pubkeyfind(bits256 pubkey) -{ - struct LP_pubkey_info *pubp=0; - portable_mutex_lock(&LP_pubkeymutex); - HASH_FIND(hh,LP_pubkeyinfos,&pubkey,sizeof(pubkey),pubp); - portable_mutex_unlock(&LP_pubkeymutex); - return(pubp); -} - -struct LP_pubkey_info *LP_pubkeyadd(bits256 pubkey) -{ - char str[65]; struct LP_pubkey_info *pubp=0; - portable_mutex_lock(&LP_pubkeymutex); - HASH_FIND(hh,LP_pubkeyinfos,&pubkey,sizeof(pubkey),pubp); - if ( pubp == 0 ) - { - pubp = calloc(1,sizeof(*pubp)); - pubp->pubkey = pubkey; - pubp->pairsock = -1; - if ( bits256_cmp(G.LP_mypub25519,pubkey) == 0 ) - { - memcpy(pubp->rmd160,G.LP_myrmd160,sizeof(pubp->rmd160)); - memcpy(pubp->pubsecp,G.LP_pubsecp,sizeof(pubp->pubsecp)); - } - HASH_ADD_KEYPTR(hh,LP_pubkeyinfos,&pubp->pubkey,sizeof(pubp->pubkey),pubp); - HASH_FIND(hh,LP_pubkeyinfos,&pubkey,sizeof(pubkey),pubp); - if ( pubp == 0 ) - printf("pubkeyadd find %s error after add\n",bits256_str(str,pubp->pubkey)); - } - portable_mutex_unlock(&LP_pubkeymutex); - return(pubp); -} - -int32_t LP_pubkey_istrusted(bits256 pubkey) -{ - struct LP_pubkey_info *pubp; - if ( (pubp= LP_pubkeyadd(pubkey)) != 0 ) - return(pubp->istrusted != 0); - return(0); -} - -char *LP_pubkey_trustset(bits256 pubkey,uint32_t trustval) -{ - struct LP_pubkey_info *pubp; - if ( (pubp= LP_pubkeyadd(pubkey)) != 0 ) - { - pubp->istrusted = trustval; - return(clonestr("{\"result\":\"success\"}")); - } - return(clonestr("{\"error\":\"pubkey not found\"}")); -} - -char *LP_pubkey_trusted() -{ - struct LP_pubkey_info *pubp,*tmp; cJSON *array = cJSON_CreateArray(); - HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) - { - if ( pubp->istrusted != 0 ) - jaddibits256(array,pubp->pubkey); - } - return(jprint(array,1)); -} - -int64_t LP_unspents_metric(struct iguana_info *coin,char *coinaddr) -{ - cJSON *array,*item; int32_t i,n; int64_t metric=0,total; - //LP_listunspent_both(coin->symbol,coinaddr,0); - if ( (array= LP_address_utxos(coin,coinaddr,1)) != 0 ) - { - total = 0; - if ( (n= cJSON_GetArraySize(array)) > 0 ) - { - for (i=0; imatrix[baseid][relid]; - if ( LP_pricevalid(price) > 0 ) - { - item = cJSON_CreateArray(); - jaddistr(item,base); - jaddistr(item,LP_priceinfos[relid].symbol); - jaddinum(item,price); - jaddi(array,item); - } - } - } - jaddbits256(obj,"pubkey",pubp->pubkey); - init_hexbytes_noT(hexstr,pubp->rmd160,sizeof(pubp->rmd160)); - jaddstr(obj,"rmd160",hexstr); - init_hexbytes_noT(hexstr2,pubp->pubsecp,sizeof(pubp->pubsecp)); - jaddstr(obj,"pubsecp",hexstr2); - init_hexbytes_noT(sigstr,pubp->sig,pubp->siglen); - jaddstr(obj,"sig",sigstr); - jaddnum(obj,"timestamp",pubp->timestamp); - jadd(obj,"asks",array); - if ( pubp->istrusted != 0 ) - jaddnum(obj,"istrusted",pubp->istrusted); - return(obj); -} - -char *LP_prices() -{ - struct LP_pubkey_info *pubp,*tmp; cJSON *array = cJSON_CreateArray(); - HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) - { - jaddi(array,LP_pubkeyjson(pubp)); - } - return(jprint(array,1)); -} - -double LP_pricecache(struct LP_quoteinfo *qp,char *base,char *rel,bits256 txid,int32_t vout) -{ - struct LP_cacheinfo *ptr; - if ( (ptr= LP_cachefind(base,rel,txid,vout)) != 0 ) - { - if ( qp != 0 ) - (*qp) = ptr->Q; - if ( ptr->price == 0. && ptr->Q.satoshis > ptr->Q.txfee ) - { - ptr->price = (double)ptr->Q.destsatoshis / (ptr->Q.satoshis - ptr->Q.txfee); - if ( LP_pricevalid(ptr->price) <= 0 ) - ptr->price = 0.; - printf("LP_pricecache: set %s/%s ptr->price %.8f\n",base,rel,ptr->price); - } - //printf(">>>>>>>>>> found %s/%s %.8f\n",base,rel,ptr->price); - return(ptr->price); - } - //char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout); - return(0.); -} - -void LP_priceinfoupdate(char *base,char *rel,double price) -{ - struct LP_priceinfo *basepp,*relpp; - if ( LP_pricevalid(price) > 0 ) - { - if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) - { - dxblend(&basepp->relvals[relpp->ind],price,0.9); - dxblend(&relpp->relvals[basepp->ind],1. / price,0.9); - //basepp->relvals[relpp->ind] = price; - //relpp->relvals[basepp->ind] = 1. / price; - } - } -} - -double LP_myprice(int32_t iambob,double *bidp,double *askp,char *base,char *rel) -{ - struct LP_priceinfo *basepp,*relpp; double val; - *bidp = *askp = 0.; - if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) - { - *askp = basepp->myprices[iambob][relpp->ind]; - if ( LP_pricevalid(*askp) > 0 ) - { - val = relpp->myprices[iambob][basepp->ind]; - if ( LP_pricevalid(val) > 0 ) - { - *bidp = 1. / val; - return((*askp + *bidp) * 0.5); - } - else - { - *bidp = 0.; - return(*askp); - } - } - else - { - val = relpp->myprices[iambob][basepp->ind]; - if ( LP_pricevalid(val) > 0 ) - { - *bidp = 1. / val; - *askp = 0.; - return(*bidp); - } - } - } - return(0.); -} - -char *LP_myprices(int32_t iambob) -{ - int32_t baseid,relid; double bid,ask; char *base,*rel; cJSON *item,*array; - array = cJSON_CreateArray(); - for (baseid=0; baseid SMALLVAL ) - { - item = cJSON_CreateObject(); - jaddstr(item,"base",base); - jaddstr(item,"rel",rel); - jaddnum(item,"bid",bid); - jaddnum(item,"ask",ask); - jaddi(array,item); - } - } - } - return(jprint(array,1)); -} - -int32_t LP_mypriceset(int32_t iambob,int32_t *changedp,char *base,char *rel,double price) -{ - struct LP_priceinfo *basepp=0,*relpp=0; struct LP_pubkey_info *pubp; double minprice,maxprice,margin,buymargin,sellmargin; - *changedp = 0; - //if ( strcmp("DEX",base) == 0 || strcmp("DEX",rel) == 0 ) - // printf("%s/%s setprice %.8f\n",base,rel,price); - if ( base != 0 && rel != 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) - { - - if ( price == 0. || fabs(basepp->myprices[iambob][relpp->ind] - price)/price > 0.001 ) - *changedp = 1; - if ( iambob != 0 ) - { - sellmargin = relpp->sellmargins[basepp->ind]; - buymargin = relpp->buymargins[basepp->ind]; - margin = (sellmargin + buymargin) * 0.5; - if ( price == 0. ) - { - relpp->minprices[basepp->ind] = 0.; - relpp->fixedprices[basepp->ind] = 0.; - relpp->buymargins[basepp->ind] = 0.; - relpp->sellmargins[basepp->ind] = 0.; - relpp->offsets[basepp->ind] = 0.; - relpp->factors[basepp->ind] = 0.; - LP_autoref_clear(base,rel); - margin = 0.; - } - else if ( (minprice= basepp->minprices[relpp->ind]) > SMALLVAL && price < minprice ) - { - //printf("%s/%s price %.8f less than minprice %.8f\n",base,rel,price,minprice); - price = minprice * (1. - margin); - } - else if ( (maxprice= relpp->minprices[basepp->ind]) > SMALLVAL ) - { - if ( price > (1. / maxprice) ) - { - //printf("%s/%s price %.8f less than maxprice %.8f, more than %.8f\n",base,rel,price,maxprice,1./maxprice); - price = (1. / maxprice) * (1. + margin); - } - } - } - /*else if ( basepp->myprices[relpp->ind] > SMALLVAL ) - { - price = (basepp->myprices[relpp->ind] * 0.9) + (0.1 * price); - }*/ - basepp->myprices[iambob][relpp->ind] = price; // ask - //printf("LP_mypriceset base.%s rel.%s <- price %.8f\n",base,rel,price); - //relpp->myprices[basepp->ind] = (1. / price); // bid, but best to do one dir at a time - if ( iambob != 0 && (pubp= LP_pubkeyadd(G.LP_mypub25519)) != 0 ) - { - pubp->timestamp = (uint32_t)time(NULL); - LP_pubkey_update(pubp,basepp->ind,relpp->ind,price,0,0,0,0,0); - //pubp->matrix[basepp->ind][relpp->ind] = price; - //pubp->timestamps[basepp->ind][relpp->ind] = pubp->timestamp; - //pubp->matrix[relpp->ind][basepp->ind] = (1. / price); - } - return(0); - } - printf("base.%s rel.%s %p %p price %.8f error case\n",base!=0?base:"",rel!=0?rel:"",basepp,relpp,price); - return(-1); -} - -double LP_price(int32_t iambob,char *base,char *rel) -{ - struct LP_priceinfo *basepp; int32_t relind; double price = 0.; - if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 ) - { - if ( (price= basepp->myprices[iambob][relind]) == 0. ) - { - price = basepp->relvals[relind]; - } - } - return(price); -} - -double LP_getmyprice(int32_t iambob,char *base,char *rel) -{ - struct LP_priceinfo *basepp; int32_t relind; double price = 0.; - if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 ) - { - if ( (price= basepp->myprices[iambob][relind]) == 0. ) - { - } - } - return(price); -} - -cJSON *LP_priceinfomatrix(int32_t iambob,int32_t usemyprices) -{ - int32_t i,j,n,m; double total,sum,val; struct LP_priceinfo *pp; uint32_t now; struct LP_cacheinfo *ptr,*tmp; cJSON *vectorjson = cJSON_CreateObject(); - now = (uint32_t)time(NULL); - HASH_ITER(hh,LP_cacheinfos,ptr,tmp) - { - if ( ptr->timestamp < now-3600*2 || ptr->price == 0. ) - continue; - LP_priceinfoupdate(ptr->Q.srccoin,ptr->Q.destcoin,ptr->price); - } - pp = LP_priceinfos; - total = m = 0; - for (i=0; idiagval = sum = n = 0; - for (j=0; jmyprices[iambob][j]) == 0. ) - val = pp->relvals[j]; - if ( val > SMALLVAL ) - { - sum += val; - n++; - } - } - if ( n > 0 ) - { - pp->diagval = sum / n; - total += pp->diagval, m++; - } - } - if ( m > 0 ) - { - pp = LP_priceinfos; - for (i=0; idiagval > SMALLVAL ) - { - pp->diagval /= total; - jaddnum(vectorjson,pp->symbol,pp->diagval); - } - } - } - return(vectorjson); -} - -struct LP_priceinfo *LP_priceinfoadd(char *symbol) -{ - struct LP_priceinfo *pp; cJSON *retjson; - if ( symbol == 0 ) - return(0); - if ( (pp= LP_priceinfofind(symbol)) != 0 ) - { - printf("%s already there\n",symbol); - return(pp); - } - if ( LP_numpriceinfos >= sizeof(LP_priceinfos)/sizeof(*LP_priceinfos) ) - { - printf("cant add any more priceinfos than %d\n",LP_numpriceinfos); - return(0); - } - pp = &LP_priceinfos[LP_numpriceinfos]; - memset(pp,0,sizeof(*pp)); - safecopy(pp->symbol,symbol,sizeof(pp->symbol)); - pp->coinbits = stringbits(symbol); - pp->ind = LP_numpriceinfos++; - //LP_numpriceinfos++; - if ( (retjson= LP_priceinfomatrix(1,0)) != 0 ) - free_json(retjson); - return(pp); -} - -struct LP_cacheinfo *LP_cacheadd(char *base,char *rel,bits256 txid,int32_t vout,double price,struct LP_quoteinfo *qp) -{ - char str[65]; struct LP_cacheinfo *ptr=0; - if ( base == 0 || rel == 0 ) - return(0); - if ( LP_pricevalid(price) > 0 ) - { - if ( (ptr= LP_cachefind(base,rel,txid,vout)) == 0 ) - { - ptr = calloc(1,sizeof(*ptr)); - if ( LP_cachekey(ptr->key,base,rel,txid,vout) == sizeof(ptr->key) ) - { - portable_mutex_lock(&LP_cachemutex); - HASH_ADD(hh,LP_cacheinfos,key,sizeof(ptr->key),ptr); - portable_mutex_unlock(&LP_cachemutex); - } else printf("LP_cacheadd keysize mismatch?\n"); - } - ptr->Q = *qp; - ptr->timestamp = (uint32_t)time(NULL); - if ( price != ptr->price ) - { - ptr->price = price; - LP_priceinfoupdate(base,rel,price); - printf("updated %s/v%d %s/%s %llu price %.8f\n",bits256_str(str,txid),vout,base,rel,(long long)qp->satoshis,price); - } else ptr->price = price; - } - return(ptr); -} - -static int _cmp_orderbook(const void *a,const void *b) -{ - int32_t retval = 0; -#define ptr_a (*(struct LP_orderbookentry **)a)->price -#define ptr_b (*(struct LP_orderbookentry **)b)->price - if ( ptr_b > ptr_a ) - retval = -1; - else if ( ptr_b < ptr_a ) - retval = 1; - else - { -#undef ptr_a -#undef ptr_b -#define ptr_a ((struct LP_orderbookentry *)a)->maxsatoshis -#define ptr_b ((struct LP_orderbookentry *)b)->maxsatoshis - if ( ptr_b > ptr_a ) - return(-1); - else if ( ptr_b < ptr_a ) - return(1); - } - // printf("%.8f vs %.8f -> %d\n",ptr_a,ptr_b,retval); - return(retval); -#undef ptr_a -#undef ptr_b -} - -static int _revcmp_orderbook(const void *a,const void *b) -{ - int32_t retval = 0; -#define ptr_a (*(struct LP_orderbookentry **)a)->price -#define ptr_b (*(struct LP_orderbookentry **)b)->price - if ( ptr_b > ptr_a ) - retval = 1; - else if ( ptr_b < ptr_a ) - retval = -1; - else - { -#undef ptr_a -#undef ptr_b -#define ptr_a ((struct LP_orderbookentry *)a)->maxsatoshis -#define ptr_b ((struct LP_orderbookentry *)b)->maxsatoshis - if ( ptr_b > ptr_a ) - return(-1); - else if ( ptr_b < ptr_a ) - return(1); - } - // printf("%.8f vs %.8f -> %d\n",ptr_a,ptr_b,retval); - return(retval); -#undef ptr_a -#undef ptr_b -} - -cJSON *LP_orderbookjson(char *symbol,struct LP_orderbookentry *op) -{ - cJSON *item = cJSON_CreateObject(); - if ( LP_pricevalid(op->price) > 0 ) - { - jaddstr(item,"coin",symbol); - jaddstr(item,"address",op->coinaddr); - jaddnum(item,"price",op->price); - jaddnum(item,"numutxos",op->numutxos); - jaddnum(item,"avevolume",dstr(op->avesatoshis)); - jaddnum(item,"maxvolume",dstr(op->maxsatoshis)); - jaddnum(item,"depth",dstr(op->depth)); - jaddbits256(item,"pubkey",op->pubkey); - jaddnum(item,"age",time(NULL)-op->timestamp); - jaddnum(item,"zcredits",dstr(op->dynamictrust)); - } - return(item); -} - -struct LP_orderbookentry *LP_orderbookentry(char *address,char *base,char *rel,double price,int32_t numutxos,int64_t avesatoshis,int64_t maxsatoshis,bits256 pubkey,uint32_t timestamp,int64_t balance,int64_t dynamictrust) -{ - struct LP_orderbookentry *op; - if ( (op= calloc(1,sizeof(*op))) != 0 ) - { - safecopy(op->coinaddr,address,sizeof(op->coinaddr)); - op->price = price; - op->numutxos = numutxos; - op->avesatoshis = avesatoshis; - op->maxsatoshis = maxsatoshis; - op->pubkey = pubkey; - op->timestamp = timestamp; - op->depth = balance; - op->dynamictrust = dynamictrust; - } - return(op); -} - -void LP_pubkeys_query() -{ - uint8_t zeroes[20]; bits256 zero; cJSON *reqjson; struct LP_pubkey_info *pubp=0,*tmp; - memset(zero.bytes,0,sizeof(zero)); - memset(zeroes,0,sizeof(zeroes)); - HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) - { - if ( memcmp(zeroes,pubp->rmd160,sizeof(pubp->rmd160)) == 0 && time(NULL) > pubp->lasttime+60 ) - { - pubp->lasttime = (uint32_t)time(NULL); - reqjson = cJSON_CreateObject(); - jaddstr(reqjson,"method","wantnotify"); - jaddbits256(reqjson,"pub",pubp->pubkey); - //printf("LP_pubkeys_query %s\n",jprint(reqjson,0)); - LP_reserved_msg(0,"","",zero,jprint(reqjson,1)); - } - } -} - -int32_t LP_orderbook_utxoentries(uint32_t now,int32_t polarity,char *base,char *rel,struct LP_orderbookentry *(**arrayp),int32_t num,int32_t cachednum,int32_t duration) -{ - char coinaddr[64]; uint8_t zeroes[20]; struct LP_pubkey_info *pubp=0,*tmp; struct LP_priceinfo *basepp; struct LP_orderbookentry *op; struct LP_address *ap; struct iguana_info *basecoin; uint32_t oldest; double price; int32_t baseid,relid,n; int64_t maxsatoshis,balance,avesatoshis; - if ( (basepp= LP_priceinfoptr(&relid,base,rel)) != 0 ) - baseid = basepp->ind; - else return(num); - if ( (basecoin= LP_coinfind(base)) == 0 ) - return(-1); - now = (uint32_t)time(NULL); - oldest = now - duration; - memset(zeroes,0,sizeof(zeroes)); - HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp) - { - if ( memcmp(zeroes,pubp->rmd160,sizeof(pubp->rmd160)) == 0 ) - { - //printf("skip pubp since no rmd160\n"); - continue; - } - if ( pubp->timestamp < oldest ) - continue; - bitcoin_address(base,coinaddr,basecoin->taddr,basecoin->pubtype,pubp->pubsecp,33); - avesatoshis = maxsatoshis = n = 0; - ap = 0; - if ( (price= LP_pubkey_price(&n,&avesatoshis,&maxsatoshis,pubp,baseid,relid)) > SMALLVAL ) //pubp->matrix[baseid][relid]) > SMALLVAL )//&& pubp->timestamps[baseid][relid] >= oldest ) - { - balance = avesatoshis * n; - //if ( (ap= LP_addressfind(basecoin,coinaddr)) != 0 ) - { - //n = LP_address_minmax(&balance,&minsatoshis,&maxsatoshis,ap); - if ( polarity > 0 ) - { - balance *= price; - avesatoshis *= price; - maxsatoshis *= price; - } - //printf("%s/%s %s n.%d ap->n.%d %.8f\n",base,rel,coinaddr,n,ap->n,dstr(ap->total)); - } - if ( (op= LP_orderbookentry(coinaddr,base,rel,polarity > 0 ? price : 1./price,n,avesatoshis,maxsatoshis,pubp->pubkey,pubp->timestamp,balance,pubp->dynamictrust)) != 0 ) - { - *arrayp = realloc(*arrayp,sizeof(*(*arrayp)) * (num+1)); - (*arrayp)[num++] = op; - } - } - //printf("pubp.(%s) %.8f %p\n",coinaddr,price,ap); - } - return(num); -} - -char *LP_orderbook(char *base,char *rel,int32_t duration) -{ - uint32_t now,i; int64_t depth,askdepth=0,biddepth=0; struct LP_priceinfo *basepp=0,*relpp=0; struct LP_orderbookentry **bids = 0,**asks = 0; cJSON *retjson,*array; struct iguana_info *basecoin,*relcoin; int32_t n,numbids=0,numasks=0,cachenumbids,cachenumasks,baseid,relid,suppress_prefetch=0; - basecoin = LP_coinfind(base); - relcoin = LP_coinfind(rel); - if ( basecoin == 0 || relcoin == 0 ) - return(clonestr("{\"error\":\"base or rel not added\"}")); - if ( (basepp= LP_priceinfofind(base)) == 0 || (relpp= LP_priceinfofind(rel)) == 0 ) - return(clonestr("{\"error\":\"base or rel not added\"}")); - if ( duration <= 0 ) - { - if ( duration < 0 ) - suppress_prefetch = 1; - duration = LP_ORDERBOOK_DURATION; - } - //LP_pubkeys_query(); - baseid = basepp->ind; - relid = relpp->ind; - now = (uint32_t)time(NULL); - basecoin->obooktime = now; - relcoin->obooktime = now; - cachenumbids = numbids, cachenumasks = numasks; - //printf("start cache.(%d %d) numbids.%d numasks.%d\n",cachenumbids,cachenumasks,numbids,numasks); - numasks = LP_orderbook_utxoentries(now,1,base,rel,&asks,numasks,cachenumasks,duration); - numbids = LP_orderbook_utxoentries(now,-1,rel,base,&bids,numbids,cachenumbids,duration); - retjson = cJSON_CreateObject(); - array = cJSON_CreateArray(); - if ( numbids > 1 ) - { - qsort(bids,numbids,sizeof(*bids),_revcmp_orderbook); - depth = 0; - for (i=0; idepth; - bids[i]->depth = depth; - } - } - if ( numasks > 1 ) - { - qsort(asks,numasks,sizeof(*asks),_cmp_orderbook); - depth = 0; - for (i=0; idepth; - asks[i]->depth = depth; - } - } - for (i=n=0; idepth; - jaddi(array,LP_orderbookjson(rel,bids[i])); - if ( suppress_prefetch == 0 && n < 3 && bids[i]->numutxos == 0 ) - { - //printf("bid ping %s %s\n",rel,bids[i]->coinaddr); - LP_address(relcoin,bids[i]->coinaddr); - /*if ( 0 && relcoin->electrum == 0 ) - { - LP_listunspent_issue(rel,bids[i]->coinaddr,0); - //else if ( (tmpjson= LP_listunspent(rel,bids[i]->coinaddr)) != 0 ) - // free_json(tmpjson); - LP_listunspent_query(rel,bids[i]->coinaddr); - }*/ - n++; - } - if ( i == 0 ) - { - LP_priceinfoupdate(rel,base,1. / bids[i]->price); - //printf("update %s/%s %.8f [%.8f]\n",rel,base,1./bids[i]->price,bids[i]->price); - } - free(bids[i]); - bids[i] = 0; - } - if ( n > 0 && relcoin->lastmonitor > 3600 ) - relcoin->lastmonitor -= 3600; - jadd(retjson,"bids",array); - jaddnum(retjson,"numbids",numbids); - jaddnum(retjson,"biddepth",dstr(biddepth)); - array = cJSON_CreateArray(); - for (i=n=0; idepth; - jaddi(array,LP_orderbookjson(base,asks[i])); - if ( suppress_prefetch == 0 && n < 3 && asks[i]->numutxos == 0 ) - { - //printf("ask ping %s %s\n",base,asks[i]->coinaddr); - LP_address(basecoin,asks[i]->coinaddr); - /*if ( 0 && basecoin->electrum == 0 ) - { - LP_listunspent_issue(base,asks[i]->coinaddr,0); - //else if ( (tmpjson= LP_listunspent(base,asks[i]->coinaddr)) != 0 ) - // free_json(tmpjson); - LP_listunspent_query(base,asks[i]->coinaddr); - }*/ - n++; - } - if ( i == 0 ) - { - LP_priceinfoupdate(base,rel,asks[i]->price); - //printf("update %s/%s %.8f [%.8f]\n",base,rel,asks[i]->price,1./asks[i]->price); - } - free(asks[i]); - asks[i] = 0; - } - if ( n > 0 && basecoin->lastmonitor > 3600 ) - basecoin->lastmonitor -= 3600; - jadd(retjson,"asks",array); - jaddnum(retjson,"numasks",numasks); - jaddnum(retjson,"askdepth",dstr(askdepth)); - jaddstr(retjson,"base",base); - jaddstr(retjson,"rel",rel); - jaddnum(retjson,"timestamp",now); - jaddnum(retjson,"netid",G.netid); - if ( bids != 0 ) - free(bids); - if ( asks != 0 ) - free(asks); - return(jprint(retjson,1)); -} - -double LP_fomoprice(char *base,char *rel,double *relvolumep) -{ - char *retstr; cJSON *retjson,*asks,*item; int32_t i,numasks; double maxvol=0.,relvolume,biggest,price,fomoprice = 0.; - relvolume = *relvolumep; - if ( (retstr= LP_orderbook(base,rel,0)) != 0 ) - { - if ( (retjson= cJSON_Parse(retstr)) != 0 ) - { - if ( (asks= jarray(&numasks,retjson,"asks")) != 0 && numasks > 0 ) - { - for (i=0; i maxvol ) - { - maxvol = biggest; - fomoprice = price; - } - printf("fomoprice (%.8f) i.%d %.8f vol %.8f [max %.8f @ %.8f]\n",relvolume,i,price,biggest,maxvol,fomoprice); - } - } - free_json(retjson); - } - free(retstr); - } - if ( maxvol > 0. && fomoprice > 0. ) - { - if ( maxvol < relvolume ) - relvolume = maxvol * 0.98; - fomoprice /= 0.95; - } else fomoprice = 0.; - *relvolumep = relvolume; - return(fomoprice); -} - -int64_t LP_KMDvalue(struct iguana_info *coin,int64_t balance) -{ - double price = 0.; int64_t KMDvalue=0; - if ( balance != 0 ) - { - if ( strcmp(coin->symbol,"KMD") == 0 ) - KMDvalue = balance; - else - { - if ( (price= LP_price(1,coin->symbol,"KMD")) > SMALLVAL ) - KMDvalue = price * balance; - } - } - return(KMDvalue); -} - -int64_t LP_kmdvalue(char *symbol,int64_t satoshis) -{ - struct iguana_info *coin; int64_t kmdvalue = 0; - if ( (coin= LP_coinfind(symbol)) != 0 ) - kmdvalue = LP_KMDvalue(coin,satoshis); - if ( kmdvalue == 0 ) - kmdvalue = satoshis; - return(kmdvalue); -} - -void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32]) -{ - LP_priceinfoupdate(base,rel,price); -} - -void LP_pricefname(char *fname,char *base,char *rel) -{ - sprintf(fname,"%s/PRICES/%s_%s",GLOBAL_DBDIR,base,rel); - OS_compatible_path(fname); -} - -void LP_priceitemadd(cJSON *retarray,uint32_t timestamp,double avebid,double aveask,double highbid,double lowask) -{ - cJSON *item = cJSON_CreateArray(); - jaddinum(item,timestamp); - jaddinum(item,avebid); - jaddinum(item,aveask); - jaddinum(item,highbid); - jaddinum(item,lowask); - jaddi(retarray,item); -} - -cJSON *LP_pricearray(char *base,char *rel,uint32_t firsttime,uint32_t lasttime,int32_t timescale) -{ - cJSON *retarray; char askfname[1024],bidfname[1024]; int64_t bidprice64,askprice64; uint32_t bidnow,asknow,bidi,aski,lastbidi,lastaski; int32_t numbids,numasks; double bidemit,askemit,bidsum,asksum,bid,ask,highbid,lowbid,highask,lowask,bidemit2,askemit2; FILE *askfp=0,*bidfp=0; - if ( timescale <= 0 ) - timescale = 60; - if ( lasttime == 0 ) - lasttime = (uint32_t)-1; - LP_pricefname(askfname,base,rel); - LP_pricefname(bidfname,rel,base); - retarray = cJSON_CreateArray(); - lastbidi = lastaski = 0; - numbids = numasks = 0; - bidsum = asksum = askemit = bidemit = highbid = lowbid = highask = lowask = 0.; - if ( (bidfp= fopen(bidfname,"rb")) != 0 && (askfp= fopen(askfname,"rb")) != 0 ) - { - while ( bidfp != 0 || askfp != 0 ) - { - bidi = aski = 0; - bidemit = askemit = bidemit2 = askemit2 = 0.; - if ( bidfp != 0 && fread(&bidnow,1,sizeof(bidnow),bidfp) == sizeof(bidnow) && fread(&bidprice64,1,sizeof(bidprice64),bidfp) == sizeof(bidprice64) ) - { - //printf("bidnow.%u %.8f\n",bidnow,dstr(bidprice64)); - if ( bidnow != 0 && bidprice64 != 0 && bidnow >= firsttime && bidnow <= lasttime ) - { - bidi = bidnow / timescale; - if ( bidi != lastbidi ) - { - if ( bidsum != 0. && numbids != 0 ) - { - bidemit = bidsum / numbids; - bidemit2 = highbid; - } - bidsum = highbid = lowbid = 0.; - numbids = 0; - } - if ( (bid= 1. / dstr(bidprice64)) != 0. ) - { - if ( bid > highbid ) - highbid = bid; - if ( lowbid == 0. || bid < lowbid ) - lowbid = bid; - bidsum += bid; - numbids++; - //printf("bidi.%u num.%d %.8f [%.8f %.8f]\n",bidi,numbids,bid,lowbid,highbid); - } - } - } else fclose(bidfp), bidfp = 0; - if ( askfp != 0 && fread(&asknow,1,sizeof(asknow),askfp) == sizeof(asknow) && fread(&askprice64,1,sizeof(askprice64),askfp) == sizeof(askprice64) ) - { - //printf("asknow.%u %.8f\n",asknow,dstr(askprice64)); - if ( asknow != 0 && askprice64 != 0 && asknow >= firsttime && asknow <= lasttime ) - { - aski = asknow / timescale; - if ( aski != lastaski ) - { - if ( asksum != 0. && numasks != 0 ) - { - askemit = asksum / numasks; - askemit2 = lowask; - } - asksum = highask = lowask = 0.; - numasks = 0; - } - if ( (ask= dstr(askprice64)) != 0. ) - { - if ( ask > highask ) - highask = ask; - if ( lowask == 0. || ask < lowask ) - lowask = ask; - asksum += ask; - numasks++; - //printf("aski.%u num.%d %.8f [%.8f %.8f]\n",aski,numasks,ask,lowask,highask); - } - } - } else fclose(askfp), askfp = 0; - if ( bidemit != 0. || askemit != 0. ) - { - if ( bidemit != 0. && askemit != 0. && lastbidi == lastaski ) - { - LP_priceitemadd(retarray,lastbidi * timescale,bidemit,askemit,bidemit2,askemit2); - highbid = lowbid = highask = lowask = 0.; - } - else - { - if ( bidemit != 0. ) - { - printf("bidonly %.8f %.8f\n",bidemit,highbid); - LP_priceitemadd(retarray,lastbidi * timescale,bidemit,0.,bidemit2,0.); - highbid = lowbid = 0.; - } - if ( askemit != 0. ) - { - printf("askonly %.8f %.8f\n",askemit,lowask); - LP_priceitemadd(retarray,lastaski * timescale,0.,askemit,0.,askemit2); - highask = lowask = 0.; - } - } - } - if ( bidi != 0 ) - lastbidi = bidi; - if ( aski != 0 ) - lastaski = aski; - } - } else printf("couldnt open either %s %p or %s %p\n",bidfname,bidfp,askfname,askfp); - if ( bidfp != 0 ) - fclose(bidfp); - if ( askfp != 0 ) - fclose(askfp); - return(retarray); -} - -void LP_pricefeedupdate(bits256 pubkey,char *base,char *rel,double price,char *utxocoin,int32_t numrelutxos,int64_t balance,int64_t minutxo,int64_t maxutxo,int64_t unconfcredits) -{ - struct LP_priceinfo *basepp,*relpp; uint32_t now; int64_t price64; struct LP_pubkey_info *pubp; char str[65],fname[512]; FILE *fp; -//printf("check PRICEFEED UPDATE.(%s/%s) %.8f %s balance %.8f min %.8f max %.8f\n",base,rel,price,bits256_str(str,pubkey),dstr(balance),dstr(minutxo),dstr(maxutxo)); - if ( LP_pricevalid(price) > 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 ) - { - //if ( (fp= basepp->fps[relpp->ind]) == 0 ) - { - LP_pricefname(fname,base,rel); - fp = OS_appendfile(fname); //basepp->fps[relpp->ind] = - } - if ( fp != 0 ) - { - now = (uint32_t)time(NULL); - price64 = price * SATOSHIDEN; - fwrite(&now,1,sizeof(now),fp); - fwrite(&price64,1,sizeof(price64),fp); - fclose(fp); - } - //if ( (fp= relpp->fps[basepp->ind]) == 0 ) - { - sprintf(fname,"%s/PRICES/%s_%s",GLOBAL_DBDIR,rel,base); - fp = OS_appendfile(fname); //relpp->fps[basepp->ind] = - } - if ( fp != 0 ) - { - now = (uint32_t)time(NULL); - price64 = (1. / price) * SATOSHIDEN; - fwrite(&now,1,sizeof(now),fp); - fwrite(&price64,1,sizeof(price64),fp); - fclose(fp); - } - if ( (pubp= LP_pubkeyadd(pubkey)) != 0 ) - { - if ( (LP_rand() % 1000) == 0 ) - printf("PRICEFEED UPDATE.(%-6s/%6s) %12.8f %s %12.8f\n",base,rel,price,bits256_str(str,pubkey),1./price); - if ( unconfcredits > pubp->unconfcredits ) - pubp->unconfcredits = unconfcredits; - pubp->timestamp = (uint32_t)time(NULL); - LP_pubkey_update(pubp,basepp->ind,relpp->ind,price,balance,utxocoin,numrelutxos,minutxo,maxutxo); - //pubp->depthinfo[basepp->ind][relpp->ind] = LP_depthinfo_compact(); - //if ( fabs(pubp->matrix[basepp->ind][relpp->ind] - price) > SMALLVAL ) - { - //pubp->matrix[basepp->ind][relpp->ind] = price; - //pubp->timestamps[basepp->ind][relpp->ind] = pubp->timestamp; - dxblend(&basepp->relvals[relpp->ind],price,0.9); - dxblend(&relpp->relvals[basepp->ind],1. / price,0.9); - } - } else printf("error finding pubkey entry %s, ok if rare\n",bits256_str(str,pubkey)); - } - //else if ( (rand() % 100) == 0 ) - // printf("error finding %s/%s %.8f\n",base,rel,price); -} - -double LP_CMCbtcprice(double *price_usdp,char *symbol) -{ - char *retstr; cJSON *ticker,*item; double price_btc = 0.; - *price_usdp = 0.; - if ( (retstr= cmc_ticker(symbol)) != 0 ) - { - if ( (ticker= cJSON_Parse(retstr)) != 0 ) - { - item = jitem(ticker,0); - price_btc = jdouble(item,"price_btc"); - *price_usdp = jdouble(item,"price_usd"); -//printf("%.8f item.(%s)\n",price_btc,jprint(item,0)); - free_json(ticker); - } - free(retstr); - } - return(price_btc); -} - -cJSON *LP_fundvalue(cJSON *argjson) -{ - cJSON *holdings,*item,*newitem,*array,*retjson; int32_t i,iter,n,missing=0; double usdprice,divisor,btcprice,balance,btcsum,KMDholdings,numKMD; struct iguana_info *coin; char *symbol,*coinaddr; int64_t fundvalue,KMDvalue = 0; - fundvalue = 0; - KMDholdings = btcsum = 0.; - array = cJSON_CreateArray(); - for (iter=0; iter<2; iter++) - { - if ( iter == 0 ) - holdings = jarray(&n,argjson,"holdings"); - else - { - if ( (coinaddr= jstr(argjson,"address")) != 0 ) - { - holdings = LP_balances(coinaddr); - n = cJSON_GetArraySize(holdings); - } else break; - } - if ( holdings != 0 ) - { - for (i=0; i SMALLVAL ) - { - newitem = cJSON_CreateObject(); - jaddstr(newitem,"coin",symbol); - jaddnum(newitem,"balance",balance); - if ( (coin= LP_coinfind(symbol)) != 0 && (KMDvalue= LP_KMDvalue(coin,SATOSHIDEN * balance)) > 0 ) - { - jaddnum(newitem,"KMD",dstr(KMDvalue)); - fundvalue += KMDvalue; - if ( strcmp(symbol,"KMD") == 0 ) - KMDholdings += dstr(KMDvalue); - } - else if ( iter == 0 && (btcprice= LP_CMCbtcprice(&usdprice,symbol)) > SMALLVAL ) - { - btcsum += btcprice * balance; - jaddnum(newitem,"BTC",btcprice * balance); - } - else jaddstr(newitem,"error","no price source"); - jaddi(array,newitem); - } else missing++; - } - } - } - retjson = cJSON_CreateObject(); - jaddstr(retjson,"result","success"); - jaddnum(retjson,"missing",missing); - jadd(retjson,"holdings",array); - btcprice = LP_CMCbtcprice(&usdprice,"komodo"); - divisor = jdouble(argjson,"divisor"); - jaddnum(retjson,"KMDholdings",KMDholdings); - if ( btcsum != 0 ) - { - if ( btcprice > SMALLVAL ) - { - numKMD = (btcsum / btcprice); - fundvalue += numKMD * SATOSHIDEN; - jaddnum(retjson,"KMD_BTC",btcprice); - jaddnum(retjson,"btcsum",btcsum); - numKMD += KMDholdings; - jaddnum(retjson,"btc2kmd",numKMD); - if ( divisor != 0 ) - { - jaddnum(retjson,"NAV_KMD",numKMD/divisor); - jaddnum(retjson,"NAV_BTC",(btcsum + (KMDholdings * btcprice))/divisor); - jaddnum(retjson,"NAV_USD",(usdprice * numKMD)/divisor); - } - } - } - jaddnum(retjson,"fundvalue",dstr(fundvalue)); - if ( divisor != 0 ) - { - jaddnum(retjson,"divisor",divisor); - numKMD = dstr(fundvalue); - jaddnum(retjson,"assetNAV_KMD",numKMD/divisor); - jaddnum(retjson,"assetNAV_BTC",(btcprice * numKMD)/divisor); - jaddnum(retjson,"assetNAV_USD",(usdprice * numKMD)/divisor); - } - return(retjson); -} - From b09cdf6891ffd30eeae09fa7da73403edb7e4570 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 22:47:33 -1100 Subject: [PATCH 18/60] Fix --- iguana/exchanges/LP_mpnet.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 457a3f30b..bba19b3bf 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -32,6 +32,7 @@ cJSON *LP_mpnet_get() void LP_mpnet_check(void *ctx,char *myipaddr,int32_t pubsock) { + cJSON *argjson; while ( (argjson= LP_mpnet_get()) != 0 ) { LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0); From 32c902403c8fa63636713cc2822d479c5ba9e392 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 23:20:41 -1100 Subject: [PATCH 19/60] Gtc persistence --- iguana/exchanges/LP_include.h | 1 + iguana/exchanges/LP_mpnet.c | 51 ++++++++++++++++++++++++++++++-- iguana/exchanges/LP_nativeDEX.c | 1 + iguana/exchanges/LP_ordermatch.c | 37 +++++++++++++---------- iguana/exchanges/LP_signatures.c | 9 ++++-- iguana/exchanges/mm.c | 6 ++++ 6 files changed, 84 insertions(+), 21 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 4cbf9a89f..11ef6e96a 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -378,6 +378,7 @@ struct LP_quoteinfo { struct basilisk_request R; bits256 srchash,desthash,txid,txid2,desttxid,feetxid,privkey; + double maxprice; int64_t othercredits; uint64_t satoshis,txfee,destsatoshis,desttxfee,aliceid; uint32_t timestamp,quotetime,tradeid,gtc,fill,mpnet; diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index bba19b3bf..53673ebea 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -19,10 +19,57 @@ // int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); +int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson); -void LP_mpnet_send(char *msg) +int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp) { - + uint64_t destvalue,destvalue2; + if ( LP_iseligible(&destvalue,&destvalue2,0,qp->destcoin,qp->desttxid,qp->destvout,qp->destsatoshis,qp->feetxid,qp->feevout) > 0 ) + { + LP_gtc_addorder(qp); + return(0); + } + return(-1); +} + +void LP_mpnet_init() +{ + char fname[1024],line[8192]; FILE *fp; struct LP_quoteinfo Q; + sprintf(fname,"%s/GTC/orders",GLOBAL_DBDIR), OS_compatible_path(fname); + if ( (fp= fopen(fname,"rb+")) != 0 ) + { + while ( fgets(line,sizeof(line),fp) > 0 ) + { + if ( (argjson= cJSON_Parse(line)) != 0 ) + { + if ( LP_quoteparse(&Q,argjson) == 0 ) + { + if ( LP_mpnet_addorder(&Q) == 0 ) + printf("GTC %s",line); + } + free_json(argjson); + } + } + fclose(fp); + } +} + +void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag) +{ + char fname[1024]; FILE *fp; + if ( localcopy != 0 ) + { + sprintf(fname,"%s/GTC/orders",GLOBAL_DBDIR), OS_compatible_path(fname); + if ( (fp= fopen(fname,"rb+")) == 0 ) + fp = fopen(fname,"wb+"); + else fseek(fp,0,SEEK_END); + fprintf(fp,"%s\n",msg); + fclose(fp); + } + if ( G.mpnet != 0 && sendflag != 0 ) + { + + } } cJSON *LP_mpnet_get() diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 5ddc56f45..5aa2749e3 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -1649,6 +1649,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu int32_t nonz,didremote=0; LP_statslog_parse(); bitcoind_RPC_inittime = 0; + LP_mpnet_init(); while ( LP_STOP_RECEIVED == 0 ) { nonz = 0; diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 6ce1b93d9..53c29910e 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -23,7 +23,6 @@ struct LP_gtcorder { struct LP_gtcorder *next,*prev; struct LP_quoteinfo Q; - double maxprice; uint32_t cancelled,pending; } *GTCorders; @@ -551,7 +550,7 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double if ( qp->mpnet != 0 && qp->fill != 0 && qp->gtc != 0 ) { char *msg = jprint(reqjson,0); - LP_mpnet_send(msg); + LP_mpnet_send(0,msg,1); free(msg); } free_json(reqjson); @@ -616,14 +615,25 @@ void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock) { gtc->pending = qp->timestamp = (uint32_t)time(NULL); LP_query(ctx,myipaddr,mypubsock,"request",qp); - LP_Alicequery = *qp, LP_Alicemaxprice = gtc->maxprice, Alice_expiration = qp->timestamp + 2*LP_AUTOTRADE_TIMEOUT, LP_Alicedestpubkey = qp->srchash; - char str[65]; printf("LP_gtc fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),gtc->maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); + LP_Alicequery = *qp, LP_Alicemaxprice = gtc->Q.maxprice, Alice_expiration = qp->timestamp + 2*LP_AUTOTRADE_TIMEOUT, LP_Alicedestpubkey = qp->srchash; + char str[65]; printf("LP_gtc fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),gtc->Q.maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); break; } } } } +void LP_gtc_addorder(struct LP_quoteinfo *qp) +{ + struct LP_gtcorder *gtc; + gtc = calloc(1,sizeof(*gtc)); + gtc->Q = *qp; + gtc->pending = (uint32_t)time(NULL); + portable_mutex_lock(&LP_gtcmutex); + DL_APPEND(GTCorders,gtc); + portable_mutex_unlock(&LP_gtcmutex); +} + char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *qp,double maxprice,int32_t timeout,int32_t duration,uint32_t tradeid,bits256 destpubkey,char *uuidstr) { struct LP_gtcorder *gtc; @@ -634,30 +644,25 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q qp->tradeid = LP_rand(); qp->srchash = destpubkey; strncpy(qp->uuidstr,uuidstr,sizeof(qp->uuidstr)-1); + qp->maxprice = maxprice; qp->timestamp = (uint32_t)time(NULL); if ( qp->gtc != 0 ) { strcpy(&qp->uuidstr[strlen(qp->uuidstr)-6],"cccccc"); - gtc = calloc(1,sizeof(*gtc)); - gtc->Q = *qp; - gtc->maxprice = maxprice; - gtc->pending = (uint32_t)time(NULL); - portable_mutex_lock(&LP_gtcmutex); - DL_APPEND(GTCorders,gtc); - portable_mutex_unlock(&LP_gtcmutex); + LP_gtc_addorder(qp); } { LP_query(ctx,myipaddr,mypubsock,"request",qp); - LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash; + LP_Alicequery = *qp, LP_Alicemaxprice = qp->maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash; } - if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) + if ( qp->gtc != 0 ) { cJSON *reqjson = LP_quotejson(qp); char *msg = jprint(reqjson,1); - LP_mpnet_send(msg); + LP_mpnet_send(1,msg,qp->fill); free(msg); } - char str[65]; printf("LP_trade mpnet.%d fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->mpnet,qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); + char str[65]; printf("LP_trade mpnet.%d fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->mpnet,qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),qp->maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); return(LP_recent_swaps(0,uuidstr)); } @@ -1203,7 +1208,7 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) { char *msg = jprint(reqjson,0); - LP_mpnet_send(msg); + LP_mpnet_send(0,msg,1); free(msg); } free_json(reqjson); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index 079dd43fe..d639ec0ad 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -45,6 +45,8 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp) if ( jobj(retjson,"gui") == 0 ) jaddstr(retjson,"gui",qp->gui[0] != 0 ? qp->gui : LP_gui); jaddstr(retjson,"uuid",qp->uuidstr); + if ( qp->maxprice != 0 ) + jaddnum(retjson,"maxprice",qp->maxprice); if ( qp->mpnet != 0 ) jaddnum(retjson,"mpnet",qp->mpnet); if ( qp->gtc != 0 ) @@ -119,6 +121,7 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) { uint32_t rid,qid; char etomic[64],activesymbol[65],*etomicstr; memset(qp,0,sizeof(*qp)); + qp->maxprice = jdouble(argjson,"maxprice"); qp->mpnet = juint(argjson,"mpnet"); qp->gtc = juint(argjson,"gtc"); qp->fill = juint(argjson,"fill"); @@ -161,9 +164,9 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) qp->destvout = jint(argjson,"destvout"); qp->desthash = jbits256(argjson,"desthash"); qp->txfee = j64bits(argjson,"txfee"); - if ( (qp->satoshis= j64bits(argjson,"satoshis")) > qp->txfee ) + if ( (qp->satoshis= j64bits(argjson,"satoshis")) > qp->txfee && fabs(qp->maxprice) < SMALLVAL ) { - //qp->price = (double)qp->destsatoshis / (qp->satoshis = qp->txfee); + qp->maxprice = (double)qp->destsatoshis / (qp->satoshis - qp->txfee); } qp->destsatoshis = j64bits(argjson,"destsatoshis"); qp->desttxfee = j64bits(argjson,"desttxfee"); @@ -739,7 +742,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ } } if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) - LP_mpnet_send(msg); + LP_mpnet_send(0,msg,1); free(msg); } diff --git a/iguana/exchanges/mm.c b/iguana/exchanges/mm.c index aa0866b07..5e17513d3 100644 --- a/iguana/exchanges/mm.c +++ b/iguana/exchanges/mm.c @@ -346,6 +346,12 @@ int main(int argc, const char * argv[]) printf("couldnt write to (%s)\n",dirname); exit(0); } + sprintf(dirname,"%s/GTC",GLOBAL_DBDIR), OS_ensure_directory(dirname); + if ( ensure_writable(dirname) < 0 ) + { + printf("couldnt write to (%s)\n",dirname); + exit(0); + } sprintf(dirname,"%s/PRICES",GLOBAL_DBDIR), OS_ensure_directory(dirname); if ( ensure_writable(dirname) < 0 ) { From a41c0e7dd2c2cae8700033f4b68dccc0be5972ae Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 23:25:45 -1100 Subject: [PATCH 20/60] Fix --- iguana/exchanges/LP_mpnet.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 53673ebea..3961ae0dd 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -20,6 +20,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson); +int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp); int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp) { @@ -34,7 +35,7 @@ int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp) void LP_mpnet_init() { - char fname[1024],line[8192]; FILE *fp; struct LP_quoteinfo Q; + char fname[1024],line[8192]; FILE *fp; struct LP_quoteinfo Q; cJSON *argjson; sprintf(fname,"%s/GTC/orders",GLOBAL_DBDIR), OS_compatible_path(fname); if ( (fp= fopen(fname,"rb+")) != 0 ) { From 314d74562dbf8af768bceca8b5a5523b28e8ff55 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 24 Jun 2018 23:26:43 -1100 Subject: [PATCH 21/60] Fix --- iguana/exchanges/LP_mpnet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 3961ae0dd..0daf5d71a 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -20,7 +20,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson); -int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp); +void LP_gtc_addorder(struct LP_quoteinfo *qp); int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp) { From c097bdc034b3bd1500e84c03ca8684c906a04d67 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 00:28:46 -1100 Subject: [PATCH 22/60] Seems better to disable persistent GTC --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 5aa2749e3..9f249c6a3 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -1649,7 +1649,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu int32_t nonz,didremote=0; LP_statslog_parse(); bitcoind_RPC_inittime = 0; - LP_mpnet_init(); + //LP_mpnet_init(); seems better to have the GUI send in persistent orders, exit mm is a cancel all while ( LP_STOP_RECEIVED == 0 ) { nonz = 0; From a83946386cb967024dff56bca1a038b889b66c28 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 02:27:11 -1100 Subject: [PATCH 23/60] Test --- iguana/exchanges/LP_mpnet.c | 41 ++++++++++++++++++++++++++++----- iguana/exchanges/LP_nativeDEX.c | 5 ++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 0daf5d71a..a2d965e9d 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -33,7 +33,7 @@ int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp) return(-1); } -void LP_mpnet_init() +void LP_mpnet_init() // problem is coins not enabled yet { char fname[1024],line[8192]; FILE *fp; struct LP_quoteinfo Q; cJSON *argjson; sprintf(fname,"%s/GTC/orders",GLOBAL_DBDIR), OS_compatible_path(fname); @@ -55,9 +55,9 @@ void LP_mpnet_init() } } -void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag) +void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag,char *otheraddr) { - char fname[1024]; FILE *fp; + char fname[1024]; int32_t len; FILE *fp; char *hexstr,*retstr; cJSON *argjson,*outputs,*item; struct iguana_info *coin; uint8_t linebuf[8192]; if ( localcopy != 0 ) { sprintf(fname,"%s/GTC/orders",GLOBAL_DBDIR), OS_compatible_path(fname); @@ -67,10 +67,39 @@ void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag) fprintf(fp,"%s\n",msg); fclose(fp); } - if ( G.mpnet != 0 && sendflag != 0 ) + if ( G.mpnet != 0 && sendflag != 0 && (coin= LP_coinfind("CHIPS")) != 0 && coin->inactive == 0 ) { - - } + len = MMJSON_encode(linebuf,msg); + //curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"withdraw\",\"coin\":\"CHIPS\",\"outputs\":[{\"RHV2As4rox97BuE3LK96vMeNY8VsGRTmBj\":0.0001}],\"opreturn\":\"deadbeef\"}" + if ( len > 0 ) + { + argjson = cJSON_CreateObject(); + outputs = cJSON_CreateArray(); + if ( otheraddr != 0 && otheraddr[0] != 0 ) + { + item = cJSON_CreateObject(); + jaddnum(item,otheraddr,dstr(10000)); + jaddi(outputs,item); + } + item = cJSON_CreateObject(); + jaddnum(item,coin->smartaddr,dstr(10000)); + jaddi(outputs,item); + jadd(argjson,"outputs",outputs); + jaddnum(argjson,"broadcast",1); + jaddstr(argjson,"coin",coin->symbol); + hexstr = calloc(1,len*2 + 1); + init_hexbytes_noT(hexstr,linebuf,len); + jaddstr(argjson,"opreturn",hexstr); + free(hexstr); + retstr = LP_withdraw(coin,argjson); + free_json(argjson); + if ( retstr != 0 ) + { + printf("mpnet.%s\n",retstr); + free(retstr); + } + } + } } cJSON *LP_mpnet_get() diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 9f249c6a3..b6e424a97 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -1649,6 +1649,11 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu int32_t nonz,didremote=0; LP_statslog_parse(); bitcoind_RPC_inittime = 0; + if ( strcmp(G.USERPASS,"9894cc3a6f94afa8871d3633dce61a4444d48d670bacb229fb607064654ae81f") == 0 ) + { + G.mpnet = 1; + printf("MPNET active\n"); + } //LP_mpnet_init(); seems better to have the GUI send in persistent orders, exit mm is a cancel all while ( LP_STOP_RECEIVED == 0 ) { From 61ee39ca0604b1460beb79e920f44087ee6e32dd Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 02:28:03 -1100 Subject: [PATCH 24/60] Fix --- iguana/exchanges/LP_ordermatch.c | 6 +++--- iguana/exchanges/LP_signatures.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 53c29910e..bd10afc4a 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -550,7 +550,7 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double if ( qp->mpnet != 0 && qp->fill != 0 && qp->gtc != 0 ) { char *msg = jprint(reqjson,0); - LP_mpnet_send(0,msg,1); + LP_mpnet_send(0,msg,1,0); free(msg); } free_json(reqjson); @@ -659,7 +659,7 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q { cJSON *reqjson = LP_quotejson(qp); char *msg = jprint(reqjson,1); - LP_mpnet_send(1,msg,qp->fill); + LP_mpnet_send(1,msg,qp->fill,0); free(msg); } char str[65]; printf("LP_trade mpnet.%d fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->mpnet,qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),qp->maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); @@ -1208,7 +1208,7 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) { char *msg = jprint(reqjson,0); - LP_mpnet_send(0,msg,1); + LP_mpnet_send(0,msg,1,0); free(msg); } free_json(reqjson); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index d639ec0ad..5746df346 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -742,7 +742,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ } } if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) - LP_mpnet_send(0,msg,1); + LP_mpnet_send(0,msg,1,0); free(msg); } From 41e60201c38f2db2b4f666acb67b5fa0769a7fc6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 02:33:29 -1100 Subject: [PATCH 25/60] Make order book not need enabled --- iguana/exchanges/LP_commands.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 38b95aa25..badc9e223 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -507,6 +507,8 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ jaddnum(retjson,"price",price); return(jprint(retjson,1)); } + else if ( strcmp(method,"orderbook") == 0 ) + return(LP_orderbook(base,rel,jint(argjson,"duration"))); if ( IAMLP == 0 && LP_isdisabled(base,rel) != 0 ) return(clonestr("{\"error\":\"at least one of coins disabled\"}")); price = jdouble(argjson,"price"); @@ -520,8 +522,6 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ return(LP_pricepings(ctx,myipaddr,LP_mypubsock,base,rel,price * LP_profitratio)); else return(clonestr("{\"result\":\"success\"}")); } - else if ( strcmp(method,"orderbook") == 0 ) - return(LP_orderbook(base,rel,jint(argjson,"duration"))); else if ( strcmp(method,"myprice") == 0 ) { if ( LP_myprice(1,&bid,&ask,base,rel) > SMALLVAL ) From 6f767138587aeda76e3aa80cdcf3f4a8ee3fe055 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 02:34:04 -1100 Subject: [PATCH 26/60] Fix --- iguana/exchanges/LP_mpnet.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index a2d965e9d..37bf9ba39 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -21,6 +21,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson); void LP_gtc_addorder(struct LP_quoteinfo *qp); +char *LP_withdraw(struct iguana_info *coin,cJSON *argjson); int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp) { From 3933a4dc1d65e2822dc2dc5d40962b7bebc5854d Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 02:55:54 -1100 Subject: [PATCH 27/60] Suppress mpnet on request --- iguana/exchanges/LP_mpnet.c | 7 ++++++- iguana/exchanges/LP_signatures.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 37bf9ba39..a0d53b290 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -96,13 +96,18 @@ void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag,char *otheraddr) free_json(argjson); if ( retstr != 0 ) { - printf("mpnet.%s\n",retstr); + //printf("mpnet.%s\n",retstr); free(retstr); } } } } +// 2151978 +// 404bc4ac452db07ed16376b3d7e77dbfc22b4a68f7243797125bd0d3bdddf8d1 +// 893b46634456034a6d5d73b67026aa157b5e2addbfc6344dfbea6bae85f7dde0 +// 717c7ef9de8504bd331f3ef52ed0a16ea0e070434e12cb4d63f5f081e999c43d dup + cJSON *LP_mpnet_get() { return(0); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index 5746df346..58713b7ff 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -741,7 +741,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg)); } } - if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) + if ( strcmp(method,"connect") == 0 && qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) LP_mpnet_send(0,msg,1,0); free(msg); } From c1723a0c5fafde5b344f4ffd954203f4a4704467 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 04:14:09 -1100 Subject: [PATCH 28/60] Initial mpnet --- iguana/exchanges/LP_mpnet.c | 103 ++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 5 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index a0d53b290..36f4a78a7 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -103,22 +103,115 @@ void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag,char *otheraddr) } } +cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid) +{ + cJSON *txobj,*argjson = 0; + + return(argjson); +} + // 2151978 // 404bc4ac452db07ed16376b3d7e77dbfc22b4a68f7243797125bd0d3bdddf8d1 // 893b46634456034a6d5d73b67026aa157b5e2addbfc6344dfbea6bae85f7dde0 // 717c7ef9de8504bd331f3ef52ed0a16ea0e070434e12cb4d63f5f081e999c43d dup -cJSON *LP_mpnet_get() +bits256 MPNET_txids[1024]; +int32_t num_MPNET_txids; + +int32_t LP_mpnet_find(bits256 txid) +{ + int32_t i; + for (i=0; i= 0 ) + { + MPNET_txids[i] = MPNET_txids[--num_MPNET_txids]; + return(i); + } + return(-1); +} + +void LP_mpnet_process(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info *coin,bits256 txid) { + if ( LP_mpnet_find(txid) < 0 ) + { + if ( (argjson= LP_mpnet_parse(coin,txid)) != 0 ) + { + printf("MPNET.(%s)\n",jprint(argjson,0)); + LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0); + free_json(argjson); + } + LP_mpnet_add(txid); + } +} + +cJSON *LP_mpnet_get(struct iguana_info *coin) +{ + static int32_t lastheight; static bits256 lasthash; + int32_t i,n=0,numtx,checkht = 0,height = 0; bits256 latesthash,zero; char hashstr[65],str[65]; cJSON *txs,*blockjson; + memset(zero.bytes,0,sizeof(zero)); + latesthash = LP_getbestblockhash(coin); + bits256_str(hashstr,latesthash); + if ( (blockjson= LP_blockjson(&checkht,coin->symbol,hashstr,0)) != 0 ) + { + hash = latesthash; + while ( bits256_cmp(lasthash,hash) != 0 && n++ < 3 ) + { + if ( (txs= jarray(&numtx,blockjson,"tx")) != 0 ) + { + for (i=0; isymbol,hashstr,0)) == 0 ) + break; + } + lasthash = latesthash; + if ( blockjson != 0 ) + free_json(blockjson); + if ( (txs= LP_getmempool(coin->symbol,coin->smartaddr,zero,zero)) != 0 ) + { + numtx = cJSON_GetArraySize(txs); + for (i=0; i Date: Mon, 25 Jun 2018 04:15:08 -1100 Subject: [PATCH 29/60] Fix --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index b6e424a97..2a3632575 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -172,11 +172,11 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ #include "LP_mmjson.c" #include "LP_socket.c" -#include "LP_mpnet.c" #include "LP_secp.c" #include "LP_bitcoin.c" #include "LP_coins.c" #include "LP_rpc.c" +#include "LP_mpnet.c" #include "LP_NXT.c" #include "LP_cache.c" #include "LP_RTmetrics.c" From 58258998e037a9a86f0ef01fe2756d6507c040b0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 04:16:41 -1100 Subject: [PATCH 30/60] Fix --- iguana/exchanges/LP_mpnet.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 36f4a78a7..7fc05bef3 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -140,6 +140,7 @@ int32_t LP_mpnet_add(bits256 txid) int32_t LP_mpnet_remove(bits256 txid) { + int32_t i; if ( (i= LP_mpnet_find(txid)) >= 0 ) { MPNET_txids[i] = MPNET_txids[--num_MPNET_txids]; @@ -150,6 +151,7 @@ int32_t LP_mpnet_remove(bits256 txid) void LP_mpnet_process(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info *coin,bits256 txid) { + cJSON *argjson; if ( LP_mpnet_find(txid) < 0 ) { if ( (argjson= LP_mpnet_parse(coin,txid)) != 0 ) @@ -162,10 +164,10 @@ void LP_mpnet_process(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_inf } } -cJSON *LP_mpnet_get(struct iguana_info *coin) +cJSON *LP_mpnet_get(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info *coin) { static int32_t lastheight; static bits256 lasthash; - int32_t i,n=0,numtx,checkht = 0,height = 0; bits256 latesthash,zero; char hashstr[65],str[65]; cJSON *txs,*blockjson; + int32_t i,n=0,numtx,checkht = 0,height = 0; bits256 latesthash,hash,txid,zero; char hashstr[65],str[65]; cJSON *txs,*blockjson; memset(zero.bytes,0,sizeof(zero)); latesthash = LP_getbestblockhash(coin); bits256_str(hashstr,latesthash); From ff0e782225c0dee6f934d57c7c7dc334cea2cc27 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 04:17:31 -1100 Subject: [PATCH 31/60] jbits256i --- iguana/exchanges/LP_mpnet.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 7fc05bef3..ec402ab80 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -180,7 +180,7 @@ cJSON *LP_mpnet_get(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info { for (i=0; i Date: Mon, 25 Jun 2018 04:23:20 -1100 Subject: [PATCH 32/60] Throttle speed, +print --- iguana/exchanges/LP_mpnet.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index ec402ab80..ea99ef6e1 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -151,9 +151,10 @@ int32_t LP_mpnet_remove(bits256 txid) void LP_mpnet_process(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info *coin,bits256 txid) { - cJSON *argjson; + cJSON *argjson; char str[65]; if ( LP_mpnet_find(txid) < 0 ) { + printf("unique %s\n",bits256_str(str,txid)); if ( (argjson= LP_mpnet_parse(coin,txid)) != 0 ) { printf("MPNET.(%s)\n",jprint(argjson,0)); @@ -183,7 +184,7 @@ cJSON *LP_mpnet_get(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info txid = jbits256i(txs,i); LP_mpnet_process(ctx,myipaddr,pubsock,coin,txid); LP_mpnet_remove(txid); - printf("ht.%d n.%d i.%d %s\n",checkht,n,i,bits256_str(str,txid)); + //printf("ht.%d n.%d i.%d %s\n",checkht,n,i,bits256_str(str,txid)); } } hash = jbits256(blockjson,"previousblockhash"); @@ -202,7 +203,7 @@ cJSON *LP_mpnet_get(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info { txid = jbits256i(txs,i); LP_mpnet_process(ctx,myipaddr,pubsock,coin,txid); - printf("mp i.%d %s\n",i,bits256_str(str,txid)); + //printf("mp i.%d %s\n",i,bits256_str(str,txid)); } } } @@ -211,9 +212,11 @@ cJSON *LP_mpnet_get(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info void LP_mpnet_check(void *ctx,char *myipaddr,int32_t pubsock) { + static uint32_t lasttime; struct iguana_info *coin = LP_coinfind("CHIPS"); - if ( coin != 0 ) + if ( coin != 0 && coin->inactive == 0 && time(NULL) > lasttime+5 ) { LP_mpnet_get(ctx,myipaddr,pubsock,coin); + lasttime = (uint32_t)time(NULL); } } From d61798d473807d78ccf87b746bddcf5bc9358c13 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 04:51:25 -1100 Subject: [PATCH 33/60] Decode I-return --- iguana/exchanges/LP_mpnet.c | 106 +++++++++++++++++++++++------------ iguana/exchanges/LP_socket.c | 2 +- 2 files changed, 71 insertions(+), 37 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index ea99ef6e1..a4d735e69 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -18,11 +18,45 @@ // marketmaker // +bits256 MPNET_txids[1024]; +int32_t num_MPNET_txids; + int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson); void LP_gtc_addorder(struct LP_quoteinfo *qp); char *LP_withdraw(struct iguana_info *coin,cJSON *argjson); +int32_t LP_mpnet_find(bits256 txid) +{ + int32_t i; + for (i=0; i= 0 ) + { + MPNET_txids[i] = MPNET_txids[--num_MPNET_txids]; + return(i); + } + return(-1); +} + int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp) { uint64_t destvalue,destvalue2; @@ -105,8 +139,42 @@ void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag,char *otheraddr) cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid) { - cJSON *txobj,*argjson = 0; - + cJSON *txobj,*vouts,*sobj,*argjson = 0; char *decodestr,*hexstr; uint8_t *buf,linebuf[8192]; int32_t len,n,hlen; + cJSON *LP_transaction_fromdata(struct iguana_info *coin,bits256 txid,uint8_t *serialized,int32_t len) + if ( (txobj= LP_gettx("mpnet",coin->symbol,txid,0)) != 0 ) + { + if ( (vouts= jarray(&n,txobj,"vout")) != 0 ) + { + if ( (sobj= jobject(jitem(vouts,n-1),"scriptPubKey")) != 0 && (hexstr= jstr(sobj,"hex")) != 0 && (hlen= strlen(hexstr)) < sizeof(linebuf)*2 ) + { + len = (hlen >> 1); + decode_hex(linebuf,len,hexstr); + buf = linebuf; + printf("hexstr.(%s)\n",hexstr); + if ( *buf == 0x6a ) + { + buf++, len--; + if ( *buf == 0x4d ) + { + buf++, len--; + n = buf[0] + buf[1]*256; + buf += 2, len -= 2; + printf("n.%d len.%d\n",n,len); + if ( n == len ) + { + if ( (decodestr= MMJSON_decode(buf,len)) != 0 ) + argjson = cJSON_Parse(decodestr); + } + } + } + if ( argjson == 0 ) + printf("unhandled case.(%s)\n",hexstr); + } + } + if ( argjson == 0 ) + printf("unhandled tx.(%s)\n",jprint(txobj,0)); + free_json(txobj); + } return(argjson); } @@ -115,40 +183,6 @@ cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid) // 893b46634456034a6d5d73b67026aa157b5e2addbfc6344dfbea6bae85f7dde0 // 717c7ef9de8504bd331f3ef52ed0a16ea0e070434e12cb4d63f5f081e999c43d dup -bits256 MPNET_txids[1024]; -int32_t num_MPNET_txids; - -int32_t LP_mpnet_find(bits256 txid) -{ - int32_t i; - for (i=0; i= 0 ) - { - MPNET_txids[i] = MPNET_txids[--num_MPNET_txids]; - return(i); - } - return(-1); -} - void LP_mpnet_process(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info *coin,bits256 txid) { cJSON *argjson; char str[65]; diff --git a/iguana/exchanges/LP_socket.c b/iguana/exchanges/LP_socket.c index e35947676..71162b67d 100644 --- a/iguana/exchanges/LP_socket.c +++ b/iguana/exchanges/LP_socket.c @@ -73,7 +73,7 @@ int32_t komodo_connect(int32_t sock,struct sockaddr *saddr,socklen_t addrlen) closesocket(sock); return(-1); } - printf("continue with select ...\n"); + //printf("continue with select ...\n"); FD_ZERO(&wfd); FD_SET(sock,&wfd); FD_ZERO(&efd); From 836bf7f9ed14142af93dbe81f79cbdd760178c1f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 04:52:55 -1100 Subject: [PATCH 34/60] Fix --- iguana/exchanges/LP_mpnet.c | 1 - 1 file changed, 1 deletion(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index a4d735e69..b0bf877f5 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -140,7 +140,6 @@ void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag,char *otheraddr) cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid) { cJSON *txobj,*vouts,*sobj,*argjson = 0; char *decodestr,*hexstr; uint8_t *buf,linebuf[8192]; int32_t len,n,hlen; - cJSON *LP_transaction_fromdata(struct iguana_info *coin,bits256 txid,uint8_t *serialized,int32_t len) if ( (txobj= LP_gettx("mpnet",coin->symbol,txid,0)) != 0 ) { if ( (vouts= jarray(&n,txobj,"vout")) != 0 ) From b593a827e3b7d08cce9fd364824a33d48b59b6fd Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 04:53:52 -1100 Subject: [PATCH 35/60] jobj --- iguana/exchanges/LP_mpnet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index b0bf877f5..3f15f9d1a 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -144,7 +144,7 @@ cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid) { if ( (vouts= jarray(&n,txobj,"vout")) != 0 ) { - if ( (sobj= jobject(jitem(vouts,n-1),"scriptPubKey")) != 0 && (hexstr= jstr(sobj,"hex")) != 0 && (hlen= strlen(hexstr)) < sizeof(linebuf)*2 ) + if ( (sobj= jobj(jitem(vouts,n-1),"scriptPubKey")) != 0 && (hexstr= jstr(sobj,"hex")) != 0 && (hlen= strlen(hexstr)) < sizeof(linebuf)*2 ) { len = (hlen >> 1); decode_hex(linebuf,len,hexstr); From 60035955deebb0f018a2a3ce809a9e10832cd3ad Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 05:00:43 -1100 Subject: [PATCH 36/60] -print and activate mpnet --- iguana/exchanges/LP_mpnet.c | 9 ++++----- iguana/exchanges/LP_nativeDEX.c | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index 3f15f9d1a..d401c84af 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -149,7 +149,7 @@ cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid) len = (hlen >> 1); decode_hex(linebuf,len,hexstr); buf = linebuf; - printf("hexstr.(%s)\n",hexstr); + //printf("hexstr.(%s)\n",hexstr); if ( *buf == 0x6a ) { buf++, len--; @@ -158,7 +158,6 @@ cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid) buf++, len--; n = buf[0] + buf[1]*256; buf += 2, len -= 2; - printf("n.%d len.%d\n",n,len); if ( n == len ) { if ( (decodestr= MMJSON_decode(buf,len)) != 0 ) @@ -166,11 +165,11 @@ cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid) } } } - if ( argjson == 0 ) + if ( 0 && argjson == 0 ) printf("unhandled case.(%s)\n",hexstr); } } - if ( argjson == 0 ) + if ( 0 && argjson == 0 ) printf("unhandled tx.(%s)\n",jprint(txobj,0)); free_json(txobj); } @@ -187,7 +186,7 @@ void LP_mpnet_process(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_inf cJSON *argjson; char str[65]; if ( LP_mpnet_find(txid) < 0 ) { - printf("unique %s\n",bits256_str(str,txid)); + //printf("unique %s\n",bits256_str(str,txid)); if ( (argjson= LP_mpnet_parse(coin,txid)) != 0 ) { printf("MPNET.(%s)\n",jprint(argjson,0)); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 2a3632575..bc59b65eb 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -1649,7 +1649,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu int32_t nonz,didremote=0; LP_statslog_parse(); bitcoind_RPC_inittime = 0; - if ( strcmp(G.USERPASS,"9894cc3a6f94afa8871d3633dce61a4444d48d670bacb229fb607064654ae81f") == 0 ) + //if ( strcmp(G.USERPASS,"9894cc3a6f94afa8871d3633dce61a4444d48d670bacb229fb607064654ae81f") == 0 ) { G.mpnet = 1; printf("MPNET active\n"); From ed3c7267f8b9920ff13c82050a760725c1dc85a6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 07:38:48 -1100 Subject: [PATCH 37/60] mpnet api, only for gtc:0 --- iguana/exchanges/LP_commands.c | 7 +++++++ iguana/exchanges/LP_nativeDEX.c | 5 ----- iguana/exchanges/LP_ordermatch.c | 8 ++++---- iguana/exchanges/LP_signatures.c | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index badc9e223..302db7006 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -173,6 +173,7 @@ unlockedspend(coin, txid)\n\ opreturndecrypt(coin, txid, passphrase)\n\ getendpoint(port=5555)\n\ getfee(coin)\n\ +mpnet(onoff)\n\ sleep(seconds=60)\n\ listtransactions(coin, address, count=10, skip=0)\n\ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ @@ -238,6 +239,12 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ } return(clonestr("{\"error\":\"cant find KMD\"}")); } + else if ( strcmp(method,"mpnet") == 0 ) + { + G.mpnet = jint(argjson,"onoff"); + printf("MPNET onoff.%d\n",G.mpnet); + return(clonestr("{\"status\":\"success\"}")); + } else if ( strcmp(method,"getendpoint") == 0 ) { int32_t err,mode; uint16_t wsport = 5555; char endpoint[64],bindpoint[64]; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index bc59b65eb..8974396f1 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -1649,11 +1649,6 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu int32_t nonz,didremote=0; LP_statslog_parse(); bitcoind_RPC_inittime = 0; - //if ( strcmp(G.USERPASS,"9894cc3a6f94afa8871d3633dce61a4444d48d670bacb229fb607064654ae81f") == 0 ) - { - G.mpnet = 1; - printf("MPNET active\n"); - } //LP_mpnet_init(); seems better to have the GUI send in persistent orders, exit mm is a cancel all while ( LP_STOP_RECEIVED == 0 ) { diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index bd10afc4a..c433720a3 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -547,7 +547,7 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double if ( IPC_ENDPOINT >= 0 ) LP_queuecommand(0,jprint(reqjson,0),IPC_ENDPOINT,-1,0); } - if ( qp->mpnet != 0 && qp->fill != 0 && qp->gtc != 0 ) + if ( qp->mpnet != 0 && qp->gtc == 0 ) { char *msg = jprint(reqjson,0); LP_mpnet_send(0,msg,1,0); @@ -655,11 +655,11 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q LP_query(ctx,myipaddr,mypubsock,"request",qp); LP_Alicequery = *qp, LP_Alicemaxprice = qp->maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash; } - if ( qp->gtc != 0 ) + if ( qp->gtc == 0 ) { cJSON *reqjson = LP_quotejson(qp); char *msg = jprint(reqjson,1); - LP_mpnet_send(1,msg,qp->fill,0); + LP_mpnet_send(1,msg,1,0); free(msg); } char str[65]; printf("LP_trade mpnet.%d fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->mpnet,qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),qp->maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); @@ -1205,7 +1205,7 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, memset(zero.bytes,0,sizeof(zero)); LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); } - if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) + if ( qp->mpnet != 0 && qp->gtc == 0 ) { char *msg = jprint(reqjson,0); LP_mpnet_send(0,msg,1,0); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index 58713b7ff..b4c90ffba 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -741,7 +741,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg)); } } - if ( strcmp(method,"connect") == 0 && qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) + if ( strcmp(method,"connect") == 0 && qp->mpnet != 0 && qp->gtc == 0 ) LP_mpnet_send(0,msg,1,0); free(msg); } From 057ef17024e88a386c40ff7eab613ffdd51208a0 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 07:49:24 -1100 Subject: [PATCH 38/60] Print from_mpnet, --- iguana/exchanges/LP_mpnet.c | 6 +++--- iguana/exchanges/LP_nativeDEX.c | 4 ++-- iguana/exchanges/LP_ordermatch.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_mpnet.c b/iguana/exchanges/LP_mpnet.c index d401c84af..ca1e7ea1c 100644 --- a/iguana/exchanges/LP_mpnet.c +++ b/iguana/exchanges/LP_mpnet.c @@ -21,7 +21,7 @@ bits256 MPNET_txids[1024]; int32_t num_MPNET_txids; -int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); +int32_t LP_tradecommand(int32_t from_mpnet,void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen); int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson); void LP_gtc_addorder(struct LP_quoteinfo *qp); char *LP_withdraw(struct iguana_info *coin,cJSON *argjson); @@ -189,8 +189,8 @@ void LP_mpnet_process(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_inf //printf("unique %s\n",bits256_str(str,txid)); if ( (argjson= LP_mpnet_parse(coin,txid)) != 0 ) { - printf("MPNET.(%s)\n",jprint(argjson,0)); - LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0); + //printf("MPNET.(%s)\n",jprint(argjson,0)); + LP_tradecommand(1,ctx,myipaddr,pubsock,argjson,0,0); free_json(argjson); } LP_mpnet_add(txid); diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 8974396f1..980857fa0 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -203,7 +203,7 @@ char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson char *retstr=0; cJSON *retjson; bits256 zero; if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 ) return(0); - if ( stats_JSONonly != 0 || LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 ) + if ( stats_JSONonly != 0 || LP_tradecommand(0,ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 ) { if ( (retstr= stats_JSON(ctx,0,myipaddr,pubsock,argjson,"127.0.0.1",stats_JSONonly)) != 0 ) { @@ -424,7 +424,7 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int /*if ( (argjson= cJSON_Parse(str)) != 0 ) { //portable_mutex_lock(&LP_commandmutex); - if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0) <= 0 ) + if ( LP_tradecommand(0,ctx,myipaddr,pubsock,argjson,0,0) <= 0 ) { if ( (retstr= stats_JSON(ctx,0,myipaddr,pubsock,argjson,remoteaddr,0)) != 0 ) free(retstr); diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index c433720a3..6d4976a85 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -1527,7 +1527,7 @@ void LP_tradecommandQ(struct LP_quoteinfo *qp,char *pairstr,int32_t funcid) //printf("queue.%d uuid.(%s)\n",funcid,qtp->Q.uuidstr); } -int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen) +int32_t LP_tradecommand(int32_t from_mpnet,void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen) { int32_t Qtrades = 1; char *method,str[65]; int32_t i,num,DEXselector = 0; uint64_t aliceid; double qprice,bestprice,price,bid,ask; cJSON *proof; uint64_t rq; struct iguana_info *coin; struct LP_quoteinfo Q,Q2; int32_t counter,retval=-1; @@ -1550,7 +1550,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson, LP_tradecommand_log(argjson); qprice = (double)Q.destsatoshis / (Q.satoshis - Q.txfee); //jdouble(argjson,"price"); //printf("%s\n",jprint(argjson,0)); - printf("%-4d uuid.%32s g.%d f.%d %12s %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.uuidstr+32,Q.gtc,Q.fill,method,Q.srccoin,Q.destcoin,dstr(Q.satoshis),dstr(Q.destsatoshis),qprice,LP_RTcount,LP_swapscount,G.netid); + printf("%-4d uuid.%32s M.%d g.%d f.%d %12s %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.uuidstr+32,from_mpnet,Q.gtc,Q.fill,method,Q.srccoin,Q.destcoin,dstr(Q.satoshis),dstr(Q.destsatoshis),qprice,LP_RTcount,LP_swapscount,G.netid); retval = 1; aliceid = j64bits(argjson,"aliceid"); if ( strcmp(method,"reserved") == 0 ) From b083c3aff6e7cb3e78b5fb3763daa50954ecf6b2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 07:50:32 -1100 Subject: [PATCH 39/60] No reason not to check mpnet --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 980857fa0..15c361f12 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -481,7 +481,7 @@ int32_t LP_nanomsg_recvs(void *ctx) nonz += LP_sock_check("PULL",ctx,origipaddr,-1,LP_mypullsock,"127.0.0.1",1); } portable_mutex_unlock(&LP_nanorecvsmutex); - if ( G.mpnet != 0 ) + //if ( G.mpnet != 0 ) LP_mpnet_check(ctx,origipaddr,LP_mypubsock); return(nonz); } From 9024dd16b71c4b289ab4360fcfa006154c8b8943 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 09:31:18 -1100 Subject: [PATCH 40/60] Reduce prints --- iguana/exchanges/LP_nativeDEX.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 15c361f12..9bf7fb867 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -520,17 +520,17 @@ void LP_coinsloop(void *_coins) if ( strcmp("BTC",coins) == 0 ) { strcpy(LP_coinsloopBTC_stats.name,"BTC coin loop"); - LP_coinsloopBTC_stats.threshold = 20000.; + LP_coinsloopBTC_stats.threshold = 200000.; } else if ( strcmp("KMD",coins) == 0 ) { strcpy(LP_coinsloopKMD_stats.name,"KMD coin loop"); - LP_coinsloopKMD_stats.threshold = 10000.; + LP_coinsloopKMD_stats.threshold = 100000.; } else { strcpy(LP_coinsloop_stats.name,"other coins loop"); - LP_coinsloop_stats.threshold = 5000.; + LP_coinsloop_stats.threshold = 50000.; } while ( LP_STOP_RECEIVED == 0 ) { From f2482f7ba2c50037d4ca10412a6e723cedc21d12 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 21:57:00 -1100 Subject: [PATCH 41/60] Otheraddr for mpnet --- iguana/exchanges/LP_ordermatch.c | 4 ++-- iguana/exchanges/LP_signatures.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_ordermatch.c b/iguana/exchanges/LP_ordermatch.c index 6d4976a85..2362f9e90 100644 --- a/iguana/exchanges/LP_ordermatch.c +++ b/iguana/exchanges/LP_ordermatch.c @@ -550,7 +550,7 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double if ( qp->mpnet != 0 && qp->gtc == 0 ) { char *msg = jprint(reqjson,0); - LP_mpnet_send(0,msg,1,0); + LP_mpnet_send(0,msg,1,qp->destaddr); free(msg); } free_json(reqjson); @@ -1208,7 +1208,7 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str, if ( qp->mpnet != 0 && qp->gtc == 0 ) { char *msg = jprint(reqjson,0); - LP_mpnet_send(0,msg,1,0); + LP_mpnet_send(0,msg,1,qp->destaddr); free(msg); } free_json(reqjson); diff --git a/iguana/exchanges/LP_signatures.c b/iguana/exchanges/LP_signatures.c index b4c90ffba..40228f796 100644 --- a/iguana/exchanges/LP_signatures.c +++ b/iguana/exchanges/LP_signatures.c @@ -742,7 +742,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_ } } if ( strcmp(method,"connect") == 0 && qp->mpnet != 0 && qp->gtc == 0 ) - LP_mpnet_send(0,msg,1,0); + LP_mpnet_send(0,msg,1,qp->coinaddr); free(msg); } From ebc733b9cee9ffb6d7623f63704311058b058009 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 21:59:39 -1100 Subject: [PATCH 42/60] Curlt timeout -> 10 sec --- crypto777/bitcoind_RPC.c | 2 +- iguana/exchanges/LP_include.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index 36ab1ce82..78c874cbe 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -289,7 +289,7 @@ try_again: count++; elapsedsum += (OS_milliseconds() - starttime); if ( (count % 100000) == 0) - printf("%d: ave %9.6f | elapsed %.3f millis | bitcoind_RPC.(%s) url.(%s)\n",count,elapsedsum/count,(OS_milliseconds() - starttime),command,url); + printf("%d: %s ave %9.6f | elapsed %.3f millis | bitcoind_RPC.(%s) url.(%s)\n",count,debugstr,elapsedsum/count,(OS_milliseconds() - starttime),command,url); if ( retstrp != 0 ) { *retstrp = retstr; diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 11ef6e96a..f95b98c61 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -59,7 +59,7 @@ void emscripten_usleep(int32_t x); // returns immediate, no sense for sleeping //#define LP_DISABLE_DISTCOMBINE #define LP_MAXVINS 64 -#define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL) +#define LP_HTTP_TIMEOUT 10 // 1 is too small due to edge cases of time(NULL) #define LP_AUTOTRADE_TIMEOUT 30 #define LP_RESERVETIME (LP_AUTOTRADE_TIMEOUT * 3) #define ELECTRUM_TIMEOUT 13 From b7fc78d147e8b1ada46f719d1dd2f9d99269e6ac Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 22:07:47 -1100 Subject: [PATCH 43/60] Prevent coin scan if >= longest chain --- iguana/exchanges/LP_nativeDEX.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 9bf7fb867..4c4f69566 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -682,6 +682,7 @@ void LP_coinsloop(void *_coins) continue; } //if ( strcmp(coin->symbol,"BTC") != 0 && strcmp(coin->symbol,"KMD") != 0 ) // SPV as backup + if ( coin->lastscanht < coin->longestchain ) { nonz++; if ( strcmp("BTC",coins) == 0 )//&& coin->lastscanht < coin->longestchain-3 ) From 40b6e766a16f6567ab7e77e55d9b16a793ea9d4f Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 22:14:16 -1100 Subject: [PATCH 44/60] +debug --- crypto777/bitcoind_RPC.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index 78c874cbe..4e351dcdc 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -170,7 +170,7 @@ char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char * else specialcase = 0; if ( url[0] == 0 ) strcpy(url,"http://127.0.0.1:7776"); - if ( specialcase != 0 && (0) ) + //if ( specialcase != 0 && (0) ) { //int32_t zeroval(); printf("<<<<<<<<<<< bitcoind_RPC: userpass.(%s) url.(%s) command.(%s) params.(%s)\n",userpass,url,command,params); From d8ebe988b69abc9203b540ad50c204b790a9783a Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 22:23:33 -1100 Subject: [PATCH 45/60] -debug --- crypto777/bitcoind_RPC.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index 4e351dcdc..78c874cbe 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -170,7 +170,7 @@ char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char * else specialcase = 0; if ( url[0] == 0 ) strcpy(url,"http://127.0.0.1:7776"); - //if ( specialcase != 0 && (0) ) + if ( specialcase != 0 && (0) ) { //int32_t zeroval(); printf("<<<<<<<<<<< bitcoind_RPC: userpass.(%s) url.(%s) command.(%s) params.(%s)\n",userpass,url,command,params); From d0f637abca14d7debe5afabd7df20e72dd5350e9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 22:43:37 -1100 Subject: [PATCH 46/60] Optimize gettxout calls --- iguana/exchanges/LP_nativeDEX.c | 2 +- iguana/exchanges/LP_rpc.c | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 4c4f69566..f58fef72f 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -579,7 +579,7 @@ void LP_coinsloop(void *_coins) LP_address_utxo_reset(&num,coin); coin->did_addrutxo_reset = 1; } - free_json(LP_address_balance(coin,coin->smartaddr,1)); + //free_json(LP_address_balance(coin,coin->smartaddr,1)); if ( coin->do_autofill_merge != 0 ) { if ( (retstr= LP_autofillbob(coin,coin->do_autofill_merge*1.02)) != 0 ) diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index b41a809fa..615942e0a 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -248,6 +248,12 @@ cJSON *LP_gettxout(char *symbol,char *coinaddr,bits256 txid,int32_t vout) return(cJSON_Parse("{\"error\":\"no coin\"}")); if ( bits256_nonz(txid) == 0 ) return(cJSON_Parse("{\"error\":\"null txid\"}")); + if ( (tx= LP_transactionfind(coin,txid)) != 0 && vout < tx->numvouts ) + { + if ( tx->outpoints[vout].spendheight > 0 ) + return(0); + //return(LP_gettxout_json(txid,vout,tx->height,tx->outpoints[vout].coinaddr,tx->outpoints[vout].value)); + } if ( coin->electrum == 0 ) { sprintf(buf,"[\"%s\", %d, true]",bits256_str(str,txid),vout); @@ -255,12 +261,6 @@ cJSON *LP_gettxout(char *symbol,char *coinaddr,bits256 txid,int32_t vout) } else { - if ( (tx= LP_transactionfind(coin,txid)) != 0 && vout < tx->numvouts ) - { - if ( tx->outpoints[vout].spendheight > 0 ) - return(0); - //return(LP_gettxout_json(txid,vout,tx->height,tx->outpoints[vout].coinaddr,tx->outpoints[vout].value)); - } if ( coinaddr[0] == 0 ) { if ( (txobj= electrum_transaction(&height,symbol,coin->electrum,&txobj,txid,0)) != 0 ) From 0d70f14d9af913002dd5cd1fcaf035baf1e45f0e Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Jun 2018 22:52:45 -1100 Subject: [PATCH 47/60] Test --- iguana/exchanges/LP_nativeDEX.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index f58fef72f..a4fa473b4 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -579,7 +579,7 @@ void LP_coinsloop(void *_coins) LP_address_utxo_reset(&num,coin); coin->did_addrutxo_reset = 1; } - //free_json(LP_address_balance(coin,coin->smartaddr,1)); + //free_json(LP_address_balance(coin,coin->smartaddr,1)); expensive invoking gettxout if ( coin->do_autofill_merge != 0 ) { if ( (retstr= LP_autofillbob(coin,coin->do_autofill_merge*1.02)) != 0 ) From 9c30b191d731ca4d251721e2b734183215991e9a Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Jun 2018 00:55:51 -1100 Subject: [PATCH 48/60] Increase hash table size --- iguana/iguana_ramchain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/iguana_ramchain.c b/iguana/iguana_ramchain.c index a7b83cd45..0f1b9527e 100755 --- a/iguana/iguana_ramchain.c +++ b/iguana/iguana_ramchain.c @@ -1015,7 +1015,7 @@ int32_t iguana_ramchain_alloc(char *fname,struct iguana_info *coin,struct iguana char str[65],str2[65]; fprintf(stderr,"ht.%d wait for allocated %s < MAXMEM %s | elapsed %.2f minutes hashsize.%ld allocsize.%ld\n",height,mbstr(str,myallocated(0,-1)+hashsize+allocsize),mbstr(str2,coin->MAXMEM),(double)(time(NULL)-coin->startutc)/60.,(long)hashsize,(long)allocsize); sleep(13); } - iguana_meminit(hashmem,"ramhashmem",0,2*hashsize + 65536,0); + iguana_meminit(hashmem,"ramhashmem",0,3*hashsize + 65536,0); iguana_meminit(mem,"ramchain",0,allocsize + 65536,0); mem->alignflag = sizeof(uint32_t); hashmem->alignflag = sizeof(uint32_t); From 9c9ff629ad8deb0376a02aa3824a7780ba70f2c8 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Jun 2018 01:31:45 -1100 Subject: [PATCH 49/60] Test --- iguana/iguana_ramchain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/iguana_ramchain.c b/iguana/iguana_ramchain.c index 0f1b9527e..90f2c335c 100755 --- a/iguana/iguana_ramchain.c +++ b/iguana/iguana_ramchain.c @@ -616,7 +616,7 @@ void *_iguana_ramchain_setptrs(RAMCHAIN_PTRPS,struct iguana_ramchaindata *rdata) *B = RAMCHAIN_PTR(rdata,Boffset); *T = RAMCHAIN_PTR(rdata,Toffset); //*B = (void *)(long)((long)rdata + (long)rdata->Boffset); - //*T = (void *)(long)((long)rdata + (long)rdata->Toffset); + *T = (void *)(long)((long)rdata + (long)rdata->Toffset); *Kspace = RAMCHAIN_PTR(rdata,Koffset); //*Kspace = (void *)(long)((long)rdata + (long)rdata->Koffset); if ( ramchain->expanded != 0 ) From 456501de1bfd480f2bc8fff3140dcc86ae1ae32c Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Jun 2018 01:37:48 -1100 Subject: [PATCH 50/60] Test --- iguana/iguana_ramchain.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/iguana_ramchain.c b/iguana/iguana_ramchain.c index 90f2c335c..f09b33dc3 100755 --- a/iguana/iguana_ramchain.c +++ b/iguana/iguana_ramchain.c @@ -616,7 +616,7 @@ void *_iguana_ramchain_setptrs(RAMCHAIN_PTRPS,struct iguana_ramchaindata *rdata) *B = RAMCHAIN_PTR(rdata,Boffset); *T = RAMCHAIN_PTR(rdata,Toffset); //*B = (void *)(long)((long)rdata + (long)rdata->Boffset); - *T = (void *)(long)((long)rdata + (long)rdata->Toffset); + //*T = (void *)(long)((long)rdata + (long)rdata->Toffset); *Kspace = RAMCHAIN_PTR(rdata,Koffset); //*Kspace = (void *)(long)((long)rdata + (long)rdata->Koffset); if ( ramchain->expanded != 0 ) @@ -1953,7 +1953,7 @@ long iguana_ramchain_data(struct supernet_info *myinfo,struct iguana_info *coin, { if ( (err= iguana_ramchain_verify(coin,ramchain)) == 0 ) { - iguana_blockzcopyRO(0*coin->chain->zcash,B,0,&RO,0); + iguana_blockzcopyRO(coin->chain->zcash,B,0,&RO,0); rdata->scriptspace = ramchain->H.scriptoffset = scriptspace; rdata->stackspace = ramchain->H.stacksize = stackspace; if ( (fpos= (int32_t)iguana_ramchain_save(coin,RAMCHAIN_ARG,(uint32_t)addr->ipbits,RO.hash2,RO.prev_block,bundlei,0,coin->chain->zcash)) >= 0 ) From 8615376c9892c2862e29aa87cde65982e5556f66 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Jun 2018 01:53:36 -1100 Subject: [PATCH 51/60] Test --- iguana/iguana777.c | 2 +- iguana/iguana_ramchain.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/iguana777.c b/iguana/iguana777.c index 7e2906963..8c88ec624 100755 --- a/iguana/iguana777.c +++ b/iguana/iguana777.c @@ -1131,7 +1131,7 @@ struct iguana_info *iguana_setcoin(char *symbol,void *launched,int32_t maxpeers, if ( coin->MAXMEM == 0 ) coin->MAXMEM = IGUANA_DEFAULTRAM; coin->MAXMEM *= (1024L * 1024 * 1024); - coin->enableCACHE = 0;//(strcmp("BTCD",coin->symbol) == 0); + coin->enableCACHE = 1;//0;//(strcmp("BTCD",coin->symbol) == 0); if ( jobj(json,"cache") != 0 ) coin->enableCACHE = juint(json,"cache"); if ( (coin->polltimeout= juint(json,"poll")) <= 0 ) diff --git a/iguana/iguana_ramchain.c b/iguana/iguana_ramchain.c index f09b33dc3..0f1b9527e 100755 --- a/iguana/iguana_ramchain.c +++ b/iguana/iguana_ramchain.c @@ -1953,7 +1953,7 @@ long iguana_ramchain_data(struct supernet_info *myinfo,struct iguana_info *coin, { if ( (err= iguana_ramchain_verify(coin,ramchain)) == 0 ) { - iguana_blockzcopyRO(coin->chain->zcash,B,0,&RO,0); + iguana_blockzcopyRO(0*coin->chain->zcash,B,0,&RO,0); rdata->scriptspace = ramchain->H.scriptoffset = scriptspace; rdata->stackspace = ramchain->H.stacksize = stackspace; if ( (fpos= (int32_t)iguana_ramchain_save(coin,RAMCHAIN_ARG,(uint32_t)addr->ipbits,RO.hash2,RO.prev_block,bundlei,0,coin->chain->zcash)) >= 0 ) From 75b8466595e3a5827b0920643c62ff770158d319 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Jun 2018 06:25:43 -1100 Subject: [PATCH 52/60] Add true arg to blockchain.transaction.get --- iguana/exchanges/LP_socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_socket.c b/iguana/exchanges/LP_socket.c index 71162b67d..f7dcca929 100644 --- a/iguana/exchanges/LP_socket.c +++ b/iguana/exchanges/LP_socket.c @@ -576,7 +576,7 @@ cJSON *electrum_hasharg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,c char params[128],str[65]; cJSON *retjson; if ( retjsonp == 0 ) retjsonp = &retjson; - sprintf(params,"[\"%s\"]",bits256_str(str,arg)); + sprintf(params,"[\"%s\", true]",bits256_str(str,arg)); return(electrum_submit(symbol,ep,retjsonp,method,params,timeout)); } From 4ac72266b02a4fb199f3166f19226a424287cb64 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 26 Jun 2018 23:53:50 -1100 Subject: [PATCH 53/60] Print lock times --- iguana/exchanges/LP_privkey.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_privkey.c b/iguana/exchanges/LP_privkey.c index 95fd9b60e..18979aaba 100644 --- a/iguana/exchanges/LP_privkey.c +++ b/iguana/exchanges/LP_privkey.c @@ -593,13 +593,13 @@ char *verusblocks() } if ( num > 0 ) { - if ( 0 ) - { for (i=0; i Date: Wed, 27 Jun 2018 00:16:46 -1100 Subject: [PATCH 54/60] Revert --- iguana/exchanges/LP_privkey.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/LP_privkey.c b/iguana/exchanges/LP_privkey.c index 18979aaba..95fd9b60e 100644 --- a/iguana/exchanges/LP_privkey.c +++ b/iguana/exchanges/LP_privkey.c @@ -593,13 +593,13 @@ char *verusblocks() } if ( num > 0 ) { + if ( 0 ) + { for (i=0; i Date: Sun, 1 Jul 2018 22:32:29 -1100 Subject: [PATCH 55/60] revert flag for transaction get --- iguana/exchanges/LP_socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/exchanges/LP_socket.c b/iguana/exchanges/LP_socket.c index f7dcca929..71162b67d 100644 --- a/iguana/exchanges/LP_socket.c +++ b/iguana/exchanges/LP_socket.c @@ -576,7 +576,7 @@ cJSON *electrum_hasharg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,c char params[128],str[65]; cJSON *retjson; if ( retjsonp == 0 ) retjsonp = &retjson; - sprintf(params,"[\"%s\", true]",bits256_str(str,arg)); + sprintf(params,"[\"%s\"]",bits256_str(str,arg)); return(electrum_submit(symbol,ep,retjsonp,method,params,timeout)); } From 5db166355d024aa3d4632521191ef236d0d5c533 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 2 Jul 2018 23:05:03 -1100 Subject: [PATCH 56/60] Gen64addrs --- iguana/exchanges/LP_commands.c | 8 +++++ iguana/exchanges/LP_privkey.c | 60 +++++++++++++++++++++++++++++++ iguana/exchanges/LP_transaction.c | 4 +-- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index 302db7006..9f4058d81 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -151,6 +151,7 @@ getprice(base, rel)\n\ //getmessages(firsti=0, num=100)\n\ //deletemessages(firsti=0, num=100)\n\ secretaddresses(prefix='secretaddress', passphrase, num=10, pubtype=60, taddr=0)\n\ +gen64addrs(passphrase, taddr=0, pubtype=60)\n\ electrum(coin, ipaddr, port)\n\ snapshot(coin, height)\n\ snapshot_balance(coin, height, addresses[])\n\ @@ -419,6 +420,13 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ { return(LP_ticker(jstr(argjson,"base"),jstr(argjson,"rel"))); } + else if ( strcmp(method,"gen64addrs") == 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_gen64addrs(ctx,jstr(argjson,"passphrase"),taddr,pubtype)); + } else if ( strcmp(method,"secretaddresses") == 0 ) { uint8_t taddr,pubtype; diff --git a/iguana/exchanges/LP_privkey.c b/iguana/exchanges/LP_privkey.c index 95fd9b60e..e8ff0d0dc 100644 --- a/iguana/exchanges/LP_privkey.c +++ b/iguana/exchanges/LP_privkey.c @@ -202,6 +202,66 @@ char *LP_secretaddresses(void *ctx,char *prefix,char *passphrase,int32_t n,uint8 return(jprint(retjson,1)); } +uint32_t komodo_segid32(char *coinaddr) +{ + bits256 addrhash; + vcalc_sha256(0,(uint8_t *)&addrhash,(uint8_t *)coinaddr,(int32_t)strlen(coinaddr)); + return(addrhash.uints[0]); +} + +char *LP_gen64addrs(void *ctx,char *passphrase,uint8_t taddr,uint8_t pubtype) +{ + int32_t i,segid,n=64; uint8_t tmptype,pubkey33[33],rmd160[20]; char str[65],str2[65],buf[8192],wifstr[64],coinaddr[64],coinaddrs[64][64],wifstrs[64][64]; uint64_t mask = 0; bits256 checkprivkey,privkey,pubkey; cJSON *retjson,*addrs,*addrs64; + if ( passphrase == 0 || passphrase[0] == 0 ) + passphrase = "password"; + memset(coinaddrs,0,sizeof(coinaddrs)); + memset(wifstrs,0,sizeof(wifstrs)); + conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase)); + bitcoin_priv2pub(ctx,"KMD",pubkey33,coinaddr,privkey,taddr,pubtype); + //sprintf(output,"\"addresses\":["); + for (i=0; bitweight(mask)<64; i++) + { + sprintf(buf,"%s %03d",passphrase,i); + conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)buf,(int32_t)strlen(buf)); + bitcoin_priv2pub(ctx,"KMD",pubkey33,coinaddr,privkey,taddr,pubtype); + bitcoin_priv2wif("KMD",0,wifstr,privkey,188); + bitcoin_wif2priv("KMD",0,&tmptype,&checkprivkey,wifstr); + bitcoin_addr2rmd160("KMD",taddr,&tmptype,rmd160,coinaddr); + if ( bits256_cmp(checkprivkey,privkey) != 0 ) + { + printf("WIF.(%s) error -> %s vs %s?\n",wifstr,bits256_str(str,privkey),bits256_str(str2,checkprivkey)); + free_json(retjson); + return(clonestr("{\"error\":\"couldnt validate wifstr\"}")); + } + else if ( tmptype != pubtype ) + { + printf("checktype.%d != pubtype.%d\n",tmptype,pubtype); + free_json(retjson); + return(clonestr("{\"error\":\"couldnt validate pubtype\"}")); + } + segid = komodo_segid32(coinaddr) & 0x3f; + if ( (mask & (1LL << segid)) == 0 ) + { + mask |= (1LL << segid); + strcpy(coinaddrs[segid],coinaddr); + strcpy(wifstrs[segid],wifstr); + printf("./komodo-cli -ac_name=POSTEST64 importprivkey %s "" %s\n",wifstr,bitweight(mask)<64?"false":"true"); + } + //sprintf(output+strlen(output),"\\\"%s\\\"%c ",coinaddr,isymbol,utxotxid,utxovout); LP_address_utxoadd(0,(uint32_t)time(NULL),"withdraw",coin,coin->smartaddr,utxotxid,utxovout,value,1,-1); if ( (up= LP_address_utxofind(coin,coin->smartaddr,utxotxid,utxovout)) != 0 ) preselected[numpre++] = up; - else printf("second couldnt add address_utxo after not finding\n"); + else printf("second couldnt add address_utxo %s/v%d after not finding\n",bits256_str(str,utxotxid),utxovout); //return(0); } } From a5de7866f5b6f749ce54ab92f4e97a397024bc50 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 2 Jul 2018 23:08:08 -1100 Subject: [PATCH 57/60] Update portfolio --- iguana/exchanges/prices/autoprice | 4 ++-- iguana/exchanges/supernet | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/iguana/exchanges/prices/autoprice b/iguana/exchanges/prices/autoprice index d58c337e7..8720311fb 100755 --- a/iguana/exchanges/prices/autoprice +++ b/iguana/exchanges/prices/autoprice @@ -39,11 +39,11 @@ sharkholdings="{\"coin\":\"iota\",\"balance\":1500000}, {\"coin\":\"komodo\",\"b curl --url "http://127.0.0.1:7783" --data "{\"base\":\"MSHARK\",\"rel\":\"KMD\",\"fundvalue_bid\":\"NAV_KMD\",\"fundvalue_ask\":\"assetNAV_KMD\",\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"margin\":$margin,\"address\":\"RTu3JZZKLJTcfNwBa19dWRagEfQq49STqC\",\"holdings\":[$sharkholdings],\"divisor\":1400000}" -curl --url "http://127.0.0.1:7783" --data "{\"margin\":$margin,\"base\":\"SUPERNET\",\"rel\":\"KMD\",\"fundvalue_bid\":\"NAV_KMD\",\"fundvalue_ask\":\"NAV_KMD\",\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"address\":\"RRyyejME7LRTuvdziWsXkAbSW1fdiohGwK\",\"holdings\":[{\"coin\":\"iota\",\"balance\":11000000}, {\"coin\":\"stratis\",\"balance\":1300000}, {\"coin\":\"zcash\",\"balance\":0.10000}, {\"coin\":\"syscoin\",\"balance\":20000000}, {\"coin\":\"waves\",\"balance\":700000}, {\"coin\":\"bitcoin\",\"balance\":570}, {\"coin\":\"bitcoin-cash\",\"balance\":1500}, {\"coin\":\"heat-ledger\",\"balance\":2323851 }, {\"coin\":\"decred\",\"balance\":0.20000}, {\"coin\":\"vericoin\",\"balance\":2199368 }, {\"coin\":\"byteball\",\"balance\":4238}, {\"coin\":\"iocoin\",\"balance\":0.150000}, {\"coin\":\"quantum-resistant-ledger\",\"balance\":0.375000}, {\"coin\":\"chips\",\"balance\":2577006 }, {\"coin\":\"hush\",\"balance\":100000 }, {\"coin\":\"mobilego\",\"balance\":100000 }, {\"coin\":\"utrum\",\"balance\":2100000}, {\"coin\":\"verus\",\"balance\":65000}],\"divisor\":612529}" +curl --url "http://127.0.0.1:7783" --data "{\"margin\":$margin,\"base\":\"SUPERNET\",\"rel\":\"KMD\",\"fundvalue_bid\":\"NAV_KMD\",\"fundvalue_ask\":\"NAV_KMD\",\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"address\":\"RRyyejME7LRTuvdziWsXkAbSW1fdiohGwK\",\"holdings\":[{\"coin\":\"iota\",\"balance\":11000000}, {\"coin\":\"stratis\",\"balance\":1300000}, {\"coin\":\"zcash\",\"balance\":0.10000}, {\"coin\":\"syscoin\",\"balance\":0.20000000}, {\"coin\":\"waves\",\"balance\":700000}, {\"coin\":\"bitcoin\",\"balance\":540}, {\"coin\":\"bitcoin-cash\",\"balance\":1500}, {\"coin\":\"heat-ledger\",\"balance\":2323851 }, {\"coin\":\"decred\",\"balance\":0.20000}, {\"coin\":\"vericoin\",\"balance\":2199368 }, {\"coin\":\"byteball\",\"balance\":4238}, {\"coin\":\"iocoin\",\"balance\":0.150000}, {\"coin\":\"quantum-resistant-ledger\",\"balance\":0.375000}, {\"coin\":\"chips\",\"balance\":2577006 }, {\"coin\":\"hush\",\"balance\":100000 }, {\"coin\":\"mobilego\",\"balance\":100000 }, {\"coin\":\"utrum\",\"balance\":2100000}, {\"coin\":\"verus\",\"balance\":65000}],\"divisor\":777777}" curl --url "http://127.0.0.1:7783" --data "{\"margin\":$margin,\"base\":\"HODL\",\"rel\":\"KMD\",\"fundvalue_bid\":\"assetNAV_KMD\",\"fundvalue_ask\":\"assetNAV_KMD\",\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"address\":\"RNcUaMUEFLxVwtTo7rgruhwYanGk1jTkeU\",\"holdings\":[{\"coin\":\"siacoin\",\"balance\":185000000,\"comment\":\"using siafunds equal to million siacoin\"}],\"divisor\":10000000}" -dexholdings="{\"coin\":\"blocknet\",\"balance\":2500000}" +dexholdings="{\"coin\":\"blocknet\",\"balance\":2000000}" curl --url "http://127.0.0.1:7783" --data "{\"base\":\"DEX\",\"rel\":\"KMD\",\"margin\":$margin,\"fundvalue_bid\":\"assetNAV_KMD\",\"fundvalue_ask\":\"assetNAV_KMD\",\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"address\":\"RThtXup6Zo7LZAi8kRWgjAyi1s4u6U9Cpf\",\"holdings\":[$dexholdings],\"divisor\":1000000}" curl --url "http://127.0.0.1:7783" --data "{\"base\":\"BOTS\",\"rel\":\"KMD\",\"margin\":$margin,\"fundvalue_bid\":\"assetNAV_KMD\",\"fundvalue_ask\":\"assetNAV_KMD\",\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"address\":\"RNdqHx26GWy9bk8MtmH1UiXjQcXE4RKK2P\",\"holdings\":[$dexholdings],\"divisor\":3333333}" diff --git a/iguana/exchanges/supernet b/iguana/exchanges/supernet index f7b09a20b..e7e11b43f 100755 --- a/iguana/exchanges/supernet +++ b/iguana/exchanges/supernet @@ -15,5 +15,5 @@ echo supernet curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"balances\",\"address\":\"RRyyejME7LRTuvdziWsXkAbSW1fdiohGwK\"}" echo supernet -curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"fundvalue\",\"address\":\"RRyyejME7LRTuvdziWsXkAbSW1fdiohGwK\",\"holdings\":[{\"coin\":\"iota\",\"balance\":11000000}, {\"coin\":\"stratis\",\"balance\":1300000}, {\"coin\":\"zcash\",\"balance\":0.10000}, {\"coin\":\"syscoin\",\"balance\":20000000}, {\"coin\":\"waves\",\"balance\":700000}, {\"coin\":\"bitcoin\",\"balance\":570}, {\"coin\":\"bitcoin-cash\",\"balance\":1500}, {\"coin\":\"heat-ledger\",\"balance\":2323851 }, {\"coin\":\"decred\",\"balance\":0.20000}, {\"coin\":\"vericoin\",\"balance\":2199368 }, {\"coin\":\"byteball\",\"balance\":4238}, {\"coin\":\"iocoin\",\"balance\":0.150000}, {\"coin\":\"quantum-resistant-ledger\",\"balance\":0.375000}, {\"coin\":\"hush\",\"balance\":100000 }, {\"coin\":\"mobilego\",\"balance\":100000 }, {\"coin\":\"utrum\",\"balance\":2350000}, {\"coin\":\"verus\",\"balance\":65000}],\"divisor\":612529}" +curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"fundvalue\",\"address\":\"RRyyejME7LRTuvdziWsXkAbSW1fdiohGwK\",\"holdings\":[{\"coin\":\"iota\",\"balance\":11000000}, {\"coin\":\"stratis\",\"balance\":1300000}, {\"coin\":\"zcash\",\"balance\":0.10000}, {\"coin\":\"syscoin\",\"balance\":0.20000000}, {\"coin\":\"waves\",\"balance\":700000}, {\"coin\":\"bitcoin\",\"balance\":540}, {\"coin\":\"bitcoin-cash\",\"balance\":1500}, {\"coin\":\"heat-ledger\",\"balance\":2323851 }, {\"coin\":\"decred\",\"balance\":0.20000}, {\"coin\":\"vericoin\",\"balance\":2199368 }, {\"coin\":\"byteball\",\"balance\":4238}, {\"coin\":\"iocoin\",\"balance\":0.150000}, {\"coin\":\"quantum-resistant-ledger\",\"balance\":0.375000}, {\"coin\":\"hush\",\"balance\":100000 }, {\"coin\":\"mobilego\",\"balance\":100000 }, {\"coin\":\"utrum\",\"balance\":2350000}, {\"coin\":\"verus\",\"balance\":65000}],\"divisor\":777777}" From 94ee70fb64138114d3963d8fb3a03f4f4bb05dce Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 2 Jul 2018 23:10:38 -1100 Subject: [PATCH 58/60] Syntax --- iguana/exchanges/LP_privkey.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iguana/exchanges/LP_privkey.c b/iguana/exchanges/LP_privkey.c index e8ff0d0dc..073e189f2 100644 --- a/iguana/exchanges/LP_privkey.c +++ b/iguana/exchanges/LP_privkey.c @@ -211,7 +211,7 @@ uint32_t komodo_segid32(char *coinaddr) char *LP_gen64addrs(void *ctx,char *passphrase,uint8_t taddr,uint8_t pubtype) { - int32_t i,segid,n=64; uint8_t tmptype,pubkey33[33],rmd160[20]; char str[65],str2[65],buf[8192],wifstr[64],coinaddr[64],coinaddrs[64][64],wifstrs[64][64]; uint64_t mask = 0; bits256 checkprivkey,privkey,pubkey; cJSON *retjson,*addrs,*addrs64; + int32_t i,segid,n=64; uint8_t tmptype,pubkey33[33],rmd160[20]; char str[65],str2[65],buf[8192],wifstr[64],coinaddr[64],coinaddrs[64][64],wifstrs[64][64]; uint64_t mask = 0; bits256 checkprivkey,privkey,pubkey; cJSON *retjson,*addrs,*array; if ( passphrase == 0 || passphrase[0] == 0 ) passphrase = "password"; memset(coinaddrs,0,sizeof(coinaddrs)); @@ -256,7 +256,7 @@ char *LP_gen64addrs(void *ctx,char *passphrase,uint8_t taddr,uint8_t pubtype) jaddstr(addrs,coinaddrs[i],wifstrs[i]); jadd(retjson,"addrpairs",addrs); for (i=0; i Date: Mon, 2 Jul 2018 23:16:41 -1100 Subject: [PATCH 59/60] gen64addrs script --- iguana/exchanges/gen64addrs | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 iguana/exchanges/gen64addrs diff --git a/iguana/exchanges/gen64addrs b/iguana/exchanges/gen64addrs new file mode 100755 index 000000000..d907807dd --- /dev/null +++ b/iguana/exchanges/gen64addrs @@ -0,0 +1,3 @@ +#!/bin/bash +source userpass +curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"gen64addrs\",\"passphrase\":\"default\"}" From a31c678ed700c462d6562183ea125ae096a07579 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 4 Jul 2018 01:34:33 -1100 Subject: [PATCH 60/60] alicedexfee --- iguana/exchanges/LP_remember.c | 1 + 1 file changed, 1 insertion(+) diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index deaacf1c9..2cc5f0211 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -681,6 +681,7 @@ cJSON *LP_swap_json(struct LP_swap_remember *rswap) jaddbits256(item,"paymentspent",rswap->paymentspent); jaddbits256(item,"Apaymentspent",rswap->Apaymentspent); jaddbits256(item,"depositspent",rswap->depositspent); + jaddbits256(item,"alicedexfee",rswap->iambob == 0 ? rswap->txids[BASILISK_MYFEE] : rswap->txids[BASILISK_OTHERFEE]); return(item); }