]> Git Repo - VerusCoin.git/blob - src/komodo_gateway.h
test
[VerusCoin.git] / src / komodo_gateway.h
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  ******************************************************************************/
15
16 // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
17
18 struct pax_transaction
19 {
20     UT_hash_handle hh;
21     uint256 txid;
22     uint64_t komodoshis,fiatoshis;
23     int32_t marked,height;
24     uint16_t vout;
25     char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag;
26 } *PAX;
27
28 uint64_t komodo_paxtotal()
29 {
30     struct pax_transaction *pax,*tmp; uint64_t total = 0;
31     /*pthread_mutex_lock(&komodo_mutex);
32     tmp = 0;
33     if ( PAX != 0 )
34     {
35         pax = (struct pax_transaction *)PAX->hh.next;
36         while ( pax != 0 && pax != tmp && n++ < 1000000 )
37         {
38             printf("PAX.[%p %p] pax.%p marked.%d fiat %.8f KMD %.8f\n",PAX->hh.next,PAX->hh.prev,pax,pax->marked,dstr(pax->fiatoshis),dstr(pax->komodoshis));
39             if ( pax->marked == 0 )
40             {
41                 if ( komodo_is_issuer() != 0 )
42                     total += pax->fiatoshis;
43                 else total += pax->komodoshis;
44             }
45             tmp = pax;
46             pax = (struct pax_transaction *)pax->hh.next;
47         }
48     }
49     pthread_mutex_unlock(&komodo_mutex);
50     if ( n >= 1000000 )
51         printf("komodo_paxtotal n.%d iterations?\n",n);*/
52     HASH_ITER(hh,PAX,pax,tmp)
53     {
54         if ( pax->marked == 0 )
55         {
56             if ( komodo_is_issuer() != 0 )
57                 total += pax->fiatoshis;
58             else total += pax->komodoshis;
59         }
60     }
61     return(total);
62 }
63
64 struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout)
65 {
66     struct pax_transaction *pax;
67     pthread_mutex_lock(&komodo_mutex);
68     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
69     if ( pax != 0 )
70         memcpy(space,pax,sizeof(*pax));
71     pthread_mutex_unlock(&komodo_mutex);
72     return(pax);
73 }
74
75 struct pax_transaction *komodo_paxmark(int32_t height,struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark)
76 {
77     struct pax_transaction *pax;
78     pthread_mutex_lock(&komodo_mutex);
79     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
80     if ( pax == 0 )
81     {
82         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
83         pax->txid = txid;
84         pax->vout = vout;
85         HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
86         //printf("ht.%d create pax.%p mark.%d\n",height,pax,mark);
87     }
88     if ( pax != 0 )
89     {
90         pax->marked = mark;
91         //int32_t i; for (i=0; i<32; i++)
92         //    printf("%02x",((uint8_t *)&txid)[i]);
93         //printf(" paxmark.ht %d vout%d\n",mark,vout);
94         memcpy(space,pax,sizeof(*pax));
95     }
96     pthread_mutex_unlock(&komodo_mutex);
97     return(pax);
98 }
99
100 void komodo_gateway_deposit(char *coinaddr,uint64_t value,int32_t shortflag,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height) // assetchain context
101 {
102     struct pax_transaction *pax; int32_t addflag = 0;
103     pthread_mutex_lock(&komodo_mutex);
104     HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
105     if ( pax == 0 )
106     {
107         pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
108         pax->txid = txid;
109         pax->vout = vout;
110         HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
111         //printf("[%s] ht.%d create pax.%p\n",ASSETCHAINS_SYMBOL,height,pax);
112     }
113     pthread_mutex_unlock(&komodo_mutex);
114     if ( coinaddr != 0 )
115     {
116         strcpy(pax->coinaddr,coinaddr);
117         pax->komodoshis = value;
118         pax->shortflag = shortflag;
119         strcpy(pax->symbol,symbol);
120         pax->fiatoshis = fiatoshis;
121         memcpy(pax->rmd160,rmd160,20);
122         pax->height = height;
123         if ( pax->marked == 0 )
124             printf("[%s] %p ADD DEPOSIT %s %.8f -> %s TO PAX ht.%d total %.8f\n",ASSETCHAINS_SYMBOL,pax,symbol,dstr(fiatoshis),coinaddr,height,dstr(komodo_paxtotal()));
125         else printf("%p MARKED.%d DEPOSIT %s %.8f -> %s TO PAX ht.%d\n",pax,pax->marked,symbol,dstr(fiatoshis),coinaddr,height);
126     }
127     else
128     {
129         pax->marked = height;
130         printf("pax.%p MARK DEPOSIT ht.%d\n",pax,height);
131     }
132 }
133
134 int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen)
135 {
136     int32_t i,n=0,j,len;
137     if ( opretbuf[opretlen-5] == '-' )
138         *shortflagp = 1;
139     else *shortflagp = 0;
140     for (i=0; i<4; i++)
141         base[i] = opretbuf[opretlen-4+i];
142     if ( (strcmp(base,"KMD") == 0 && ASSETCHAINS_SYMBOL[0] == 0) || strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag
143     {
144         //printf("BASE.(%s) vs (%s)\n",base,ASSETCHAINS_SYMBOL);
145         opretbuf++, opretlen--;
146         for (n=len=0; n<opretlen/34; n++)
147         {
148             for (j=0; j<32; j++)
149             {
150                 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
151                 //printf("%02x",((uint8_t *)&txids[n])[j]);
152             }
153             vouts[n] = opretbuf[len++];
154             vouts[n] = (opretbuf[len++] << 8) | vouts[n];
155             //printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
156         }
157     }
158     return(n);
159 }
160
161 void komodo_gateway_deposits(CMutableTransaction *txNew,int32_t shortflag,char *symbol)
162 {
163     struct pax_transaction *pax,*tmp; uint8_t *script,opcode,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1;
164     PENDING_KOMODO_TX = 0;
165     if ( strcmp(symbol,"KMD") == 0 )
166         opcode = 'I';
167     else opcode = 'X';
168     HASH_ITER(hh,PAX,pax,tmp)
169     {
170         if ( pax->marked != 0 )
171             continue;
172         if ( ASSETCHAINS_SYMBOL[0] != 0 )
173             printf("pax.%p marked.%d %.8f -> %.8f\n",pax,pax->marked,dstr(pax->komodoshis),dstr(pax->fiatoshis));
174         txNew->vout.resize(numvouts+1);
175         txNew->vout[numvouts].nValue = pax->fiatoshis;
176         txNew->vout[numvouts].scriptPubKey.resize(25);
177         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
178         *script++ = 0x76;
179         *script++ = 0xa9;
180         *script++ = 20;
181         memcpy(script,pax->rmd160,20), script += 20;
182         *script++ = 0x88;
183         *script++ = 0xac;
184         for (i=0; i<32; i++)
185         {
186             printf("%02x",((uint8_t *)&pax->txid)[i]);
187             data[len++] = ((uint8_t *)&pax->txid)[i];
188         }
189         data[len++] = pax->vout & 0xff;
190         data[len++] = (pax->vout >> 8) & 0xff;
191         if ( strcmp(symbol,"KMD") != 0 )
192             PENDING_KOMODO_TX += pax->fiatoshis;
193         else PENDING_KOMODO_TX += pax->komodoshis;
194         printf(" vout.%u DEPOSIT %.8f <- paxdeposit.%s pending %.8f\n",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,symbol,dstr(PENDING_KOMODO_TX));
195         if ( numvouts++ >= 64 )
196             break;
197     }
198     if ( numvouts > 1 )
199     {
200         if ( shortflag != 0 )
201             data[len++] = '-';
202         for (i=0; symbol[i]!=0; i++)
203             data[len++] = symbol[i];
204         data[len++] = 0;
205         opretlen = komodo_opreturnscript(opret,opcode,data,len);
206         txNew->vout.resize(numvouts+1);
207         txNew->vout[numvouts].nValue = 0;
208         txNew->vout[numvouts].scriptPubKey.resize(opretlen);
209         script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
210         memcpy(script,opret,opretlen);
211         printf("total numvouts.%d %.8f opretlen.%d\n",numvouts,dstr(PENDING_KOMODO_TX),opretlen);
212     }
213 }
214
215 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
216 {
217     int32_t i,j,n,num,opretlen,offset=1; uint256 hash,txids[64]; uint8_t shortflag; char symbol[16],base[16]; uint16_t vouts[64]; uint8_t *script,opcode; struct pax_transaction *pax,space;
218     n = block.vtx[0].vout.size();
219     script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
220     if ( n <= 2 || script[0] != 0x6a )
221         return(0);
222     offset += komodo_scriptitemlen(&opretlen,&script[offset]);
223     //printf("checkdeposit n.%d [%02x] [%c] %d vs %d\n",n,script[0],script[offset],script[offset],'I');
224     if ( ASSETCHAINS_SYMBOL[0] == 0 )
225     {
226         opcode = 'X';
227         strcpy(symbol,"KMD");
228     }
229     else
230     {
231         strcpy(symbol,ASSETCHAINS_SYMBOL);
232         opcode = 'I';
233     }
234     if ( script[offset] == opcode && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
235     {
236         if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen)) > 0 )
237         {
238             for (i=1; i<n-1; i++)
239             {
240                 if ( (pax= komodo_paxfind(&space,txids[i-1],vouts[i-1])) != 0 && ((opcode == 'I' && pax->fiatoshis == block.vtx[0].vout[i].nValue) || (opcode == 'X' && pax->komodoshis == block.vtx[0].vout[i].nValue)) )
241                 {
242                     //printf("i.%d match %.8f == %.8f\n",i,dstr(pax != 0 ? pax->fiatoshis:-1),dstr(block.vtx[0].vout[i].nValue));
243                     komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
244                 }
245                 else
246                 {
247                     hash = block.GetHash();
248                     //for (j=0; j<32; j++)
249                     //   printf("%02x",((uint8_t *)&hash)[j]);
250                     //printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i);
251                     komodo_paxmark(height,&space,txids[i-1],vouts[i-1],height);
252                 }
253             }
254         }
255         //printf("opretlen.%d num.%d\n",opretlen,num);
256     }
257     return(0);
258 }
259
260 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout)
261 {
262     uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo,kmdheight; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; double diff; uint64_t seed; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
263     tokomodo = (komodo_is_issuer() == 0);
264     if ( opretbuf[0] == 'D' )
265     {
266         if ( opretlen == 38 ) // any KMD tx
267         {
268             iguana_rwnum(0,&opretbuf[34],sizeof(kmdheight),&kmdheight);
269             memset(base,0,sizeof(base));
270             PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
271             if ( fiatoshis < 0 )
272                 fiatoshis = -fiatoshis;
273             bitcoin_address(coinaddr,addrtype,rmd160,20);
274             checktoshis = PAX_fiatdest(&seed,tokomodo,destaddr,pubkey33,coinaddr,kmdheight,base,fiatoshis);
275             typestr = "deposit";
276             printf("kmdheight.%d vs height.%d check %.8f vs %.8f tokomodo.%d %d seed.%llx\n",kmdheight,height,dstr(checktoshis),dstr(value),komodo_is_issuer(),strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0,(long long)seed);
277             diff = ((double)value / checktoshis) - 1.;
278             if ( diff < 0. )
279                 diff = -diff;
280             if ( kmdheight <= height )
281             {
282                 if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
283                 {
284                     for (i=0; i<opretlen; i++)
285                         printf("%02x",opretbuf[i]);
286                     printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo);
287                 }
288                 //paxprice seed.0 sum 0.07766840 densum 1000.00000000 basevol 0.01000000 height.59532
289                 if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 && shortflag == ASSETCHAINS_SHORTFLAG )
290                 {
291                     if ( shortflag == 0 )
292                     {
293                         for (i=0; i<32; i++)
294                             printf("%02x",((uint8_t *)&txid)[i]);
295                         printf(" <- txid.v%u ",vout);
296                         for (i=0; i<33; i++)
297                             printf("%02x",pubkey33[i]);
298                         printf(" checkpubkey check %.8f v %.8f dest.(%s) kmdheight.%d height.%d\n",dstr(checktoshis),dstr(value),destaddr,kmdheight,height);
299                         if ( value >= checktoshis || (seed == 0 && diff < .01) )
300                         {
301                             if ( komodo_paxfind(&space,txid,vout) == 0 )
302                                 komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,kmdheight);
303                         }
304                     }
305                     else // short
306                     {
307                         for (i=0; i<opretlen; i++)
308                             printf("%02x",opretbuf[i]);
309                         printf(" opret[%c] tokomodo.%d value %.8f vs check %.8f\n",opretbuf[0],tokomodo,dstr(value),dstr(checktoshis));
310                         if ( value <= checktoshis || (seed == 0 && diff < .01) )
311                         {
312                             
313                         }
314                     }
315                 }
316             }
317         }
318     }
319     else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
320     {
321         if ( tokomodo == 0 && opretbuf[0] == 'I' ) // assetchain coinbase
322         {
323             if ( 0 && ASSETCHAINS_SYMBOL[0] != 0 )
324             {
325                 for (i=0; i<opretlen; i++)
326                     printf("%02x",opretbuf[i]);
327                 printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo);
328             }
329             if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG )
330             {
331                 for (i=0; i<n; i++)
332                 {
333                     for (j=0; j<32; j++)
334                         printf("%02x",((uint8_t *)&txids[i])[j]);
335                     printf(" issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen);
336                     if ( komodo_paxmark(height,&space,txids[i],vouts[i],height) == 0 )
337                         komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height);
338                 }
339             }
340         }
341         else if ( tokomodo != 0 && opretbuf[0] == 'X' )
342         {
343             // verify and update limits
344         }
345     }
346     return(typestr);
347 }
348
349 void komodo_gateway_voutupdate(char *symbol,int32_t isspecial,int32_t height,int32_t txi,bits256 txid,int32_t vout,int32_t numvouts,uint64_t value,uint8_t *script,int32_t len)
350 {
351     int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
352     typestr = "unknown";
353     memcpy(&utxid,&txid,sizeof(utxid));
354     if ( script[offset++] == 0x6a )
355     {
356         offset += komodo_scriptitemlen(&opretlen,&script[offset]);
357         if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 )
358             typestr = "notarized";
359         else if ( txi == 0 && vout == 1 && opretlen == 149 )
360         {
361             typestr = "pricefeed";
362             komodo_paxpricefeed(height,&script[offset],opretlen);
363             //printf("height.%d pricefeed len.%d\n",height,opretlen);
364         }
365         else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout);
366     }
367     else if ( numvouts >= KOMODO_MINRATIFY )
368         typestr = "ratify";
369 }
370
371 int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
372 {
373     char *retstr,params[256],*hexstr; uint8_t script[10000]; cJSON *oldpub,*newpub,*json,*result,*vouts,*item,*sobj; int32_t vout,n,len,isspecial,retval = -1; uint64_t value; bits256 txid;
374     sprintf(params,"[\"%s\", 1]",txidstr);
375     if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
376     {
377         if ( (json= cJSON_Parse(retstr)) != 0 )
378         {
379             if ( (result= jobj(json,(char *)"result")) != 0 )
380             {
381                 oldpub = jobj(result,(char *)"vpub_old");
382                 newpub = jobj(result,(char *)"vpub_new");
383                 retval = 0;
384                 if ( oldpub == 0 && newpub == 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
385                 {
386                     isspecial = 0;
387                     txid = jbits256(result,(char *)"txid");
388                     for (vout=0; vout<n; vout++)
389                     {
390                         item = jitem(vouts,vout);
391                         value = SATOSHIDEN * jdouble(item,(char *)"value");
392                         if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
393                         {
394                             if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
395                             {
396                                 len = (int32_t)strlen(hexstr) >> 1;
397                                 if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
398                                     isspecial = 1;
399                                 else if ( len <= sizeof(script) )
400                                 {
401                                     decode_hex(script,len,hexstr);
402                                     komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len);
403                                 }
404                             }
405                         }
406                     }
407                 }
408             } else printf("error getting txids.(%s) %p\n",retstr,result);
409             free_json(json);
410         }
411         free(retstr);
412     }
413     return(retval);
414 }
415
416 int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
417 {
418     char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2;
419     sprintf(params,"[%d]",height);
420     if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
421     {
422         if ( (result= cJSON_Parse(retstr)) != 0 )
423         {
424             if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
425             {
426                 sprintf(params,"[\"%s\"]",txidstr);
427                 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
428                 {
429                     //printf("getblock.(%s)\n",retstr2);
430                     if ( (json= cJSON_Parse(retstr2)) != 0 )
431                     {
432                         if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
433                         {
434                             for (i=0; i<n; i++)
435                                 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
436                                     break;
437                             if ( i == n )
438                                 retval = 0;
439                             else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n);
440                         } else printf("cant get result.%p or tx.%p\n",result,tx);
441                         free_json(json);
442                     } else printf("cant parse2.(%s)\n",retstr2);
443                     free(retstr2);
444                 } else printf("error getblock %s\n",params);
445             } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
446             free_json(result);
447         } else printf("couldnt parse.(%s)\n",retstr);
448         free(retstr);
449     } else printf("error from getblockhash %d\n",height);
450     return(retval);
451 }
452
453 void komodo_gateway_iteration(char *symbol)
454 {
455     char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
456     if ( KMDHEIGHT <= 0 )
457         KMDHEIGHT = 1;
458     KOMODO_REALTIME = 0;
459     if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
460     {
461         if ( (infoobj= cJSON_Parse(retstr)) != 0 )
462         {
463             if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
464             {
465                 //printf("gateway KMDHEIGHT.%d kmdheight.%d\n",KMDHEIGHT,kmdheight);
466                 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
467                 {
468                     if ( (KMDHEIGHT % 10) == 0 )
469                     {
470                         if ( (KMDHEIGHT % 100) == 0 )
471                             fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
472                         memset(&zero,0,sizeof(zero));
473                         komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0);
474                     }
475                     if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
476                     {
477                         printf("error KMDHEIGHT %d\n",KMDHEIGHT);
478                         break;
479                     }
480                     usleep(10000);
481                 }
482                 if ( KMDHEIGHT >= kmdheight )
483                     KOMODO_REALTIME = (uint32_t)time(NULL);
484             }
485             free_json(infoobj);
486         }
487         free(retstr);
488     }
489     else
490     {
491         printf("error from %s\n",symbol);
492         sleep(30);
493     }
494 }
495
496 void komodo_iteration(char *symbol)
497 {
498     char *retstr,*base,*coinaddr,*txidstr,cmd[512]; uint64_t value,fiatoshis; cJSON *array,*item; int32_t i,n,vout,shortflag,height; uint256 txid; uint8_t rmd160[20],addrtype;
499     if ( ASSETCHAINS_SYMBOL[0] == 0 )
500     {
501         //[{"prev_hash":"5d5c9a49489b558de9e84f991f996dedaae6b9d0f157f82b2fec64662476d5cf","prev_vout":2,"EUR":0.78329000,"fiat":"EUR","height":57930,"KMD":0.10000000,"address":"RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5"}]
502         sprintf(cmd,"{\"agent\":\"dpow\",\"method\":\"pending\",\"fiat\":\"%s\"}",symbol);
503         if ( (retstr= issue_curl(cmd)) != 0 )
504         {
505             if ( (array= cJSON_Parse(retstr)) != 0 )
506             {
507                 if ( (n= cJSON_GetArraySize(array)) > 0 )
508                 {
509                     for (i=0; i<n; i++)
510                     {
511                         item = jitem(array,i);
512                         coinaddr = jstr(item,(char *)"address");
513                         value = jdouble(item,(char *)"KMD") * COIN;
514                         shortflag = juint(item,(char *)"short");
515                         vout = jint(item,(char *)"prev_vout");
516                         height = jint(item,(char *)"height");
517                         base = jstr(item,(char *)"fiat");
518                         txidstr = jstr(item,(char *)"prev_hash");
519                         if ( coinaddr != 0 && base != 0 && value > 0 && height > 0 )
520                         {
521                             fiatoshis = jdouble(item,base) * COIN;
522                             decode_hex((uint8_t *)&txid,sizeof(txid),txidstr);
523                             bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
524                             komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height);
525                         }
526                     }
527                 }
528             }
529             printf("retstr.(%s)\n",retstr);
530             free(retstr);
531         }
532     }
533 }
This page took 0.056429 seconds and 4 git commands to generate.