You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

608 lines
24 KiB

9 years ago
/******************************************************************************
* Copyright © 2014-2016 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
// included from basilisk.c
9 years ago
// requestid is invariant for a specific request
// quoteid is invariant for a specific request after dest fields are set
uint32_t basilisk_requestid(struct basilisk_request *rp)
{
struct basilisk_request R;
R = *rp;
9 years ago
R.requestid = R.quoteid = R.quotetime = 0;
9 years ago
R.destamount = 0;
R.relaybits = 0;
memset(R.desthash.bytes,0,sizeof(R.desthash.bytes));
9 years ago
if ( 0 )
9 years ago
{
int32_t i;
for (i=0; i<sizeof(R); i++)
printf("%02x",((uint8_t *)&R)[i]);
printf(" <- crc.%u\n",calc_crc32(0,(void *)&R,sizeof(R)));
9 years ago
char str[65],str2[65]; printf("B REQUESTID: t.%u r.%u q.%u %s %.8f %s -> %s %.8f %s crc.%u\n",R.timestamp,R.requestid,R.quoteid,R.src,dstr(R.srcamount),bits256_str(str,R.hash),R.dest,dstr(R.destamount),bits256_str(str2,R.desthash),calc_crc32(0,(void *)&R,sizeof(R)));
9 years ago
}
9 years ago
return(calc_crc32(0,(void *)&R,sizeof(R)));
9 years ago
}
uint32_t basilisk_quoteid(struct basilisk_request *rp)
{
struct basilisk_request R;
R = *rp;
9 years ago
R.requestid = R.quoteid = R.relaybits = 0;
9 years ago
return(calc_crc32(0,(void *)&R,sizeof(R)));
9 years ago
}
9 years ago
int32_t basilisk_rwDEXquote(int32_t rwflag,uint8_t *serialized,struct basilisk_request *rp)
{
int32_t len = 0;
len += iguana_rwnum(rwflag,&serialized[len],sizeof(rp->requestid),&rp->requestid);
9 years ago
len += iguana_rwnum(rwflag,&serialized[len],sizeof(rp->timestamp),&rp->timestamp); // must be 2nd
9 years ago
len += iguana_rwnum(rwflag,&serialized[len],sizeof(rp->quoteid),&rp->quoteid);
9 years ago
len += iguana_rwnum(rwflag,&serialized[len],sizeof(rp->quotetime),&rp->quotetime);
9 years ago
len += iguana_rwnum(rwflag,&serialized[len],sizeof(rp->relaybits),&rp->relaybits);
9 years ago
len += iguana_rwnum(rwflag,&serialized[len],sizeof(rp->srcamount),&rp->srcamount);
9 years ago
len += iguana_rwnum(rwflag,&serialized[len],sizeof(rp->minamount),&rp->minamount);
9 years ago
len += iguana_rwbignum(rwflag,&serialized[len],sizeof(rp->hash),rp->hash.bytes);
9 years ago
len += iguana_rwbignum(rwflag,&serialized[len],sizeof(rp->desthash),rp->desthash.bytes);
len += iguana_rwnum(rwflag,&serialized[len],sizeof(rp->destamount),&rp->destamount);
9 years ago
if ( rwflag != 0 )
{
memcpy(&serialized[len],rp->src,sizeof(rp->src)), len += sizeof(rp->src);
memcpy(&serialized[len],rp->dest,sizeof(rp->dest)), len += sizeof(rp->dest);
}
else
{
memcpy(rp->src,&serialized[len],sizeof(rp->src)), len += sizeof(rp->src);
memcpy(rp->dest,&serialized[len],sizeof(rp->dest)), len += sizeof(rp->dest);
}
9 years ago
if ( rp->quoteid != 0 && basilisk_quoteid(rp) != rp->quoteid )
9 years ago
printf("basilisk_rwDEXquote.%d: quoteid.%u mismatch calc %u\n",rwflag,rp->quoteid,basilisk_quoteid(rp));
9 years ago
if ( basilisk_requestid(rp) != rp->requestid )
9 years ago
printf("basilisk_rwDEXquote.%d: requestid.%u mismatch calc %u\n",rwflag,rp->requestid,basilisk_requestid(rp));
9 years ago
return(len);
}
9 years ago
uint32_t basilisk_request_enqueue(struct supernet_info *myinfo,struct basilisk_request *rp)
9 years ago
{
9 years ago
uint8_t serialized[256]; int32_t len; struct queueitem *item;
len = basilisk_rwDEXquote(1,serialized+1,rp);
if ( (item= calloc(1,sizeof(*item) + len + 1)) != 0 )
9 years ago
{
9 years ago
serialized[0] = len;
9 years ago
memcpy(&item[1],serialized,len + 1);
portable_mutex_lock(&myinfo->DEX_mutex);
DL_APPEND(myinfo->DEX_quotes,item);
portable_mutex_unlock(&myinfo->DEX_mutex);
9 years ago
printf("ENQUEUE.%u calc.%u\n",rp->requestid,basilisk_requestid(rp));
9 years ago
return(rp->requestid);
9 years ago
}
9 years ago
return(0);
9 years ago
}
9 years ago
struct basilisk_request *basilisk_parsejson(struct basilisk_request *rp,cJSON *reqjson)
{
9 years ago
uint32_t requestid,quoteid; //char *msgstr;
9 years ago
memset(rp,0,sizeof(*rp));
rp->hash = jbits256(reqjson,"hash");
rp->desthash = jbits256(reqjson,"desthash");
rp->srcamount = j64bits(reqjson,"srcamount");
rp->minamount = j64bits(reqjson,"minamount");
rp->destamount = j64bits(reqjson,"destamount");
requestid = juint(reqjson,"requestid");
quoteid = juint(reqjson,"quoteid");
if ( jstr(reqjson,"relay") != 0 )
rp->relaybits = (uint32_t)calc_ipbits(jstr(reqjson,"relay"));
rp->timestamp = juint(reqjson,"timestamp");
9 years ago
rp->quotetime = juint(reqjson,"quotetime");
9 years ago
safecopy(rp->src,jstr(reqjson,"src"),sizeof(rp->src));
safecopy(rp->dest,jstr(reqjson,"dest"),sizeof(rp->dest));
if ( quoteid != 0 )
{
rp->quoteid = basilisk_quoteid(rp);
if ( quoteid != rp->quoteid )
printf("basilisk_parsejson quoteid.%u != %u error\n",quoteid,rp->quoteid);
}
rp->requestid = basilisk_requestid(rp);
if ( requestid != rp->requestid )
printf("basilisk_parsejson requestid.%u != %u error\n",requestid,rp->requestid);
return(rp);
}
9 years ago
cJSON *basilisk_requestjson(struct basilisk_request *rp)
9 years ago
{
9 years ago
char ipaddr[64]; cJSON *item = cJSON_CreateObject(); //*msgobj,
9 years ago
if ( rp->relaybits != 0 )
{
expand_ipbits(ipaddr,rp->relaybits);
jaddstr(item,"relay",ipaddr);
}
9 years ago
jaddbits256(item,"hash",rp->hash);
9 years ago
if ( bits256_nonz(rp->desthash) != 0 )
jaddbits256(item,"desthash",rp->desthash);
9 years ago
jaddstr(item,"src",rp->src);
if ( rp->srcamount != 0 )
9 years ago
jadd64bits(item,"srcamount",rp->srcamount);
9 years ago
if ( rp->minamount != 0 )
9 years ago
jadd64bits(item,"minamount",rp->minamount);
9 years ago
jaddstr(item,"dest",rp->dest);
if ( rp->destamount != 0 )
9 years ago
jadd64bits(item,"destamount",rp->destamount);
9 years ago
jaddnum(item,"quotetime",rp->quotetime);
9 years ago
jaddnum(item,"timestamp",rp->timestamp);
jaddnum(item,"requestid",rp->requestid);
jaddnum(item,"quoteid",rp->quoteid);
9 years ago
if ( rp->quoteid != 0 && basilisk_quoteid(rp) != rp->quoteid )
printf("quoteid mismatch %u vs %u\n",basilisk_quoteid(rp),rp->quoteid);
if ( basilisk_requestid(rp) != rp->requestid )
9 years ago
printf("requestid mismatch %u vs calc %u\n",rp->requestid,basilisk_requestid(rp));
9 years ago
{
int32_t i; struct basilisk_request R;
if ( basilisk_parsejson(&R,item) != 0 )
{
if ( memcmp(&R,rp,sizeof(*rp)) != 0 )
{
for (i=0; i<sizeof(*rp); i++)
printf("%02x",((uint8_t *)rp)[i]);
printf(" <- rp\n");
for (i=0; i<sizeof(R); i++)
printf("%02x",((uint8_t *)&R)[i]);
printf(" <- R mismatch\n");
9 years ago
for (i=0; i<sizeof(R); i++)
if ( ((uint8_t *)rp)[i] != ((uint8_t *)&R)[i] )
9 years ago
printf("(%02x %02x).%d ",((uint8_t *)rp)[i],((uint8_t *)&R)[i],i);
9 years ago
printf("mismatches\n");
9 years ago
} //else printf("matched JSON conv %u %u\n",basilisk_requestid(&R),basilisk_requestid(rp));
9 years ago
}
}
9 years ago
return(item);
}
9 years ago
int32_t basilisk_request_create(struct basilisk_request *rp,cJSON *valsobj,bits256 hash,uint32_t timestamp)
{
9 years ago
char *dest,*src; uint32_t i;
9 years ago
memset(rp,0,sizeof(*rp));
if ( (dest= jstr(valsobj,"dest")) != 0 && (src= jstr(valsobj,"source")) != 0 && (rp->srcamount= j64bits(valsobj,"satoshis")) != 0 )
{
if ( (rp->destamount= j64bits(valsobj,"destsatoshis")) != 0 )
{
rp->desthash = jbits256(valsobj,"desthash");
for (i=0; i<4; i++)
if ( rp->desthash.ulongs[i] != 0 )
break;
if ( i != 4 )
rp->destamount = 0;
}
9 years ago
rp->minamount = j64bits(valsobj,"minamount");
9 years ago
rp->timestamp = timestamp;
rp->hash = hash;
strncpy(rp->src,src,sizeof(rp->src)-1);
strncpy(rp->dest,dest,sizeof(rp->dest)-1);
rp->requestid = basilisk_requestid(rp);
if ( rp->destamount != 0 && bits256_nonz(rp->desthash) != 0 )
{
rp->quoteid = basilisk_quoteid(rp);
printf("set quoteid.%u\n",rp->quoteid);
}
//printf("create.%u calc.%u\n",rp->requestid,basilisk_requestid(rp));
return(0);
}
return(-1);
}
9 years ago
char *basilisk_start(struct supernet_info *myinfo,struct basilisk_request *rp,uint32_t statebits)
{
9 years ago
cJSON *retjson;
9 years ago
if ( (bits256_cmp(rp->hash,myinfo->myaddr.persistent) == 0 || bits256_cmp(rp->desthash,myinfo->myaddr.persistent) == 0) )
9 years ago
{
9 years ago
printf("START thread to complete %u/%u for (%s %.8f) <-> (%s %.8f) q.%u\n",rp->requestid,rp->quoteid,rp->src,dstr(rp->srcamount),rp->dest,dstr(rp->destamount),rp->quoteid);
9 years ago
if ( basilisk_thread_start(myinfo,rp) != 0 )
9 years ago
{
9 years ago
basilisk_request_enqueue(myinfo,rp);
return(clonestr("{\"result\":\"started atomic swap thread\"}"));
9 years ago
}
9 years ago
else return(clonestr("{\"error\":\"couldnt atomic swap thread\"}"));
}
9 years ago
else if ( myinfo->RELAYID >= 0 )
9 years ago
{
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","basilisk node needs to start atomic thread locally");
return(jprint(retjson,1));
} else return(clonestr("{\"error\":\"unexpected basilisk_start not mine and amrelay\"}"));
9 years ago
}
9 years ago
struct basilisk_relay *basilisk_request_ensure(struct supernet_info *myinfo,uint32_t senderipbits,int32_t numrequests)
{
int32_t j; struct basilisk_relay *relay = 0;
if ( (j= basilisk_relayid(myinfo,senderipbits)) >= 0 )
{
relay = &myinfo->relays[j];
if ( numrequests > relay->maxrequests )
{
relay->maxrequests = numrequests;
relay->requests = realloc(relay->requests,sizeof(*relay->requests) * numrequests);
}
}
return(relay);
}
int32_t basilisk_ping_processDEX(struct supernet_info *myinfo,uint32_t senderipbits,uint8_t *data,int32_t datalen)
{
9 years ago
int32_t i,n,len=0; struct basilisk_relay *relay; struct basilisk_request R; uint8_t clen,serialized[256]; uint16_t sn; uint32_t crc;
9 years ago
portable_mutex_lock(&myinfo->DEX_reqmutex);
len += iguana_rwnum(0,&data[len],sizeof(sn),&sn);
if ( (relay= basilisk_request_ensure(myinfo,senderipbits,sn)) != 0 )
{
9 years ago
relay->numrequests = 0;
9 years ago
for (i=0; i<sn; i++)
{
clen = data[len++];
if ( len+clen <= datalen )
{
if ( relay->numrequests < relay->maxrequests )
{
memcpy(serialized,&data[len],clen);
9 years ago
printf("ping processDEX\n");
9 years ago
n = basilisk_rwDEXquote(0,serialized,&R);
if ( n != clen )
printf("n.%d clen.%d\n",n,clen);
len += clen;
9 years ago
crc = basilisk_requestid(&R);
if ( crc == R.requestid )
9 years ago
{
relay->requests[relay->numrequests++] = R;
9 years ago
printf("[(%s %.8f) -> (%s %.8f) r.%u q.%u] ",R.src,dstr(R.srcamount),R.dest,dstr(R.destamount),R.requestid,R.quoteid);
9 years ago
} else printf("crc.%u error vs %u\n",crc,R.requestid);
9 years ago
} else printf("relay num.%d >= max.%d\n",relay->numrequests,relay->maxrequests);
} else len += clen;
}
}
else
{
for (i=0; i<sn; i++)
{
if ( len+clen <= datalen )
{
clen = data[len++];
len += clen;
}
}
}
portable_mutex_unlock(&myinfo->DEX_reqmutex);
return(len);
}
int32_t basilisk_ping_genDEX(struct supernet_info *myinfo,uint8_t *data,int32_t maxlen)
{
struct queueitem *item,*tmp; uint8_t clen; int32_t i,datalen = 0; uint16_t sn; uint32_t timestamp,now;
datalen += sizeof(uint16_t);
i = 0;
now = (uint32_t)time(NULL);
portable_mutex_lock(&myinfo->DEX_mutex);
DL_FOREACH_SAFE(myinfo->DEX_quotes,item,tmp)
{
memcpy(&clen,&item[1],sizeof(clen));
if ( datalen+clen < maxlen )
{
memcpy(&data[datalen],&item[1],clen+1), datalen += (clen + 1);
i++;
}
9 years ago
iguana_rwnum(0,(void *)((long)&item[1] + 1 + sizeof(uint32_t)),sizeof(timestamp),&timestamp);
9 years ago
if ( now > timestamp + BASILISK_DEXDURATION )
{
DL_DELETE(myinfo->DEX_quotes,item);
free(item);
9 years ago
} else printf("now.%u vs timestamp.%u, lag.%d\n",now,timestamp,now-timestamp);
9 years ago
}
portable_mutex_unlock(&myinfo->DEX_mutex);
sn = i;
iguana_rwnum(1,data,sizeof(sn),&sn); // fill in at beginning
return(datalen);
}
9 years ago
static int _cmp_requests(const void *a,const void *b)
{
#define uint32_a (*(struct basilisk_request *)a).requestid
#define uint32_b (*(struct basilisk_request *)b).requestid
if ( uint32_b > uint32_a )
return(1);
else if ( uint32_b < uint32_a )
return(-1);
else
{
#undef uint32_a
#undef uint32_b
#define uint32_a (*(struct basilisk_request *)a).quoteid
#define uint32_b (*(struct basilisk_request *)b).quoteid
if ( uint32_b > uint32_a )
return(1);
else if ( uint32_b < uint32_a )
return(-1);
}
return(0);
#undef uint32_a
#undef uint32_b
}
9 years ago
struct basilisk_request *_basilisk_requests_uniq(struct supernet_info *myinfo,int32_t *nump,uint8_t *space,int32_t spacesize)
9 years ago
{
9 years ago
int32_t i,j,n,k,m; struct basilisk_relay *relay; struct basilisk_request *requests,*rp;
for (j=m=0; j<myinfo->numrelays; j++)
m += myinfo->relays[j].numrequests;
if ( m*sizeof(*requests) <= spacesize )
requests = (void *)space;
else requests = calloc(m,sizeof(*requests));
9 years ago
for (j=m=0; j<myinfo->numrelays; j++)
{
relay = &myinfo->relays[j];
if ( (n= relay->numrequests) > 0 )
{
for (i=0; i<n; i++)
{
rp = &relay->requests[i];
9 years ago
for (k=0; k<m; k++)
if ( memcmp(&requests[k],rp,sizeof(requests[k])) == 0 )
break;
if ( k == m )
9 years ago
{
9 years ago
requests[m].relaybits = relay->ipbits;
requests[m++] = *rp;
9 years ago
}
}
}
}
9 years ago
qsort(requests,m,sizeof(*requests),_cmp_requests);
9 years ago
*nump = m;
return(requests);
}
9 years ago
char *basilisk_respond_swapstatus(struct supernet_info *myinfo,bits256 hash,uint32_t requestid,uint32_t quoteid)
{
cJSON *array,*retjson;
array = cJSON_CreateArray();
retjson = cJSON_CreateObject();
jadd(retjson,"result",array);
return(jprint(retjson,1));
}
9 years ago
char *basilisk_respond_requests(struct supernet_info *myinfo,bits256 hash,uint32_t requestid,uint32_t quoteid)
{
9 years ago
int32_t i,qflag,num=0; cJSON *retjson,*array; struct basilisk_request *requests,*rp; uint8_t space[4096];
9 years ago
array = cJSON_CreateArray();
portable_mutex_lock(&myinfo->DEX_reqmutex);
if ( (requests= _basilisk_requests_uniq(myinfo,&num,space,sizeof(space))) != 0 )
{
9 years ago
//printf("numrequests.%d r.%u q.%u\n",num,requestid,quoteid);
9 years ago
for (i=0; i<num; i++)
{
rp = &requests[i];
9 years ago
if ( quoteid == 0 || (quoteid == rp->quoteid && (bits256_cmp(hash,rp->hash) == 0 || bits256_cmp(hash,rp->desthash) == 0)) )
qflag = 1;
else qflag = 0;
if ( requestid == 0 || (rp->requestid == requestid && qflag != 0) )
9 years ago
jaddi(array,basilisk_requestjson(rp));
9 years ago
}
}
9 years ago
portable_mutex_unlock(&myinfo->DEX_reqmutex);
9 years ago
if ( requests != (void *)space )
free(requests);
9 years ago
retjson = cJSON_CreateObject();
jadd(retjson,"result",array);
return(jprint(retjson,1));
}
9 years ago
char *basilisk_respond_accept(struct supernet_info *myinfo,uint32_t requestid,uint32_t quoteid)
9 years ago
{
9 years ago
int32_t i,num=0; char *retstr=0; struct basilisk_request *requests,*rp; uint8_t space[4096];
9 years ago
portable_mutex_lock(&myinfo->DEX_reqmutex);
9 years ago
if ( (requests= _basilisk_requests_uniq(myinfo,&num,space,sizeof(space))) != 0 )
9 years ago
{
9 years ago
for (i=0; i<num; i++)
9 years ago
{
9 years ago
rp = &requests[i];
9 years ago
if ( rp->requestid == requestid && rp->quoteid == quoteid )
9 years ago
{
9 years ago
printf("start from accept\n");
9 years ago
retstr = basilisk_start(myinfo,rp,1);
break;
9 years ago
}
}
}
9 years ago
portable_mutex_unlock(&myinfo->DEX_reqmutex);
9 years ago
if ( requests != (void *)space )
free(requests);
9 years ago
if ( retstr == 0 )
retstr = clonestr("{\"error\":\"couldnt find to requestid to choose\"}");
9 years ago
return(retstr);
}
9 years ago
// respond to incoming RID, ACC, DEX, QST
9 years ago
char *basilisk_respond_RID(struct supernet_info *myinfo,char *CMD,void *addr,char *remoteaddr,uint32_t basilisktag,cJSON *valsobj,uint8_t *data,int32_t datalen,bits256 hash,int32_t from_basilisk)
{
9 years ago
return(basilisk_respond_requests(myinfo,hash,juint(valsobj,"requestid"),0));
9 years ago
}
9 years ago
char *basilisk_respond_SWP(struct supernet_info *myinfo,char *CMD,void *addr,char *remoteaddr,uint32_t basilisktag,cJSON *valsobj,uint8_t *data,int32_t datalen,bits256 hash,int32_t from_basilisk)
9 years ago
{
9 years ago
return(basilisk_respond_swapstatus(myinfo,hash,juint(valsobj,"requestid"),juint(valsobj,"quoteid")));
9 years ago
}
9 years ago
char *basilisk_respond_ACC(struct supernet_info *myinfo,char *CMD,void *addr,char *remoteaddr,uint32_t basilisktag,cJSON *valsobj,uint8_t *data,int32_t datalen,bits256 hash,int32_t from_basilisk)
9 years ago
{
9 years ago
uint32_t requestid,quoteid;
9 years ago
if ( (requestid= juint(valsobj,"requestid")) != 0 && (quoteid= juint(valsobj,"quoteid")) != 0 )
9 years ago
return(basilisk_respond_accept(myinfo,requestid,quoteid));
else return(clonestr("{\"error\":\"need nonzero requestid and quoteid\"}"));
9 years ago
}
char *basilisk_respond_DEX(struct supernet_info *myinfo,char *CMD,void *addr,char *remoteaddr,uint32_t basilisktag,cJSON *valsobj,uint8_t *data,int32_t datalen,bits256 hash,int32_t from_basilisk)
{
9 years ago
char *retstr=0,buf[256]; struct basilisk_request R;
if ( basilisk_request_create(&R,valsobj,hash,juint(valsobj,"timestamp")) == 0 )
9 years ago
{
9 years ago
char str[65]; printf("DEX.(%s %.8f) -> %s %s\n",R.src,dstr(R.srcamount),R.dest,bits256_str(str,hash));
9 years ago
if ( basilisk_request_enqueue(myinfo,&R) != 0 )
9 years ago
{
9 years ago
sprintf(buf,"{\"result\":\"DEX request added\",\"requestid\":%u}",R.requestid);
9 years ago
retstr = clonestr(buf);
} else retstr = clonestr("{\"error\":\"DEX quote couldnt be created\"}");
9 years ago
} else retstr = clonestr("{\"error\":\"missing or invalid fields\"}");
9 years ago
return(retstr);
}
#include "../includes/iguana_apidefs.h"
#include "../includes/iguana_apideclares.h"
9 years ago
THREE_STRINGS_AND_DOUBLE(tradebot,aveprice,comment,base,rel,basevolume)
{
double retvals[4],aveprice; cJSON *retjson = cJSON_CreateObject();
aveprice = instantdex_avehbla(myinfo,retvals,base,rel,basevolume);
jaddstr(retjson,"result","success");
jaddnum(retjson,"aveprice",aveprice);
jaddnum(retjson,"avebid",retvals[0]);
jaddnum(retjson,"bidvol",retvals[1]);
jaddnum(retjson,"aveask",retvals[2]);
jaddnum(retjson,"askvol",retvals[3]);
return(jprint(retjson,1));
}
9 years ago
ZERO_ARGS(InstantDEX,allcoins)
{
9 years ago
struct iguana_info *tmp; cJSON *basilisk,*virtual,*full,*retjson = cJSON_CreateObject();
full = cJSON_CreateArray();
basilisk = cJSON_CreateArray();
virtual = cJSON_CreateArray();
9 years ago
HASH_ITER(hh,myinfo->allcoins,coin,tmp)
{
9 years ago
if ( coin->virtualchain != 0 )
jaddistr(virtual,coin->symbol);
if ( coin->RELAYNODE != 0 || coin->VALIDATENODE != 0 )
jaddistr(full,coin->symbol);
else jaddistr(basilisk,coin->symbol);
9 years ago
}
9 years ago
jadd(retjson,"basilisk",basilisk);
jadd(retjson,"full",full);
jadd(retjson,"virtual",virtual);
9 years ago
return(jprint(retjson,1));
}
STRING_ARG(InstantDEX,available,source)
{
9 years ago
if ( source != 0 && source[0] != 0 && (coin= iguana_coinfind(source)) != 0 )
9 years ago
{
if ( myinfo->expiration != 0 )
return(bitcoinrpc_getbalance(myinfo,coin,json,remoteaddr,"*",coin->chain->minconfirms,1,1<<30));
else return(clonestr("{\"error\":\"need to unlock wallet\"}"));
} else return(clonestr("{\"error\":\"specified coin is not active\"}"));
}
9 years ago
HASH_ARRAY_STRING(InstantDEX,request,hash,vals,hexstr)
9 years ago
{
9 years ago
myinfo->DEXactive = (uint32_t)time(NULL) + INSTANTDEX_LOCKTIME;
9 years ago
jadd64bits(vals,"minamount",jdouble(vals,"minprice") * jdouble(vals,"amount") * SATOSHIDEN);
9 years ago
if ( jobj(vals,"desthash") == 0 )
jaddbits256(vals,"desthash",hash);
jadd64bits(vals,"satoshis",jdouble(vals,"amount") * SATOSHIDEN);
jadd64bits(vals,"destsatoshis",jdouble(vals,"destamount") * SATOSHIDEN);
9 years ago
jaddnum(vals,"timestamp",time(NULL));
hash = myinfo->myaddr.persistent;
9 years ago
printf("service.(%s)\n",jprint(vals,0));
9 years ago
{
9 years ago
uint8_t serialized[512]; struct basilisk_request R; cJSON *reqjson;
9 years ago
memset(&R,0,sizeof(R));
if ( basilisk_request_create(&R,vals,hash,juint(vals,"timestamp")) == 0 )
{
printf("R.requestid.%u vs calc %u, q.%u\n",R.requestid,basilisk_requestid(&R),R.quoteid);
9 years ago
if ( myinfo->RELAYID >= 0 )
R.relaybits = myinfo->myaddr.myipbits;
if ( (reqjson= basilisk_requestjson(&R)) != 0 )
9 years ago
free_json(reqjson);
printf("R.requestid.%u vs calc %u, q.%u\n",R.requestid,basilisk_requestid(&R),R.quoteid);
basilisk_rwDEXquote(1,serialized,&R);
basilisk_rwDEXquote(0,serialized,&R);
} else printf("error creating request\n");
}
9 years ago
return(basilisk_standardservice("DEX",myinfo,0,myinfo->myaddr.persistent,vals,"",1));
9 years ago
}
9 years ago
INT_ARG(InstantDEX,automatched,requestid)
{
// return quoteid
9 years ago
myinfo->DEXactive = (uint32_t)time(NULL) + INSTANTDEX_LOCKTIME;
9 years ago
return(clonestr("{\"result\":\"automatched not yet\"}"));
}
9 years ago
INT_ARG(InstantDEX,incoming,requestid)
{
cJSON *vals; char *retstr;
9 years ago
myinfo->DEXactive = (uint32_t)time(NULL) + INSTANTDEX_LOCKTIME;
9 years ago
if ( myinfo->RELAYID >= 0 )
9 years ago
return(basilisk_respond_requests(myinfo,myinfo->myaddr.persistent,requestid,0));
9 years ago
else
{
vals = cJSON_CreateObject();
9 years ago
jaddnum(vals,"requestid",(uint32_t)requestid);
9 years ago
jaddbits256(vals,"hash",myinfo->myaddr.persistent);
retstr = basilisk_standardservice("RID",myinfo,0,myinfo->myaddr.persistent,vals,"",1);
free_json(vals);
return(retstr);
}
}
9 years ago
TWO_INTS(InstantDEX,swapstatus,requestid,quoteid)
9 years ago
{
cJSON *vals; char *retstr;
9 years ago
myinfo->DEXactive = (uint32_t)time(NULL) + INSTANTDEX_LOCKTIME;
9 years ago
if ( myinfo->RELAYID >= 0 )
9 years ago
return(basilisk_respond_swapstatus(myinfo,myinfo->myaddr.persistent,requestid,quoteid));
9 years ago
else
{
vals = cJSON_CreateObject();
9 years ago
jaddnum(vals,"requestid",(uint32_t)requestid);
jaddnum(vals,"quoteid",(uint32_t)quoteid);
9 years ago
jaddbits256(vals,"hash",myinfo->myaddr.persistent);
9 years ago
retstr = basilisk_standardservice("SWP",myinfo,0,myinfo->myaddr.persistent,vals,"",1);
9 years ago
free_json(vals);
return(retstr);
}
}
9 years ago
TWO_INTS(InstantDEX,accept,requestid,quoteid)
9 years ago
{
9 years ago
cJSON *vals; char *retstr;
9 years ago
myinfo->DEXactive = (uint32_t)time(NULL) + INSTANTDEX_LOCKTIME;
9 years ago
if ( myinfo->RELAYID >= 0 )
9 years ago
return(basilisk_respond_accept(myinfo,requestid,quoteid));
9 years ago
else
{
vals = cJSON_CreateObject();
9 years ago
jaddnum(vals,"quoteid",(uint32_t)quoteid);
jaddnum(vals,"requestid",(uint32_t)requestid);
9 years ago
retstr = basilisk_standardservice("ACC",myinfo,0,myinfo->myaddr.persistent,vals,"",1);
9 years ago
free_json(vals);
return(retstr);
}
}
#include "../includes/iguana_apiundefs.h"
9 years ago
int32_t basilisk_request_cmpref(struct basilisk_request *ref,struct basilisk_request *rp)
9 years ago
{
9 years ago
if ( bits256_cmp(rp->hash,ref->hash) != 0 || memcmp(rp->src,ref->src,sizeof(ref->src)) != 0 || memcmp(rp->dest,ref->dest,sizeof(ref->dest)) != 0 || rp->srcamount != ref->srcamount || rp->timestamp != ref->timestamp )
9 years ago
{
9 years ago
printf("basilisk_request_listprocess mismatched hash\n");
return(-1);
} else return(0);
}