int32_t calc_generators(struct crypto777_node *nn,uint64_t sortbuf[MAXPEERS+1][4],struct consensus_model *mp,uint32_t blocknum) { struct crypto777_generator gen; struct crypto777_block *prev; struct crypto777_node *peer; uint64_t stake; int32_t i,n = 0; uint32_t timestamp = (uint32_t)time(NULL); if ( (prev= mp->blocks[blocknum-1]) == 0 ) return(-1); for (i=0; i<=nn->numpeers; i++) { peer = (i < nn->numpeers) ? nn->peers[i] : nn; if ( peer != 0 ) { stake = calc_stake(peer,mp); sortbuf[n][0] = calc_generator(&gen,stake,peer->email,timestamp,prev->gen.metric,prev->gen.hash,mp->POW_DIFF); sortbuf[n][1] = peer->nodeid; sortbuf[n][2] = gen.hit; sortbuf[n][3] = prev->gen.metric; n++; } } if ( n > 1 ) revsort64s(&sortbuf[0][0],n,sizeof(sortbuf[0])); return(n); }
double instantdex_aveprice(struct supernet_info *myinfo,struct exchange_quote *sortbuf,int32_t max,double *totalvolp,char *base,char *rel,double relvolume,cJSON *argjson) { char *str; double totalvol,pricesum; uint32_t timestamp; struct exchange_quote quote; int32_t i,n,dir,num,depth = 100; struct exchange_info *exchange; struct exchange_request *req,*active[64]; timestamp = (uint32_t)time(NULL); if ( relvolume < 0. ) relvolume = -relvolume, dir = -1; else dir = 1; memset(sortbuf,0,sizeof(*sortbuf) * max); if ( base != 0 && rel != 0 && relvolume > SMALLVAL ) { for (i=num=0; i<myinfo->numexchanges && num < sizeof(active)/sizeof(*active); i++) { if ( (exchange= myinfo->tradingexchanges[i]) != 0 ) { if ( (req= exchanges777_baserelfind(exchange,base,rel,'M')) == 0 ) { if ( (str= exchanges777_Qprices(exchange,base,rel,30,1,depth,argjson,1,exchange->commission)) != 0 ) free(str); req = exchanges777_baserelfind(exchange,base,rel,'M'); } if ( req == 0 ) { if ( (*exchange->issue.supports)(exchange,base,rel,argjson) != 0 ) printf("unexpected null req.(%s %s) %s\n",base,rel,exchange->name); } else { //printf("active.%s\n",exchange->name); active[num++] = req; } } } for (i=n=0; i<num; i++) { if ( dir < 0 && active[i]->numbids > 0 ) n = instantdex_updatesources(sortbuf,n,max,i,1,active[i]->bidasks,active[i]->numbids); else if ( dir > 0 && active[i]->numasks > 0 ) n = instantdex_updatesources(sortbuf,n,max,i,-1,&active[i]->bidasks[1],active[i]->numasks); } //printf("dir.%d %s/%s numX.%d n.%d\n",dir,base,rel,num,n); if ( dir < 0 ) revsort64s(&sortbuf[0].satoshis,n,sizeof(*sortbuf)); else sort64s(&sortbuf[0].satoshis,n,sizeof(*sortbuf)); for (totalvol=pricesum=i=0; i<n && totalvol < relvolume; i++) { quote = sortbuf[i]; //printf("n.%d i.%d price %.8f %.8f %.8f\n",n,i,dstr(sortbuf[i].satoshis),sortbuf[i].price,quote.volume); if ( quote.satoshis != 0 ) { pricesum += (quote.price * quote.volume); totalvol += quote.volume; //printf("i.%d of %d %12.8f vol %.8f %s | aveprice %.8f total vol %.8f\n",i,n,sortbuf[i].price,quote.volume,active[quote.val]->exchange->name,pricesum/totalvol,totalvol); } } if ( totalvol > 0. ) { *totalvolp = pricesum; return(pricesum / totalvol); } } *totalvolp = 0; return(0); }