Browse Source

bit coin buy

release/v0.1
jl777 9 years ago
parent
commit
f6c935e8c3
  1. 2
      crypto777/cJSON.c
  2. 68
      iguana/exchanges/bitcoin.c
  3. 4
      iguana/exchanges777.h
  4. 2
      iguana/iguana777.h
  5. 25
      iguana/iguana_exchanges.c
  6. 46
      iguana/iguana_instantdex.c
  7. 2
      iguana/iguana_msg.c
  8. 2
      iguana/iguana_peers.c
  9. 4
      iguana/iguana_recv.c
  10. 2
      iguana/main.c

2
crypto777/cJSON.c

@ -1075,6 +1075,8 @@ uint64_t calc_nxt64bits(const char *NXTaddr)
printf("calc_nxt64bits: warning: 64bit overflow %llx < %llx\n",(long long)nxt64bits,(long long)lastval);
lastval = nxt64bits;
}
while ( *NXTaddr == '0' && *NXTaddr != 0 )
NXTaddr++;
if ( cmp_nxt64bits(NXTaddr,nxt64bits) != 0 )
printf("error calculating nxt64bits: %s -> %llx -> %s\n",NXTaddr,(long long)nxt64bits,nxt64str(nxt64bits));
if ( polarity < 0 )

68
iguana/exchanges/bitcoin.c

@ -1379,9 +1379,75 @@ cJSON *BALANCES(struct exchange_info *exchange,cJSON *argjson)
return(cJSON_Parse("{\"error\":\"bitcoin is not yet\"}"));
}
int32_t is_valid_BTCother(char *other)
{
if ( iguana_coinfind(other) != 0 )
return(1);
else if ( strcmp(other,"NXT") == 0 || strcmp(other,"nxt") == 0 )
return(1);
else if ( is_decimalstr(other) > 0 )
return(1);
else return(0);
}
uint64_t TRADE(int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *base,char *rel,int32_t dir,double price,double volume,cJSON *argjson)
{
return(0);
struct instantdex_accept *ap; char *str; uint64_t txid = 0; cJSON *json; struct supernet_info *myinfo; int32_t hops = 3;
myinfo = SuperNET_MYINFO(0);
if ( strcmp(base,"BTC") == 0 || strcmp(base,"btc") == 0 )
{
base = rel;
rel = "BTC";
dir = -dir;
volume *= price;
price = 1. / price;
}
if ( is_valid_BTCother(base) != 0 && (strcmp(rel,"BTC") == 0 || strcmp(rel,"btc") == 0) )
{
ap = 0;//instantdex_acceptable(exchange,base,rel,"BTC",dir,price,volume);
if ( dotrade == 0 )
{
if ( retstrp != 0 )
{
if ( ap != 0 )
*retstrp = jprint(instantdex_acceptjson(ap),1);
else *retstrp = clonestr("{\"result\":\"would issue new trade\"}");
}
}
else
{
if ( ap != 0 )
{
}
else if ( dir < 0 )
{
printf("bitcoin sell is not yet\n");
return(0);
}
else
{
json = cJSON_CreateObject();
jaddstr(json,"base",base);
jaddstr(json,"rel","BTC");
jaddnum(json,"maxprice",price);
jaddnum(json,"volume",volume);
if ( (str= instantdex_sendcmd(myinfo,json,"BTCoffer",myinfo->ipaddr,hops)) != 0 )
free(str);
free_json(json);
if ( (str= instantdex_queueaccept(exchange,base,"BTC",price,volume,-1,"BTC",INSTANTDEX_OFFERDURATION)) != 0 )
{
if ( (json= cJSON_Parse(str)) != 0 )
{
txid = j64bits(json,"orderid");
free_json(json);
}
free(str);
}
}
}
}
return(txid);
}
char *ORDERSTATUS(struct exchange_info *exchange,uint64_t orderid,cJSON *argjson)

4
iguana/exchanges777.h

