diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index a88e5de2d..9e4c782ae 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -291,6 +291,8 @@ char *stats_JSON(cJSON *argjson,char *remoteaddr,uint16_t port) // from rpc port retstr = LP_quotereceived(argjson); else if ( strcmp(method,"connected") == 0 ) retstr = LP_connected(argjson); + else if ( strcmp(method,"checktxid") == 0 ) + retstr = LP_spentcheck(argjson); else if ( strcmp(method,"getprice") == 0 ) retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel")); else if ( strcmp(method,"orderbook") == 0 ) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 63923eadb..314dbc92d 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -21,6 +21,9 @@ #ifndef LP_INCLUDE_H #define LP_INCLUDE_H +#define LP_DEXFEE(destsatoshis) ((destsatoshis) / INSTANTDEX_INSURANCEDIV) +#define LP_DEPOSITSATOSHIS(satoshis) ((satoshis) + (satoshis >> 3)) + #define INSTANTDEX_DECKSIZE 1000 #define INSTANTDEX_LOCKTIME (3600*2 + 300*2) #define INSTANTDEX_INSURANCEDIV 777 @@ -167,7 +170,7 @@ struct LP_utxoinfo uint64_t value,satoshis,value2; uint8_t key[sizeof(bits256) + sizeof(int32_t)]; int32_t vout,vout2,pair; - uint32_t lasttime,errors,swappending,published; + uint32_t lasttime,errors,swappending,published,spentflag; double profitmargin; char ipaddr[64],coinaddr[64],spendscript[256],coin[16]; uint16_t port; @@ -188,7 +191,7 @@ struct LP_quoteinfo { struct basilisk_request R; bits256 srchash,desthash,txid,txid2,desttxid,feetxid,privkey; - uint64_t value,satoshis,satoshis2,txfee,destsatoshis,desttxfee; + uint64_t satoshis,txfee,destsatoshis,desttxfee; uint32_t timestamp,quotetime; int32_t vout,vout2,destvout,feevout,pair; char srccoin[16],coinaddr[64],destcoin[16],destaddr[64]; }; diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index a5c5c9bfd..1d22abaa1 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -162,7 +162,17 @@ void LP_mainloop(struct LP_peerinfo *mypeer,uint16_t mypubport,int32_t pubsock,i { HASH_ITER(hh,LP_utxoinfos,utxo,utmp) { - if ( LP_ismine(utxo) != 0 ) + if ( LP_txvalue(utxo->coin,utxo->txid,utxo->vout) == 0 ) + { + printf("txid %s %.8f has been spent\n",utxo->coin,dstr(utxo->value)); + LP_spentnotify(utxo,0); + } + else if ( LP_txvalue(utxo->coin,utxo->txid2,utxo->vout2) == 0 ) + { + printf("txid2 %s %.8f has been spent\n",utxo->coin,dstr(utxo->value2)); + LP_spentnotify(utxo,1); + } + else if ( LP_ismine(utxo) != 0 ) { if ( strcmp(utxo->coin,"KMD") == 0 ) LP_priceping(pubsock,utxo,"BTC",profitmargin); diff --git a/iguana/exchanges/LP_quotes.c b/iguana/exchanges/LP_quotes.c index 5a0cf09f8..44722ea15 100644 --- a/iguana/exchanges/LP_quotes.c +++ b/iguana/exchanges/LP_quotes.c @@ -66,8 +66,6 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp) { jaddbits256(retjson,"txid2",qp->txid2); jaddnum(retjson,"vout2",qp->vout2); - if ( qp->satoshis2 != 0 ) - jadd64bits(retjson,"satoshis2",qp->satoshis2); } if ( bits256_nonz(qp->desttxid) != 0 ) { @@ -114,10 +112,7 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson) qp->feetxid = jbits256(argjson,"feetxid"); qp->destvout = jint(argjson,"destvout"); qp->desthash = jbits256(argjson,"desthash"); - //qp->feesatoshis = j64bits(argjson,"feesatoshis"); qp->satoshis = j64bits(argjson,"satoshis"); - qp->satoshis2 = j64bits(argjson,"satoshis2"); - qp->value = j64bits(argjson,"value"); qp->destsatoshis = j64bits(argjson,"destsatoshis"); qp->txfee = j64bits(argjson,"txfee"); qp->desttxfee = j64bits(argjson,"desttxfee"); @@ -131,16 +126,14 @@ int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char * safecopy(qp->destcoin,destcoin,sizeof(qp->destcoin)); if ( (qp->txfee= LP_getestimatedrate(utxo->coin)*LP_AVETXSIZE) < 10000 ) qp->txfee = 10000; - if ( qp->txfee >= utxo->value || qp->txfee >= utxo->value2 || utxo->value2 < utxo->satoshis+(utxo->satoshis>>3) ) + if ( qp->txfee >= utxo->satoshis || qp->txfee >= utxo->value2 || utxo->value2 < LP_DEPOSITSATOSHIS(utxo->satoshis) ) return(-1); qp->txid = utxo->txid; qp->vout = utxo->vout; qp->txid2 = utxo->txid2; qp->vout2 = utxo->vout2; - qp->satoshis = utxo->satoshis; - qp->satoshis2 = utxo->satoshis + (utxo->satoshis >> 3); + qp->satoshis = utxo->satoshis - qp->txfee; qp->destsatoshis = qp->satoshis * price; - //qp->feesatoshis = qp->destsatoshis / INSTANTDEX_INSURANCEDIV; if ( (qp->desttxfee= LP_getestimatedrate(qp->destcoin) * LP_AVETXSIZE) < 10000 ) qp->desttxfee = 10000; if ( qp->desttxfee >= qp->destsatoshis ) @@ -164,7 +157,6 @@ int32_t LP_quoteinfoset(struct LP_quoteinfo *qp,uint32_t timestamp,uint32_t quot qp->timestamp = timestamp; qp->quotetime = quotetime; qp->destsatoshis = destsatoshis; - //qp->feesatoshis = qp->destsatoshis / INSTANTDEX_INSURANCEDIV; qp->desttxfee = desttxfee; qp->desttxid = desttxid; qp->destvout = destvout; @@ -229,8 +221,13 @@ cJSON *LP_tradecandidates(struct LP_utxoinfo *myutxo,char *base) LP_quoteparse(&Q,item); safecopy(coinstr,jstr(item,"base"),sizeof(coinstr)); if ( strcmp(coinstr,base) == 0 ) - if ( LP_arrayfind(retarray,Q.txid,Q.vout) < 0 ) - jaddi(retarray,jduplicate(item)); + { + if ( LP_iseligible(Q.txid,Q.vout) != 0 ) + { + if ( LP_arrayfind(retarray,Q.txid,Q.vout) < 0 ) + jaddi(retarray,jduplicate(item)); + } + } } } free_json(array); diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 6a1def41e..f788c087b 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -818,20 +818,20 @@ struct basilisk_swap *bitcoin_swapinit(bits256 privkey,uint8_t *pubkey33,bits256 printf(">>>>>>>>>> jumblrflag.%d <<<<<<<<< use smart address, %.8f bobconfs.%d, %.8f aliceconfs.%d\n",jumblrflag,dstr(swap->I.bobsatoshis),swap->I.bobconfirms,dstr(swap->I.alicesatoshis),swap->I.aliceconfirms); if ( swap->I.iambob != 0 ) { - basilisk_rawtx_setparms("myfee",swap->I.req.quoteid,&swap->myfee,&swap->bobcoin,0,0,swap->I.bobsatoshis/INSTANTDEX_INSURANCEDIV,0,0,jumblrflag); - basilisk_rawtx_setparms("otherfee",swap->I.req.quoteid,&swap->otherfee,&swap->alicecoin,0,0,swap->I.alicesatoshis/INSTANTDEX_INSURANCEDIV,0,0,jumblrflag); + basilisk_rawtx_setparms("myfee",swap->I.req.quoteid,&swap->myfee,&swap->bobcoin,0,0,LP_DEXFEE(swap->I.bobsatoshis),0,0,jumblrflag); + basilisk_rawtx_setparms("otherfee",swap->I.req.quoteid,&swap->otherfee,&swap->alicecoin,0,0,LP_DEXFEE(swap->I.alicesatoshis),0,0,jumblrflag); bobpub33 = pubkey33; } else { - basilisk_rawtx_setparms("otherfee",swap->I.req.quoteid,&swap->otherfee,&swap->bobcoin,0,0,swap->I.bobsatoshis/INSTANTDEX_INSURANCEDIV,0,0,jumblrflag); - basilisk_rawtx_setparms("myfee",swap->I.req.quoteid,&swap->myfee,&swap->alicecoin,0,0,swap->I.alicesatoshis/INSTANTDEX_INSURANCEDIV,0,0,jumblrflag); + basilisk_rawtx_setparms("otherfee",swap->I.req.quoteid,&swap->otherfee,&swap->bobcoin,0,0,LP_DEXFEE(swap->I.bobsatoshis),0,0,jumblrflag); + basilisk_rawtx_setparms("myfee",swap->I.req.quoteid,&swap->myfee,&swap->alicecoin,0,0,LP_DEXFEE(swap->I.alicesatoshis),0,0,jumblrflag); alicepub33 = pubkey33; } - basilisk_rawtx_setparms("bobdeposit",swap->I.req.quoteid,&swap->bobdeposit,&swap->bobcoin,swap->I.bobconfirms,0,swap->I.bobsatoshis + (swap->I.bobsatoshis>>3) + swap->bobcoin.txfee,4,0,jumblrflag); - basilisk_rawtx_setparms("bobrefund",swap->I.req.quoteid,&swap->bobrefund,&swap->bobcoin,1,4,swap->I.bobsatoshis + (swap->I.bobsatoshis>>3),1,bobpub33,jumblrflag); + basilisk_rawtx_setparms("bobdeposit",swap->I.req.quoteid,&swap->bobdeposit,&swap->bobcoin,swap->I.bobconfirms,0,LP_DEPOSITSATOSHIS(swap->I.bobsatoshis) + swap->bobcoin.txfee,4,0,jumblrflag); + basilisk_rawtx_setparms("bobrefund",swap->I.req.quoteid,&swap->bobrefund,&swap->bobcoin,1,4,LP_DEPOSITSATOSHIS(swap->I.bobsatoshis),1,bobpub33,jumblrflag); swap->bobrefund.I.suppress_pubkeys = 1; - basilisk_rawtx_setparms("aliceclaim",swap->I.req.quoteid,&swap->aliceclaim,&swap->bobcoin,1,4,swap->I.bobsatoshis + (swap->I.bobsatoshis>>3),1,alicepub33,jumblrflag); + basilisk_rawtx_setparms("aliceclaim",swap->I.req.quoteid,&swap->aliceclaim,&swap->bobcoin,1,4,LP_DEPOSITSATOSHIS(swap->I.bobsatoshis),1,alicepub33,jumblrflag); swap->aliceclaim.I.suppress_pubkeys = 1; swap->aliceclaim.I.locktime = swap->I.started + swap->I.putduration+swap->I.callduration + 1; diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 4bb325b2f..652acea7d 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -29,6 +29,13 @@ int32_t LP_ismine(struct LP_utxoinfo *utxo) else return(0); } +int32_t LP_isunspent(struct LP_utxoinfo *utxo) +{ + if ( utxo->spentflag == 0 && utxo->swappending == 0 ) + return(1); + else return(0); +} + struct LP_utxoinfo *LP_utxofind(bits256 txid,int32_t vout) { struct LP_utxoinfo *utxo=0; uint8_t key[sizeof(txid) + sizeof(vout)]; @@ -60,6 +67,8 @@ cJSON *LP_inventoryjson(cJSON *item,struct LP_utxoinfo *utxo) jaddnum(item,"socket",utxo->pair); if ( utxo->swap != 0 ) jaddstr(item,"swap","in progress"); + if ( utxo->spentflag != 0 ) + jaddnum(item,"spent",utxo->spentflag); return(item); } @@ -94,6 +103,63 @@ char *LP_utxos(struct LP_peerinfo *mypeer,char *coin,int32_t lastn) return(jprint(utxosjson,1)); } +void LP_spentnotify(struct LP_utxoinfo *utxo,int32_t selector) +{ + cJSON *argjson; + utxo->spentflag = (uint32_t)time(NULL); + if ( LP_mypubsock >= 0 ) + { + argjson = cJSON_CreateObject(); + jaddstr(argjson,"method","checktxid"); + jaddbits256(argjson,"txid",utxo->txid); + jaddnum(argjson,"vout",utxo->vout); + if ( selector != 0 ) + { + jaddbits256(argjson,"checktxid",utxo->txid2); + jaddnum(argjson,"checkvout",utxo->vout2); + } + LP_send(LP_mypubsock,jprint(argjson,1),1); + } +} + +char *LP_spentcheck(cJSON *argjson) +{ + bits256 txid,checktxid; int32_t vout,checkvout; struct LP_utxoinfo *utxo; + txid = jbits256(argjson,"txid"); + vout = jint(argjson,"vout"); + if ( (utxo= LP_utxofind(txid,vout)) != 0 ) + { + if ( jobj(argjson,"check") == 0 ) + checktxid = txid, checkvout = vout; + else + { + checktxid = jbits256(argjson,"checktxid"); + checkvout = jint(argjson,"checkvout"); + } + if ( LP_txvalue(utxo->coin,checktxid,checkvout) == 0 ) + { + utxo->spentflag = (uint32_t)time(NULL); + printf("indeed txid was spent\n"); + return(clonestr("{\"result\":\"marked as spent\"}")); + } else return(clonestr("{\"error\":\"txid is still unspent?\"}")); + } else return(clonestr("{\"error\":\"cant find txid to check spent status\"}")); +} + +int32_t LP_iseligible(bits256 txid,int32_t vout) +{ + struct LP_utxoinfo *utxo; + if ( (utxo= LP_utxofind(txid,vout)) != 0 ) + { + if ( LP_txvalue(utxo->coin,utxo->txid,utxo->vout) == utxo->value ) + { + if ( LP_txvalue(utxo->coin,utxo->txid2,utxo->vout2) == utxo->value2 ) + return(1); + else printf("mismatched txid value2\n"); + } else printf("mismatched txid value\n"); + } + return(0); +} + struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32_t mypubsock,char *coin,bits256 txid,int32_t vout,int64_t value,bits256 txid2,int32_t vout2,int64_t value2,char *spendscript,char *coinaddr,char *ipaddr,uint16_t port,double profitmargin) { uint64_t tmpsatoshis; struct LP_utxoinfo *utxo = 0; @@ -107,8 +173,8 @@ struct LP_utxoinfo *LP_addutxo(int32_t amclient,struct LP_peerinfo *mypeer,int32 printf("LP node got localhost utxo\n"); return(0); } - if ( IAMCLIENT == 0 || value2 < 9 * (value >> 3) ) - tmpsatoshis = (value2 / 9) << 3; + if ( IAMCLIENT == 0 || value2 < 9 * (value >> 3) + 100000 ) + tmpsatoshis = ((value2 / 9) << 3) - 100000; else tmpsatoshis = value; if ( (utxo= LP_utxofind(txid,vout)) != 0 ) { @@ -244,7 +310,7 @@ char *LP_inventory(char *symbol) else myipaddr = "127.0.0.1"; HASH_ITER(hh,LP_utxoinfos,utxo,tmp) { - if ( strcmp(symbol,utxo->coin) == 0 && (IAMCLIENT != 0 || strcmp(utxo->ipaddr,myipaddr) == 0) ) + if ( LP_isunspent(utxo) != 0 && strcmp(symbol,utxo->coin) == 0 && (IAMCLIENT != 0 || LP_ismine(utxo) != 0) ) jaddi(array,LP_inventoryjson(cJSON_CreateObject(),utxo)); } return(jprint(array,1)); @@ -332,8 +398,8 @@ uint64_t LP_privkey_init(struct LP_peerinfo *mypeer,int32_t mypubsock,char *symb depositval = values[i]; values[i] = 0, used++; if ( amclient != 0 ) - targetval = (depositval / 776) + 50000; - else targetval = (depositval / 9) * 8; + targetval = (depositval / 776) + 100000; + else targetval = (depositval / 9) * 8 + 100000; //printf("i.%d %.8f target %.8f\n",i,dstr(depositval),dstr(targetval)); if ( (i= LP_nearestvalue(values,n,targetval)) >= 0 ) {