Browse Source

Merge pull request #608 from jl777/dev

BTC txfee tweaks
patch-1
jl777 7 years ago
committed by GitHub
parent
commit
ed58cae593
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      iguana/exchanges/LP_commands.c
  2. 2
      iguana/exchanges/LP_include.h
  3. 33
      iguana/exchanges/LP_ordermatch.c
  4. 2
      iguana/exchanges/LP_remember.c
  5. 48
      iguana/exchanges/LP_rpc.c
  6. 1
      iguana/exchanges/LP_signatures.c
  7. 2
      iguana/exchanges/LP_transaction.c
  8. 21
      iguana/exchanges/LP_utxo.c
  9. 3
      iguana/exchanges/inuse

3
iguana/exchanges/LP_commands.c

@ -140,6 +140,7 @@ balances(address)\n\
fundvalue(address="", holdings=[], divisor=0)\n\
orderbook(base, rel, duration=3600)\n\
getprices()\n\
inuse()\n\
getmyprice(base, rel)\n\
getprice(base, rel)\n\
//sendmessage(base=coin, rel="", pubkey=zero, <argjson method2>)\n\
@ -363,6 +364,8 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
}
return(clonestr("{\"error\":\"cant find address\"}"));
}
else if ( strcmp(method,"inuse") == 0 )
return(jprint(LP_inuse_json(),1));
else if ( (retstr= LP_istradebots_command(ctx,pubsock,method,argjson)) != 0 )
return(retstr);
if ( base[0] != 0 && rel[0] != 0 )

2
iguana/exchanges/LP_include.h

@ -59,7 +59,7 @@ void emscripten_usleep(int32_t x); // returns immediate, no sense for sleeping
#define LP_MAXVINS 64
#define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL)
#define LP_AUTOTRADE_TIMEOUT 30
#define LP_RESERVETIME 600 //(LP_AUTOTRADE_TIMEOUT * 2)
#define LP_RESERVETIME (LP_AUTOTRADE_TIMEOUT * 3)
#define ELECTRUM_TIMEOUT 13
#define LP_ELECTRUM_KEEPALIVE 60
#define LP_ELECTRUM_MAXERRORS 777

33
iguana/exchanges/LP_ordermatch.c

