diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index a4a964ee1..c0197a77b 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -60,7 +60,7 @@ double LP_query(char *method,struct LP_quoteinfo *qp,char *base,char *rel,bits25 int32_t LP_connectstart(int32_t pubsock,struct LP_utxoinfo *utxo,cJSON *argjson,char *myipaddr,char *base,char *rel,double profitmargin) { - char *retstr,pairstr[512]; cJSON *retjson; double price; bits256 privkey; int32_t pair=-1,retval = -1,DEXselector = 0; uint64_t destvalue; struct LP_quoteinfo Q; struct basilisk_swap *swap; + char *retstr,pairstr[512],destaddr[64]; cJSON *retjson; double price; bits256 privkey; int32_t pair=-1,retval = -1,DEXselector = 0; uint64_t destvalue; struct LP_quoteinfo Q; struct basilisk_swap *swap; if ( (price= LP_price(base,rel)) > SMALLVAL ) { price *= (1. + profitmargin); @@ -78,7 +78,7 @@ int32_t LP_connectstart(int32_t pubsock,struct LP_utxoinfo *utxo,cJSON *argjson, printf("not eligible\n"); return(-1); } - if ( bits256_nonz(privkey) != 0 && Q.quotetime >= Q.timestamp-3 && Q.quotetime < utxo->T.swappending && bits256_cmp(utxo->S.mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee ) + if ( bits256_nonz(privkey) != 0 && Q.quotetime >= Q.timestamp-3 && Q.quotetime < utxo->T.swappending && bits256_cmp(utxo->S.mypub,Q.srchash) == 0 && (destvalue= LP_txvalue(destaddr,rel,Q.desttxid,Q.destvout)) >= price*Q.satoshis+Q.desttxfee && destvalue >= Q.destsatoshis+Q.desttxfee ) { nanomsg_tcpname(pairstr,myipaddr,10000+(rand() % 10000)); if ( (pair= nn_socket(AF_SP,NN_PAIR)) < 0 ) @@ -107,7 +107,7 @@ int32_t LP_connectstart(int32_t pubsock,struct LP_utxoinfo *utxo,cJSON *argjson, } else { - printf("dest %.8f < required %.8f (%d %d %d %d %d %d) %.8f %.8f\n",dstr(Q.satoshis),dstr(price*(utxo->S.satoshis-Q.txfee)),bits256_nonz(privkey) != 0 ,Q.timestamp == utxo->T.swappending-LP_RESERVETIME ,Q.quotetime >= Q.timestamp ,Q.quotetime < utxo->T.swappending ,bits256_cmp(utxo->S.mypub,Q.srchash) == 0 , LP_txvalue(rel,Q.desttxid,Q.destvout) >= price*Q.satoshis+Q.desttxfee,dstr(LP_txvalue(rel,Q.desttxid,Q.destvout)),dstr(price*Q.satoshis+Q.desttxfee)); + printf("dest %.8f < required %.8f (%d %d %d %d %d %d) %.8f %.8f\n",dstr(Q.satoshis),dstr(price*(utxo->S.satoshis-Q.txfee)),bits256_nonz(privkey) != 0 ,Q.timestamp == utxo->T.swappending-LP_RESERVETIME ,Q.quotetime >= Q.timestamp ,Q.quotetime < utxo->T.swappending ,bits256_cmp(utxo->S.mypub,Q.srchash) == 0 , LP_txvalue(destaddr,rel,Q.desttxid,Q.destvout) >= price*Q.satoshis+Q.desttxfee,dstr(LP_txvalue(destaddr,rel,Q.desttxid,Q.destvout)),dstr(price*Q.satoshis+Q.desttxfee)); } } else printf("no price for %s/%s\n",base,rel); if ( retval < 0 ) diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 1e354bfb5..9dc4042df 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -156,18 +156,18 @@ int32_t LP_subsock_check(struct LP_peerinfo *peer) void LP_utxo_spentcheck(int32_t pubsock,struct LP_utxoinfo *utxo,double profitmargin) { - struct _LP_utxoinfo u; char str[65]; uint32_t now = (uint32_t)time(NULL); + struct _LP_utxoinfo u; char str[65],destaddr[64]; uint32_t now = (uint32_t)time(NULL); //printf("%s lag.%d\n",bits256_str(str,utxo->txid),now-utxo->lastspentcheck); if ( utxo->T.spentflag == 0 && now > utxo->T.lastspentcheck+60 ) { u = (utxo->iambob != 0) ? utxo->deposit : utxo->fee; utxo->T.lastspentcheck = now; - if ( LP_txvalue(utxo->coin,utxo->payment.txid,utxo->payment.vout) == 0 ) + if ( LP_txvalue(destaddr,utxo->coin,utxo->payment.txid,utxo->payment.vout) == 0 ) { printf("txid.%s %s/v%d %.8f has been spent\n",utxo->coin,bits256_str(str,utxo->payment.txid),utxo->payment.vout,dstr(utxo->payment.value)); LP_spentnotify(utxo,0); } - else if ( LP_txvalue(utxo->coin,u.txid,u.vout) == 0 ) + else if ( LP_txvalue(destaddr,utxo->coin,u.txid,u.vout) == 0 ) { printf("txid2.%s %s/v%d %.8f has been spent\n",utxo->coin,bits256_str(str,u.txid),u.vout,dstr(u.value)); LP_spentnotify(utxo,1); diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index a5aed366b..5c07e0539 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -103,9 +103,10 @@ uint64_t oldLP_txvalue(char *symbol,bits256 txid,int32_t vout) return(value); } -uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) +uint64_t LP_txvalue(char *coinaddr,char *symbol,bits256 txid,int32_t vout) { - uint64_t value = 0; double interest; cJSON *txobj; + uint64_t value = 0; double interest; cJSON *txobj,*sobj,*array; int32_t n; + coinaddr = 0; if ( (txobj= LP_gettxout(symbol,txid,vout)) != 0 ) { if ( (value= jdouble(txobj,"amount")*SATOSHIDEN) == 0 && (value= jdouble(txobj,"value")*SATOSHIDEN) == 0 ) @@ -120,6 +121,8 @@ uint64_t LP_txvalue(char *symbol,bits256 txid,int32_t vout) value += SATOSHIDEN * interest; } } + if ( (sobj= jobj(txobj,"scriptPubKey")) != 0 && (array= jarray(&n,sobj,"addresses")) != 0 ) + strcpy(coinaddr,jstri(array,0)); //char str[65]; printf("%.8f <- %s.(%s) txobj.(%s)\n",dstr(value),symbol,bits256_str(str,txid),jprint(txobj,0)); free_json(txobj); } @@ -164,10 +167,10 @@ int32_t LP_vinscan(bits256 *spendtxidp,int32_t *spendvinip,char *symbol,bits256 int32_t LP_spendsearch(bits256 *spendtxidp,int32_t *indp,char *symbol,bits256 searchtxid,int32_t searchvout) { - cJSON *blockjson,*txids,*txobj; bits256 hash,txid; int32_t h,i,j,numtxids,loadheight,errs = 0; + char destaddr[64]; cJSON *blockjson,*txids,*txobj; bits256 hash,txid; int32_t h,i,j,numtxids,loadheight,errs = 0; *indp = -1; memset(spendtxidp,0,sizeof(*spendtxidp)); - if ( LP_txvalue(symbol,searchtxid,searchvout) > 0 ) + if ( LP_txvalue(destaddr,symbol,searchtxid,searchvout) > 0 ) return(0); if ( (txobj= LP_gettx(symbol,searchtxid)) == 0 ) return(0); @@ -725,7 +728,7 @@ int32_t iguana_signrawtransaction(void *ctx,char *symbol,uint8_t pubtype,uint8_t char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,char *symbol,uint8_t pubtype,uint8_t p2shtype,uint8_t isPoS,uint8_t wiftype,void *ctx,bits256 privkey,bits256 *privkey2p,uint8_t *redeemscript,int32_t redeemlen,uint8_t *userdata,int32_t userdatalen,bits256 utxotxid,int32_t vout,char *destaddr,uint8_t *pubkey33,int32_t finalseqid,uint32_t expiration,int64_t *destamountp,uint64_t satoshis,char *changeaddr,char *vinaddr,int32_t suppress_pubkeys) { - char *rawtxbytes=0,*signedtx=0,tmpaddr[64],hexstr[999],wifstr[128],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1; struct vin_info V[16]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; struct iguana_msgtx msgtx; + char *rawtxbytes=0,*signedtx=0,tmpaddr[64],hexstr[999],wifstr[128],txdestaddr[64],_destaddr[64]; uint8_t spendscript[512],addrtype,rmd160[20]; cJSON *txobj,*vins,*item,*privkeys; int32_t completed,spendlen,ignore_cltverr=1; struct vin_info V[16]; uint32_t timestamp,locktime = 0,sequenceid = 0xffffffff * finalseqid; bits256 txid; uint64_t value,change = 0; struct iguana_msgtx msgtx; *destamountp = 0; memset(signedtxidp,0,sizeof(*signedtxidp)); if ( finalseqid == 0 ) @@ -734,7 +737,7 @@ char *basilisk_swap_bobtxspend(bits256 *signedtxidp,uint64_t txfee,char *name,ch if ( redeemlen < 0 ) return(0); #ifndef BASILISK_DISABLESENDTX - if ( (value= LP_txvalue(symbol,utxotxid,vout)) == 0 ) + if ( (value= LP_txvalue(txdestaddr,symbol,utxotxid,vout)) == 0 ) { char str[65]; printf("basilisk_swap_bobtxspend.%s %s utxo.(%s) already spent or doesnt exist\n",name,symbol,bits256_str(str,utxotxid)); diff --git a/iguana/exchanges/LP_utxos.c b/iguana/exchanges/LP_utxos.c index 11b0175e7..a2720c1a1 100644 --- a/iguana/exchanges/LP_utxos.c +++ b/iguana/exchanges/LP_utxos.c @@ -294,7 +294,7 @@ void LP_spentnotify(struct LP_utxoinfo *utxo,int32_t selector) char *LP_spentcheck(cJSON *argjson) { - bits256 txid,checktxid; int32_t vout,checkvout; struct LP_utxoinfo *utxo; int32_t iambob,retval = 0; + char destaddr[64]; bits256 txid,checktxid; int32_t vout,checkvout; struct LP_utxoinfo *utxo; int32_t iambob,retval = 0; txid = jbits256(argjson,"txid"); vout = jint(argjson,"vout"); for (iambob=0; iambob<=1; iambob++) @@ -308,7 +308,7 @@ char *LP_spentcheck(cJSON *argjson) checktxid = jbits256(argjson,"checktxid"); checkvout = jint(argjson,"checkvout"); } - if ( LP_txvalue(utxo->coin,checktxid,checkvout) == 0 ) + if ( LP_txvalue(destaddr,utxo->coin,checktxid,checkvout) == 0 ) { if ( LP_mypeer != 0 && LP_mypeer->numutxos > 0 ) LP_mypeer->numutxos--; @@ -323,29 +323,35 @@ char *LP_spentcheck(cJSON *argjson) return(clonestr("{\"error\":\"cant find txid to check spent status\"}")); } -int32_t LP_iseligible(int32_t iambob,char *coin,bits256 txid,int32_t vout,uint64_t satoshis,bits256 txid2,int32_t vout2) +int32_t LP_iseligible(int32_t iambob,char *symbol,bits256 txid,int32_t vout,uint64_t satoshis,bits256 txid2,int32_t vout2) { - uint64_t val,val2,threshold; - if ( (val= LP_txvalue(coin,txid,vout)) >= satoshis ) + uint64_t val,val2,threshold; char destaddr[64]; + if ( (val= LP_txvalue(destaddr,symbol,txid,vout)) >= satoshis ) { threshold = (iambob != 0) ? LP_DEPOSITSATOSHIS(satoshis) : LP_DEXFEE(satoshis); - if ( (val2= LP_txvalue(coin,txid2,vout2)) >= threshold ) + if ( (val2= LP_txvalue(destaddr,symbol,txid2,vout2)) >= threshold ) { //printf("val %.8f and val2 %.8f vs %.8f\n",dstr(val),dstr(val2),dstr(satoshis)); return(1); - } else printf("mismatched %s txid value2 %.8f < %.8f\n",coin,dstr(val2),dstr(LP_DEPOSITSATOSHIS(satoshis))); - } else printf("mismatched %s txid value %.8f < %.8f\n",coin,dstr(val),dstr(satoshis)); + } else printf("mismatched %s txid value2 %.8f < %.8f\n",symbol,dstr(val2),dstr(LP_DEPOSITSATOSHIS(satoshis))); + } else printf("mismatched %s txid value %.8f < %.8f\n",symbol,dstr(val),dstr(satoshis)); return(0); } struct LP_utxoinfo *LP_utxoadd(int32_t iambob,int32_t mypubsock,char *symbol,bits256 txid,int32_t vout,int64_t value,bits256 txid2,int32_t vout2,int64_t value2,char *spendscript,char *coinaddr,bits256 pubkey,double profitmargin) { - char str[65]; uint64_t tmpsatoshis; int32_t spendvini,selector; bits256 spendtxid; struct _LP_utxoinfo u; struct LP_utxoinfo *utxo = 0; + char str[65],destaddr[64],destaddr2[64]; uint64_t val,val2=0,tmpsatoshis; int32_t spendvini,selector; bits256 spendtxid; struct _LP_utxoinfo u; struct LP_utxoinfo *utxo = 0; if ( symbol == 0 || symbol[0] == 0 || spendscript == 0 || spendscript[0] == 0 || coinaddr == 0 || coinaddr[0] == 0 || bits256_nonz(txid) == 0 || bits256_nonz(txid2) == 0 || vout < 0 || vout2 < 0 || value <= 0 || value2 <= 0 ) { printf("malformed addutxo %d %d %d %d %d %d %d %d %d\n", symbol == 0,spendscript == 0,coinaddr == 0,bits256_nonz(txid) == 0,bits256_nonz(txid2) == 0,vout < 0,vout2 < 0,value <= 0,value2 <= 0); return(0); } + destaddr2[0] = 0; + if ( (val= LP_txvalue(destaddr,symbol,txid,vout)) != value || (val2= LP_txvalue(destaddr2,symbol,txid2,vout2)) != value2 || strcmp(destaddr,destaddr2) != 0 || strcmp(coinaddr,destaddr) != 0 ) + { + printf("utxoadd mismatch (%s %.8f) + (%s %.8f) != %s %.8f %.8f\n",destaddr,dstr(val),destaddr2,dstr(val2),coinaddr,dstr(value),dstr(value2)); + return(0); + } if ( iambob != 0 && value2 < 9 * (value >> 3) + 100000 ) // big txfee padding tmpsatoshis = (((value2 - 100000) / 9) << 3); else tmpsatoshis = value;