@ -19,6 +19,7 @@
#include <curl/curl.h>
#include <curl/easy.h>
#define INSTANTDEX_OFFERDURATION 3600
#define EXCHANGES777_MINPOLLGAP 3
#define EXCHANGES777_MAXDEPTH 200
@ -93,7 +94,7 @@ struct instantdex_accept { struct queueitem DL; uint64_t orderid; uint32_t dead;
struct instantdex_accept *instantdex_acceptablefind(struct exchange_info *exchange,cJSON *bids,cJSON *asks,uint64_t orderid,char *base,char *rel);
cJSON *instantdex_acceptjson(struct instantdex_accept *ap);
struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,cJSON *array,char *refstr,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume);
struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,char *refstr,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume);
void *curl_post(void **cHandlep,char *url,char *userpass,char *postfields,char *hdr0,char *hdr1,char *hdr2,char *hdr3);
char *instantdex_sendcmd(struct supernet_info *myinfo,cJSON *argjson,char *cmdstr,char *ipaddr,int32_t hops);
@ -111,5 +112,6 @@ double truefx_price(struct exchange_info *exchange,char *base,char *rel,struct e
double fxcm_price(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert);
double instaforex_price(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert);
char *instantdex_queueaccept(struct exchange_info *exchange,char *base,char *rel,double price,double basevolume,int32_t acceptdir,char *myside,int32_t duration);
#endif

2
iguana/iguana777.h

@ -65,7 +65,7 @@ struct iguana_txdatabits { uint64_t addrind:IGUANA_LOG2MAXPEERS,filecount:10,fpo
#define IGUANA_DEDICATED_THREADS
#ifdef IGUANA_DEDICATED_THREADS
#define IGUANA_MAXCONNTHREADS 16
#define IGUANA_MAXCONNTHREADS 3
#define IGUANA_MAXSENDTHREADS IGUANA_MAXPEERS
#define IGUANA_MAXRECVTHREADS IGUANA_MAXPEERS
#else

25
iguana/iguana_exchanges.c

@ -239,7 +239,15 @@ void exchanges777_json_quotes(struct exchange_info *exchange,double commission,c
timestamp = juint(jitem(item,2),0);
else orderid = j64bits(jitem(item,2),0);
}
else continue;
else
{
price = jdouble(item,"price");
volume = jdouble(item,"volume");
timestamp = juint(item,"timestamp");
orderid = j64bits(item,"orderid");
}
if ( price == 0. || volume == 0. )
continue;
if ( price > SMALLVAL && volume > SMALLVAL )
{
price = exchange_setquote(bidasks,&numbids,&numasks,bidask,invert,price,volume,commission,orderid,timestamp,offerNXT);
@ -585,10 +593,12 @@ void exchanges777_loop(void *ptr)
_crypto_update(PEGS,PEGS->cryptovols,&PEGS->data,1,peggyflag);
PEGS->lastupdate = (uint32_t)time(NULL);
}
printf("exchanges loop.(%s) %p\n",exchange->name,&exchange->requestQ);
while ( 1 )
{
if ( peggyflag != 0 )
{
printf("nonz peggy\n");
PAX_idle(PEGS,peggyflag,3);
if ( time(NULL) > PEGS->lastupdate+100 )
{
@ -713,18 +723,20 @@ char *exchanges777_submit(struct exchange_info *exchange,struct exchange_request
if ( maxseconds == 0 )
maxseconds = EXCHANGES777_DEFAULT_TIMEOUT;
retstrp = req->retstrp;
//printf("submit to %p\n",&exchange->requestQ);
queue_enqueue("exchangeQ",&exchange->requestQ,&req->DL,0);
for (i=0; i<maxseconds; i++)
{
if ( retstrp != 0 && (retstr= *retstrp) != 0 )
{
//printf("GOT.(%s)\n",retstr);
//printf("exchanges777_submit GOT.(%s)\n",retstr);
free(retstrp);
return(retstr);
}
sleep(1);
}
req->timedout = (uint32_t)time(NULL);
printf("exchanges777_submit timed out.(%c)\n",func);
return(clonestr("{\"error\":\"request timed out\"}"));
}
@ -790,6 +802,7 @@ char *exchanges777_Qrequest(struct exchange_info *exchange,int32_t func,char *ba
safecopy(req->rel,rel,sizeof(req->rel));
req->retstrp = calloc(1,sizeof(void *));
req->orderid = orderid;
//printf("Qrequest\n");
return(exchanges777_submit(exchange,req,func,maxseconds));
}
@ -887,6 +900,7 @@ struct exchange_info *exchange_create(char *exchangestr,cJSON *argjson)
exchange->commission *= .01;
printf("ADDEXCHANGE.(%s) [%s, %s, %s] commission %.3f%%\n",exchangestr,exchange->apikey,exchange->userid,exchange->apisecret,exchange->commission * 100.);
Exchanges[exchangeid] = exchange;
iguana_launch(iguana_coinadd("BTCD"),"exchangeloop",(void *)exchanges777_loop,exchange,IGUANA_EXCHANGETHREAD);
return(exchange);
}
@ -899,7 +913,6 @@ struct exchange_info *exchanges777_info(char *exchangestr,int32_t sleepflag,cJSO
{
if ( (exchange= exchange_create(exchangestr,json)) != 0 )
{
iguana_launch(iguana_coinadd("BTCD"),"exchangeloop",(void *)exchanges777_loop,exchange,IGUANA_EXCHANGETHREAD);
if ( sleepflag > 0 )
sleep(sleepflag);
}
@ -912,13 +925,15 @@ struct exchange_info *exchanges777_info(char *exchangestr,int32_t sleepflag,cJSO
void exchanges777_init(struct supernet_info *myinfo,cJSON *exchanges,int32_t sleepflag)
{
int32_t i,n; cJSON *argjson,*item; bits256 instantdexhash; struct exchange_info *exchange;
if ( (exchange= exchanges777_find("bitcoin")) == 0 && (exchange= exchange_create("bitcoin",0)) != 0 )
myinfo->tradingexchanges[myinfo->numexchanges++] = exchange;
if ( 0 && exchanges != 0 )
{
n = cJSON_GetArraySize(exchanges);
for (i=0; i<n; i++)
{
item = jitem(exchanges,i);
if ( (exchange= exchange_create(jstr(item,"name"),item)) != 0 )
if ( (exchange= exchanges777_find(jstr(item,"name"))) == 0 && (exchange= exchange_create(jstr(item,"name"),item)) != 0 )
myinfo->tradingexchanges[myinfo->numexchanges++] = exchange;
}
}
@ -926,7 +941,7 @@ void exchanges777_init(struct supernet_info *myinfo,cJSON *exchanges,int32_t sle
{
argjson = cJSON_CreateObject();
for (i=0; i<sizeof(Exchange_funcs)/sizeof(*Exchange_funcs); i++)
if ( (exchange= exchanges777_info(Exchange_funcs[i]->name,sleepflag,argjson,0)) != 0 )
if ( (exchange= exchanges777_find(Exchange_funcs[i]->name)) == 0 && (exchange= exchanges777_info(Exchange_funcs[i]->name,sleepflag,argjson,0)) != 0 )
myinfo->tradingexchanges[myinfo->numexchanges++] = exchange;
free_json(argjson);
}

46
iguana/iguana_instantdex.c

@ -19,7 +19,6 @@
#define INSTANTDEX_HOPS 3
#define INSTANTDEX_DURATION 60
#define INSTANTDEX_OFFERDURATION 3600
cJSON *InstantDEX_argjson(char *reference,char *message,char *othercoinaddr,char *otherNXTaddr,int32_t iter,int32_t val,int32_t val2)
{
@ -208,17 +207,18 @@ double instantdex_aveprice(struct supernet_info *myinfo,struct exchange_quote *s
int32_t instantdex_bidaskdir(struct instantdex_accept *ap)
{
if ( ap->A.myside == 0 && ap->A.acceptdir > 0 ) // base
return(1);
else if ( ap->A.myside == 1 && ap->A.acceptdir < 0 ) // rel
return(-1);
else if ( ap->A.myside == 1 && ap->A.acceptdir < 0 ) // rel
return(1);
else return(0);
}
cJSON *instantdex_acceptjson(struct instantdex_accept *ap)
{
int32_t dir;
int32_t dir; struct supernet_info *myinfo = SuperNET_MYINFO(0);
cJSON *item = cJSON_CreateObject();
jadd64bits(item,"orderid",ap->orderid);
jadd64bits(item,"offerNXT",myinfo->myaddr.nxt64bits);
if ( ap->dead != 0 )
jadd64bits(item,"dead",ap->dead);
if ( (dir= instantdex_bidaskdir(ap)) > 0 )
@ -233,6 +233,7 @@ cJSON *instantdex_acceptjson(struct instantdex_accept *ap)
}
jaddstr(item,"base",ap->A.base);
jaddstr(item,"rel",ap->A.rel);
jaddnum(item,"timestamp",ap->A.expiration);
jaddnum(item,"price",ap->A.price);
jaddnum(item,"volume",ap->A.basevolume);
jaddnum(item,"pendingvolume",ap->A.pendingvolume);
@ -256,11 +257,12 @@ struct instantdex_accept *instantdex_acceptablefind(struct exchange_info *exchan
}
if ( (item= instantdex_acceptjson(ap)) != 0 )
{
//printf("item.(%s)\n",jprint(item,0));
if ( (type= jstr(item,"type")) != 0 )
{
if ( strcmp(type,"bid") == 0 && bids != 0 )
jaddi(bids,item);
else if ( strcmp(type,"ask") != 0 && asks != 0 )
else if ( strcmp(type,"ask") == 0 && asks != 0 )
jaddi(asks,item);
}
}
@ -270,7 +272,7 @@ struct instantdex_accept *instantdex_acceptablefind(struct exchange_info *exchan
return(retap);
}
struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,cJSON *array,char *refstr,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume)
struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,char *refstr,char *base,char *rel,char *offerside,int32_t offerdir,double offerprice,double volume)
{
struct instantdex_accept PAD,*ap,*retap = 0; double bestprice = 0.; uint32_t now;
now = (uint32_t)time(NULL);
@ -291,8 +293,6 @@ struct instantdex_accept *instantdex_acceptable(struct exchange_info *exchange,c
}
}
}
if ( array != 0 )
jaddi(array,instantdex_acceptjson(ap));
queue_enqueue("acceptableQ",&exchange->acceptableQ,&ap->DL,0);
} else free(ap);
}
@ -514,25 +514,25 @@ char *InstantDEX_hexmsg(struct supernet_info *myinfo,void *ptr,int32_t len,char
char *instantdex_queueaccept(struct exchange_info *exchange,char *base,char *rel,double price,double basevolume,int32_t acceptdir,char *myside,int32_t duration)
{
struct instantdex_accept A; bits256 hash;
struct instantdex_accept *ap; bits256 hash;
if ( exchange != 0 )
{
memset(&A,0,sizeof(A));
OS_randombytes((uint8_t *)&A.A.nonce,sizeof(A.A.nonce));
safecopy(A.A.base,base,sizeof(A.A.base));
safecopy(A.A.rel,rel,sizeof(A.A.rel));
ap = calloc(1,sizeof(*ap));
OS_randombytes((uint8_t *)&ap->A.nonce,sizeof(ap->A.nonce));
safecopy(ap->A.base,base,sizeof(ap->A.base));
safecopy(ap->A.rel,rel,sizeof(ap->A.rel));
if ( strcmp(myside,base) == 0 )
A.A.myside = 0;
ap->A.myside = 0;
else if ( strcmp(myside,rel) == 0 )
A.A.myside = 1;
else A.A.myside = -1;
A.A.acceptdir = acceptdir;
A.A.price = price, A.A.basevolume = basevolume;
A.A.expiration = (uint32_t)time(NULL) + duration;
vcalc_sha256(0,hash.bytes,(void *)&A.A,sizeof(A.A));
A.orderid = hash.txid;
queue_enqueue("acceptableQ",&exchange->acceptableQ,&A.DL,0);
return(clonestr("{\"result\":\"added acceptable\"}"));
ap->A.myside = 1;
else ap->A.myside = -1;
ap->A.acceptdir = acceptdir;
ap->A.price = price, ap->A.basevolume = basevolume;
ap->A.expiration = (uint32_t)time(NULL) + duration;
vcalc_sha256(0,hash.bytes,(void *)&ap->A,sizeof(ap->A));
ap->orderid = hash.txid;
queue_enqueue("acceptableQ",&exchange->acceptableQ,&ap->DL,0);
return(jprint(instantdex_acceptjson(ap),1));
}
else return(clonestr("{\"error\":\"invalid exchange\"}"));
}

2
iguana/iguana_msg.c

@ -557,7 +557,7 @@ int32_t iguana_msgparser(struct iguana_info *coin,struct iguana_peer *addr,struc
addr->lastgotaddr = (uint32_t)time(NULL);
addr->msgcounts.addr++;
}
printf("%s -> addr recvlen.%d num.%d\n",addr->ipaddr,recvlen,(int32_t)x);
//printf("%s -> addr recvlen.%d num.%d\n",addr->ipaddr,recvlen,(int32_t)x);
}
else if ( strcmp(H->command,"headers") == 0 )
{

2
iguana/iguana_peers.c

@ -445,7 +445,7 @@ int32_t iguana_recv(char *ipaddr,int32_t usock,uint8_t *recvbuf,int32_t len)
{
if ( errno == EAGAIN )
{
printf("%s recv errno.%d %s\n",ipaddr,errno,strerror(errno));
//printf("%s recv errno.%d %s\n",ipaddr,errno,strerror(errno));
//printf("EAGAIN for len %d, remains.%d\n",len,remains);
sleep(1);
} else return(-errno);

4
iguana/iguana_recv.c

@ -53,7 +53,7 @@ int32_t iguana_sendblockreqPT(struct iguana_info *coin,struct iguana_peer *addr,
coin->numreqsent++;
addr->pendblocks++;
addr->pendtime = (uint32_t)time(NULL);
printf("REQ.%s bundlei.%d hdrsi.%d\n",bits256_str(hexstr,hash2),bundlei,bp!=0?bp->hdrsi:-1);
//printf("REQ.%s bundlei.%d hdrsi.%d\n",bits256_str(hexstr,hash2),bundlei,bp!=0?bp->hdrsi:-1);
} else printf("MSG_BLOCK null datalen.%d\n",len);
return(len);
}
@ -568,7 +568,7 @@ struct iguana_bundlereq *iguana_recvblockhashes(struct iguana_info *coin,struct
}
}
}
printf("issue block1\n");
//printf("issue block1\n");
struct iguana_block *block;
if ( num == coin->chain->bundlesize+1 && (block= iguana_blockhashset(coin,-1,blockhashes[1],1)) != 0 )
block->blockhashes = blockhashes, req->hashes = 0;

2
iguana/main.c

@ -354,7 +354,7 @@ void iguana_main(void *arg)
strcpy(MYINFO.rpcsymbol,"BTCD");
iguana_launchcoin(MYINFO.rpcsymbol,cJSON_Parse("{}"));
if ( 0 && (str= SuperNET_JSON(&MYINFO,cJSON_Parse("{\"wallet\":\"password\",\"agent\":\"iguana\",\"method\":\"addcoin\",\"services\":128,\"maxpeers\":3,\"newcoin\":\"BTCD\",\"active\":1}"),0)) != 0 )
if ( 0 && (str= SuperNET_JSON(&MYINFO,cJSON_Parse("{\"wallet\":\"password\",\"agent\":\"iguana\",\"method\":\"addcoin\",\"services\":128,\"maxpeers\":3,\"newcoin\":\"BTCD\",\"active\":0}"),0)) != 0 )
{
printf("got.(%s)\n",str);
free(str);

Loading…
Cancel
Save