@ -73,7 +73,7 @@ uint64_t LP_txfeecalc(struct iguana_info *coin,uint64_t txfee,int32_t txlen)
coin->rate = LP_getestimatedrate(coin);
if ( (txfee= SATOSHIDEN * coin->rate * txlen) <= 10000 )
{
coin->rate = -1.;
//coin->rate = -1.;
coin->rate = _LP_getestimatedrate(coin);
if ( (txfee= SATOSHIDEN * coin->rate * txlen) <= 10000 )
txfee = 10000;
@ -187,7 +187,7 @@ double LP_quote_validate(struct LP_utxoinfo *autxo,struct LP_utxoinfo *butxo,str
desttxfee = qp->desttxfee;
if ( qp->satoshis != 0 )
qprice = ((double)qp->destsatoshis / (qp->satoshis-qp->txfee));
printf("qprice %.8f <- %.8f/%.8f txfees.(%.8f %.8f) vs (%.8f %.8f)\n",qprice,dstr(qp->destsatoshis),dstr(qp->satoshis),dstr(qp->txfee),dstr(qp->desttxfee),dstr(txfee),dstr(desttxfee));
//printf("qprice %.8f <- %.8f/%.8f txfees.(%.8f %.8f) vs (%.8f %.8f)\n",qprice,dstr(qp->destsatoshis),dstr(qp->satoshis),dstr(qp->txfee),dstr(qp->desttxfee),dstr(txfee),dstr(desttxfee));
if ( qp->txfee < LP_REQUIRED_TXFEE*txfee || qp->desttxfee < LP_REQUIRED_TXFEE*desttxfee )
{
printf("error -14: txfee %.8f >= %.8f or desttxfee %.8f >= %.8f\n",dstr(qp->txfee),dstr(LP_REQUIRED_TXFEE*txfee),dstr(qp->desttxfee),dstr(LP_REQUIRED_TXFEE*desttxfee));
@ -307,7 +307,7 @@ int32_t LP_nearest_utxovalue(struct iguana_info *coin,char *coinaddr,struct LP_a
}
if ( replacei >= 0 )
{
printf("REPLACE bestdist %.8f height %d with dist %.8f height %d\n",dstr(bestdist),bestup->U.height,dstr(utxos[replacei]->U.value - targetval),utxos[replacei]->U.height);
//printf("REPLACE bestdist %.8f height %d with dist %.8f height %d\n",dstr(bestdist),bestup->U.height,dstr(utxos[replacei]->U.value - targetval),utxos[replacei]->U.height);
return(replacei);
}
}
@ -443,8 +443,9 @@ struct LP_utxoinfo *LP_address_myutxopair(struct LP_utxoinfo *butxo,int32_t iamb
if ( targetval == 0 || mini < 0 )
break;
}
} else printf("no %s %s utxos pass LP_address_utxo_ptrs filter %.8f %.8f\n",coin->symbol,coinaddr,dstr(targetval),dstr(targetval2));
} else printf("address_myutxopair couldnt find %s %s\n",coin->symbol,coinaddr);
} //else printf("no %s %s utxos pass LP_address_utxo_ptrs filter %.8f %.8f\n",coin->symbol,coinaddr,dstr(targetval),dstr(targetval2));
}
printf("address_myutxopair couldnt find %s %s targets %.8f %.8f\n",coin->symbol,coinaddr,dstr(targetval),dstr(targetval2));
return(0);
}
@ -841,7 +842,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; struct iguana_info *coin; struct LP_utxoinfo A,B,*autxo,*butxo; cJSON *reqjson; char str[65]; struct LP_address_utxo *utxos[1000]; int32_t i,r,counter,max = (int32_t)(sizeof(utxos)/sizeof(*utxos));
*newqp = *qp;
qp = newqp;
printf("bob %s received REQUEST.(%llu)\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid);
//printf("bob %s received REQUEST.(%llu)\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid);
if ( (coin= LP_coinfind(qp->srccoin)) == 0 )
return(0);
if ( (myprice= LP_trades_bobprice(&bid,&ask,qp)) == 0. )
@ -864,7 +865,7 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
} else return(0);
if ( qprice >= myprice )
{
r = (LP_rand() % 100);
r = (LP_rand() % 90) + 10;
range = (qprice - myprice);
price = myprice + ((r * range) / 100.);
bestprice = LP_bob_competition(&counter,qp->aliceid,price,0);
@ -887,7 +888,7 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
//LP_address_utxo_reset(coin);
//printf("done LP_address_utxo_reset.%s\n",coin->symbol);
i = 0;
while ( i < 100 && price >= myprice )
while ( i < 33 && price >= myprice )
{
if ( (butxo= LP_address_myutxopair(butxo,1,utxos,max,LP_coinfind(qp->srccoin),qp->coinaddr,qp->txfee,dstr(qp->destsatoshis),price,qp->desttxfee)) != 0 )
{
@ -913,12 +914,13 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
return(0);
if ( p >= qprice )
break;
price *= 0.99777;
price /= 0.99;
i++;
}
printf("i.%d qprice %.8f myprice %.8f price %.8f [%.8f]\n",i,qprice,myprice,price,p);
if ( LP_allocated(qp->txid,qp->vout) == 0 && LP_allocated(qp->txid2,qp->vout2) == 0 )
{
printf("found unallocated txids\n");
reqjson = LP_quotejson(qp);
LP_unavailableset(qp->txid,qp->vout,qp->timestamp + LP_RESERVETIME,qp->desthash);
LP_unavailableset(qp->txid2,qp->vout2,qp->timestamp + LP_RESERVETIME,qp->desthash);
@ -927,12 +929,12 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
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));
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));
free_json(reqjson);
//printf("Send RESERVED id.%llu\n",(long long)qp->aliceid);
printf("Send RESERVED id.%llu\n",(long long)qp->aliceid);
return(qp);
} else printf("request processing selected ineligible utxos?\n");
return(0);
@ -940,12 +942,13 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
struct LP_quoteinfo *LP_trades_gotreserved(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp)
{
char *retstr;
//char str[65]; printf("alice %s received RESERVED.(%llu) %.8f\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid,(double)qp->destsatoshis/(qp->satoshis+1));
char *retstr; double qprice;
char str[65]; printf("alice %s received RESERVED.(%llu) %.8f\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid,(double)qp->destsatoshis/(qp->satoshis+1));
*newqp = *qp;
qp = newqp;
if ( LP_trades_alicevalidate(ctx,qp) > 0. )
if ( (qprice= LP_trades_alicevalidate(ctx,qp)) > 0. )
{
printf("got qprice %.8f\n",qprice);
LP_aliceid(qp->tradeid,qp->aliceid,"reserved",0,0);
if ( (retstr= LP_quotereceived(qp)) != 0 )
free(retstr);
@ -1401,7 +1404,7 @@ char *LP_autobuy(void *ctx,char *myipaddr,int32_t mypubsock,char *base,char *rel
if ( destsatoshis < (autxo->payment.value / LP_MINCLIENTVOL) || autxo->payment.value < desttxfee*LP_MINSIZE_TXFEEMULT )
{
printf("destsatoshis %.8f vs utxo %.8f this would have triggered an quote error -13\n",dstr(destsatoshis),dstr(autxo->payment.value));
return(clonestr("{\"error\":\"cant find a deposit that is close enough in size. make another deposit that is just a bit larger than what you want to trade\"}"));
return(clonestr("{\"error\":\"cant find a deposit that is close enough in size. make another deposit that is a bit larger than what you want to trade\"}"));
}
bestsatoshis = 1.001 * LP_basesatoshis(dstr(destsatoshis),maxprice,txfee,desttxfee);
memset(&B,0,sizeof(B));

2
iguana/exchanges/LP_remember.c

@ -1188,7 +1188,7 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti
}
LP_txbytes_update("aliceclaim",rswap.bobcoin,rswap.txbytes[BASILISK_ALICECLAIM],&rswap.txids[BASILISK_ALICECLAIM],&rswap.depositspent,&rswap.sentflags[BASILISK_ALICECLAIM]);
}
} else printf("now %u before expiration %u\n",(uint32_t)time(NULL),rswap.expiration);
} //else printf("now %u before expiration %u\n",(uint32_t)time(NULL),rswap.expiration);
}
if ( (rswap.sentflags[BASILISK_ALICEPAYMENT] != 0 || bits256_nonz(rswap.txids[BASILISK_ALICEPAYMENT]) != 0)&& bits256_nonz(rswap.Apaymentspent) == 0 && rswap.sentflags[BASILISK_ALICERECLAIM] == 0 )
{

48
iguana/exchanges/LP_rpc.c

@ -616,6 +616,17 @@ cJSON *LP_importprivkey(char *symbol,char *wifstr,char *label,int32_t flag)
} else return(cJSON_Parse("{\"result\":\"success\"}"));
}
cJSON *LP_bitcoinfees()
{
char *retstr; cJSON *retjson = 0;
if ( (retstr= issue_curlt("https://bitcoinfees.earn.com/api/v1/fees/recommended",LP_HTTP_TIMEOUT)) != 0 )
{
retjson = cJSON_Parse(retstr);
free(retstr);
}
return(retjson);
}
double _LP_getestimatedrate(struct iguana_info *coin)
{
char buf[512],*retstr=0; int32_t numblocks,err=0; cJSON *errjson,*retjson; double rate = 0.00000005;
@ -623,7 +634,7 @@ double _LP_getestimatedrate(struct iguana_info *coin)
{
if ( coin->estimatefeestr[0] == 0 )
strcpy(coin->estimatefeestr,"estimatefee");
numblocks = 4;//strcmp(coin->symbol,"BTC") == 0 ? 6 : 2;
numblocks = 2;//strcmp(coin->symbol,"BTC") == 0 ? 6 : 2;
again:
if ( coin->electrum == 0 )
{
@ -632,12 +643,29 @@ again:
}
else
{
if ( (retjson= electrum_estimatefee(coin->symbol,coin->electrum,&retjson,numblocks)) != 0 )
// {"fastestFee":70,"halfHourFee":70,"hourFee":10}
if ( strcmp(coin->symbol,"BTC") == 0 && (retjson= LP_bitcoinfees()) != 0 )
{
int32_t fastest,half,hour,best=0;
fastest = jint(retjson,"fastestFee");
half = jint(retjson,"halfHourFee");
hour = jint(retjson,"hourFee");
if ( hour*2 > half )
best = hour*2;
else best = half;
if ( fastest < best )
best = fastest;
retstr = calloc(1,16);
sprintf(retstr,"%0.8f",((double)best * 1024)/SATOSHIDEN);
printf("LP_getestimatedrate (%s) -> %s\n",jprint(retjson,0),retstr);
free(retjson);
}
/*if ( (retjson= electrum_estimatefee(coin->symbol,coin->electrum,&retjson,numblocks)) != 0 )
{
retstr = jprint(retjson,1);
//free_json(retjson), retjson = 0; causes crash?
printf("estfee numblocks.%d (%s)\n",numblocks,retstr);
}
}*/
}
if ( retstr != 0 )
{
@ -659,18 +687,19 @@ again:
rate = atof(retstr) / 1024.;
if ( rate != 0. )
{
rate *= 1.5;
rate *= 1.25;
if ( rate < 0.00000005 )
rate = 0.00000005;
if ( coin->electrum != 0 )
rate *= 1.5;
//if ( coin->electrum != 0 )
// rate *= 1.5;
if ( fabs(rate - coin->rate) > SMALLVAL )
printf("t%u estimated rate.(%s) (%s) -> %.8f %.8f\n",coin->ratetime,coin->symbol,retstr,rate,coin->rate);
printf("%u t%u estimated rate.(%s) (%s) -> %.8f %.8f\n",(uint32_t)time(NULL),coin->ratetime,coin->symbol,retstr,rate,coin->rate);
coin->rate = rate;
coin->ratetime = (uint32_t)time(NULL);
//printf("set rate %.8f t%u\n",rate,coin->ratetime);
}
free(retstr);
if ( err == 1 && coin->electrum == 0 && strcmp("BTC",coin->symbol) == 0 )
if ( err == 1 && coin->electrum == 0 && strcmp(coin->estimatefeestr,"estimatefee") == 0 )
{
strcpy(coin->estimatefeestr,"estimatesmartfee");
err = 2;
@ -678,12 +707,13 @@ again:
}
} else rate = coin->rate;
} else rate = coin->rate;
coin->rate = rate;
return(rate);
}
double LP_getestimatedrate(struct iguana_info *coin)
{
double rate = 0.00000020;
double rate = 0.00000005;
if ( coin == 0 )
return(rate);
if ( (rate= _LP_getestimatedrate(coin)) <= 0. )

1
iguana/exchanges/LP_signatures.c

@ -681,6 +681,7 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_
if ( bits256_nonz(qp->srchash) != 0 )
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg));
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg));
LP_reserved_msg(0,qp->srccoin,qp->destcoin,zero,clonestr(msg));
free(msg);
/*portable_mutex_lock(&LP_reservedmutex);
if ( num_Reserved_msgs[1] < sizeof(Reserved_msgs[1])/sizeof(*Reserved_msgs[1])-2 )

2
iguana/exchanges/LP_transaction.c

@ -1180,7 +1180,7 @@ int32_t LP_vins_select(void *ctx,struct iguana_info *coin,int64_t *totalp,int64_
vp->suppress_pubkeys = suppress_pubkeys;
vp->ignore_cltverr = ignore_cltverr;
jaddi(vins,LP_inputjson(up->U.txid,up->U.vout,spendscriptstr));
LP_unavailableset(up->U.txid,up->U.vout,(uint32_t)time(NULL)+LP_RESERVETIME,G.LP_mypub25519);
LP_unavailableset(up->U.txid,up->U.vout,(uint32_t)time(NULL)+LP_RESERVETIME*2,G.LP_mypub25519);
if ( remains <= 0 && i >= numpre-1 )
break;
if ( numunspents < 0 || n >= LP_MAXVINS )

21
iguana/exchanges/LP_utxo.c

@ -27,6 +27,27 @@ struct LP_inuse_info
} LP_inuse[1024];
int32_t LP_numinuse;
cJSON *LP_inuse_json()
{
int32_t i; cJSON *item,*array; struct LP_inuse_info *lp;
array = cJSON_CreateArray();
for (i=0; i<LP_numinuse; i++)
{
lp = &LP_inuse[i];
if ( lp->expiration != 0 )
{
item = cJSON_CreateObject();
jaddnum(item,"expiration",lp->expiration);
jaddbits256(item,"txid",lp->txid);
jaddnum(item,"vout",lp->vout);
if ( bits256_nonz(lp->otherpub) != 0 )
jaddbits256(item,"otherpub",lp->otherpub);
jaddi(array,item);
}
}
return(array);
}
struct LP_inuse_info *_LP_inuse_find(bits256 txid,int32_t vout)
{
int32_t i;

3
iguana/exchanges/inuse

@ -0,0 +1,3 @@
#!/bin/bash
source userpass
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"inuse\"}"
Loading…
Cancel
Save