]>
Commit | Line | Data |
---|---|---|
d019c447 | 1 | /****************************************************************************** |
2 | * Copyright © 2014-2016 The SuperNET Developers. * | |
3 | * * | |
4 | * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * | |
5 | * the top-level directory of this distribution for the individual copyright * | |
6 | * holder information and the developer policies on copyright and licensing. * | |
7 | * * | |
8 | * Unless otherwise agreed in a custom licensing agreement, no part of the * | |
9 | * SuperNET software, including this file may be copied, modified, propagated * | |
10 | * or distributed except according to the terms contained in the LICENSE file * | |
11 | * * | |
12 | * Removal or modification of this copyright notice is prohibited. * | |
13 | * * | |
14 | ******************************************************************************/ | |
1e81ccb7 | 15 | |
16 | int32_t NUM_PRICES; uint32_t *PVALS; | |
17 | ||
41a4a4db | 18 | #define USD 0 |
19 | ||
1e81ccb7 | 20 | #define MAX_CURRENCIES 32 |
18d9697a | 21 | extern char CURRENCIES[][8]; |
1e81ccb7 | 22 | |
a4ebaad7 | 23 | uint64_t M1SUPPLY[] = { 3317900000000, 6991604000000, 667780000000000, 1616854000000, 331000000000, 861909000000, 584629000000, 46530000000, // major currencies |
05dfe053 | 24 | 45434000000000, 16827000000000, 3473357229000, 306435000000, 27139000000000, 2150641000000, 347724099000, 1469583000000, 749543000000, 1826110000000, 2400434000000, 1123925000000, 3125276000000, 13975000000000, 317657000000, 759706000000000, 354902000000, 2797061000000, 162189000000, 163745000000, 1712000000000, 39093000000, 1135490000000000, 80317000000, |
a4ebaad7 | 25 | 100000000 }; |
26 | ||
27 | #define MIND 1000 | |
28 | uint32_t MINDENOMS[] = { MIND, MIND, 100*MIND, MIND, MIND, MIND, MIND, MIND, // major currencies | |
29 | 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 | |
30 | 10*MIND, | |
1e81ccb7 | 31 | }; |
32 | ||
2e9292fe | 33 | int32_t Peggy_inds[539] = {289, 404, 50, 490, 59, 208, 87, 508, 366, 288, 13, 38, 159, 440, 120, 480, 361, 104, 534, 195, 300, 362, 489, 108, 143, 220, 131, 244, 133, 473, 315, 439, 210, 456, 219, 352, 153, 444, 397, 491, 286, 479, 519, 384, 126, 369, 155, 427, 373, 360, 135, 297, 256, 506, 322, 425, 501, 251, 75, 18, 420, 537, 443, 438, 407, 145, 173, 78, 340, 240, 422, 160, 329, 32, 127, 128, 415, 495, 372, 522, 60, 238, 129, 364, 471, 140, 171, 215, 378, 292, 432, 526, 252, 389, 459, 350, 233, 408, 433, 51, 423, 19, 62, 115, 211, 22, 247, 197, 530, 7, 492, 5, 53, 318, 313, 283, 169, 464, 224, 282, 514, 385, 228, 175, 494, 237, 446, 105, 150, 338, 346, 510, 6, 348, 89, 63, 536, 442, 414, 209, 216, 227, 380, 72, 319, 259, 305, 334, 236, 103, 400, 176, 267, 355, 429, 134, 257, 527, 111, 287, 386, 15, 392, 535, 405, 23, 447, 399, 291, 112, 74, 36, 435, 434, 330, 520, 335, 201, 478, 17, 162, 483, 33, 130, 436, 395, 93, 298, 498, 511, 66, 487, 218, 65, 309, 419, 48, 214, 377, 409, 462, 139, 349, 4, 513, 497, 394, 170, 307, 241, 185, 454, 29, 367, 465, 194, 398, 301, 229, 212, 477, 303, 39, 524, 451, 116, 532, 30, 344, 85, 186, 202, 517, 531, 515, 230, 331, 466, 147, 426, 234, 304, 64, 100, 416, 336, 199, 383, 200, 166, 258, 95, 188, 246, 136, 90, 68, 45, 312, 354, 184, 314, 518, 326, 401, 269, 217, 512, 81, 88, 272, 14, 413, 328, 393, 198, 226, 381, 161, 474, 353, 337, 294, 295, 302, 505, 137, 207, 249, 46, 98, 27, 458, 482, 262, 253, 71, 25, 0, 40, 525, 122, 341, 107, 80, 165, 243, 168, 250, 375, 151, 503, 124, 52, 343, 371, 206, 178, 528, 232, 424, 163, 273, 191, 149, 493, 177, 144, 193, 388, 1, 412, 265, 457, 255, 475, 223, 41, 430, 76, 102, 132, 96, 97, 316, 472, 213, 263, 3, 317, 324, 274, 396, 486, 254, 205, 285, 101, 21, 279, 58, 467, 271, 92, 538, 516, 235, 332, 117, 500, 529, 113, 445, 390, 358, 79, 34, 488, 245, 83, 509, 203, 476, 496, 347, 280, 12, 84, 485, 323, 452, 10, 146, 391, 293, 86, 94, 523, 299, 91, 164, 363, 402, 110, 321, 181, 138, 192, 469, 351, 276, 308, 277, 428, 182, 260, 55, 152, 157, 382, 121, 507, 225, 61, 431, 31, 106, 327, 154, 16, 49, 499, 73, 70, 449, 460, 187, 24, 248, 311, 275, 158, 387, 125, 67, 284, 35, 463, 190, 179, 266, 376, 221, 42, 26, 290, 357, 268, 43, 167, 99, 374, 242, 156, 239, 403, 339, 183, 320, 180, 306, 379, 441, 20, 481, 141, 77, 484, 69, 410, 502, 172, 417, 118, 461, 261, 47, 333, 450, 296, 453, 368, 359, 437, 421, 264, 504, 281, 270, 114, 278, 56, 406, 448, 411, 521, 418, 470, 123, 455, 148, 356, 468, 109, 204, 533, 365, 8, 345, 174, 370, 28, 57, 11, 2, 231, 310, 196, 119, 82, 325, 44, 342, 37, 189, 142, 222, 9, 54, }; |
34 | ||
35 | uint64_t peggy_smooth_coeffs[sizeof(Peggy_inds)/sizeof(*Peggy_inds)] = // numprimes.13 | |
36 | { | |
37 | 962714545, 962506087, 962158759, 961672710, 961048151, 960285354, 959384649, 958346426, 957171134, // x.8 | |
38 | 955859283, 954411438, 952828225, 951110328, 949258485, 947273493, 945156207, 942907532, 940528434, // x.17 | |
39 | 938019929, 935383089, 932619036, 929728945, 926714044, 923575608, 920314964, 916933485, 913432593, // x.26 | |
40 | 909813756, 906078486, 902228342, 898264923, 894189872, 890004874, 885711650, 881311964, 876807614, // x.35 | |
41 | 872200436, 867492300, 862685110, 857780804, 852781347, 847688737, 842505000, 837232189, 831872382, // x.44 | |
42 | 826427681, 820900212, 815292123, 809605581, 803842772, 798005901, 792097186, 786118864, 780073180, // x.53 | |
43 | 773962395, 767788778, 761554609, 755262175, 748913768, 742511686, 736058231, 729555707, 723006417, // x.62 | |
44 | 716412665, 709776755, 703100984, 696387648, 689639036, 682857428, 676045100, 669204315, 662337327, // x.71 | |
45 | 655446378, 648533696, 641601496, 634651978, 627687325, 620709702, 613721256, 606724115, 599720386, // x.80 | |
46 | 592712154, 585701482, 578690411, 571680955, 564675105, 557674825, 550682053, 543698699, 536726645, // x.89 | |
47 | 529767743, 522823816, 515896658, 508988029, 502099660, 495233249, 488390461, 481572928, 474782249, // x.98 | |
48 | 468019988, 461287675, 454586804, 447918836, 441285195, 434687268, 428126409, 421603932, 415121117, // x.107 | |
49 | 408679208, 402279408, 395922888, 389610779, 383344175, 377124134, 370951677, 364827785, 358753406, // x.116 | |
50 | 352729449, 346756785, 340836251, 334968645, 329154729, 323395230, 317690838, 312042206, 306449955, // x.125 | |
51 | 300914667, 295436891, 290017141, 284655897, 279353604, 274110676, 268927490, 263804394, 258741701, // x.134 | |
52 | 253739694, 248798623, 243918709, 239100140, 234343077, 229647649, 225013957, 220442073, 215932043, // x.143 | |
53 | 211483883, 207097585, 202773112, 198510404, 194309373, 190169909, 186091877, 182075118, 178119452, // x.152 | |
54 | 174224676, 170390565, 166616873, 162903335, 159249664, 155655556, 152120688, 148644718, 145227287, // x.161 | |
55 | 141868021, 138566528, 135322401, 132135218, 129004542, 125929924, 122910901, 119946997, 117037723, // x.170 | |
56 | 114182582, 111381062, 108632643, 105936795, 103292978, 100700645, 98159238, 95668194, 93226942, // x.179 | |
57 | 90834903, 88491495, 86196126, 83948203, 81747126, 79592292, 77483092, 75418916, 73399150, // x.188 | |
58 | 71423178, 69490383, 67600142, 65751837, 63944844, 62178541, 60452305, 58765515, 57117547, // x.197 | |
59 | 55507781, 53935597, 52400377, 50901505, 49438366, 48010349, 46616844, 45257246, 43930951, // x.206 | |
60 | 42637360, 41375878, 40145912, 38946876, 37778185, 36639262, 35529533, 34448428, 33395384, // x.215 | |
61 | 32369842, 31371249, 30399057, 29452725, 28531717, 27635503, 26763558, 25915365, 25090413, // x.224 | |
62 | 24288196, 23508216, 22749980, 22013003, 21296806, 20600917, 19924870, 19268206, 18630475, // x.233 | |
63 | 18011231, 17410035, 16826458, 16260073, 15710466, 15177224, 14659944, 14158231, 13671694, // x.242 | |
64 | 13199950, 12742625, 12299348, 11869759, 11453500, 11050225, 10659590, 10281262, 9914910, // x.251 | |
65 | 9560213, 9216856, 8884529, 8562931, 8251764, 7950739, 7659571, 7377984, 7105706, // x.260 | |
66 | 6842471, 6588020, 6342099, 6104460, 5874861, 5653066, 5438844, 5231969, 5032221, // x.269 | |
67 | 4839386, 4653254, 4473620, 4300287, 4133059, 3971747, 3816167, 3666139, 3521488, // x.278 | |
68 | 3382043, 3247640, 3118115, 2993313, 2873079, 2757266, 2645728, 2538325, 2434919, // x.287 | |
69 | 2335380, 2239575, 2147382, 2058677, 1973342, 1891262, 1812325, 1736424, 1663453, // x.296 | |
70 | 1593311, 1525898, 1461118, 1398879, 1339091, 1281666, 1226519, 1173569, 1122736, // x.305 | |
71 | 1073944, 1027117, 982185, 939076, 897725, 858065, 820033, 783568, 748612, // x.314 | |
72 | 715108, 682999, 652233, 622759, 594527, 567488, 541597, 516808, 493079, // x.323 | |
73 | 470368, 448635, 427841, 407948, 388921, 370725, 353326, 336692, 320792, // x.332 | |
74 | 305596, 291075, 277202, 263950, 251292, 239204, 227663, 216646, 206130, // x.341 | |
75 | 196094, 186517, 177381, 168667, 160356, 152430, 144874, 137671, 130806, // x.350 | |
76 | 124264, 118031, 112093, 106437, 101050, 95921, 91039, 86391, 81968, // x.359 | |
77 | 77759, 73755, 69945, 66322, 62877, 59602, 56488, 53528, 50716, // x.368 | |
78 | 48043, 45505, 43093, 40803, 38629, 36564, 34604, 32745, 30980, // x.377 | |
79 | 29305, 27717, 26211, 24782, 23428, 22144, 20927, 19774, 18681, // x.386 | |
80 | 17646, 16665, 15737, 14857, 14025, 13237, 12491, 11786, 11118, // x.395 | |
81 | 10487, 9890, 9325, 8791, 8287, 7810, 7359, 6933, 6531, // x.404 | |
82 | 6151, 5792, 5453, 5133, 4831, 4547, 4278, 4024, 3785, // x.413 | |
83 | 3560, 3347, 3147, 2958, 2779, 2612, 2454, 2305, 2164, // x.422 | |
84 | 2032, 1908, 1791, 1681, 1577, 1480, 1388, 1302, 1221, // x.431 | |
85 | 1145, 1073, 1006, 942, 883, 827, 775, 725, 679, // x.440 | |
86 | 636, 595, 557, 521, 487, 456, 426, 399, 373, // x.449 | |
87 | 348, 325, 304, 284, 265, 248, 231, 216, 202, // x.458 | |
88 | 188, 175, 164, 153, 142, 133, 124, 115, 107, // x.467 | |
89 | 100, 93, 87, 81, 75, 70, 65, 61, 56, // x.476 | |
90 | 53, 49, 45, 42, 39, 36, 34, 31, 29, // x.485 | |
91 | 27, 25, 23, 22, 20, 19, 17, 16, 15, // x.494 | |
92 | 14, 13, 12, 11, 10, 9, 9, 8, 7, // x.503 | |
93 | 7, 6, 6, 5, 5, 5, 4, 4, 4, // x.512 | |
94 | 3, 3, 3, 3, 2, 2, 2, 2, 2, // x.521 | |
95 | 2, 2, 1, 1, 1, 1, 1, 1, 1, // x.530 | |
96 | 1, 1, 1, 1, 1, 1, 0, 0, // isum 100000000000 | |
97 | }; | |
98 | ||
ab6984e9 | 99 | uint64_t komodo_paxvol(uint64_t volume,uint64_t price) |
100 | { | |
101 | if ( volume < 10000000000 ) | |
102 | return((volume * price) / 1000000000); | |
103 | else if ( volume < (uint64_t)10 * 10000000000 ) | |
104 | return((volume * (price / 10)) / 100000000); | |
105 | else if ( volume < (uint64_t)100 * 10000000000 ) | |
106 | return(((volume / 10) * (price / 10)) / 10000000); | |
107 | else if ( volume < (uint64_t)1000 * 10000000000 ) | |
108 | return(((volume / 10) * (price / 100)) / 1000000); | |
109 | else if ( volume < (uint64_t)10000 * 10000000000 ) | |
110 | return(((volume / 100) * (price / 100)) / 100000); | |
111 | else if ( volume < (uint64_t)100000 * 10000000000 ) | |
112 | return(((volume / 100) * (price / 1000)) / 10000); | |
113 | else if ( volume < (uint64_t)1000000 * 10000000000 ) | |
114 | return(((volume / 1000) * (price / 1000)) / 1000); | |
115 | else if ( volume < (uint64_t)10000000 * 10000000000 ) | |
116 | return(((volume / 1000) * (price / 10000)) / 100); | |
117 | else return(((volume / 10000) * (price / 10000)) / 10); | |
118 | } | |
119 | ||
a4ebaad7 | 120 | void pax_rank(uint64_t *ranked,uint32_t *pvals) |
121 | { | |
122 | int32_t i; uint64_t vals[32],sum = 0; | |
123 | for (i=0; i<32; i++) | |
124 | { | |
125 | vals[i] = komodo_paxvol(M1SUPPLY[i] / MINDENOMS[i],pvals[i]); | |
126 | sum += vals[i]; | |
127 | } | |
128 | for (i=0; i<32; i++) | |
129 | { | |
130 | ranked[i] = (vals[i] * 1000000000) / sum; | |
56d91e9c | 131 | //printf("%.6f ",(double)ranked[i]/1000000000.); |
a4ebaad7 | 132 | } |
56d91e9c | 133 | //printf("sum %llu\n",(long long)sum); |
a4ebaad7 | 134 | }; |
135 | ||
1e81ccb7 | 136 | int32_t dpow_readprices(uint8_t *data,uint32_t *timestampp,double *KMDBTCp,double *BTCUSDp,double *CNYUSDp,uint32_t *pvals) |
137 | { | |
1bf82154 | 138 | uint32_t kmdbtc,btcusd,cnyusd; int32_t i,n,nonz,len = 0; |
0201d1a6 | 139 | if ( data[0] == 'P' && data[5] == 35 ) |
140 | data++; | |
1e81ccb7 | 141 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)timestampp); |
142 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&n); | |
7eca18ee | 143 | if ( n != 35 ) |
7d59e5c9 | 144 | { |
145 | printf("dpow_readprices illegal n.%d\n",n); | |
146 | return(-1); | |
147 | } | |
1e81ccb7 | 148 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&kmdbtc); // /= 1000 |
149 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&btcusd); // *= 1000 | |
150 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&cnyusd); | |
151 | *KMDBTCp = ((double)kmdbtc / (1000000000. * 1000.)); | |
152 | *BTCUSDp = ((double)btcusd / (1000000000. / 1000.)); | |
153 | *CNYUSDp = ((double)cnyusd / 1000000000.); | |
1bf82154 | 154 | for (i=nonz=0; i<n-3; i++) |
1e81ccb7 | 155 | { |
1bf82154 | 156 | if ( pvals[i] != 0 ) |
157 | nonz++; | |
49df008c | 158 | //else if ( nonz != 0 ) |
159 | // printf("pvals[%d] is zero\n",i); | |
1e81ccb7 | 160 | len += iguana_rwnum(0,&data[len],sizeof(uint32_t),(void *)&pvals[i]); |
161 | //printf("%u ",pvals[i]); | |
162 | } | |
32f7d394 | 163 | /*if ( nonz < n-3 ) |
ef7b1ba6 | 164 | { |
49df008c | 165 | //printf("nonz.%d n.%d retval -1\n",nonz,n); |
1bf82154 | 166 | return(-1); |
32f7d394 | 167 | }*/ |
1e81ccb7 | 168 | pvals[i++] = kmdbtc; |
169 | pvals[i++] = btcusd; | |
170 | pvals[i++] = cnyusd; | |
171 | //printf("OP_RETURN prices\n"); | |
172 | return(n); | |
173 | } | |
174 | ||
d019c447 | 175 | int32_t komodo_pax_opreturn(uint8_t *opret,int32_t maxsize) |
176 | { | |
177 | static uint32_t lastcrc; | |
d351e231 | 178 | FILE *fp; char fname[512]; uint32_t crc32,check,timestamp; int32_t i,n=0,retval,fsize,len=0; uint8_t data[8192]; |
d019c447 | 179 | #ifdef WIN32 |
180 | sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),(char *)"komodofeed"); | |
181 | #else | |
182 | sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),(char *)"komodofeed"); | |
183 | #endif | |
184 | if ( (fp= fopen(fname,"rb")) != 0 ) | |
185 | { | |
186 | fseek(fp,0,SEEK_END); | |
187 | fsize = (int32_t)ftell(fp); | |
188 | rewind(fp); | |
189 | if ( fsize <= maxsize-4 && fsize <= sizeof(data) && fsize > sizeof(crc32) ) | |
190 | { | |
191 | if ( (retval= (int32_t)fread(data,1,fsize,fp)) == fsize ) | |
192 | { | |
193 | len = iguana_rwnum(0,data,sizeof(crc32),(void *)&crc32); | |
194 | check = calc_crc32(0,data+sizeof(crc32),(int32_t)(fsize-sizeof(crc32))); | |
195 | if ( check == crc32 ) | |
196 | { | |
197 | double KMDBTC,BTCUSD,CNYUSD; uint32_t pvals[128]; | |
1bf82154 | 198 | if ( dpow_readprices(&data[len],×tamp,&KMDBTC,&BTCUSD,&CNYUSD,pvals) > 0 ) |
d019c447 | 199 | { |
d019c447 | 200 | if ( 0 && lastcrc != crc32 ) |
201 | { | |
1bf82154 | 202 | for (i=0; i<32; i++) |
203 | printf("%u ",pvals[i]); | |
204 | printf("t%u n.%d KMD %f BTC %f CNY %f (%f)\n",timestamp,n,KMDBTC,BTCUSD,CNYUSD,CNYUSD!=0?1./CNYUSD:0); | |
d019c447 | 205 | } |
1bf82154 | 206 | if ( timestamp > time(NULL)-600 ) |
207 | { | |
208 | n = komodo_opreturnscript(opret,'P',data+sizeof(crc32),(int32_t)(fsize-sizeof(crc32))); | |
209 | if ( 0 && lastcrc != crc32 ) | |
210 | { | |
211 | for (i=0; i<n; i++) | |
212 | printf("%02x",opret[i]); | |
213 | printf(" coinbase opret[%d] crc32.%u:%u\n",n,crc32,check); | |
214 | } | |
215 | } //else printf("t%u too old for %u\n",timestamp,(uint32_t)time(NULL)); | |
216 | lastcrc = crc32; | |
217 | } | |
d019c447 | 218 | } else printf("crc32 %u mismatch %u\n",crc32,check); |
219 | } else printf("fread.%d error != fsize.%d\n",retval,fsize); | |
220 | } else printf("fsize.%d > maxsize.%d or data[%d]\n",fsize,maxsize,(int32_t)sizeof(data)); | |
221 | fclose(fp); | |
222 | } | |
223 | return(n); | |
224 | } | |
225 | ||
1e81ccb7 | 226 | /*uint32_t PAX_val32(double val) |
227 | { | |
228 | uint32_t val32 = 0; struct price_resolution price; | |
229 | if ( (price.Pval= val*1000000000) != 0 ) | |
230 | { | |
231 | if ( price.Pval > 0xffffffff ) | |
232 | printf("Pval overflow error %lld\n",(long long)price.Pval); | |
233 | else val32 = (uint32_t)price.Pval; | |
234 | } | |
235 | return(val32); | |
236 | }*/ | |
237 | ||
57abdbaf | 238 | int32_t PAX_pubkey(int32_t rwflag,uint8_t *pubkey33,uint8_t *addrtypep,uint8_t rmd160[20],char fiat[4],uint8_t *shortflagp,int64_t *fiatoshisp) |
d019c447 | 239 | { |
57abdbaf | 240 | if ( rwflag != 0 ) |
241 | { | |
242 | memset(pubkey33,0,33); | |
243 | pubkey33[0] = 0x02 | (*shortflagp != 0); | |
244 | memcpy(&pubkey33[1],fiat,3); | |
245 | iguana_rwnum(rwflag,&pubkey33[4],sizeof(*fiatoshisp),(void *)fiatoshisp); | |
246 | pubkey33[12] = *addrtypep; | |
247 | memcpy(&pubkey33[13],rmd160,20); | |
248 | } | |
249 | else | |
250 | { | |
251 | *shortflagp = (pubkey33[0] == 0x03); | |
4d272ae4 | 252 | memcpy(fiat,&pubkey33[1],3); |
4d272ae4 | 253 | fiat[3] = 0; |
57abdbaf | 254 | iguana_rwnum(rwflag,&pubkey33[4],sizeof(*fiatoshisp),(void *)fiatoshisp); |
255 | if ( *shortflagp != 0 ) | |
256 | *fiatoshisp = -(*fiatoshisp); | |
257 | *addrtypep = pubkey33[12]; | |
258 | memcpy(rmd160,&pubkey33[13],20); | |
259 | } | |
d351e231 | 260 | return(33); |
d019c447 | 261 | } |
262 | ||
1e81ccb7 | 263 | double PAX_val(uint32_t pval,int32_t baseid) |
264 | { | |
265 | //printf("PAX_val baseid.%d pval.%u\n",baseid,pval); | |
266 | if ( baseid >= 0 && baseid < MAX_CURRENCIES ) | |
267 | return(((double)pval / 1000000000.) / MINDENOMS[baseid]); | |
268 | return(0.); | |
269 | } | |
270 | ||
271 | void komodo_pvals(int32_t height,uint32_t *pvals,uint8_t numpvals) | |
272 | { | |
5c481612 | 273 | int32_t i,nonz; uint32_t kmdbtc,btcusd,cnyusd; double KMDBTC,BTCUSD,CNYUSD; |
1e81ccb7 | 274 | if ( numpvals >= 35 ) |
275 | { | |
276 | for (nonz=i=0; i<32; i++) | |
277 | { | |
278 | if ( pvals[i] != 0 ) | |
279 | nonz++; | |
280 | //printf("%u ",pvals[i]); | |
281 | } | |
282 | if ( nonz == 32 ) | |
283 | { | |
284 | kmdbtc = pvals[i++]; | |
285 | btcusd = pvals[i++]; | |
286 | cnyusd = pvals[i++]; | |
5c481612 | 287 | KMDBTC = ((double)kmdbtc / (1000000000. * 1000.)); |
288 | BTCUSD = ((double)btcusd / (1000000000. / 1000.)); | |
289 | CNYUSD = ((double)cnyusd / 1000000000.); | |
1e81ccb7 | 290 | PVALS = (uint32_t *)realloc(PVALS,(NUM_PRICES+1) * sizeof(*PVALS) * 36); |
291 | PVALS[36 * NUM_PRICES] = height; | |
292 | memcpy(&PVALS[36 * NUM_PRICES + 1],pvals,sizeof(*pvals) * 35); | |
293 | NUM_PRICES++; | |
5c481612 | 294 | if ( 0 ) |
295 | printf("OP_RETURN.%d KMD %.8f BTC %.6f CNY %.6f NUM_PRICES.%d (%llu %llu %llu)\n",height,KMDBTC,BTCUSD,CNYUSD,NUM_PRICES,(long long)kmdbtc,(long long)btcusd,(long long)cnyusd); | |
1e81ccb7 | 296 | } |
297 | } | |
298 | } | |
299 | ||
d019c447 | 300 | uint64_t komodo_paxcalc(uint32_t *pvals,int32_t baseid,int32_t relid,uint64_t basevolume) |
a4ebaad7 | 301 | { |
d351e231 | 302 | uint32_t pvalb,pvalr,kmdbtc,btcusd; uint64_t usdvol,baseusd,usdkmd,baserel,ranked[32]; |
3ba8f923 | 303 | if ( basevolume > 1000000*COIN ) |
2e4514ce | 304 | return(0); |
a4ebaad7 | 305 | if ( (pvalb= pvals[baseid]) != 0 ) |
306 | { | |
307 | if ( relid == MAX_CURRENCIES ) | |
308 | { | |
309 | kmdbtc = pvals[MAX_CURRENCIES]; | |
310 | btcusd = pvals[MAX_CURRENCIES + 1]; | |
311 | if ( pvals[USD] != 0 && kmdbtc != 0 && btcusd != 0 ) | |
312 | { | |
313 | baseusd = ((uint64_t)pvalb * 1000000000) / pvals[USD]; | |
74fcb540 | 314 | usdvol = komodo_paxvol(basevolume,baseusd) / MINDENOMS[baseid]; |
048ae852 | 315 | usdkmd = ((uint64_t)btcusd * 1000000000) / kmdbtc; |
a4ebaad7 | 316 | //printf("base -> USD %llu, BTC %llu KMDUSD %llu\n",(long long)baseusd,(long long)btcusd,(long long)kmdusd); |
e647aac3 | 317 | //printf("usdkmd.%llu basevolume.%llu baseusd.%llu paxvol.%llu usdvol.%llu -> %.8f\n",(long long)usdkmd,(long long)basevolume,(long long)baseusd,(long long)komodo_paxvol(basevolume,baseusd),(long long)usdvol,dstr(MINDENOMS[USD] * komodo_paxvol(usdvol,usdkmd))); |
74fcb540 | 318 | return(MINDENOMS[USD] * komodo_paxvol(usdvol,usdkmd)); |
a4ebaad7 | 319 | } |
320 | } | |
321 | else if ( baseid == relid ) | |
322 | { | |
56d91e9c | 323 | if ( baseid != MAX_CURRENCIES ) |
324 | { | |
325 | pax_rank(ranked,pvals); | |
d836ec3f | 326 | return(10 * ranked[baseid]); // map to percentage |
56d91e9c | 327 | } |
a4ebaad7 | 328 | } |
329 | else if ( (pvalr= pvals[relid]) != 0 ) | |
330 | { | |
331 | baserel = ((uint64_t)pvalb * 1000000000) / pvalr; | |
d019c447 | 332 | return(komodo_paxvol(basevolume,baserel)); |
a4ebaad7 | 333 | } |
334 | } | |
08ef004d | 335 | return(0); |
a4ebaad7 | 336 | } |
337 | ||
2e9292fe | 338 | uint64_t _komodo_paxprice(int32_t height,char *base,char *rel,uint64_t basevolume) |
1e81ccb7 | 339 | { |
c149d44c | 340 | int32_t baseid=-1,relid=-1,i; uint32_t *ptr; |
1e81ccb7 | 341 | if ( (baseid= komodo_baseid(base)) >= 0 && (relid= komodo_baseid(rel)) >= 0 ) |
342 | { | |
343 | for (i=NUM_PRICES-1; i>=0; i--) | |
344 | { | |
345 | ptr = &PVALS[36 * i]; | |
346 | if ( *ptr < height ) | |
d019c447 | 347 | return(komodo_paxcalc(&ptr[1],baseid,relid,basevolume)); |
1e81ccb7 | 348 | } |
349 | } else printf("paxprice invalid base.%s %d, rel.%s %d\n",base,baseid,rel,relid); | |
350 | return(0); | |
351 | } | |
352 | ||
2e9292fe | 353 | uint64_t komodo_paxprice(int32_t height,char *base,char *rel,uint64_t basevolume) |
354 | { | |
ddb52326 | 355 | int32_t i,j,k,start,ind,numvotes,wt; int64_t delta; uint64_t seed,tolerance,den,densum,sum=0,votes[539]; |
2e9292fe | 356 | numvotes = (int32_t)(sizeof(Peggy_inds)/sizeof(*Peggy_inds)); |
357 | memset(votes,0,sizeof(votes)); | |
358 | for (i=0; i<numvotes; i++) | |
359 | if ( (votes[i]= _komodo_paxprice(height-i,base,rel,COIN)) == 0 ) | |
360 | return(0); | |
361 | start = (uint32_t)komodo_seed(height); | |
362 | tolerance = 10000; | |
363 | for (k=0; k<numvotes; k++) | |
364 | { | |
365 | ind = Peggy_inds[(k + start) % numvotes]; | |
366 | i = (int32_t)(ind % numvotes); | |
367 | wt = 0; | |
368 | if ( votes[i] != 0 ) | |
369 | { | |
370 | for (j=0; j<numvotes; j++) | |
371 | { | |
372 | if ( votes[j] != 0 ) | |
373 | { | |
374 | if ( (delta= (votes[i] - votes[j])) < 0 ) | |
375 | delta = -delta; | |
376 | if ( delta <= tolerance ) | |
377 | { | |
378 | wt++; | |
379 | if ( wt > (numvotes >> 1) ) | |
380 | break; | |
381 | } | |
382 | } | |
383 | } | |
384 | } | |
385 | if ( wt > (numvotes >> 1) ) | |
386 | { | |
387 | ind = i; | |
388 | for (densum=sum=j=0; j<numvotes; j++) | |
389 | { | |
390 | den = peggy_smooth_coeffs[j]; | |
391 | densum += den; | |
392 | sum += (den * votes[(ind + j) % numvotes]); | |
393 | } | |
394 | sum /= densum; | |
395 | sum = (sum * basevolume) / COIN; | |
a8323aa6 | 396 | printf("seed.%llx sum %.8f densum %.8f basevol %.8f\n",(long long)seed,dstr(sum),dstr(densum),dstr(basevolume)); |
2e9292fe | 397 | break; |
398 | } | |
399 | } | |
400 | return(sum); | |
401 | } | |
402 | ||
d836ec3f | 403 | int32_t komodo_paxprices(int32_t *heights,uint64_t *prices,int32_t max,char *base,char *rel) |
a4ebaad7 | 404 | { |
b20a5d7f | 405 | int32_t baseid=-1,relid=-1,i,num = 0; uint32_t *ptr; |
a4ebaad7 | 406 | if ( (baseid= komodo_baseid(base)) >= 0 && (relid= komodo_baseid(rel)) >= 0 ) |
407 | { | |
408 | for (i=NUM_PRICES-1; i>=0; i--) | |
409 | { | |
410 | ptr = &PVALS[36 * i]; | |
56d91e9c | 411 | heights[num] = *ptr; |
412 | prices[num] = komodo_paxcalc(&ptr[1],baseid,relid,COIN); | |
413 | num++; | |
414 | if ( num >= max ) | |
415 | return(num); | |
a4ebaad7 | 416 | } |
417 | } | |
56d91e9c | 418 | return(num); |
a4ebaad7 | 419 | } |
420 | ||
cd1fcb48 | 421 | void komodo_paxpricefeed(int32_t height,uint8_t *pricefeed,int32_t opretlen) |
47e5cbc1 | 422 | { |
423 | double KMDBTC,BTCUSD,CNYUSD; uint32_t numpvals,timestamp,pvals[128]; uint256 zero; | |
424 | numpvals = dpow_readprices(pricefeed,×tamp,&KMDBTC,&BTCUSD,&CNYUSD,pvals); | |
425 | memset(&zero,0,sizeof(zero)); | |
429dabb5 | 426 | komodo_stateupdate(height,0,0,0,zero,0,0,pvals,numpvals,0,0,0,0,0); |
0765bc71 | 427 | //printf("komodo_paxpricefeed vout OP_RETURN.%d prices numpvals.%d opretlen.%d\n",height,numpvals,opretlen); |
47e5cbc1 | 428 | } |
429 | ||
d95bcb64 | 430 | uint64_t PAX_fiatdest(int32_t tokomodo,char *destaddr,uint8_t pubkey33[33],char *coinaddr,int32_t height,char *origbase,int64_t fiatoshis) |
1e81ccb7 | 431 | { |
fe965b1c | 432 | uint8_t shortflag = 0; char base[4]; int32_t i,baseid; uint8_t addrtype,rmd160[20]; int64_t komodoshis = 0; |
7c6f8298 | 433 | if ( (baseid= komodo_baseid(origbase)) < 0 || baseid == MAX_CURRENCIES ) |
d019c447 | 434 | return(0); |
3ba8f923 | 435 | for (i=0; i<3; i++) |
436 | base[i] = toupper(origbase[i]); | |
437 | base[i] = 0; | |
30e79ca6 | 438 | if ( fiatoshis < 0 ) |
439 | shortflag = 1, fiatoshis = -fiatoshis; | |
3ba8f923 | 440 | komodoshis = komodo_paxprice(height,base,(char *)"KMD",(uint64_t)fiatoshis); |
d019c447 | 441 | if ( bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr) == 20 ) |
1e81ccb7 | 442 | { |
7c6f8298 | 443 | PAX_pubkey(1,pubkey33,&addrtype,rmd160,base,&shortflag,tokomodo != 0 ? &komodoshis : &fiatoshis); |
d019c447 | 444 | bitcoin_address(destaddr,KOMODO_PUBTYPE,pubkey33,33); |
1e81ccb7 | 445 | } |
d019c447 | 446 | return(komodoshis); |
1e81ccb7 | 447 | } |