]>
Commit | Line | Data |
---|---|---|
1e81ccb7 | 1 | |
2 | ||
3 | int32_t NUM_PRICES; uint32_t *PVALS; | |
4 | ||
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", | |
8 | "KMD" }; | |
9 | ||
10 | uint32_t MINDENOMS[] = { 1000, 1000, 100000, 1000, 1000, 1000, 1000, 1000, // major currencies | |
11 | 10000, 100000, 10000, 1000, 100000, 10000, 1000, 10000, 1000, 10000, 10000, 10000, 10000, 100000, 1000, 1000000, 1000, 10000, 1000, 1000, 10000, 1000, 10000000, 10000, // end of currencies | |
12 | }; | |
13 | ||
14 | int32_t dpow_readprices(uint8_t *data,uint32_t *timestampp,double *KMDBTCp,double *BTCUSDp,double *CNYUSDp,uint32_t *pvals) | |
15 | { | |
16 | uint32_t kmdbtc,btcusd,cnyusd; int32_t i,n,len = 0; | |
17 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)timestampp); | |
18 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&n); | |
19 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&kmdbtc); // /= 1000 | |
20 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&btcusd); // *= 1000 | |
21 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&cnyusd); | |
22 | *KMDBTCp = ((double)kmdbtc / (1000000000. * 1000.)); | |
23 | *BTCUSDp = ((double)btcusd / (1000000000. / 1000.)); | |
24 | *CNYUSDp = ((double)cnyusd / 1000000000.); | |
25 | for (i=0; i<n-3; i++) | |
26 | { | |
27 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&pvals[i]); | |
28 | //printf("%u ",pvals[i]); | |
29 | } | |
30 | pvals[i++] = kmdbtc; | |
31 | pvals[i++] = btcusd; | |
32 | pvals[i++] = cnyusd; | |
33 | //printf("OP_RETURN prices\n"); | |
34 | return(n); | |
35 | } | |
36 | ||
37 | /*uint32_t PAX_val32(double val) | |
38 | { | |
39 | uint32_t val32 = 0; struct price_resolution price; | |
40 | if ( (price.Pval= val*1000000000) != 0 ) | |
41 | { | |
42 | if ( price.Pval > 0xffffffff ) | |
43 | printf("Pval overflow error %lld\n",(long long)price.Pval); | |
44 | else val32 = (uint32_t)price.Pval; | |
45 | } | |
46 | return(val32); | |
47 | }*/ | |
48 | ||
49 | double PAX_val(uint32_t pval,int32_t baseid) | |
50 | { | |
51 | //printf("PAX_val baseid.%d pval.%u\n",baseid,pval); | |
52 | if ( baseid >= 0 && baseid < MAX_CURRENCIES ) | |
53 | return(((double)pval / 1000000000.) / MINDENOMS[baseid]); | |
54 | return(0.); | |
55 | } | |
56 | ||
57 | void komodo_pvals(int32_t height,uint32_t *pvals,uint8_t numpvals) | |
58 | { | |
59 | int32_t i,nonz; double KMDBTC,BTCUSD,CNYUSD; uint32_t kmdbtc,btcusd,cnyusd; | |
60 | if ( numpvals >= 35 ) | |
61 | { | |
62 | for (nonz=i=0; i<32; i++) | |
63 | { | |
64 | if ( pvals[i] != 0 ) | |
65 | nonz++; | |
66 | //printf("%u ",pvals[i]); | |
67 | } | |
68 | if ( nonz == 32 ) | |
69 | { | |
70 | kmdbtc = pvals[i++]; | |
71 | btcusd = pvals[i++]; | |
72 | cnyusd = pvals[i++]; | |
73 | KMDBTC = ((double)kmdbtc / (1000000000. * 1000.)); | |
74 | BTCUSD = ((double)btcusd / (1000000000. / 1000.)); | |
75 | CNYUSD = ((double)cnyusd / 1000000000.); | |
76 | PVALS = (uint32_t *)realloc(PVALS,(NUM_PRICES+1) * sizeof(*PVALS) * 36); | |
77 | PVALS[36 * NUM_PRICES] = height; | |
78 | memcpy(&PVALS[36 * NUM_PRICES + 1],pvals,sizeof(*pvals) * 35); | |
79 | NUM_PRICES++; | |
80 | //printf("OP_RETURN.%d KMD %.8f BTC %.6f CNY %.6f NUM_PRICES.%d\n",height,KMDBTC,BTCUSD,CNYUSD,NUM_PRICES); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | int32_t komodo_baseid(char *origbase) | |
86 | { | |
87 | int32_t i; char base[64]; | |
88 | for (i=0; origbase[i]!=0&&i<sizeof(base); i++) | |
89 | base[i] = toupper((int32_t)(origbase[i] & 0xff)); | |
90 | base[i] = 0; | |
91 | for (i=0; i<=MAX_CURRENCIES; i++) | |
92 | if ( strcmp(CURRENCIES[i],base) == 0 ) | |
93 | return(i); | |
94 | printf("illegal base.(%s) %s\n",origbase,base); | |
95 | return(-1); | |
96 | } | |
97 | ||
98 | #define USD 0 | |
99 | uint64_t komodo_paxprice(int32_t height,char *base,char *rel,uint64_t volume) | |
100 | { | |
101 | int32_t baseid=-1,relid=-1,i,ht; uint32_t kmdbtc,btcusd,pvalb,pvalr,*ptr; | |
102 | if ( (baseid= komodo_baseid(base)) >= 0 && (relid= komodo_baseid(rel)) >= 0 ) | |
103 | { | |
104 | for (i=NUM_PRICES-1; i>=0; i--) | |
105 | { | |
106 | ptr = &PVALS[36 * i]; | |
107 | if ( *ptr < height ) | |
108 | { | |
109 | if ( (pvalb= ptr[1 + baseid]) != 0 ) | |
110 | { | |
111 | if ( relid == MAX_CURRENCIES ) | |
112 | { | |
113 | kmdbtc = ptr[1 + MAX_CURRENCIES]; | |
114 | btcusd = ptr[1 + MAX_CURRENCIES + 1]; | |
115 | if ( ptr[1 + USD] != 0 && kmdbtc != 0 && btcusd != 0 ) | |
116 | { | |
117 | baseusd = ((uint64_t)pvalb * 1000000000) / ptr[1 + USD]; | |
118 | kmdusd = ((uint64_t)kmdbtc * 1000000000) / btcusd; | |
119 | //usdval = PAX_val(ptr[1 + USD],USD); | |
120 | //KMDBTC = ((double)kmdbtc / (1000000000. * 1000.)); | |
121 | //BTCUSD = ((double)btcusd / (1000000000. / 1000.)); | |
122 | //KMDUSD = (KMDBTC * BTCUSD); | |
123 | printf("base -> USD %llu, BTC %llu KMDUSD %llu\n",(long long)baseusd,(long long)btcusd,(long long)kmdusd); | |
124 | return((volume * baseusd) / kmdbtc); | |
125 | } | |
126 | } | |
127 | else if ( (pvalr= ptr[1 + relid]) != 0 ) | |
128 | { | |
129 | baserel = ((uint64_t)pvalb * 1000000000) / pvalr; | |
130 | return(volume * baserel); | |
131 | } | |
132 | } | |
133 | return(0); | |
134 | } | |
135 | } | |
136 | } else printf("paxprice invalid base.%s %d, rel.%s %d\n",base,baseid,rel,relid); | |
137 | return(0); | |
138 | } | |
139 | ||
140 | int32_t komodo_pax_opreturn(uint8_t *opret,int32_t maxsize) | |
141 | { | |
142 | static uint32_t lastcrc; | |
143 | FILE *fp; char fname[512]; uint32_t crc32,check,timestamp; int32_t i,n,retval,fsize,len=0; uint8_t data[8192]; | |
144 | #ifdef WIN32 | |
145 | sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodofeed"); | |
146 | #else | |
147 | sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodofeed"); | |
148 | #endif | |
149 | if ( (fp= fopen(fname,"rb")) != 0 ) | |
150 | { | |
151 | fseek(fp,0,SEEK_END); | |
152 | fsize = (int32_t)ftell(fp); | |
153 | rewind(fp); | |
154 | if ( fsize <= maxsize-4 && fsize <= sizeof(data) && fsize > sizeof(crc32) ) | |
155 | { | |
156 | if ( (retval= (int32_t)fread(data,1,fsize,fp)) == fsize ) | |
157 | { | |
158 | len = iguana_rwnum(0,data,sizeof(crc32),(void *)&crc32); | |
159 | check = calc_crc32(0,data+sizeof(crc32),(int32_t)(fsize-sizeof(crc32))); | |
160 | if ( check == crc32 ) | |
161 | { | |
162 | double KMDBTC,BTCUSD,CNYUSD; uint32_t pvals[128]; | |
163 | dpow_readprices(&data[len],×tamp,&KMDBTC,&BTCUSD,&CNYUSD,pvals); | |
164 | if ( 0 && lastcrc != crc32 ) | |
165 | { | |
166 | for (i=0; i<32; i++) | |
167 | printf("%u ",pvals[i]); | |
168 | printf("t%u n.%d KMD %f BTC %f CNY %f (%f)\n",timestamp,n,KMDBTC,BTCUSD,CNYUSD,CNYUSD!=0?1./CNYUSD:0); | |
169 | } | |
170 | if ( timestamp > time(NULL)-600 ) | |
171 | { | |
172 | n = komodo_opreturnscript(opret,'P',data+sizeof(crc32),(int32_t)(fsize-sizeof(crc32))); | |
173 | if ( 0 && lastcrc != crc32 ) | |
174 | { | |
175 | for (i=0; i<n; i++) | |
176 | printf("%02x",opret[i]); | |
177 | printf(" coinbase opret[%d] crc32.%u:%u\n",n,crc32,check); | |
178 | } | |
179 | } else printf("t%u too old for %u\n",timestamp,(uint32_t)time(NULL)); | |
180 | lastcrc = crc32; | |
181 | } else printf("crc32 %u mismatch %u\n",crc32,check); | |
182 | } else printf("fread.%d error != fsize.%d\n",retval,fsize); | |
183 | } else printf("fsize.%d > maxsize.%d or data[%d]\n",fsize,maxsize,(int32_t)sizeof(data)); | |
184 | fclose(fp); | |
185 | } | |
186 | return(n); | |
187 | } |