3 int32_t NUM_PRICES; uint32_t *PVALS;
5 #define MAX_CURRENCIES 32
6 char CURRENCIES[][8] = { "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", // major currencies
7 "CNY", "RUB", "MXN", "BRL", "INR", "HKD", "TRY", "ZAR", "PLN", "NOK", "SEK", "DKK", "CZK", "HUF", "ILS", "KRW", "MYR", "PHP", "RON", "SGD", "THB", "BGN", "IDR", "HRK",
10 uint64_t M1SUPPLY[] = { 3317900000000, 6991604000000, 667780000000000, 1616854000000, 331000000000, 861909000000, 584629000000, 46530000000, // major currencies
11 45434000000000, 16827000000, 3473357229000, 306435000000, 27139000000000, 2150641000000, 347724099000, 1469583000000, 749543000000, 1826110000000, 2400434000000, 1123925000000, 3125276000000, 13975000000000, 317657000000, 759706000000000, 354902000000, 2797061000000, 162189000000, 163745000000, 1712000000000, 39093000000, 1135490000000000, 80317000000,
15 uint32_t MINDENOMS[] = { MIND, MIND, 100*MIND, MIND, MIND, MIND, MIND, MIND, // major currencies
16 10*MIND, 100*MIND, 10*MIND, MIND, 100*MIND, 10*MIND, MIND, 10*MIND, MIND, 10*MIND, 10*MIND, 10*MIND, 10*MIND, 100*MIND, MIND, 1000*MIND, MIND, 10*MIND, MIND, MIND, 10*MIND, MIND, 10000*MIND, 10*MIND, // end of currencies
20 uint64_t komodo_paxvol(uint64_t volume,uint64_t price)
22 if ( volume < 10000000000 )
23 return((volume * price) / 1000000000);
24 else if ( volume < (uint64_t)10 * 10000000000 )
25 return((volume * (price / 10)) / 100000000);
26 else if ( volume < (uint64_t)100 * 10000000000 )
27 return(((volume / 10) * (price / 10)) / 10000000);
28 else if ( volume < (uint64_t)1000 * 10000000000 )
29 return(((volume / 10) * (price / 100)) / 1000000);
30 else if ( volume < (uint64_t)10000 * 10000000000 )
31 return(((volume / 100) * (price / 100)) / 100000);
32 else if ( volume < (uint64_t)100000 * 10000000000 )
33 return(((volume / 100) * (price / 1000)) / 10000);
34 else if ( volume < (uint64_t)1000000 * 10000000000 )
35 return(((volume / 1000) * (price / 1000)) / 1000);
36 else if ( volume < (uint64_t)10000000 * 10000000000 )
37 return(((volume / 1000) * (price / 10000)) / 100);
38 else return(((volume / 10000) * (price / 10000)) / 10);
41 void pax_rank(uint64_t *ranked,uint32_t *pvals)
43 int32_t i; uint64_t vals[32],sum = 0;
46 vals[i] = komodo_paxvol(M1SUPPLY[i] / MINDENOMS[i],pvals[i]);
51 ranked[i] = (vals[i] * 1000000000) / sum;
52 printf("%.6f ",(double)ranked[i]/1000000000.);
56 int32_t dpow_readprices(uint8_t *data,uint32_t *timestampp,double *KMDBTCp,double *BTCUSDp,double *CNYUSDp,uint32_t *pvals)
58 uint32_t kmdbtc,btcusd,cnyusd; int32_t i,n,len = 0;
59 len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)timestampp);
60 len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&n);
61 len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&kmdbtc); // /= 1000
62 len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&btcusd); // *= 1000
63 len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&cnyusd);
64 *KMDBTCp = ((double)kmdbtc / (1000000000. * 1000.));
65 *BTCUSDp = ((double)btcusd / (1000000000. / 1000.));
66 *CNYUSDp = ((double)cnyusd / 1000000000.);
69 len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&pvals[i]);
70 //printf("%u ",pvals[i]);
75 //printf("OP_RETURN prices\n");
79 /*uint32_t PAX_val32(double val)
81 uint32_t val32 = 0; struct price_resolution price;
82 if ( (price.Pval= val*1000000000) != 0 )
84 if ( price.Pval > 0xffffffff )
85 printf("Pval overflow error %lld\n",(long long)price.Pval);
86 else val32 = (uint32_t)price.Pval;
91 double PAX_val(uint32_t pval,int32_t baseid)
93 //printf("PAX_val baseid.%d pval.%u\n",baseid,pval);
94 if ( baseid >= 0 && baseid < MAX_CURRENCIES )
95 return(((double)pval / 1000000000.) / MINDENOMS[baseid]);
99 void komodo_pvals(int32_t height,uint32_t *pvals,uint8_t numpvals)
101 int32_t i,nonz; double KMDBTC,BTCUSD,CNYUSD; uint32_t kmdbtc,btcusd,cnyusd;
102 if ( numpvals >= 35 )
104 for (nonz=i=0; i<32; i++)
108 //printf("%u ",pvals[i]);
115 KMDBTC = ((double)kmdbtc / (1000000000. * 1000.));
116 BTCUSD = ((double)btcusd / (1000000000. / 1000.));
117 CNYUSD = ((double)cnyusd / 1000000000.);
118 PVALS = (uint32_t *)realloc(PVALS,(NUM_PRICES+1) * sizeof(*PVALS) * 36);
119 PVALS[36 * NUM_PRICES] = height;
120 memcpy(&PVALS[36 * NUM_PRICES + 1],pvals,sizeof(*pvals) * 35);
122 //printf("OP_RETURN.%d KMD %.8f BTC %.6f CNY %.6f NUM_PRICES.%d\n",height,KMDBTC,BTCUSD,CNYUSD,NUM_PRICES);
127 int32_t komodo_baseid(char *origbase)
129 int32_t i; char base[64];
130 for (i=0; origbase[i]!=0&&i<sizeof(base); i++)
131 base[i] = toupper((int32_t)(origbase[i] & 0xff));
133 for (i=0; i<=MAX_CURRENCIES; i++)
134 if ( strcmp(CURRENCIES[i],base) == 0 )
136 printf("illegal base.(%s) %s\n",origbase,base);
140 uint64_t komodo_paxcalc(uint32_t *pvals,int32_t baseid,int32_t relid,uint64_t volume)
142 uint32_t pvalb,pvalr,kmdbtc,btcusd; uint64_t sum,ranked[32]; int32_t i;
143 if ( (pvalb= pvals[baseid]) != 0 )
145 if ( relid == MAX_CURRENCIES )
147 kmdbtc = pvals[MAX_CURRENCIES];
148 btcusd = pvals[MAX_CURRENCIES + 1];
149 if ( pvals[USD] != 0 && kmdbtc != 0 && btcusd != 0 )
151 baseusd = ((uint64_t)pvalb * 1000000000) / pvals[USD];
152 kmdusd = ((uint64_t)kmdbtc * 1000000000) / btcusd;
153 //printf("base -> USD %llu, BTC %llu KMDUSD %llu\n",(long long)baseusd,(long long)btcusd,(long long)kmdusd);
154 return(volume * ((baseusd * 1000000000) / kmdusd));
157 else if ( baseid == relid )
159 pax_rank(ranked,pvals);
162 else if ( (pvalr= pvals[relid]) != 0 )
164 baserel = ((uint64_t)pvalb * 1000000000) / pvalr;
165 return(komodo_paxvol(volume,baserel));
171 uint64_t komodo_paxprice(int32_t height,char *base,char *rel,uint64_t volume)
173 int32_t baseid=-1,relid=-1,i,ht; uint32_t kmdbtc,btcusd,pvalb,pvalr,*ptr; uint64_t baserel,baseusd,kmdusd;
174 if ( (baseid= komodo_baseid(base)) >= 0 && (relid= komodo_baseid(rel)) >= 0 )
176 for (i=NUM_PRICES-1; i>=0; i--)
178 ptr = &PVALS[36 * i];
180 return(komodo_paxcalc(&ptr[1],baseid,relid,volume));
182 } else printf("paxprice invalid base.%s %d, rel.%s %d\n",base,baseid,rel,relid);
186 int32_t komodo_paxprices(uint32_t *timestamps,uint64_t *prices,int32_t max,int32_t width,char *base,char *rel)
188 int32_t baseid=-1,relid=-1,i,ht; uint32_t kmdbtc,btcusd,pvalb,pvalr,*ptr; uint64_t baserel,baseusd,kmdusd;
189 if ( (baseid= komodo_baseid(base)) >= 0 && (relid= komodo_baseid(rel)) >= 0 )
191 for (i=NUM_PRICES-1; i>=0; i--)
193 ptr = &PVALS[36 * i];
198 int32_t komodo_pax_opreturn(uint8_t *opret,int32_t maxsize)
200 static uint32_t lastcrc;
201 FILE *fp; char fname[512]; uint32_t crc32,check,timestamp; int32_t i,n,retval,fsize,len=0; uint8_t data[8192];
203 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodofeed");
205 sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodofeed");
207 if ( (fp= fopen(fname,"rb")) != 0 )
209 fseek(fp,0,SEEK_END);
210 fsize = (int32_t)ftell(fp);
212 if ( fsize <= maxsize-4 && fsize <= sizeof(data) && fsize > sizeof(crc32) )
214 if ( (retval= (int32_t)fread(data,1,fsize,fp)) == fsize )
216 len = iguana_rwnum(0,data,sizeof(crc32),(void *)&crc32);
217 check = calc_crc32(0,data+sizeof(crc32),(int32_t)(fsize-sizeof(crc32)));
218 if ( check == crc32 )
220 double KMDBTC,BTCUSD,CNYUSD; uint32_t pvals[128];
221 dpow_readprices(&data[len],×tamp,&KMDBTC,&BTCUSD,&CNYUSD,pvals);
222 if ( 0 && lastcrc != crc32 )
225 printf("%u ",pvals[i]);
226 printf("t%u n.%d KMD %f BTC %f CNY %f (%f)\n",timestamp,n,KMDBTC,BTCUSD,CNYUSD,CNYUSD!=0?1./CNYUSD:0);
228 if ( timestamp > time(NULL)-600 )
230 n = komodo_opreturnscript(opret,'P',data+sizeof(crc32),(int32_t)(fsize-sizeof(crc32)));
231 if ( 0 && lastcrc != crc32 )
234 printf("%02x",opret[i]);
235 printf(" coinbase opret[%d] crc32.%u:%u\n",n,crc32,check);
237 } else printf("t%u too old for %u\n",timestamp,(uint32_t)time(NULL));
239 } else printf("crc32 %u mismatch %u\n",crc32,check);
240 } else printf("fread.%d error != fsize.%d\n",retval,fsize);
241 } else printf("fsize.%d > maxsize.%d or data[%d]\n",fsize,maxsize,(int32_t)sizeof(data));