diff --git a/crypto777/bitcoind_RPC.c b/crypto777/bitcoind_RPC.c index 63f5bb72c..91bcfdb40 100755 --- a/crypto777/bitcoind_RPC.c +++ b/crypto777/bitcoind_RPC.c @@ -189,7 +189,7 @@ try_again: free(s.ptr); return(0); } - else if ( numretries >= 2 ) + else if ( numretries >= 1 ) { printf("Maximum number of retries exceeded!\n"); free(s.ptr); @@ -285,7 +285,7 @@ static size_t WriteMemoryCallback(void *ptr,size_t size,size_t nmemb,void *data) mem->size += realsize; mem->memory[mem->size] = 0; } - printf("got %d bytes\n",(int32_t)(size*nmemb)); + //printf("got %d bytes\n",(int32_t)(size*nmemb)); return(realsize); } diff --git a/crypto777/iguana_OS.c b/crypto777/iguana_OS.c index be8eddf25..74fef127e 100755 --- a/crypto777/iguana_OS.c +++ b/crypto777/iguana_OS.c @@ -19,6 +19,7 @@ #include "OS_portable.h" #include +#include #ifndef MAP_FILE #define MAP_FILE 0 #endif diff --git a/iguana/SuperNET.h b/iguana/SuperNET.h index 0260952d8..c55e1bf2c 100644 --- a/iguana/SuperNET.h +++ b/iguana/SuperNET.h @@ -118,6 +118,7 @@ struct category_info extern struct category_info *Categories; struct category_msg { struct queueitem DL; struct tai t; uint64_t remoteipbits; int32_t len; uint8_t msg[]; }; +struct exchange_quote { uint64_t satoshis,orderid,offerNXT; double price,volume; uint32_t timestamp,val; }; void expand_epbits(char *endpoint,struct endpoint epbits); struct endpoint calc_epbits(char *transport,uint32_t ipbits,uint16_t port,int32_t type); @@ -160,7 +161,7 @@ struct category_chain *category_chain_functions(struct supernet_info *myinfo,bit #define category_default_latest() (*cchain->default_func)(cchain,'L',0,0,0,0,zero) void category_init(struct supernet_info *myinfo); char *SuperNET_keysinit(struct supernet_info *myinfo,char *jsonstr); -double instantdex_aveprice(struct supernet_info *myinfo,char *base,char *rel,double volume,cJSON *argjson); +double instantdex_aveprice(struct supernet_info *myinfo,struct exchange_quote *sortbuf,int32_t max,double *totalvolp,char *base,char *rel,double volume,cJSON *argjson); #endif diff --git a/iguana/exchanges/bitfinex.c b/iguana/exchanges/bitfinex.c index 15b79cc1e..764167ca3 100755 --- a/iguana/exchanges/bitfinex.c +++ b/iguana/exchanges/bitfinex.c @@ -34,11 +34,11 @@ static char *BASERELS[][2] = { {"btc","usd"}, {"ltc","usd"}, {"ltc","btc"}, {"",""} }; #include "exchange_supports.h" -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char url[1024]; sprintf(url,"https://api.bitfinex.com/v1/book/%s%s",base,rel); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,"price","amount",maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,"price","amount",maxdepth,0,invert)); } char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr,cJSON *argjson) diff --git a/iguana/exchanges/bitstamp.c b/iguana/exchanges/bitstamp.c index 465041201..30c9b9a5a 100755 --- a/iguana/exchanges/bitstamp.c +++ b/iguana/exchanges/bitstamp.c @@ -34,11 +34,11 @@ static char *BASERELS[][2] = { {"btc","usd"} }; #include "exchange_supports.h" -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char url[1024]; sprintf(url,"https://www.bitstamp.net/api/order_book/"); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *method,char *payload) diff --git a/iguana/exchanges/bittrex.c b/iguana/exchanges/bittrex.c index b93254073..f032b764b 100755 --- a/iguana/exchanges/bittrex.c +++ b/iguana/exchanges/bittrex.c @@ -73,7 +73,7 @@ int32_t SUPPORTS(struct exchange_info *exchange,char *base,char *rel,cJSON *argj else return(0); } -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { cJSON *json,*obj; char *jsonstr,market[128],url[1024]; double hbla = 0.; sprintf(market,"%s-%s",rel,base); @@ -84,7 +84,7 @@ double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchang if ( (json = cJSON_Parse(jsonstr)) != 0 ) { if ( (obj= cJSON_GetObjectItem(json,"success")) != 0 && is_cJSON_True(obj) != 0 ) - hbla = exchanges777_json_orderbook(exchange,commission,base,rel,quotes,maxdepth,json,"result","buy","sell","Rate","Quantity"); + hbla = exchanges777_json_orderbook(exchange,commission,base,rel,quotes,maxdepth,json,"result","buy","sell","Rate","Quantity",invert); free_json(json); } free(jsonstr); diff --git a/iguana/exchanges/btc38.c b/iguana/exchanges/btc38.c index 42e4d33a9..268dce776 100755 --- a/iguana/exchanges/btc38.c +++ b/iguana/exchanges/btc38.c @@ -90,11 +90,11 @@ int32_t SUPPORTS(struct exchange_info *exchange,char *_base,char *_rel,cJSON *ar if ( strcmp(btc38_cnypairs[i],base) == 0 ) return(1); } - printf("BTC38 doesnt support (%s/%s)\n",base,rel); + //printf("BTC38 doesnt support (%s/%s)\n",base,rel); return(0); } -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char url[1024],lrel[16],lbase[16]; strcpy(lrel,rel), strcpy(lbase,base); @@ -102,7 +102,7 @@ double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchang if ( strcmp(lbase,"cny") == 0 && strcmp(lrel,"btc") == 0 ) sprintf(url,"http://api.btc38.com/v1/depth.php?c=%s&mk_type=%s","btc","cny"); else sprintf(url,"http://api.btc38.com/v1/depth.php?c=%s&mk_type=%s",lbase,lrel); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,char *path) diff --git a/iguana/exchanges/btce.c b/iguana/exchanges/btce.c index 9064d9fc7..c7a255b06 100755 --- a/iguana/exchanges/btce.c +++ b/iguana/exchanges/btce.c @@ -35,14 +35,14 @@ static char *BASERELS[][2] = { {"btc","usd"}, {"btc","rur"}, {"btc","eur"}, {"lt #include "exchange_supports.h" -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char field[64],url[1024],lbase[16],lrel[16]; strcpy(lrel,rel), strcpy(lbase,base); tolowercase(lrel), tolowercase(lbase); sprintf(field,"%s_%s",lbase,lrel); sprintf(url,"https://btc-e.com/api/3/depth/%s",field); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,field)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,field,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *url,char *payload) diff --git a/iguana/exchanges/coinbase.c b/iguana/exchanges/coinbase.c index b1292abd9..1a0fd190d 100755 --- a/iguana/exchanges/coinbase.c +++ b/iguana/exchanges/coinbase.c @@ -34,11 +34,11 @@ static char *BASERELS[][2] = { {"btc","usd"} }; #include "exchange_supports.h" -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char url[1024]; sprintf(url,"https://api.exchange.coinbase.com/products/%s-%s/book?level=2",base,rel); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,uint64_t nonce,char *path,char *method) diff --git a/iguana/exchanges/huobi.c b/iguana/exchanges/huobi.c index e1fd2b874..402049a7c 100755 --- a/iguana/exchanges/huobi.c +++ b/iguana/exchanges/huobi.c @@ -33,12 +33,12 @@ static char *BASERELS[][2] = { {"btc","cny"}, {"ltc","cny"} }; #include "exchange_supports.h" -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char url[1024],lbase[16]; strcpy(lbase,base), tolowercase(lbase); sprintf(url,"http://api.huobi.com/staticmarket/depth_%s_json.js ",lbase); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload) diff --git a/iguana/exchanges/lakebtc.c b/iguana/exchanges/lakebtc.c index a51a5b602..bbf897c20 100755 --- a/iguana/exchanges/lakebtc.c +++ b/iguana/exchanges/lakebtc.c @@ -33,7 +33,7 @@ static char *BASERELS[][2] = { {"btc","usd"}, {"btc","cny"} }; #include "exchange_supports.h" -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char url[1024]; if ( strcmp(rel,"USD") == 0 ) @@ -41,7 +41,7 @@ double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchang else if ( strcmp(rel,"CNY") == 0 ) sprintf(url,"https://www.LakeBTC.com/api_v1/bcorderbook_cny"); else printf("illegal lakebtc pair.(%s/%s)\n",base,rel); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,char *hdr1,uint64_t tonce) diff --git a/iguana/exchanges/nxtae.c b/iguana/exchanges/nxtae.c new file mode 100755 index 000000000..0cdb065f3 --- /dev/null +++ b/iguana/exchanges/nxtae.c @@ -0,0 +1,175 @@ +/****************************************************************************** + * 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. * + * * + ******************************************************************************/ + +#define EXCHANGE_NAME "nxtae" +#define UPDATE nxtae ## _price +#define SUPPORTS nxtae ## _supports +#define SIGNPOST nxtae ## _signpost +#define TRADE nxtae ## _trade +#define ORDERSTATUS nxtae ## _orderstatus +#define CANCELORDER nxtae ## _cancelorder +#define OPENORDERS nxtae ## _openorders +#define TRADEHISTORY nxtae ## _tradehistory +#define BALANCES nxtae ## _balances +#define PARSEBALANCE nxtae ## _parsebalance +#define WITHDRAW nxtae ## _withdraw +#define CHECKBALANCE nxtae ## _checkbalance +#define ALLPAIRS nxtae ## _allpairs +#define FUNCS nxtae ## _funcs +#define BASERELS nxtae ## _baserels + +static char *BASERELS[][2] = { {"btc","nxt"}, {"btc","btcd"}, {"btc","ltc"}, {"btc","vrc"}, {"btc","doge"} }; +#include "exchange_supports.h" + +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) +{ + char url[1024],lrel[16],lbase[16]; + strcpy(lrel,rel), strcpy(lbase,base); + tolowercase(lrel), tolowercase(lbase); + sprintf(url,"http://api.quadrigacx.com/v2/order_book?book=%s_%s",lbase,lrel); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); +} + +cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,char *path) +{ + char url[1024],req[1024],md5secret[128],tmp[1024],dest[1025],hdr1[512],hdr2[512],hdr3[512],hdr4[512],*sig,*data = 0; + cJSON *json; uint64_t nonce; + hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0; + json = 0; + nonce = exchange_nonce(exchange) * 1000 + ((uint64_t)OS_milliseconds() % 1000); + sprintf(tmp,"%llu%s%s",(long long)nonce,exchange->userid,exchange->apikey); + calc_md5(md5secret,exchange->apisecret,(int32_t)strlen(exchange->apisecret)); + if ( (sig= hmac_sha256_str(dest,md5secret,(int32_t)strlen(md5secret),tmp)) != 0 ) + { + sprintf(req,"{\"key\":\"%s\",%s\"nonce\":%llu,\"signature\":\"%s\"}",exchange->apikey,payload,(long long)nonce,sig); + sprintf(hdr1,"Content-Type:application/json"); + sprintf(hdr2,"charset=utf-8"); + sprintf(hdr3,"Content-Length:%ld",(long)strlen(req)); + sprintf(url,"https://api.quadrigacx.com/v2/%s",path); + if ( dotrade == 0 ) + data = exchange_would_submit(req,hdr1,hdr2,hdr3,hdr4); + else if ( (data= curl_post(&exchange->cHandle,url,0,req,hdr1,hdr2,hdr3,hdr4)) != 0 ) + json = cJSON_Parse(data); + } + if ( retstrp != 0 ) + *retstrp = data; + else if ( data != 0 ) + free(data); + return(json); +} + +char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr,cJSON *argjson) +{ + //[{"btc_available":"0.00000000","btc_reserved":"0.00000000","btc_balance":"0.00000000","cad_available":"0.00","cad_reserved":"0.00","cad_balance":"0.00","usd_available":"0.00","usd_reserved":"0.00","usd_balance":"0.00","xau_available":"0.000000","xau_reserved":"0.000000","xau_balance":"0.000000","fee":"0.5000"}] + char field[128],*str,*itemstr = 0; cJSON *obj; double reserv,total; + *balancep = 0.; + strcpy(field,coinstr); + tolowercase(field); + strcat(field,"_available"); + if ( argjson != 0 && (str= jstr(argjson,field)) != 0 ) + { + *balancep = jdouble(argjson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_reserved"); + reserv = jdouble(argjson,field); + strcpy(field,coinstr), tolowercase(field), strcat(field,"_balance"); + total = jdouble(argjson,field); + obj = cJSON_CreateObject(); + jaddnum(obj,"balance",*balancep); + jaddnum(obj,"locked_balance",reserv); + jaddnum(obj,"total",total); + itemstr = jprint(obj,1); + } + if ( itemstr == 0 ) + return(clonestr("{\"error\":\"cant find coin balance\"}")); + return(itemstr); +} + +cJSON *BALANCES(struct exchange_info *exchange,cJSON *argjson) +{ + return(SIGNPOST(&exchange->cHandle,1,0,exchange,"","balance")); +} + +#include "checkbalance.c" + +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) +{ + char payload[1024],pairstr[64],*extra,*path; cJSON *json; uint64_t txid = 0; + if ( (extra= *retstrp) != 0 ) + *retstrp = 0; + if ( (dir= flipstr_for_exchange(exchange,pairstr,"%s_%s",dir,&price,&volume,base,rel,argjson)) == 0 ) + { + printf("cant find baserel (%s/%s)\n",base,rel); + return(0); + } + path = (dir > 0) ? "buy" : "sell"; + //key - API key + //signature - signature + //nonce - nonce + //amount - amount of major currency + //price - price to buy at + //book - optional, if not specified, will default to btc_cad + sprintf(payload,"\"amount\":%.6f,\"price\":%.3f,\"book\":\"%s_%s\",",volume,price,base,rel); + if ( CHECKBALANCE(retstrp,dotrade,exchange,dir,base,rel,price,volume,argjson) == 0 && (json= SIGNPOST(&exchange->cHandle,dotrade,retstrp,exchange,payload,path)) != 0 ) + { + // parse json and set txid + free_json(json); + } + return(txid); +} + +char *ORDERSTATUS(struct exchange_info *exchange,uint64_t quoteid,cJSON *argjson) +{ + char buf[64]; + sprintf(buf,"\"id\":%llu,",(long long)quoteid); + return(jprint(SIGNPOST(&exchange->cHandle,1,0,exchange,buf,"lookup_order"),1)); +} + +char *CANCELORDER(struct exchange_info *exchange,uint64_t quoteid,cJSON *argjson) +{ + char buf[64]; + sprintf(buf,"\"id\":%llu,",(long long)quoteid); + return(jprint(SIGNPOST(&exchange->cHandle,1,0,exchange,buf,"cancel_order"),1)); +} + +char *OPENORDERS(struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(SIGNPOST(&exchange->cHandle,1,0,exchange,"","open_orders"),1)); +} + +char *TRADEHISTORY(struct exchange_info *exchange,cJSON *argjson) +{ + return(jprint(SIGNPOST(&exchange->cHandle,1,0,exchange,"","user_transactions"),1)); +} + +char *WITHDRAW(struct exchange_info *exchange,char *base,double amount,char *destaddr,cJSON *argjson) +{ + uint64_t txid,assetid,assetoshis; cJSON *retjson = cJSON_CreateObject(); + if ( is_validNXT(destaddr) < 0 ) + jaddstr(retjson,"error","invalid NXT address"); + else if ( (assetid= is_MGW_asset(base)) == 0 ) + jaddstr(retjson,"error","invalid MGW asset"); + else if ( is_validNXT_amount(base) < 0 ) + jaddstr(retjson,"error","invalid NXT asset"); + else if ( (txid= MGW_redeem(passphrase,assetid,assetoshis,destaddr)) != 0 ) + { + jaddstr(retjson,"result","success"); + jadd64bits(retjson,"redeemtxid",txid); + } else jaddstr(retjson,"error","couldnt submit MGW redeem"); + return(jprint(retjson,1)); +} + +struct exchange_funcs nxtae_funcs = EXCHANGE_FUNCS(nxtae,EXCHANGE_NAME); + +#include "exchange_undefs.h" diff --git a/iguana/exchanges/okcoin.c b/iguana/exchanges/okcoin.c index 677f5682b..2ab09f883 100755 --- a/iguana/exchanges/okcoin.c +++ b/iguana/exchanges/okcoin.c @@ -34,7 +34,7 @@ static char *BASERELS[][2] = { {"btc","usd"}, {"ltc","usd"} }; #include "exchange_supports.h" -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char url[1024],lrel[16],lbase[16]; strcpy(lrel,rel), strcpy(lbase,base); @@ -47,7 +47,7 @@ double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchang exit(-1); return(0); } - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *url,char *payload) diff --git a/iguana/exchanges/poloniex.c b/iguana/exchanges/poloniex.c index b6e507eb2..186fa51b6 100755 --- a/iguana/exchanges/poloniex.c +++ b/iguana/exchanges/poloniex.c @@ -81,12 +81,12 @@ int32_t SUPPORTS(struct exchange_info *exchange,char *base,char *rel,cJSON *argj else return(0); } -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char market[128],url[1024]; sprintf(market,"%s_%s",rel,base); sprintf(url,"https://poloniex.com/public?command=returnOrderBook¤cyPair=%s&depth=%d",market,maxdepth); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *url,char *payload) diff --git a/iguana/exchanges/quadriga.c b/iguana/exchanges/quadriga.c index 3d95ead94..6347c5118 100755 --- a/iguana/exchanges/quadriga.c +++ b/iguana/exchanges/quadriga.c @@ -33,13 +33,13 @@ static char *BASERELS[][2] = { {"btc","usd"}, {"btc","cad"} }; #include "exchange_supports.h" -double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson) +double UPDATE(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert) { char url[1024],lrel[16],lbase[16]; strcpy(lrel,rel), strcpy(lbase,base); tolowercase(lrel), tolowercase(lbase); sprintf(url,"https://api.quadrigacx.com/v2/order_book?book=%s_%s",lbase,lrel); - return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0)); + return(exchanges777_standardprices(exchange,commission,base,rel,url,quotes,0,0,maxdepth,0,invert)); } cJSON *SIGNPOST(void **cHandlep,int32_t dotrade,char **retstrp,struct exchange_info *exchange,char *payload,char *path) diff --git a/iguana/exchanges777.h b/iguana/exchanges777.h index 7683fb85c..fd8b7c18c 100755 --- a/iguana/exchanges777.h +++ b/iguana/exchanges777.h @@ -25,12 +25,11 @@ #define EXCHANGES777_DEFAULT_TIMEOUT 30 struct exchange_info; -struct exchange_quote { double price,volume; uint64_t orderid,offerNXT; uint32_t timestamp,val; }; struct exchange_funcs { char name[32]; - double (*price)(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson); + double (*price)(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *quotes,int32_t maxdepth,double commission,cJSON *argjson,int32_t invert); int32_t (*supports)(struct exchange_info *exchange,char *base,char *rel,cJSON *argjson); char *(*parsebalance)(struct exchange_info *exchange,double *balancep,char *coinstr,cJSON *argjson); cJSON *(*balances)(struct exchange_info *exchange,cJSON *argjson); diff --git a/iguana/iguana.sources b/iguana/iguana.sources index 9ad5d06e4..efb83c985 100755 --- a/iguana/iguana.sources +++ b/iguana/iguana.sources @@ -1,3 +1,3 @@ #iguana_html.c -SOURCES := SuperNET.c SuperNET_category.c SuperNET_hexmsg.c iguana_exchanges.c iguana_tradebots.c iguana_instantdex.c pangea_api.c pangea_bets.c cards777.c pangea_summary.c pangea_json.c pangea_hand.c poker.c ramchain_api.c iguana_tx.c iguana_wallet.c iguana_pubkeys.c iguana_recv.c iguana_bundles.c iguana_msg.c iguana_rpc.c iguana777.c iguana_chains.c iguana_peers.c iguana_accept.c iguana_bitmap.c iguana_init.c iguana_ramchain.c iguana_blocks.c iguana_json.c main.c +SOURCES := SuperNET.c SuperNET_keys.c SuperNET_category.c SuperNET_hexmsg.c iguana_exchanges.c iguana_tradebots.c iguana_instantdex.c pangea_api.c pangea_bets.c cards777.c pangea_summary.c pangea_json.c pangea_hand.c poker.c ramchain_api.c iguana_tx.c iguana_wallet.c iguana_pubkeys.c iguana_recv.c iguana_bundles.c iguana_msg.c iguana_rpc.c iguana777.c iguana_chains.c iguana_peers.c iguana_accept.c iguana_bitmap.c iguana_init.c iguana_ramchain.c iguana_blocks.c iguana_json.c main.c diff --git a/iguana/iguana_exchanges.c b/iguana/iguana_exchanges.c index baa02e3ed..fca1da8d8 100755 --- a/iguana/iguana_exchanges.c +++ b/iguana/iguana_exchanges.c @@ -19,9 +19,9 @@ #define EXCHANGE777_ISPENDING 2 #define EXCHANGE777_REQUEUE 3 -char *Exchange_names[] = { "poloniex", "bitfinex", "btc38", "bitstamp", "btce", "bittrex", "huobi", "coinbase", "okcoin", "lakebtc", "quadriga",};// "truefx", "ecb", "instaforex", "fxcm", "yahoo" }; +char *Exchange_names[] = { "poloniex", "bittrex", "btc38", "huobi", };// "bitstamp", "bitfinex", "btce", "coinbase", "okcoin", "lakebtc", "quadriga",};// "truefx", "ecb", "instaforex", "fxcm", "yahoo" }; - struct exchange_info *Exchanges[sizeof(Exchange_names)/sizeof(*Exchange_names)]; +struct exchange_info *Exchanges[sizeof(Exchange_names)/sizeof(*Exchange_names)]; void prices777_processprice(struct exchange_info *exchange,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth) { @@ -165,9 +165,9 @@ char *exchanges777_orderbook_jsonstr(struct exchange_info *exchange,char *_base, return(jprint(json,1)); } -void exchanges777_json_quotes(struct exchange_info *exchange,double commission,char *base,char *rel,double *lastbidp,double *lastaskp,double *hblap,struct exchange_quote *bidasks,cJSON *bids,cJSON *asks,int32_t maxdepth,char *pricefield,char *volfield,uint32_t reftimestamp) +void exchanges777_json_quotes(struct exchange_info *exchange,double commission,char *base,char *rel,double *lastbidp,double *lastaskp,double *hblap,struct exchange_quote *bidasks,cJSON *bids,cJSON *asks,int32_t maxdepth,char *pricefield,char *volfield,uint32_t reftimestamp,int32_t invert) { - int32_t i,slot,n=0,m=0,dir,bidask,slot_ba,numitems,numbids,numasks; uint64_t orderid,offerNXT; + int32_t i,slot,ba2,n=0,m=0,dir,bidask,slot_ba,numitems,numbids,numasks; uint64_t orderid,offerNXT; cJSON *item; struct exchange_quote *quote; uint32_t timestamp; double price,volume,hbla = 0.; *lastbidp = *lastaskp = 0.; numbids = numasks = 0; @@ -214,29 +214,37 @@ void exchanges777_json_quotes(struct exchange_info *exchange,double commission,c else continue; if ( price > SMALLVAL && volume > SMALLVAL ) { + if ( invert != 0 ) + { + ba2 = (1 ^ bidask); + volume *= price; + price = 1. / price; + } + else ba2 = bidask; if ( commission != 0. ) { //printf("price %f fee %f -> ",price,prices->commission * price); - if ( bidask == 0 ) + if ( ba2 == 0 ) price -= commission * price; else price += commission * price; //printf("%f\n",price); } - quote = (bidask == 0) ? &bidasks[numbids<<1] : &bidasks[(numasks<<1) + 1]; + quote = (ba2 == 0) ? &bidasks[numbids<<1] : &bidasks[(numasks<<1) + 1]; quote->price = price, quote->volume = volume, quote->timestamp = timestamp, quote->orderid = orderid, quote->offerNXT = offerNXT; - if ( bidask == 0 ) + if ( ba2 == 0 ) slot_ba = (numbids++ << 1); else slot_ba = (numasks++ << 1) | 1; if ( i == 0 ) { - if ( bidask == 0 ) + if ( ba2 == 0 ) *lastbidp = price; else *lastaskp = price; if ( hbla == 0. ) hbla = price; else hbla = 0.5 * (hbla + price); } - printf("%d,%d: %-8s %s %5s/%-5s %13.8f vol %13.8f | i %13.8f vol %13.8f | t.%u\n",numbids,numasks,exchange->name,dir>0?"bid":"ask",base,rel,price,volume,1./price,volume*price,timestamp); + quote->satoshis = (price * SATOSHIDEN); + //printf("%d,%d: %-8s %s %5s/%-5s %13.8f vol %13.8f | i %13.8f vol %13.8f | t.%u\n",numbids,numasks,exchange->name,dir>0?"bid":"ask",base,rel,price,volume,1./price,volume*price,timestamp); } } } @@ -244,7 +252,7 @@ void exchanges777_json_quotes(struct exchange_info *exchange,double commission,c *hblap = hbla; } -double exchanges777_json_orderbook(struct exchange_info *exchange,double commission,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth,cJSON *json,char *resultfield,char *bidfield,char *askfield,char *pricefield,char *volfield) +double exchanges777_json_orderbook(struct exchange_info *exchange,double commission,char *base,char *rel,struct exchange_quote *bidasks,int32_t maxdepth,cJSON *json,char *resultfield,char *bidfield,char *askfield,char *pricefield,char *volfield,int32_t invert) { cJSON *obj = 0,*bidobj=0,*askobj=0; double lastbid,lastask,hbla = 0.; int32_t numasks=0,numbids=0; if ( resultfield == 0 ) @@ -257,22 +265,22 @@ double exchanges777_json_orderbook(struct exchange_info *exchange,double commiss askobj = jarray(&numasks,obj,askfield); if ( bidobj != 0 || askobj != 0 ) { - exchanges777_json_quotes(exchange,commission,base,rel,&lastbid,&lastask,&hbla,bidasks,bidobj,askobj,maxdepth,pricefield,volfield,0); + exchanges777_json_quotes(exchange,commission,base,rel,&lastbid,&lastask,&hbla,bidasks,bidobj,askobj,maxdepth,pricefield,volfield,0,invert); } } return(hbla); } -double exchanges777_standardprices(struct exchange_info *exchange,double commission,char *base,char *rel,char *url,struct exchange_quote *quotes,char *price,char *volume,int32_t maxdepth,char *field) +double exchanges777_standardprices(struct exchange_info *exchange,double commission,char *base,char *rel,char *url,struct exchange_quote *quotes,char *price,char *volume,int32_t maxdepth,char *field,int32_t invert) { char *jsonstr; cJSON *json; double hbla = 0.; if ( (jsonstr= issue_curl(url)) != 0 ) { //if ( strcmp(exchangestr,"btc38") == 0 ) - printf("(%s) -> (%s)\n",url,jsonstr); + //printf("(%s) -> (%s)\n",url,jsonstr); if ( (json= cJSON_Parse(jsonstr)) != 0 ) { - hbla = exchanges777_json_orderbook(exchange,commission,base,rel,quotes,maxdepth,json,field,"bids","asks",price,volume); + hbla = exchanges777_json_orderbook(exchange,commission,base,rel,quotes,maxdepth,json,field,"bids","asks",price,volume,invert); free_json(json); } free(jsonstr); @@ -423,9 +431,36 @@ int32_t baserel_polarity(char *pairs[][2],int32_t n,char *_base,char *_rel) #include "exchanges/coinbase.c" #include "exchanges/bitstamp.c" +int32_t exchanges777_orient(struct exchange_info *exchange,char *base,char *rel,double *pricep,double *volumep,struct exchange_request *req) +{ + int32_t polarity = 1; + if ( exchange->issue.supports != 0 ) + polarity = (*exchange->issue.supports)(exchange,req->base,req->rel,req->argjson); + if ( polarity < 0 ) + { + strcpy(base,req->rel); + strcpy(rel,req->base); + if ( volumep != 0 ) + *volumep = (req->price * req->volume); + if ( pricep != 0 && req->price != 0 ) + *pricep = 1. / req->price; + } + else + { + strcpy(rel,req->rel); + strcpy(base,req->base); + if ( pricep != 0 ) + *pricep = req->price; + if ( volumep != 0 ) + *volumep = req->volume; + } + touppercase(base), touppercase(rel); + return(polarity); +} + char *exchanges777_process(struct exchange_info *exchange,int32_t *retvalp,struct exchange_request *req) { - char *retstr = 0; int32_t dir; uint64_t orderid; double balance; cJSON *balancejson,*retjson; + char base[32],rel[32],*retstr = 0; int32_t polarity; uint64_t orderid; double price,volume,balance; cJSON *balancejson,*retjson; *retvalp = EXCHANGE777_DONE; switch ( req->func ) { @@ -433,23 +468,25 @@ char *exchanges777_process(struct exchange_info *exchange,int32_t *retvalp,struc if ( exchange->issue.price != 0 ) { memset(req->bidasks,0,req->depth * sizeof(*req->bidasks) * 2); - (*exchange->issue.price)(exchange,req->base,req->rel,req->bidasks,req->depth,req->commission,req->argjson); - retstr = exchanges777_orderbook_jsonstr(exchange,req->base,req->rel,req->bidasks,req->depth,req->invert,req->allflag); + polarity = exchanges777_orient(exchange,base,rel,0,0,req); + (*exchange->issue.price)(exchange,base,rel,req->bidasks,req->depth,req->commission,req->argjson,polarity < 0); + retstr = exchanges777_orderbook_jsonstr(exchange,base,rel,req->bidasks,req->depth,0,req->allflag); } break; case 'S': if ( exchange->issue.supports != 0 ) { - dir = (*exchange->issue.supports)(exchange,req->base,req->rel,req->argjson); + polarity = (*exchange->issue.supports)(exchange,req->base,req->rel,req->argjson); retjson = cJSON_CreateObject(); - jaddnum(retjson,"result",dir); + jaddnum(retjson,"result",polarity); retstr = jprint(retjson,1); } break; case 'T': if ( exchange->issue.trade != 0 ) { - orderid = (*exchange->issue.trade)(req->dotrade,&retstr,exchange,req->base,req->rel,req->dir,req->price,req->volume,req->argjson); + polarity = exchanges777_orient(exchange,base,rel,&price,&volume,req); + orderid = (*exchange->issue.trade)(req->dotrade,&retstr,exchange,base,rel,polarity * req->dir,price,volume,req->argjson); if ( retstr == 0 ) { retjson = cJSON_CreateObject(); @@ -511,7 +548,7 @@ void exchanges777_loop(void *ptr) if ( req->dead == 0 ) { retstr = exchanges777_process(exchange,&retval,req); - printf("retval.%d (%p) retstrp.%p timedout.%u\n",retval,retstr,req->retstrp,req->timedout); + //printf("retval.%d (%p) retstrp.%p timedout.%u\n",retval,retstr,req->retstrp,req->timedout); if ( retval == EXCHANGE777_DONE ) { if ( retstr != 0 ) @@ -553,20 +590,21 @@ void exchanges777_loop(void *ptr) { if ( (req= queue_dequeue(&exchange->pricesQ,0)) != 0 ) { - printf("check %s pricesQ (%s %s)\n",exchange->name,req->base,req->rel); + //printf("check %s pricesQ (%s %s)\n",exchange->name,req->base,req->rel); if ( req->dead == 0 ) { if ( req->base[0] != 0 ) { req->timestamp = exchange->lastpoll = (uint32_t)time(NULL); req->exchange = exchange; - req->hbla = (*exchange->issue.price)(exchange,req->base,req->rel,req->bidasks,req->depth,req->commission,req->argjson); + req->hbla = (*exchange->issue.price)(exchange,req->base,req->rel,req->bidasks,req->depth,req->commission,req->argjson,req->invert); for (i=req->numbids=0; idepth; i++) if ( req->bidasks[i << 1].price > SMALLVAL ) req->numbids++; for (i=req->numasks=0; idepth; i++) if ( req->bidasks[(i << 1) + 1].price > SMALLVAL ) req->numasks++; + //printf("%-10s %s/%s numbids.%d numasks.%d\n",exchange->name,req->base,req->rel,req->numbids,req->numasks); prices777_processprice(exchange,req->base,req->rel,req->bidasks,req->depth); } queue_enqueue("pricesQ",&exchange->pricesQ,&req->DL,0); @@ -591,7 +629,7 @@ struct exchange_request *exchanges777_baserelfind(struct exchange_info *exchange queue_enqueue("pricesQ",&exchange->pricesQ,&PAD.DL,0); while ( (req= queue_dequeue(&exchange->pricesQ,0)) != 0 && req != &PAD ) { - if ( ((strcmp(base,req->base) == 0 && strcmp(rel,req->rel) == 0) || (strcmp(rel,req->base) == 0 && strcmp(base,req->rel) == 0)) && (func < 0 || req->func == func) ) + if ( ((req->invert == 0 && strcmp(base,req->base) == 0 && strcmp(rel,req->rel) == 0) || (req->invert != 0 && strcmp(rel,req->base) == 0 && strcmp(base,req->rel) == 0)) && (func < 0 || req->func == func) ) retreq = req; queue_enqueue("pricesQ",&exchange->pricesQ,&req->DL,0); } @@ -614,18 +652,19 @@ char *exchanges777_unmonitor(struct exchange_info *exchange,char *base,char *rel char *exchanges777_submit(struct exchange_info *exchange,struct exchange_request *req,int32_t func,int32_t maxseconds) { - int32_t i; + int32_t i; char **retstrp,*retstr; req->func = func; if ( maxseconds == 0 ) maxseconds = EXCHANGES777_DEFAULT_TIMEOUT; - printf("retstrp.%p\n",req->retstrp); + retstrp = req->retstrp; queue_enqueue("exchangeQ",&exchange->requestQ,&req->DL,0); for (i=0; iretstrp != 0 && *req->retstrp != 0 ) + if ( retstrp != 0 && (retstr= *retstrp) != 0 ) { - printf("GOT.(%s)\n",*req->retstrp); - return(*req->retstrp); + //printf("GOT.(%s)\n",retstr); + free(retstrp); + return(retstr); } sleep(1); } @@ -635,17 +674,15 @@ char *exchanges777_submit(struct exchange_info *exchange,struct exchange_request char *exchanges777_Qtrade(struct exchange_info *exchange,char *base,char *rel,int32_t maxseconds,int32_t dotrade,int32_t dir,double price,double volume,cJSON *argjson) { - struct exchange_request *req; int32_t polarity; char *retstr = 0; + struct exchange_request *req; int32_t polarity; if ( exchange->issue.supports == 0 ) return(clonestr("{\"error\":\"no supports function\"}")); - if ( base[0] == 0 || rel[0] == 0 || (polarity= (*exchange->issue.supports)(exchange,base,rel,argjson)) == 0 || price < SMALLVAL || volume < SMALLVAL ) + if ( base[0] == 0 || rel[0] == 0 || (polarity= (*exchange->issue.supports)(exchange,base,rel,argjson)) <= 0 || price < SMALLVAL || volume < SMALLVAL ) return(clonestr("{\"error\":\"invalid base or rel\"}")); req = calloc(1,sizeof(*req)); - req->argjson = argjson; req->retstrp = &retstr; + req->argjson = argjson; req->retstrp = calloc(1,sizeof(void *)); safecopy(req->base,base,sizeof(req->base)); safecopy(req->rel,rel,sizeof(req->rel)); - if ( polarity < 0 ) - dir *= -1, volume *= price, price = 1. / price; req->price = price, req->volume = volume, req->dir = dir; req->dotrade = dotrade; return(exchanges777_submit(exchange,req,'T',maxseconds)); @@ -653,7 +690,7 @@ char *exchanges777_Qtrade(struct exchange_info *exchange,char *base,char *rel,in char *exchanges777_Qprices(struct exchange_info *exchange,char *base,char *rel,int32_t maxseconds,int32_t allfields,int32_t depth,cJSON *argjson,int32_t monitor,double commission) { - struct exchange_request *req; char *retstr = 0; int32_t polarity; + struct exchange_request *req; int32_t polarity; if ( exchange->issue.supports == 0 ) return(clonestr("{\"error\":\"no supports function\"}")); if ( base[0] == 0 || rel[0] == 0 || (polarity= (*exchange->issue.supports)(exchange,base,rel,argjson)) == 0 ) @@ -661,11 +698,19 @@ char *exchanges777_Qprices(struct exchange_info *exchange,char *base,char *rel,i if ( depth <= 0 ) depth = 1; req = calloc(1,sizeof(*req) + sizeof(*req->bidasks)*depth*2); - req->argjson = argjson; req->retstrp = &retstr; - safecopy(req->base,base,sizeof(req->base)); - safecopy(req->rel,rel,sizeof(req->rel)); + req->argjson = argjson; req->retstrp = calloc(1,sizeof(void *)); + if ( polarity < 0 ) + { + req->invert = 1; + safecopy(req->base,rel,sizeof(req->base)); + safecopy(req->rel,base,sizeof(req->rel)); + } + else + { + safecopy(req->base,base,sizeof(req->base)); + safecopy(req->rel,rel,sizeof(req->rel)); + } req->depth = depth, req->allflag = allfields; - req->invert = (polarity < 0); if ( (req->commission= commission) == 0. ) req->commission = exchange->commission; if ( monitor == 0 ) @@ -673,7 +718,7 @@ char *exchanges777_Qprices(struct exchange_info *exchange,char *base,char *rel,i else { req->func = 'M'; - printf("Monitor.%s (%s %s)\n",exchange->name,base,rel); + printf("Monitor.%s (%s %s) invert.%d\n",exchange->name,base,rel,req->invert); queue_enqueue("pricesQ",&exchange->pricesQ,&req->DL,0); return(clonestr("{\"result\":\"start monitoring\"}")); } @@ -681,13 +726,13 @@ char *exchanges777_Qprices(struct exchange_info *exchange,char *base,char *rel,i char *exchanges777_Qrequest(struct exchange_info *exchange,int32_t func,char *base,char *rel,int32_t maxseconds,uint64_t orderid,char *destaddr,double amount,cJSON *argjson) { - struct exchange_request *req; char *retstr = 0; + struct exchange_request *req; req = calloc(1,sizeof(*req)); req->volume = amount; safecopy(req->destaddr,destaddr,sizeof(req->destaddr)); safecopy(req->base,base,sizeof(req->base)); safecopy(req->rel,rel,sizeof(req->rel)); - req->retstrp = &retstr; + req->retstrp = calloc(1,sizeof(void *)); req->orderid = orderid; return(exchanges777_submit(exchange,req,func,maxseconds)); } @@ -730,9 +775,7 @@ struct exchange_info *exchange_create(char *exchangestr,cJSON *argjson) for (i=0; itradingexchanges[myinfo->numexchanges++] = exchange; } } - if ( 0 ) + if ( 1 ) { argjson = cJSON_CreateObject(); for (i=0; iprice,quote->volume); if ( quote->price > SMALLVAL ) { - quote->val = ind; - sortbuf[n*2] = quote->price * SATOSHIDEN; + sortbuf[n] = *quote; + sortbuf[n].val = ind; //printf("sortbuf[%d] <-\n",n*2); - memcpy(&sortbuf[n*2 + 1],"e,sizeof(quote)); - n++; + if ( ++n >= max ) + break; } } return(n); } -double instantdex_aveprice(struct supernet_info *myinfo,char *base,char *rel,double volume,cJSON *argjson) +double instantdex_aveprice(struct supernet_info *myinfo,struct exchange_quote *sortbuf,int32_t max,double *totalvolp,char *base,char *rel,double relvolume,cJSON *argjson) { char *str; double totalvol,pricesum; uint32_t timestamp; - uint64_t sortbuf[64*32][2]; int32_t i,n,num,depth = 30; struct exchange_quote *quote; + struct exchange_quote quote; int32_t i,n,dir,num,depth = 100; struct exchange_info *exchange; struct exchange_request *req,*active[64]; timestamp = (uint32_t)time(NULL); - //InstantDEX_supports(myinfo,0,argjson,0,"poloniex",base,rel); - if ( base != 0 && rel != 0 && volume > SMALLVAL ) + if ( relvolume < 0. ) + relvolume = -relvolume, dir = -1; + else dir = 1; + memset(sortbuf,0,sizeof(*sortbuf) * max); + if ( base != 0 && rel != 0 && relvolume > SMALLVAL ) { for (i=num=0; inumexchanges && num < sizeof(active)/sizeof(*active); i++) { @@ -123,43 +127,46 @@ double instantdex_aveprice(struct supernet_info *myinfo,char *base,char *rel,dou } else { + //printf("active.%s\n",exchange->name); active[num++] = req; } } } for (i=n=0; itimestamp > timestamp-30 ) - { - if ( volume < 0. && active[i]->numbids > 0 ) - n = instantdex_updatesources(&sortbuf[0][0],n,i,1,active[i]->bidasks,active[i]->numbids); - else if ( volume > 0. && active[i]->numasks > 0 ) - n = instantdex_updatesources(&sortbuf[0][0],n,i,-1,&active[i]->bidasks[1],active[i]->numasks); - } + if ( dir < 0 && active[i]->numbids > 0 ) + n = instantdex_updatesources(sortbuf,n,max,i,1,active[i]->bidasks,active[i]->numbids); + else if ( dir > 0 && active[i]->numasks > 0 ) + n = instantdex_updatesources(sortbuf,n,max,i,-1,&active[i]->bidasks[1],active[i]->numasks); } - if ( volume < 0. ) - revsort64s(&sortbuf[0][0],n,sizeof(*sortbuf) * 2); - else sort64s(&sortbuf[0][0],n,sizeof(*sortbuf) * 2); - for (totalvol=pricesum=i=0; iprice * quote->volume); - totalvol += quote->volume; - printf("i.%d %12.8f vol %.8f %s | aveprice %.8f total vol %.8f\n",i,dstr(sortbuf[i][0]),quote->volume,active[quote->val]->exchange->name,pricesum/totalvol,totalvol); + pricesum += (quote.price * quote.volume); + totalvol += quote.volume; + //printf("i.%d of %d %12.8f vol %.8f %s | aveprice %.8f total vol %.8f\n",i,n,sortbuf[i].price,quote.volume,active[quote.val]->exchange->name,pricesum/totalvol,totalvol); } } if ( totalvol > 0. ) + { + *totalvolp = pricesum; return(pricesum / totalvol); + } } + *totalvolp = 0; return(0); } char *instantdex_request(struct supernet_info *myinfo,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) { - char *base,*rel,*request; double volume,aveprice; int32_t num,depth; + char *base,*rel,*request; double volume,aveprice,totalvol; int32_t num,depth; struct exchange_quote sortbuf[1000]; if ( argjson != 0 ) { num = 0; @@ -168,7 +175,7 @@ char *instantdex_request(struct supernet_info *myinfo,struct instantdex_msghdr * base = jstr(argjson,"base"); rel = jstr(argjson,"rel"); volume = jdouble(argjson,"volume"); - aveprice = instantdex_aveprice(myinfo,base,rel,volume,argjson); + aveprice = instantdex_aveprice(myinfo,sortbuf,(int32_t)(sizeof(sortbuf)/sizeof(*sortbuf)),&totalvol,base,rel,volume,argjson); return(clonestr("{\"result\":\"request calculated aveprice\"}")); return(clonestr("{\"error\":\"request missing parameter\"}")); } else return(clonestr("{\"error\":\"request needs argjson\"}")); diff --git a/iguana/main.c b/iguana/main.c index 24e3d3f3f..3fa0590b3 100644 --- a/iguana/main.c +++ b/iguana/main.c @@ -295,10 +295,6 @@ void mainloop(struct supernet_info *myinfo) } if ( flag == 0 ) { - //double avebid,aveask; cJSON *argjson = cJSON_Parse("{}"); - //avebid = instantdex_aveprice(myinfo,"NXT","BTC",10000,argjson); - //aveask = instantdex_aveprice(myinfo,"NXT","BTC",-10000,argjson); - //printf("avebid %.8f aveask %.8f\n",avebid,aveask); sleep(10); } } diff --git a/iguana/pnacl/Release/iguana.nmf b/iguana/pnacl/Release/iguana.nmf new file mode 100644 index 000000000..9d37ed227 --- /dev/null +++ b/iguana/pnacl/Release/iguana.nmf @@ -0,0 +1,12 @@ +{ + "program": { + "portable": { + "pnacl-translate": { + "url": "iguana.pexe" + }, + "pnacl-debug": { + "url": "iguana_unstripped.bc" + } + } + } +} diff --git a/iguana/pnacl/Release/iguana.pexe b/iguana/pnacl/Release/iguana.pexe new file mode 100644 index 000000000..2de67d2cc Binary files /dev/null and b/iguana/pnacl/Release/iguana.pexe differ