You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1072 lines
37 KiB

8 years ago
/******************************************************************************
* Copyright © 2014-2017 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; uint64_t minsatoshis,maxsatoshis,depth; uint32_t timestamp; int32_t numutxos; char coinaddr[64]; };
8 years ago
8 years ago
struct LP_priceinfo
{
char symbol[16];
uint64_t coinbits;
int32_t ind,pad;
8 years ago
double diagval,high[2],low[2],last[2],bid[2],ask[2]; //volume,btcvolume,prevday; // mostly bittrex info
8 years ago
double relvals[LP_MAXPRICEINFOS];
double myprices[LP_MAXPRICEINFOS];
8 years ago
double minprices[LP_MAXPRICEINFOS]; // autoprice
8 years ago
double margins[LP_MAXPRICEINFOS];
7 years ago
double offsets[LP_MAXPRICEINFOS];
double factors[LP_MAXPRICEINFOS];
8 years ago
//double maxprices[LP_MAXPRICEINFOS]; // autofill of base/rel
//double relvols[LP_MAXPRICEINFOS];
8 years ago
FILE *fps[LP_MAXPRICEINFOS];
8 years ago
} LP_priceinfos[LP_MAXPRICEINFOS];
8 years ago
int32_t LP_numpriceinfos;
8 years ago
8 years ago
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;
7 years ago
struct LP_priceinfo *LP_priceinfo(int32_t ind)
{
if ( ind < 0 || ind >= LP_MAXPRICEINFOS )
return(0);
else return(&LP_priceinfos[ind]);
}
7 years ago
char *LP_priceinfostr(int32_t ind)
{
if ( ind < 0 || ind >= LP_MAXPRICEINFOS )
return("UNKNOWN");
else return(LP_priceinfos[ind].symbol);
}
8 years ago
int32_t LP_pricevalid(double price)
{
if ( price > SMALLVAL && isnan(price) == 0 && price < SATOSHIDEN )
return(1);
else return(0);
}
8 years ago
struct LP_priceinfo *LP_priceinfofind(char *symbol)
{
int32_t i; struct LP_priceinfo *pp; uint64_t coinbits;
8 years ago
if ( symbol == 0 || symbol[0] == 0 )
return(0);
8 years ago
if ( LP_numpriceinfos > 0 )
{
coinbits = stringbits(symbol);
pp = LP_priceinfos;
for (i=0; i<LP_numpriceinfos; i++,pp++)
if ( pp->coinbits == coinbits )
return(pp);
}
return(0);
}
7 years ago
int32_t LP_priceinfoind(char *symbol)
{
struct LP_priceinfo *pp;
if ( (pp= LP_priceinfofind(symbol)) != 0 )
return(pp->ind);
else return(-1);
}
8 years ago
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);
}
}
8 years ago
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)];
8 years ago
if ( base == 0 || rel == 0 )
return(0);
8 years ago
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_pubkeyinfo *LP_pubkey_rmd160find(uint8_t rmd160[20])
{
struct LP_pubkeyinfo *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_pubkeyinfo *pubp;
HASH_FIND(hh,coin->addresses,coinaddr,strlen(coinaddr),ap);
7 years ago
if ( ap != 0 && bits256_nonz(ap->pubkey) == 0 )
{
bitcoin_addr2rmd160(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_pubkeyinfo *pubp;
ap = calloc(1,sizeof(*ap));
safecopy(ap->coinaddr,coinaddr,sizeof(ap->coinaddr));
bitcoin_addr2rmd160(coin->taddr,&addrtype,rmd160,coinaddr);
if ( (pubp= LP_pubkey_rmd160find(rmd160)) != 0 )
{
ap->pubkey = pubp->pubkey;
memcpy(ap->pubsecp,pubp->pubsecp,sizeof(ap->pubsecp));
}
7 years ago
//printf("LP_ADDRESS %s ADD.(%s)\n",coin->symbol,coinaddr);
HASH_ADD_KEYPTR(hh,coin->addresses,ap->coinaddr,strlen(ap->coinaddr),ap);
return(ap);
}
8 years ago
struct LP_pubkeyinfo *LP_pubkeyfind(bits256 pubkey)
{
struct LP_pubkeyinfo *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_pubkeyinfo *LP_pubkeyadd(bits256 pubkey)
{
struct LP_pubkeyinfo *pubp=0;
7 years ago
portable_mutex_lock(&LP_pubkeymutex);
HASH_FIND(hh,LP_pubkeyinfos,&pubkey,sizeof(pubkey),pubp);
if ( pubp == 0 )
8 years ago
{
8 years ago
pubp = calloc(1,sizeof(*pubp));
pubp->pubkey = pubkey;
7 years ago
if ( bits256_cmp(G.LP_mypub25519,pubkey) == 0 )
7 years ago
{
7 years ago
memcpy(pubp->rmd160,G.LP_myrmd160,sizeof(pubp->rmd160));
7 years ago
memcpy(pubp->pubsecp,G.LP_pubsecp,sizeof(pubp->pubsecp));
}
8 years ago
HASH_ADD_KEYPTR(hh,LP_pubkeyinfos,&pubp->pubkey,sizeof(pubp->pubkey),pubp);
7 years ago
HASH_FIND(hh,LP_pubkeyinfos,&pubkey,sizeof(pubkey),pubp);
if ( pubp == 0 )
8 years ago
printf("pubkeyadd find error after add\n");
}
7 years ago
portable_mutex_unlock(&LP_pubkeymutex);
8 years ago
return(pubp);
}
8 years ago
int32_t LP_pubkey_istrusted(bits256 pubkey)
{
struct LP_pubkeyinfo *pubp;
7 years ago
if ( (pubp= LP_pubkeyadd(pubkey)) != 0 )
8 years ago
return(pubp->istrusted != 0);
return(0);
}
8 years ago
char *LP_pubkey_trustset(bits256 pubkey,uint32_t trustval)
8 years ago
{
struct LP_pubkeyinfo *pubp;
7 years ago
if ( (pubp= LP_pubkeyadd(pubkey)) != 0 )
8 years ago
{
pubp->istrusted = trustval;
return(clonestr("{\"result\":\"success\"}"));
}
return(clonestr("{\"error\":\"pubkey not found\"}"));
}
7 years ago
char *LP_pubkey_trusted()
{
struct LP_pubkeyinfo *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));
}
7 years ago
uint64_t LP_unspents_metric(struct iguana_info *coin,char *coinaddr)
{
cJSON *array,*item; int32_t i,n; uint64_t metric=0,total;
7 years ago
LP_listunspent_both(coin->symbol,coinaddr,0);
7 years ago
if ( (array= LP_address_utxos(coin,coinaddr,1)) != 0 )
{
total = 0;
if ( (n= cJSON_GetArraySize(array)) > 0 )
{
for (i=0; i<n; i++)
{
item = jitem(array,i);
total += j64bits(item,"value");
}
}
metric = _LP_unspents_metric(total,n);
}
return(metric);
}
8 years ago
cJSON *LP_pubkeyjson(struct LP_pubkeyinfo *pubp)
8 years ago
{
7 years ago
int32_t baseid,relid; char *base,hexstr[67],hexstr2[67],sigstr[256]; double price; cJSON *item,*array,*obj;
8 years ago
obj = cJSON_CreateObject();
array = cJSON_CreateArray();
for (baseid=0; baseid<LP_numpriceinfos; baseid++)
8 years ago
{
8 years ago
base = LP_priceinfos[baseid].symbol;
for (relid=0; relid<LP_numpriceinfos; relid++)
8 years ago
{
8 years ago
price = pubp->matrix[baseid][relid];
if ( LP_pricevalid(price) > 0 )
8 years ago
{
item = cJSON_CreateArray();
jaddistr(item,base);
jaddistr(item,LP_priceinfos[relid].symbol);
jaddinum(item,price);
jaddi(array,item);
}
8 years ago
}
}
8 years ago
jaddbits256(obj,"pubkey",pubp->pubkey);
7 years ago
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);
8 years ago
jaddnum(obj,"timestamp",pubp->timestamp);
jadd(obj,"asks",array);
8 years ago
if ( pubp->istrusted != 0 )
jaddnum(obj,"istrusted",pubp->istrusted);
8 years ago
return(obj);
8 years ago
}
8 years ago
char *LP_prices()
8 years ago
{
8 years ago
struct LP_pubkeyinfo *pubp,*tmp; cJSON *array = cJSON_CreateArray();
HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp)
8 years ago
{
8 years ago
jaddi(array,LP_pubkeyjson(pubp));
8 years ago
}
8 years ago
return(jprint(array,1));
8 years ago
}
7 years ago
void LP_prices_parse(struct LP_peerinfo *peer,cJSON *obj)
8 years ago
{
7 years ago
struct LP_pubkeyinfo *pubp; struct LP_priceinfo *basepp,*relpp; uint32_t timestamp; bits256 pubkey; cJSON *asks,*item; uint8_t rmd160[20]; int32_t i,n,relid,mismatch; char *base,*rel,*hexstr; double askprice; uint32_t now;
7 years ago
now = (uint32_t)time(NULL);
8 years ago
pubkey = jbits256(obj,"pubkey");
if ( bits256_nonz(pubkey) != 0 && (pubp= LP_pubkeyadd(pubkey)) != 0 )
8 years ago
{
7 years ago
if ( (hexstr= jstr(obj,"rmd160")) != 0 && strlen(hexstr) == 2*sizeof(rmd160) )
decode_hex(rmd160,sizeof(rmd160),hexstr);
7 years ago
if ( memcmp(pubp->rmd160,rmd160,sizeof(rmd160)) != 0 )
mismatch = 1;
else mismatch = 0;
if ( bits256_cmp(pubkey,G.LP_mypub25519) == 0 && mismatch == 0 )
peer->needping = 0;
LP_pubkey_sigcheck(pubp,obj);
7 years ago
timestamp = juint(obj,"timestamp");
if ( timestamp > now )
timestamp = now;
7 years ago
if ( timestamp >= pubp->timestamp && (asks= jarray(&n,obj,"asks")) != 0 )
8 years ago
{
pubp->timestamp = timestamp;
for (i=0; i<n; i++)
{
item = jitem(asks,i);
base = jstri(item,0);
rel = jstri(item,1);
askprice = jdoublei(item,2);
8 years ago
if ( LP_pricevalid(askprice) > 0 )
8 years ago
{
8 years ago
if ( (basepp= LP_priceinfoptr(&relid,base,rel)) != 0 )
8 years ago
{
7 years ago
//char str[65]; printf("gotprice %s %s/%s (%d/%d) %.8f\n",bits256_str(str,pubkey),base,rel,basepp->ind,relid,askprice);
8 years ago
pubp->matrix[basepp->ind][relid] = askprice;
if ( (relpp= LP_priceinfofind(rel)) != 0 )
{
dxblend(&basepp->relvals[relpp->ind],askprice,0.9);
dxblend(&relpp->relvals[basepp->ind],1. / askprice,0.9);
}
8 years ago
}
8 years ago
}
}
}
8 years ago
}
8 years ago
}
7 years ago
void LP_peer_pricesquery(struct LP_peerinfo *peer)
8 years ago
{
char *retstr; cJSON *array; int32_t i,n;
7 years ago
if ( strcmp(peer->ipaddr,LP_myipaddr) == 0 )
return;
7 years ago
peer->needping = (uint32_t)time(NULL);
if ( (retstr= issue_LP_getprices(peer->ipaddr,peer->port)) != 0 )
8 years ago
{
7 years ago
#ifdef FROM_JS
printf("%s\n",retstr);
#endif
8 years ago
if ( (array= cJSON_Parse(retstr)) != 0 )
{
if ( is_cJSON_Array(array) && (n= cJSON_GetArraySize(array)) > 0 )
{
for (i=0; i<n; i++)
7 years ago
LP_prices_parse(peer,jitem(array,i));
8 years ago
}
free_json(array);
}
free(retstr);
}
7 years ago
if ( peer->needping != 0 )
{
7 years ago
//printf("%s needs ping\n",peer->ipaddr);
7 years ago
}
8 years ago
}
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 != 0 )
{
ptr->price = (double)ptr->Q.destsatoshis / ptr->Q.satoshis;
8 years ago
if ( LP_pricevalid(ptr->price) <= 0 )
8 years ago
ptr->price = 0.;
7 years ago
printf("LP_pricecache: set %s/%s ptr->price %.8f\n",base,rel,ptr->price);
8 years ago
}
printf(">>>>>>>>>> found %s/%s %.8f\n",base,rel,ptr->price);
8 years ago
return(ptr->price);
8 years ago
}
//char str[65]; printf("cachemiss %s/%s %s/v%d\n",base,rel,bits256_str(str,txid),vout);
8 years ago
return(0.);
8 years ago
}
void LP_priceinfoupdate(char *base,char *rel,double price)
{
struct LP_priceinfo *basepp,*relpp;
8 years ago
if ( LP_pricevalid(price) > 0 )
8 years ago
{
8 years ago
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;
}
8 years ago
}
}
8 years ago
double LP_myprice(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 )
{
8 years ago
*askp = basepp->myprices[relpp->ind];
if ( LP_pricevalid(*askp) > 0 )
8 years ago
{
8 years ago
val = relpp->myprices[basepp->ind];
if ( LP_pricevalid(val) > 0 )
8 years ago
{
*bidp = 1. / val;
return((*askp + *bidp) * 0.5);
}
else
{
*bidp = 0.;
return(*askp);
}
}
else
{
8 years ago
val = relpp->myprices[basepp->ind];
if ( LP_pricevalid(val) > 0 )
8 years ago
{
*bidp = 1. / val;
*askp = 0.;
return(*bidp);
}
}
}
return(0.);
}
char *LP_myprices()
{
int32_t baseid,relid; double bid,ask; char *base,*rel; cJSON *item,*array;
array = cJSON_CreateArray();
for (baseid=0; baseid<LP_numpriceinfos; baseid++)
{
base = LP_priceinfos[baseid].symbol;
for (relid=0; relid<LP_numpriceinfos; relid++)
{
rel = LP_priceinfos[relid].symbol;
if ( LP_myprice(&bid,&ask,base,rel) > 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));
8 years ago
}
8 years ago
int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price)
8 years ago
{
8 years ago
struct LP_priceinfo *basepp,*relpp; struct LP_pubkeyinfo *pubp;
8 years ago
*changedp = 0;
if ( base != 0 && rel != 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 )
8 years ago
{
7 years ago
8 years ago
if ( fabs(basepp->myprices[relpp->ind] - price) > SMALLVAL )
*changedp = 1;
8 years ago
basepp->myprices[relpp->ind] = price; // ask
8 years ago
//printf("LP_mypriceset base.%s rel.%s <- price %.8f\n",base,rel,price);
8 years ago
//relpp->myprices[basepp->ind] = (1. / price); // bid
7 years ago
if ( (pubp= LP_pubkeyadd(G.LP_mypub25519)) != 0 )
8 years ago
{
pubp->matrix[basepp->ind][relpp->ind] = price;
//pubp->matrix[relpp->ind][basepp->ind] = (1. / price);
pubp->timestamp = (uint32_t)time(NULL);
}
8 years ago
return(0);
} else return(-1);
}
double LP_price(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. )
price = basepp->relvals[relind];
}
return(price);
}
cJSON *LP_priceinfomatrix(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)
{
8 years ago
if ( ptr->timestamp < now-3600*2 || ptr->price == 0. )
8 years ago
continue;
LP_priceinfoupdate(ptr->Q.srccoin,ptr->Q.destcoin,ptr->price);
}
pp = LP_priceinfos;
total = m = 0;
for (i=0; i<LP_numpriceinfos; i++,pp++)
{
pp->diagval = sum = n = 0;
for (j=0; j<LP_numpriceinfos; j++)
{
if ( usemyprices == 0 || (val= pp->myprices[j]) == 0. )
val = pp->relvals[j];
8 years ago
if ( val > SMALLVAL )
8 years ago
{
sum += val;
n++;
}
}
if ( n > 0 )
{
pp->diagval = sum / n;
total += pp->diagval, m++;
}
}
if ( m > 0 )
{
pp = LP_priceinfos;
for (i=0; i<LP_numpriceinfos; i++,pp++)
{
8 years ago
if ( pp->diagval > SMALLVAL )
{
pp->diagval /= total;
jaddnum(vectorjson,pp->symbol,pp->diagval);
}
8 years ago
}
}
return(vectorjson);
}
struct LP_priceinfo *LP_priceinfoadd(char *symbol)
{
8 years ago
struct LP_priceinfo *pp; cJSON *retjson;
8 years ago
if ( symbol == 0 )
return(0);
8 years ago
if ( LP_numpriceinfos >= sizeof(LP_priceinfos)/sizeof(*LP_priceinfos) )
{
printf("cant add any more priceinfos\n");
return(0);
}
8 years ago
pp = &LP_priceinfos[LP_numpriceinfos];
memset(pp,0,sizeof(*pp));
safecopy(pp->symbol,symbol,sizeof(pp->symbol));
pp->coinbits = stringbits(symbol);
8 years ago
pp->ind = LP_numpriceinfos++;
8 years ago
LP_numpriceinfos++;
if ( (retjson= LP_priceinfomatrix(0)) != 0 )
free_json(retjson);
return(pp);
}
8 years ago
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;
8 years ago
if ( base == 0 || rel == 0 )
return(0);
8 years ago
if ( LP_pricevalid(price) > 0 )
8 years ago
{
8 years ago
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 )
8 years ago
{
8 years ago
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;
8 years ago
}
return(ptr);
}
static int _cmp_orderbook(const void *a,const void *b)
{
8 years ago
int32_t retval = 0;
8 years ago
#define ptr_a (*(struct LP_orderbookentry **)a)->price
#define ptr_b (*(struct LP_orderbookentry **)b)->price
8 years ago
if ( ptr_b > ptr_a )
8 years ago
retval = -1;
8 years ago
else if ( ptr_b < ptr_a )
retval = 1;
8 years ago
else
8 years ago
{
#undef ptr_a
#undef ptr_b
7 years ago
#define ptr_a ((struct LP_orderbookentry *)a)->maxsatoshis
#define ptr_b ((struct LP_orderbookentry *)b)->maxsatoshis
8 years ago
if ( ptr_b > ptr_a )
return(-1);
8 years ago
else if ( ptr_b < ptr_a )
return(1);
8 years ago
}
7 years ago
// 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);
8 years ago
return(retval);
8 years ago
#undef ptr_a
#undef ptr_b
}
7 years ago
cJSON *LP_orderbookjson(char *symbol,struct LP_orderbookentry *op)
8 years ago
{
8 years ago
cJSON *item = cJSON_CreateObject();
8 years ago
if ( LP_pricevalid(op->price) > 0 )
8 years ago
{
7 years ago
jaddstr(item,"coin",symbol);
7 years ago
jaddstr(item,"address",op->coinaddr);
8 years ago
jaddnum(item,"price",op->price);
7 years ago
jaddnum(item,"numutxos",op->numutxos);
7 years ago
jaddnum(item,"minvolume",dstr(op->minsatoshis)*0.8);
jaddnum(item,"maxvolume",dstr(op->maxsatoshis)*0.8);
jaddnum(item,"depth",dstr(op->depth)*0.8);
8 years ago
jaddbits256(item,"pubkey",op->pubkey);
7 years ago
jaddnum(item,"age",time(NULL)-op->timestamp);
8 years ago
}
return(item);
}
struct LP_orderbookentry *LP_orderbookentry(char *address,char *base,char *rel,double price,int32_t numutxos,uint64_t minsatoshis,uint64_t maxsatoshis,bits256 pubkey,uint32_t timestamp,uint64_t depth)
8 years ago
{
struct LP_orderbookentry *op;
if ( (op= calloc(1,sizeof(*op))) != 0 )
{
7 years ago
safecopy(op->coinaddr,address,sizeof(op->coinaddr));
8 years ago
op->price = price;
7 years ago
op->numutxos = numutxos;
7 years ago
op->minsatoshis = minsatoshis;
7 years ago
op->maxsatoshis = maxsatoshis;
8 years ago
op->pubkey = pubkey;
7 years ago
op->timestamp = timestamp;
op->depth = depth;
8 years ago
}
return(op);
}
7 years ago
void LP_pubkeys_query()
{
uint8_t zeroes[20]; bits256 zero; cJSON *reqjson; struct LP_pubkeyinfo *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 )
{
reqjson = cJSON_CreateObject();
jaddstr(reqjson,"method","wantnotify");
jaddbits256(reqjson,"pub",pubp->pubkey);
7 years ago
//printf("LP_pubkeys_query %s\n",jprint(reqjson,0));
7 years ago
LP_reserved_msg("","",zero,jprint(reqjson,1));
}
}
}
8 years ago
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)
8 years ago
{
char coinaddr[64]; uint8_t zeroes[20]; struct LP_pubkeyinfo *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; uint64_t minsatoshis,maxsatoshis,balance,depth;
8 years ago
if ( (basepp= LP_priceinfoptr(&relid,base,rel)) != 0 )
baseid = basepp->ind;
else return(num);
7 years ago
if ( (basecoin= LP_coinfind(base)) == 0 )
return(-1);
8 years ago
now = (uint32_t)time(NULL);
oldest = now - duration;
7 years ago
memset(zeroes,0,sizeof(zeroes));
depth = 0;
7 years ago
HASH_ITER(hh,LP_pubkeyinfos,pubp,tmp)
8 years ago
{
7 years ago
if ( memcmp(zeroes,pubp->rmd160,sizeof(pubp->rmd160)) == 0 )
7 years ago
{
7 years ago
//printf("skip pubp since no rmd160\n");
8 years ago
continue;
7 years ago
}
7 years ago
if ( pubp->timestamp < oldest )
continue;
7 years ago
bitcoin_address(coinaddr,basecoin->taddr,basecoin->pubtype,pubp->rmd160,sizeof(pubp->rmd160));
7 years ago
minsatoshis = maxsatoshis = n = 0;
7 years ago
ap = 0;
7 years ago
if ( (price= pubp->matrix[baseid][relid]) > SMALLVAL )
8 years ago
{
7 years ago
if ( (ap= LP_addressfind(basecoin,coinaddr)) != 0 )
7 years ago
{
n = LP_address_minmax(&balance,&minsatoshis,&maxsatoshis,ap);
7 years ago
if ( polarity > 0 )
7 years ago
{
balance *= price;
7 years ago
minsatoshis *= price;
7 years ago
maxsatoshis *= price;
7 years ago
}
depth += balance;
7 years ago
//printf("%s/%s %s n.%d ap->n.%d %.8f\n",base,rel,coinaddr,n,ap->n,dstr(ap->total));
7 years ago
}
if ( (op= LP_orderbookentry(coinaddr,base,rel,polarity > 0 ? price : 1./price,n,minsatoshis,maxsatoshis,pubp->pubkey,pubp->timestamp,depth)) != 0 )
7 years ago
{
*arrayp = realloc(*arrayp,sizeof(*(*arrayp)) * (num+1));
(*arrayp)[num++] = op;
}
8 years ago
}
7 years ago
//printf("pubp.(%s) %.8f %p\n",coinaddr,price,ap);
8 years ago
}
return(num);
}
8 years ago
char *LP_orderbook(char *base,char *rel,int32_t duration)
8 years ago
{
7 years ago
uint32_t now,i; 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;
7 years ago
basecoin = LP_coinfind(base);
relcoin = LP_coinfind(rel);
if ( basecoin == 0 || relcoin == 0 )
return(clonestr("{\"error\":\"base or rel not added\"}"));
8 years ago
if ( (basepp= LP_priceinfofind(base)) == 0 || (relpp= LP_priceinfofind(rel)) == 0 )
return(clonestr("{\"error\":\"base or rel not added\"}"));
8 years ago
if ( duration <= 0 )
7 years ago
{
if ( duration < 0 )
suppress_prefetch = 1;
8 years ago
duration = LP_ORDERBOOK_DURATION;
7 years ago
}
7 years ago
LP_pubkeys_query();
8 years ago
baseid = basepp->ind;
relid = relpp->ind;
8 years ago
now = (uint32_t)time(NULL);
7 years ago
basecoin->obooktime = now;
relcoin->obooktime = now;
8 years ago
cachenumbids = numbids, cachenumasks = numasks;
8 years ago
//printf("start cache.(%d %d) numbids.%d numasks.%d\n",cachenumbids,cachenumasks,numbids,numasks);
8 years ago
numasks = LP_orderbook_utxoentries(now,1,base,rel,&asks,numasks,cachenumasks,duration);
numbids = LP_orderbook_utxoentries(now,-1,rel,base,&bids,numbids,cachenumbids,duration);
8 years ago
retjson = cJSON_CreateObject();
array = cJSON_CreateArray();
if ( numbids > 1 )
7 years ago
qsort(bids,numbids,sizeof(*bids),_revcmp_orderbook);
8 years ago
if ( numasks > 1 )
8 years ago
{
8 years ago
//for (i=0; i<numasks; i++)
// printf("%.8f ",asks[i]->price);
//printf(" -> ");
8 years ago
qsort(asks,numasks,sizeof(*asks),_cmp_orderbook);
8 years ago
//for (i=0; i<numasks; i++)
// printf("%.8f ",asks[i]->price);
//printf("sorted asks.%d\n",numasks);
8 years ago
}
7 years ago
for (i=n=0; i<numbids; i++)
8 years ago
{
7 years ago
jaddi(array,LP_orderbookjson(rel,bids[i]));
7 years ago
if ( suppress_prefetch == 0 && n < 7 && bids[i]->numutxos == 0 )
7 years ago
{
7 years ago
//printf("bid ping %s %s\n",rel,bids[i]->coinaddr);
7 years ago
LP_address(relcoin,bids[i]->coinaddr);
7 years ago
if ( relcoin->electrum == 0 )
7 years ago
LP_listunspent_issue(rel,bids[i]->coinaddr,0);
7 years ago
LP_listunspent_query(rel,bids[i]->coinaddr);
7 years ago
n++;
}
8 years ago
free(bids[i]);
bids[i] = 0;
}
7 years ago
if ( n > 0 && relcoin->lastmonitor > 3600 )
relcoin->lastmonitor -= 3600;
8 years ago
jadd(retjson,"bids",array);
jaddnum(retjson,"numbids",numbids);
array = cJSON_CreateArray();
7 years ago
for (i=n=0; i<numasks; i++)
8 years ago
{
7 years ago
jaddi(array,LP_orderbookjson(base,asks[i]));
7 years ago
if ( suppress_prefetch == 0 && n < 7 && asks[i]->numutxos == 0 )
7 years ago
{
7 years ago
//printf("ask ping %s %s\n",base,asks[i]->coinaddr);
7 years ago
LP_address(basecoin,asks[i]->coinaddr);
7 years ago
if ( basecoin->electrum == 0 )
7 years ago
LP_listunspent_issue(base,asks[i]->coinaddr,0);
7 years ago
LP_listunspent_query(base,asks[i]->coinaddr);
7 years ago
n++;
}
8 years ago
free(asks[i]);
asks[i] = 0;
}
7 years ago
if ( n > 0 && basecoin->lastmonitor > 3600 )
7 years ago
basecoin->lastmonitor -= 3600;
8 years ago
jadd(retjson,"asks",array);
8 years ago
jaddnum(retjson,"numasks",numasks);
8 years ago
jaddstr(retjson,"base",base);
jaddstr(retjson,"rel",rel);
jaddnum(retjson,"timestamp",now);
8 years ago
if ( bids != 0 )
free(bids);
if ( asks != 0 )
free(asks);
8 years ago
return(jprint(retjson,1));
}
8 years ago
7 years ago
uint64_t LP_KMDvalue(struct iguana_info *coin,uint64_t balance)
{
cJSON *bids,*asks,*orderbook,*item; double bid=0,ask=0,price = 0.; int32_t numasks,numbids; char *retstr; uint64_t KMDvalue=0;
if ( balance != 0 )
{
if ( strcmp(coin->symbol,"KMD") == 0 )
KMDvalue = balance;
7 years ago
else if ( (retstr= LP_orderbook(coin->symbol,"KMD",-1)) != 0 )
7 years ago
{
if ( (orderbook= cJSON_Parse(retstr)) != 0 )
{
if ( (asks= jarray(&numasks,orderbook,"asks")) != 0 && numasks > 0 )
{
item = jitem(asks,0);
price = ask = jdouble(item,"price");
7 years ago
//printf("%s/%s ask %.8f\n",coin->symbol,"KMD",ask);
7 years ago
}
if ( (bids= jarray(&numbids,orderbook,"bids")) != 0 && numbids > 0 )
{
item = jitem(asks,0);
bid = jdouble(item,"price");
if ( price == 0. )
price = bid;
else price = (bid + ask) * 0.5;
7 years ago
//printf("%s/%s bid %.8f ask %.8f price %.8f\n",coin->symbol,"KMD",bid,ask,price);
7 years ago
}
7 years ago
KMDvalue = price * balance;
7 years ago
free_json(orderbook);
}
free(retstr);
}
}
return(KMDvalue);
}
8 years ago
void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32])
8 years ago
{
8 years ago
LP_priceinfoupdate(base,rel,price);
8 years ago
}
8 years ago
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);
}
8 years ago
cJSON *LP_pricearray(char *base,char *rel,uint32_t firsttime,uint32_t lasttime,int32_t timescale)
8 years ago
{
8 years ago
cJSON *retarray; char askfname[1024],bidfname[1024]; uint64_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;
8 years ago
if ( timescale <= 0 )
timescale = 60;
8 years ago
if ( lasttime == 0 )
lasttime = (uint32_t)-1;
8 years ago
LP_pricefname(askfname,base,rel);
LP_pricefname(bidfname,rel,base);
retarray = cJSON_CreateArray();
8 years ago
lastbidi = lastaski = 0;
8 years ago
numbids = numasks = 0;
bidsum = asksum = askemit = bidemit = highbid = lowbid = highask = lowask = 0.;
if ( (bidfp= fopen(bidfname,"rb")) != 0 && (askfp= fopen(askfname,"rb")) != 0 )
{
8 years ago
while ( bidfp != 0 || askfp != 0 )
8 years ago
{
8 years ago
bidi = aski = 0;
8 years ago
bidemit = askemit = bidemit2 = askemit2 = 0.;
8 years ago
if ( bidfp != 0 && fread(&bidnow,1,sizeof(bidnow),bidfp) == sizeof(bidnow) && fread(&bidprice64,1,sizeof(bidprice64),bidfp) == sizeof(bidprice64) )
8 years ago
{
8 years ago
//printf("bidnow.%u %.8f\n",bidnow,dstr(bidprice64));
8 years ago
if ( bidnow != 0 && bidprice64 != 0 && bidnow >= firsttime && bidnow <= lasttime )
8 years ago
{
8 years ago
bidi = bidnow / timescale;
if ( bidi != lastbidi )
{
if ( bidsum != 0. && numbids != 0 )
8 years ago
{
8 years ago
bidemit = bidsum / numbids;
8 years ago
bidemit2 = highbid;
}
bidsum = highbid = lowbid = 0.;
8 years ago
numbids = 0;
}
8 years ago
if ( (bid= 1. / dstr(bidprice64)) != 0. )
8 years ago
{
if ( bid > highbid )
highbid = bid;
if ( lowbid == 0. || bid < lowbid )
lowbid = bid;
bidsum += bid;
numbids++;
8 years ago
//printf("bidi.%u num.%d %.8f [%.8f %.8f]\n",bidi,numbids,bid,lowbid,highbid);
8 years ago
}
8 years ago
}
8 years ago
} else fclose(bidfp), bidfp = 0;
if ( askfp != 0 && fread(&asknow,1,sizeof(asknow),askfp) == sizeof(asknow) && fread(&askprice64,1,sizeof(askprice64),askfp) == sizeof(askprice64) )
8 years ago
{
8 years ago
//printf("asknow.%u %.8f\n",asknow,dstr(askprice64));
8 years ago
if ( asknow != 0 && askprice64 != 0 && asknow >= firsttime && asknow <= lasttime )
8 years ago
{
8 years ago
aski = asknow / timescale;
if ( aski != lastaski )
{
if ( asksum != 0. && numasks != 0 )
8 years ago
{
8 years ago
askemit = asksum / numasks;
8 years ago
askemit2 = lowask;
}
asksum = highask = lowask = 0.;
8 years ago
numasks = 0;
}
if ( (ask= dstr(askprice64)) != 0. )
{
if ( ask > highask )
highask = ask;
if ( lowask == 0. || ask < lowask )
lowask = ask;
asksum += ask;
numasks++;
8 years ago
//printf("aski.%u num.%d %.8f [%.8f %.8f]\n",aski,numasks,ask,lowask,highask);
8 years ago
}
8 years ago
}
8 years ago
} else fclose(askfp), askfp = 0;
if ( bidemit != 0. || askemit != 0. )
8 years ago
{
8 years ago
if ( bidemit != 0. && askemit != 0. && lastbidi == lastaski )
8 years ago
{
8 years ago
LP_priceitemadd(retarray,lastbidi * timescale,bidemit,askemit,bidemit2,askemit2);
8 years ago
highbid = lowbid = highask = lowask = 0.;
8 years ago
}
8 years ago
else
8 years ago
{
8 years ago
if ( bidemit != 0. )
{
8 years ago
printf("bidonly %.8f %.8f\n",bidemit,highbid);
8 years ago
LP_priceitemadd(retarray,lastbidi * timescale,bidemit,0.,bidemit2,0.);
8 years ago
highbid = lowbid = 0.;
}
if ( askemit != 0. )
{
8 years ago
printf("askonly %.8f %.8f\n",askemit,lowask);
8 years ago
LP_priceitemadd(retarray,lastaski * timescale,0.,askemit,0.,askemit2);
8 years ago
highask = lowask = 0.;
}
8 years ago
}
}
8 years ago
if ( bidi != 0 )
lastbidi = bidi;
if ( aski != 0 )
lastaski = aski;
8 years ago
}
8 years ago
} else printf("couldnt open either %s %p or %s %p\n",bidfname,bidfp,askfname,askfp);
8 years ago
if ( bidfp != 0 )
fclose(bidfp);
if ( askfp != 0 )
fclose(askfp);
return(retarray);
}
8 years ago
void LP_pricefeedupdate(bits256 pubkey,char *base,char *rel,double price)
{
8 years ago
struct LP_priceinfo *basepp,*relpp; uint32_t now; uint64_t price64; struct LP_pubkeyinfo *pubp; char str[65],fname[512]; FILE *fp;
8 years ago
//printf("check PRICEFEED UPDATE.(%s/%s) %.8f %s\n",base,rel,price,bits256_str(str,pubkey));
8 years ago
if ( LP_pricevalid(price) > 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 )
8 years ago
{
8 years ago
if ( (fp= basepp->fps[relpp->ind]) == 0 )
{
8 years ago
LP_pricefname(fname,base,rel);
8 years ago
fp = basepp->fps[relpp->ind] = OS_appendfile(fname);
}
8 years ago
if ( fp != 0 )
8 years ago
{
now = (uint32_t)time(NULL);
price64 = price * SATOSHIDEN;
fwrite(&now,1,sizeof(now),fp);
fwrite(&price64,1,sizeof(price64),fp);
fflush(fp);
}
if ( (fp= relpp->fps[basepp->ind]) == 0 )
{
sprintf(fname,"%s/PRICES/%s_%s",GLOBAL_DBDIR,rel,base);
fp = relpp->fps[basepp->ind] = OS_appendfile(fname);
}
8 years ago
if ( fp != 0 )
8 years ago
{
now = (uint32_t)time(NULL);
price64 = (1. / price) * SATOSHIDEN;
fwrite(&now,1,sizeof(now),fp);
fwrite(&price64,1,sizeof(price64),fp);
fflush(fp);
}
7 years ago
if ( (pubp= LP_pubkeyadd(pubkey)) != 0 )
8 years ago
{
7 years ago
if ( fabs(pubp->matrix[basepp->ind][relpp->ind] - price) > SMALLVAL )
8 years ago
{
7 years ago
if ( (rand() % 5000) == 0 )
7 years ago
printf("PRICEFEED UPDATE.(%-6s/%6s) %12.8f %s %12.8f\n",base,rel,price,bits256_str(str,pubkey),1./price);
8 years ago
pubp->matrix[basepp->ind][relpp->ind] = price;
dxblend(&basepp->relvals[relpp->ind],price,0.9);
dxblend(&relpp->relvals[basepp->ind],1. / price,0.9);
}
8 years ago
pubp->timestamp = (uint32_t)time(NULL);
7 years ago
} else printf("error finding pubkey entry %s, ok if rare\n",bits256_str(str,pubkey));
8 years ago
}
7 years ago
//else if ( (rand() % 100) == 0 )
// printf("error finding %s/%s %.8f\n",base,rel,price);
8 years ago
}