Browse Source

Merge pull request #910 from jl777/jl777

mpnet, optimize curl requests
pass-iguana-arg
jl777 7 years ago
committed by GitHub
parent
commit
befc701375
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      crypto777/bitcoind_RPC.c
  2. 11
      iguana/exchanges/LP_commands.c
  3. 3
      iguana/exchanges/LP_include.h
  4. 254
      iguana/exchanges/LP_mpnet.c
  5. 19
      iguana/exchanges/LP_nativeDEX.c
  6. 54
      iguana/exchanges/LP_ordermatch.c
  7. 12
      iguana/exchanges/LP_rpc.c
  8. 13
      iguana/exchanges/LP_signatures.c
  9. 2
      iguana/exchanges/LP_socket.c
  10. 6
      iguana/exchanges/mm.c

2
crypto777/bitcoind_RPC.c

@ -289,7 +289,7 @@ try_again:
count++; count++;
elapsedsum += (OS_milliseconds() - starttime); elapsedsum += (OS_milliseconds() - starttime);
if ( (count % 100000) == 0) if ( (count % 100000) == 0)
printf("%d: ave %9.6f | elapsed %.3f millis | bitcoind_RPC.(%s) url.(%s)\n",count,elapsedsum/count,(OS_milliseconds() - starttime),command,url); printf("%d: %s ave %9.6f | elapsed %.3f millis | bitcoind_RPC.(%s) url.(%s)\n",count,debugstr,elapsedsum/count,(OS_milliseconds() - starttime),command,url);
if ( retstrp != 0 ) if ( retstrp != 0 )
{ {
*retstrp = retstr; *retstrp = retstr;

11
iguana/exchanges/LP_commands.c

@ -173,6 +173,7 @@ unlockedspend(coin, txid)\n\
opreturndecrypt(coin, txid, passphrase)\n\ opreturndecrypt(coin, txid, passphrase)\n\
getendpoint(port=5555)\n\ getendpoint(port=5555)\n\
getfee(coin)\n\ getfee(coin)\n\
mpnet(onoff)\n\
sleep(seconds=60)\n\ sleep(seconds=60)\n\
listtransactions(coin, address, count=10, skip=0)\n\ listtransactions(coin, address, count=10, skip=0)\n\
jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
@ -238,6 +239,12 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
} }
return(clonestr("{\"error\":\"cant find KMD\"}")); return(clonestr("{\"error\":\"cant find KMD\"}"));
} }
else if ( strcmp(method,"mpnet") == 0 )
{
G.mpnet = jint(argjson,"onoff");
printf("MPNET onoff.%d\n",G.mpnet);
return(clonestr("{\"status\":\"success\"}"));
}
else if ( strcmp(method,"getendpoint") == 0 ) else if ( strcmp(method,"getendpoint") == 0 )
{ {
int32_t err,mode; uint16_t wsport = 5555; char endpoint[64],bindpoint[64]; int32_t err,mode; uint16_t wsport = 5555; char endpoint[64],bindpoint[64];
@ -507,6 +514,8 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
jaddnum(retjson,"price",price); jaddnum(retjson,"price",price);
return(jprint(retjson,1)); return(jprint(retjson,1));
} }
else if ( strcmp(method,"orderbook") == 0 )
return(LP_orderbook(base,rel,jint(argjson,"duration")));
if ( IAMLP == 0 && LP_isdisabled(base,rel) != 0 ) if ( IAMLP == 0 && LP_isdisabled(base,rel) != 0 )
return(clonestr("{\"error\":\"at least one of coins disabled\"}")); return(clonestr("{\"error\":\"at least one of coins disabled\"}"));
price = jdouble(argjson,"price"); price = jdouble(argjson,"price");
@ -520,8 +529,6 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
return(LP_pricepings(ctx,myipaddr,LP_mypubsock,base,rel,price * LP_profitratio)); return(LP_pricepings(ctx,myipaddr,LP_mypubsock,base,rel,price * LP_profitratio));
else return(clonestr("{\"result\":\"success\"}")); else return(clonestr("{\"result\":\"success\"}"));
} }
else if ( strcmp(method,"orderbook") == 0 )
return(LP_orderbook(base,rel,jint(argjson,"duration")));
else if ( strcmp(method,"myprice") == 0 ) else if ( strcmp(method,"myprice") == 0 )
{ {
if ( LP_myprice(1,&bid,&ask,base,rel) > SMALLVAL ) if ( LP_myprice(1,&bid,&ask,base,rel) > SMALLVAL )

3
iguana/exchanges/LP_include.h

@ -59,7 +59,7 @@ void emscripten_usleep(int32_t x); // returns immediate, no sense for sleeping
//#define LP_DISABLE_DISTCOMBINE //#define LP_DISABLE_DISTCOMBINE
#define LP_MAXVINS 64 #define LP_MAXVINS 64
#define LP_HTTP_TIMEOUT 3 // 1 is too small due to edge cases of time(NULL) #define LP_HTTP_TIMEOUT 10 // 1 is too small due to edge cases of time(NULL)
#define LP_AUTOTRADE_TIMEOUT 30 #define LP_AUTOTRADE_TIMEOUT 30
#define LP_RESERVETIME (LP_AUTOTRADE_TIMEOUT * 3) #define LP_RESERVETIME (LP_AUTOTRADE_TIMEOUT * 3)
#define ELECTRUM_TIMEOUT 13 #define ELECTRUM_TIMEOUT 13
@ -378,6 +378,7 @@ struct LP_quoteinfo
{ {
struct basilisk_request R; struct basilisk_request R;
bits256 srchash,desthash,txid,txid2,desttxid,feetxid,privkey; bits256 srchash,desthash,txid,txid2,desttxid,feetxid,privkey;
double maxprice;
int64_t othercredits; int64_t othercredits;
uint64_t satoshis,txfee,destsatoshis,desttxfee,aliceid; uint64_t satoshis,txfee,destsatoshis,desttxfee,aliceid;
uint32_t timestamp,quotetime,tradeid,gtc,fill,mpnet; uint32_t timestamp,quotetime,tradeid,gtc,fill,mpnet;

254
iguana/exchanges/LP_mpnet.c

@ -0,0 +1,254 @@
/******************************************************************************
* Copyright © 2014-2018 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. *
* *
******************************************************************************/
//
// LP_mpnet.c
// marketmaker
//
bits256 MPNET_txids[1024];
int32_t num_MPNET_txids;
int32_t LP_tradecommand(int32_t from_mpnet,void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen);
int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson);
void LP_gtc_addorder(struct LP_quoteinfo *qp);
char *LP_withdraw(struct iguana_info *coin,cJSON *argjson);
int32_t LP_mpnet_find(bits256 txid)
{
int32_t i;
for (i=0; i<num_MPNET_txids; i++)
if ( bits256_cmp(txid,MPNET_txids[i]) == 0 )
return(i);
return(-1);
}
int32_t LP_mpnet_add(bits256 txid)
{
if ( num_MPNET_txids < sizeof(MPNET_txids)/sizeof(*MPNET_txids) )
{
MPNET_txids[num_MPNET_txids++] = txid;
return(num_MPNET_txids);
}
printf("MPNET_txids[] overflow\n");
return(-1);
}
int32_t LP_mpnet_remove(bits256 txid)
{
int32_t i;
if ( (i= LP_mpnet_find(txid)) >= 0 )
{
MPNET_txids[i] = MPNET_txids[--num_MPNET_txids];
return(i);
}
return(-1);
}
int32_t LP_mpnet_addorder(struct LP_quoteinfo *qp)
{
uint64_t destvalue,destvalue2;
if ( LP_iseligible(&destvalue,&destvalue2,0,qp->destcoin,qp->desttxid,qp->destvout,qp->destsatoshis,qp->feetxid,qp->feevout) > 0 )
{
LP_gtc_addorder(qp);
return(0);
}
return(-1);
}
void LP_mpnet_init() // problem is coins not enabled yet
{
char fname[1024],line[8192]; FILE *fp; struct LP_quoteinfo Q; cJSON *argjson;
sprintf(fname,"%s/GTC/orders",GLOBAL_DBDIR), OS_compatible_path(fname);
if ( (fp= fopen(fname,"rb+")) != 0 )
{
while ( fgets(line,sizeof(line),fp) > 0 )
{
if ( (argjson= cJSON_Parse(line)) != 0 )
{
if ( LP_quoteparse(&Q,argjson) == 0 )
{
if ( LP_mpnet_addorder(&Q) == 0 )
printf("GTC %s",line);
}
free_json(argjson);
}
}
fclose(fp);
}
}
void LP_mpnet_send(int32_t localcopy,char *msg,int32_t sendflag,char *otheraddr)
{
char fname[1024]; int32_t len; FILE *fp; char *hexstr,*retstr; cJSON *argjson,*outputs,*item; struct iguana_info *coin; uint8_t linebuf[8192];
if ( localcopy != 0 )
{
sprintf(fname,"%s/GTC/orders",GLOBAL_DBDIR), OS_compatible_path(fname);
if ( (fp= fopen(fname,"rb+")) == 0 )
fp = fopen(fname,"wb+");
else fseek(fp,0,SEEK_END);
fprintf(fp,"%s\n",msg);
fclose(fp);
}
if ( G.mpnet != 0 && sendflag != 0 && (coin= LP_coinfind("CHIPS")) != 0 && coin->inactive == 0 )
{
len = MMJSON_encode(linebuf,msg);
//curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"withdraw\",\"coin\":\"CHIPS\",\"outputs\":[{\"RHV2As4rox97BuE3LK96vMeNY8VsGRTmBj\":0.0001}],\"opreturn\":\"deadbeef\"}"
if ( len > 0 )
{
argjson = cJSON_CreateObject();
outputs = cJSON_CreateArray();
if ( otheraddr != 0 && otheraddr[0] != 0 )
{
item = cJSON_CreateObject();
jaddnum(item,otheraddr,dstr(10000));
jaddi(outputs,item);
}
item = cJSON_CreateObject();
jaddnum(item,coin->smartaddr,dstr(10000));
jaddi(outputs,item);
jadd(argjson,"outputs",outputs);
jaddnum(argjson,"broadcast",1);
jaddstr(argjson,"coin",coin->symbol);
hexstr = calloc(1,len*2 + 1);
init_hexbytes_noT(hexstr,linebuf,len);
jaddstr(argjson,"opreturn",hexstr);
free(hexstr);
retstr = LP_withdraw(coin,argjson);
free_json(argjson);
if ( retstr != 0 )
{
//printf("mpnet.%s\n",retstr);
free(retstr);
}
}
}
}
cJSON *LP_mpnet_parse(struct iguana_info *coin,bits256 txid)
{
cJSON *txobj,*vouts,*sobj,*argjson = 0; char *decodestr,*hexstr; uint8_t *buf,linebuf[8192]; int32_t len,n,hlen;
if ( (txobj= LP_gettx("mpnet",coin->symbol,txid,0)) != 0 )
{
if ( (vouts= jarray(&n,txobj,"vout")) != 0 )
{
if ( (sobj= jobj(jitem(vouts,n-1),"scriptPubKey")) != 0 && (hexstr= jstr(sobj,"hex")) != 0 && (hlen= strlen(hexstr)) < sizeof(linebuf)*2 )
{
len = (hlen >> 1);
decode_hex(linebuf,len,hexstr);
buf = linebuf;
//printf("hexstr.(%s)\n",hexstr);
if ( *buf == 0x6a )
{
buf++, len--;
if ( *buf == 0x4d )
{
buf++, len--;
n = buf[0] + buf[1]*256;
buf += 2, len -= 2;
if ( n == len )
{
if ( (decodestr= MMJSON_decode(buf,len)) != 0 )
argjson = cJSON_Parse(decodestr);
}
}
}
if ( 0 && argjson == 0 )
printf("unhandled case.(%s)\n",hexstr);
}
}
if ( 0 && argjson == 0 )
printf("unhandled tx.(%s)\n",jprint(txobj,0));
free_json(txobj);
}
return(argjson);
}
// 2151978
// 404bc4ac452db07ed16376b3d7e77dbfc22b4a68f7243797125bd0d3bdddf8d1
// 893b46634456034a6d5d73b67026aa157b5e2addbfc6344dfbea6bae85f7dde0
// 717c7ef9de8504bd331f3ef52ed0a16ea0e070434e12cb4d63f5f081e999c43d dup
void LP_mpnet_process(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info *coin,bits256 txid)
{
cJSON *argjson; char str[65];
if ( LP_mpnet_find(txid) < 0 )
{
//printf("unique %s\n",bits256_str(str,txid));
if ( (argjson= LP_mpnet_parse(coin,txid)) != 0 )
{
//printf("MPNET.(%s)\n",jprint(argjson,0));
LP_tradecommand(1,ctx,myipaddr,pubsock,argjson,0,0);
free_json(argjson);
}
LP_mpnet_add(txid);
}
}
cJSON *LP_mpnet_get(void *ctx,char *myipaddr,int32_t pubsock,struct iguana_info *coin)
{
static int32_t lastheight; static bits256 lasthash;
int32_t i,n=0,numtx,checkht = 0,height = 0; bits256 latesthash,hash,txid,zero; char hashstr[65],str[65]; cJSON *txs,*blockjson;
memset(zero.bytes,0,sizeof(zero));
latesthash = LP_getbestblockhash(coin);
bits256_str(hashstr,latesthash);
if ( (blockjson= LP_blockjson(&checkht,coin->symbol,hashstr,0)) != 0 )
{
hash = latesthash;
while ( bits256_cmp(lasthash,hash) != 0 && n++ < 3 )
{
if ( (txs= jarray(&numtx,blockjson,"tx")) != 0 )
{
for (i=0; i<numtx; i++)
{
txid = jbits256i(txs,i);
LP_mpnet_process(ctx,myipaddr,pubsock,coin,txid);
LP_mpnet_remove(txid);
//printf("ht.%d n.%d i.%d %s\n",checkht,n,i,bits256_str(str,txid));
}
}
hash = jbits256(blockjson,"previousblockhash");
free_json(blockjson);
bits256_str(hashstr,hash);
if ( (blockjson= LP_blockjson(&checkht,coin->symbol,hashstr,0)) == 0 )
break;
}
lasthash = latesthash;
if ( blockjson != 0 )
free_json(blockjson);
if ( (txs= LP_getmempool(coin->symbol,coin->smartaddr,zero,zero)) != 0 )
{
numtx = cJSON_GetArraySize(txs);
for (i=0; i<numtx; i++)
{
txid = jbits256i(txs,i);
LP_mpnet_process(ctx,myipaddr,pubsock,coin,txid);
//printf("mp i.%d %s\n",i,bits256_str(str,txid));
}
}
}
return(0);
}
void LP_mpnet_check(void *ctx,char *myipaddr,int32_t pubsock)
{
static uint32_t lasttime;
struct iguana_info *coin = LP_coinfind("CHIPS");
if ( coin != 0 && coin->inactive == 0 && time(NULL) > lasttime+5 )
{
LP_mpnet_get(ctx,myipaddr,pubsock,coin);
lasttime = (uint32_t)time(NULL);
}
}

19
iguana/exchanges/LP_nativeDEX.c

@ -120,7 +120,7 @@ struct LP_globals
uint64_t LP_skipstatus[10000]; uint64_t LP_skipstatus[10000];
uint16_t netid; uint16_t netid;
uint8_t LP_myrmd160[20],LP_pubsecp[33]; uint8_t LP_myrmd160[20],LP_pubsecp[33];
uint32_t LP_sessionid,counter; uint32_t LP_sessionid,counter,mpnet;
int32_t LP_IAMLP,LP_pendingswaps,USERPASS_COUNTER,LP_numprivkeys,initializing,waiting,LP_numskips; int32_t LP_IAMLP,LP_pendingswaps,USERPASS_COUNTER,LP_numprivkeys,initializing,waiting,LP_numskips;
char seednode[64],USERPASS[65],USERPASS_WIFSTR[64],LP_myrmd160str[41],gui[65],LP_NXTaddr[64]; char seednode[64],USERPASS[65],USERPASS_WIFSTR[64],LP_myrmd160str[41],gui[65],LP_NXTaddr[64];
struct LP_privkey LP_privkeys[100]; struct LP_privkey LP_privkeys[100];
@ -176,6 +176,7 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_
#include "LP_bitcoin.c" #include "LP_bitcoin.c"
#include "LP_coins.c" #include "LP_coins.c"
#include "LP_rpc.c" #include "LP_rpc.c"
#include "LP_mpnet.c"
#include "LP_NXT.c" #include "LP_NXT.c"
#include "LP_cache.c" #include "LP_cache.c"
#include "LP_RTmetrics.c" #include "LP_RTmetrics.c"
@ -202,7 +203,7 @@ char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson
char *retstr=0; cJSON *retjson; bits256 zero; char *retstr=0; cJSON *retjson; bits256 zero;
if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 ) if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 0 )
return(0); return(0);
if ( stats_JSONonly != 0 || LP_tradecommand(ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 ) if ( stats_JSONonly != 0 || LP_tradecommand(0,ctx,myipaddr,pubsock,argjson,data,datalen) <= 0 )
{ {
if ( (retstr= stats_JSON(ctx,0,myipaddr,pubsock,argjson,"127.0.0.1",stats_JSONonly)) != 0 ) if ( (retstr= stats_JSON(ctx,0,myipaddr,pubsock,argjson,"127.0.0.1",stats_JSONonly)) != 0 )
{ {
@ -423,7 +424,7 @@ int32_t LP_sock_check(char *typestr,void *ctx,char *myipaddr,int32_t pubsock,int
/*if ( (argjson= cJSON_Parse(str)) != 0 ) /*if ( (argjson= cJSON_Parse(str)) != 0 )
{ {
//portable_mutex_lock(&LP_commandmutex); //portable_mutex_lock(&LP_commandmutex);
if ( LP_tradecommand(ctx,myipaddr,pubsock,argjson,0,0) <= 0 ) if ( LP_tradecommand(0,ctx,myipaddr,pubsock,argjson,0,0) <= 0 )
{ {
if ( (retstr= stats_JSON(ctx,0,myipaddr,pubsock,argjson,remoteaddr,0)) != 0 ) if ( (retstr= stats_JSON(ctx,0,myipaddr,pubsock,argjson,remoteaddr,0)) != 0 )
free(retstr); free(retstr);
@ -480,6 +481,8 @@ int32_t LP_nanomsg_recvs(void *ctx)
nonz += LP_sock_check("PULL",ctx,origipaddr,-1,LP_mypullsock,"127.0.0.1",1); nonz += LP_sock_check("PULL",ctx,origipaddr,-1,LP_mypullsock,"127.0.0.1",1);
} }
portable_mutex_unlock(&LP_nanorecvsmutex); portable_mutex_unlock(&LP_nanorecvsmutex);
//if ( G.mpnet != 0 )
LP_mpnet_check(ctx,origipaddr,LP_mypubsock);
return(nonz); return(nonz);
} }
@ -517,17 +520,17 @@ void LP_coinsloop(void *_coins)
if ( strcmp("BTC",coins) == 0 ) if ( strcmp("BTC",coins) == 0 )
{ {
strcpy(LP_coinsloopBTC_stats.name,"BTC coin loop"); strcpy(LP_coinsloopBTC_stats.name,"BTC coin loop");
LP_coinsloopBTC_stats.threshold = 20000.; LP_coinsloopBTC_stats.threshold = 200000.;
} }
else if ( strcmp("KMD",coins) == 0 ) else if ( strcmp("KMD",coins) == 0 )
{ {
strcpy(LP_coinsloopKMD_stats.name,"KMD coin loop"); strcpy(LP_coinsloopKMD_stats.name,"KMD coin loop");
LP_coinsloopKMD_stats.threshold = 10000.; LP_coinsloopKMD_stats.threshold = 100000.;
} }
else else
{ {
strcpy(LP_coinsloop_stats.name,"other coins loop"); strcpy(LP_coinsloop_stats.name,"other coins loop");
LP_coinsloop_stats.threshold = 5000.; LP_coinsloop_stats.threshold = 50000.;
} }
while ( LP_STOP_RECEIVED == 0 ) while ( LP_STOP_RECEIVED == 0 )
{ {
@ -576,7 +579,7 @@ void LP_coinsloop(void *_coins)
LP_address_utxo_reset(&num,coin); LP_address_utxo_reset(&num,coin);
coin->did_addrutxo_reset = 1; coin->did_addrutxo_reset = 1;
} }
free_json(LP_address_balance(coin,coin->smartaddr,1)); //free_json(LP_address_balance(coin,coin->smartaddr,1)); expensive invoking gettxout
if ( coin->do_autofill_merge != 0 ) if ( coin->do_autofill_merge != 0 )
{ {
if ( (retstr= LP_autofillbob(coin,coin->do_autofill_merge*1.02)) != 0 ) if ( (retstr= LP_autofillbob(coin,coin->do_autofill_merge*1.02)) != 0 )
@ -679,6 +682,7 @@ void LP_coinsloop(void *_coins)
continue; continue;
} }
//if ( strcmp(coin->symbol,"BTC") != 0 && strcmp(coin->symbol,"KMD") != 0 ) // SPV as backup //if ( strcmp(coin->symbol,"BTC") != 0 && strcmp(coin->symbol,"KMD") != 0 ) // SPV as backup
if ( coin->lastscanht < coin->longestchain )
{ {
nonz++; nonz++;
if ( strcmp("BTC",coins) == 0 )//&& coin->lastscanht < coin->longestchain-3 ) if ( strcmp("BTC",coins) == 0 )//&& coin->lastscanht < coin->longestchain-3 )
@ -1646,6 +1650,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
int32_t nonz,didremote=0; int32_t nonz,didremote=0;
LP_statslog_parse(); LP_statslog_parse();
bitcoind_RPC_inittime = 0; bitcoind_RPC_inittime = 0;
//LP_mpnet_init(); seems better to have the GUI send in persistent orders, exit mm is a cancel all
while ( LP_STOP_RECEIVED == 0 ) while ( LP_STOP_RECEIVED == 0 )
{ {
nonz = 0; nonz = 0;

54
iguana/exchanges/LP_ordermatch.c

@ -23,7 +23,6 @@ struct LP_gtcorder
{ {
struct LP_gtcorder *next,*prev; struct LP_gtcorder *next,*prev;
struct LP_quoteinfo Q; struct LP_quoteinfo Q;
double maxprice;
uint32_t cancelled,pending; uint32_t cancelled,pending;
} *GTCorders; } *GTCorders;
@ -548,9 +547,11 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
if ( IPC_ENDPOINT >= 0 ) if ( IPC_ENDPOINT >= 0 )
LP_queuecommand(0,jprint(reqjson,0),IPC_ENDPOINT,-1,0); LP_queuecommand(0,jprint(reqjson,0),IPC_ENDPOINT,-1,0);
} }
if ( qp->mpnet != 0 && qp->fill != 0 && qp->gtc != 0 ) if ( qp->mpnet != 0 && qp->gtc == 0 )
{ {
// send to mpnet char *msg = jprint(reqjson,0);
LP_mpnet_send(0,msg,1,qp->destaddr);
free(msg);
} }
free_json(reqjson); free_json(reqjson);
retval = 0; retval = 0;
@ -614,14 +615,25 @@ void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock)
{ {
gtc->pending = qp->timestamp = (uint32_t)time(NULL); gtc->pending = qp->timestamp = (uint32_t)time(NULL);
LP_query(ctx,myipaddr,mypubsock,"request",qp); LP_query(ctx,myipaddr,mypubsock,"request",qp);
LP_Alicequery = *qp, LP_Alicemaxprice = gtc->maxprice, Alice_expiration = qp->timestamp + 2*LP_AUTOTRADE_TIMEOUT, LP_Alicedestpubkey = qp->srchash; LP_Alicequery = *qp, LP_Alicemaxprice = gtc->Q.maxprice, Alice_expiration = qp->timestamp + 2*LP_AUTOTRADE_TIMEOUT, LP_Alicedestpubkey = qp->srchash;
char str[65]; printf("LP_gtc fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),gtc->maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); char str[65]; printf("LP_gtc fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),gtc->Q.maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc);
break; break;
} }
} }
} }
} }
void LP_gtc_addorder(struct LP_quoteinfo *qp)
{
struct LP_gtcorder *gtc;
gtc = calloc(1,sizeof(*gtc));
gtc->Q = *qp;
gtc->pending = (uint32_t)time(NULL);
portable_mutex_lock(&LP_gtcmutex);
DL_APPEND(GTCorders,gtc);
portable_mutex_unlock(&LP_gtcmutex);
}
char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *qp,double maxprice,int32_t timeout,int32_t duration,uint32_t tradeid,bits256 destpubkey,char *uuidstr) char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *qp,double maxprice,int32_t timeout,int32_t duration,uint32_t tradeid,bits256 destpubkey,char *uuidstr)
{ {
struct LP_gtcorder *gtc; struct LP_gtcorder *gtc;
@ -632,27 +644,25 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q
qp->tradeid = LP_rand(); qp->tradeid = LP_rand();
qp->srchash = destpubkey; qp->srchash = destpubkey;
strncpy(qp->uuidstr,uuidstr,sizeof(qp->uuidstr)-1); strncpy(qp->uuidstr,uuidstr,sizeof(qp->uuidstr)-1);
qp->maxprice = maxprice;
qp->timestamp = (uint32_t)time(NULL); qp->timestamp = (uint32_t)time(NULL);
if ( qp->gtc != 0 ) if ( qp->gtc != 0 )
{ {
strcpy(&qp->uuidstr[strlen(qp->uuidstr)-6],"cccccc"); strcpy(&qp->uuidstr[strlen(qp->uuidstr)-6],"cccccc");
gtc = calloc(1,sizeof(*gtc)); LP_gtc_addorder(qp);
gtc->Q = *qp;
gtc->maxprice = maxprice;
gtc->pending = (uint32_t)time(NULL);
portable_mutex_lock(&LP_gtcmutex);
DL_APPEND(GTCorders,gtc);
portable_mutex_unlock(&LP_gtcmutex);
} }
{ {
LP_query(ctx,myipaddr,mypubsock,"request",qp); LP_query(ctx,myipaddr,mypubsock,"request",qp);
LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash; LP_Alicequery = *qp, LP_Alicemaxprice = qp->maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash;
} }
if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) if ( qp->gtc == 0 )
{ {
// send to mpnet cJSON *reqjson = LP_quotejson(qp);
char *msg = jprint(reqjson,1);
LP_mpnet_send(1,msg,1,0);
free(msg);
} }
char str[65]; printf("LP_trade mpnet.%d fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->mpnet,qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc); char str[65]; printf("LP_trade mpnet.%d fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d\n",qp->mpnet,qp->fill,qp->gtc,qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),qp->maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc);
return(LP_recent_swaps(0,uuidstr)); return(LP_recent_swaps(0,uuidstr));
} }
@ -1195,9 +1205,11 @@ printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str,
memset(zero.bytes,0,sizeof(zero)); memset(zero.bytes,0,sizeof(zero));
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0)); LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0));
} }
if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) if ( qp->mpnet != 0 && qp->gtc == 0 )
{ {
// send to mpnet char *msg = jprint(reqjson,0);
LP_mpnet_send(0,msg,1,qp->destaddr);
free(msg);
} }
free_json(reqjson); free_json(reqjson);
//printf("Send RESERVED id.%llu\n",(long long)qp->aliceid); //printf("Send RESERVED id.%llu\n",(long long)qp->aliceid);
@ -1515,7 +1527,7 @@ void LP_tradecommandQ(struct LP_quoteinfo *qp,char *pairstr,int32_t funcid)
//printf("queue.%d uuid.(%s)\n",funcid,qtp->Q.uuidstr); //printf("queue.%d uuid.(%s)\n",funcid,qtp->Q.uuidstr);
} }
int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen) int32_t LP_tradecommand(int32_t from_mpnet,void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen)
{ {
int32_t Qtrades = 1; int32_t Qtrades = 1;
char *method,str[65]; int32_t i,num,DEXselector = 0; uint64_t aliceid; double qprice,bestprice,price,bid,ask; cJSON *proof; uint64_t rq; struct iguana_info *coin; struct LP_quoteinfo Q,Q2; int32_t counter,retval=-1; char *method,str[65]; int32_t i,num,DEXselector = 0; uint64_t aliceid; double qprice,bestprice,price,bid,ask; cJSON *proof; uint64_t rq; struct iguana_info *coin; struct LP_quoteinfo Q,Q2; int32_t counter,retval=-1;
@ -1538,7 +1550,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
LP_tradecommand_log(argjson); LP_tradecommand_log(argjson);
qprice = (double)Q.destsatoshis / (Q.satoshis - Q.txfee); //jdouble(argjson,"price"); qprice = (double)Q.destsatoshis / (Q.satoshis - Q.txfee); //jdouble(argjson,"price");
//printf("%s\n",jprint(argjson,0)); //printf("%s\n",jprint(argjson,0));
printf("%-4d uuid.%32s g.%d f.%d %12s %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.uuidstr+32,Q.gtc,Q.fill,method,Q.srccoin,Q.destcoin,dstr(Q.satoshis),dstr(Q.destsatoshis),qprice,LP_RTcount,LP_swapscount,G.netid); printf("%-4d uuid.%32s M.%d g.%d f.%d %12s %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.uuidstr+32,from_mpnet,Q.gtc,Q.fill,method,Q.srccoin,Q.destcoin,dstr(Q.satoshis),dstr(Q.destsatoshis),qprice,LP_RTcount,LP_swapscount,G.netid);
retval = 1; retval = 1;
aliceid = j64bits(argjson,"aliceid"); aliceid = j64bits(argjson,"aliceid");
if ( strcmp(method,"reserved") == 0 ) if ( strcmp(method,"reserved") == 0 )
@ -1795,7 +1807,7 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha
} }
} }
int32_t changed; int32_t changed;
Q.mpnet = 0; Q.mpnet = G.mpnet;
Q.fill = fillflag; Q.fill = fillflag;
Q.gtc = gtcflag; Q.gtc = gtcflag;
LP_mypriceset(0,&changed,rel,base,1. / maxprice); LP_mypriceset(0,&changed,rel,base,1. / maxprice);

12
iguana/exchanges/LP_rpc.c

@ -248,6 +248,12 @@ cJSON *LP_gettxout(char *symbol,char *coinaddr,bits256 txid,int32_t vout)
return(cJSON_Parse("{\"error\":\"no coin\"}")); return(cJSON_Parse("{\"error\":\"no coin\"}"));
if ( bits256_nonz(txid) == 0 ) if ( bits256_nonz(txid) == 0 )
return(cJSON_Parse("{\"error\":\"null txid\"}")); return(cJSON_Parse("{\"error\":\"null txid\"}"));
if ( (tx= LP_transactionfind(coin,txid)) != 0 && vout < tx->numvouts )
{
if ( tx->outpoints[vout].spendheight > 0 )
return(0);
//return(LP_gettxout_json(txid,vout,tx->height,tx->outpoints[vout].coinaddr,tx->outpoints[vout].value));
}
if ( coin->electrum == 0 ) if ( coin->electrum == 0 )
{ {
sprintf(buf,"[\"%s\", %d, true]",bits256_str(str,txid),vout); sprintf(buf,"[\"%s\", %d, true]",bits256_str(str,txid),vout);
@ -255,12 +261,6 @@ cJSON *LP_gettxout(char *symbol,char *coinaddr,bits256 txid,int32_t vout)
} }
else else
{ {
if ( (tx= LP_transactionfind(coin,txid)) != 0 && vout < tx->numvouts )
{
if ( tx->outpoints[vout].spendheight > 0 )
return(0);
//return(LP_gettxout_json(txid,vout,tx->height,tx->outpoints[vout].coinaddr,tx->outpoints[vout].value));
}
if ( coinaddr[0] == 0 ) if ( coinaddr[0] == 0 )
{ {
if ( (txobj= electrum_transaction(&height,symbol,coin->electrum,&txobj,txid,0)) != 0 ) if ( (txobj= electrum_transaction(&height,symbol,coin->electrum,&txobj,txid,0)) != 0 )

13
iguana/exchanges/LP_signatures.c

@ -45,6 +45,8 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp)
if ( jobj(retjson,"gui") == 0 ) if ( jobj(retjson,"gui") == 0 )
jaddstr(retjson,"gui",qp->gui[0] != 0 ? qp->gui : LP_gui); jaddstr(retjson,"gui",qp->gui[0] != 0 ? qp->gui : LP_gui);
jaddstr(retjson,"uuid",qp->uuidstr); jaddstr(retjson,"uuid",qp->uuidstr);
if ( qp->maxprice != 0 )
jaddnum(retjson,"maxprice",qp->maxprice);
if ( qp->mpnet != 0 ) if ( qp->mpnet != 0 )
jaddnum(retjson,"mpnet",qp->mpnet); jaddnum(retjson,"mpnet",qp->mpnet);
if ( qp->gtc != 0 ) if ( qp->gtc != 0 )
@ -119,6 +121,7 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson)
{ {
uint32_t rid,qid; char etomic[64],activesymbol[65],*etomicstr; uint32_t rid,qid; char etomic[64],activesymbol[65],*etomicstr;
memset(qp,0,sizeof(*qp)); memset(qp,0,sizeof(*qp));
qp->maxprice = jdouble(argjson,"maxprice");
qp->mpnet = juint(argjson,"mpnet"); qp->mpnet = juint(argjson,"mpnet");
qp->gtc = juint(argjson,"gtc"); qp->gtc = juint(argjson,"gtc");
qp->fill = juint(argjson,"fill"); qp->fill = juint(argjson,"fill");
@ -161,9 +164,9 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson)
qp->destvout = jint(argjson,"destvout"); qp->destvout = jint(argjson,"destvout");
qp->desthash = jbits256(argjson,"desthash"); qp->desthash = jbits256(argjson,"desthash");
qp->txfee = j64bits(argjson,"txfee"); qp->txfee = j64bits(argjson,"txfee");
if ( (qp->satoshis= j64bits(argjson,"satoshis")) > qp->txfee ) if ( (qp->satoshis= j64bits(argjson,"satoshis")) > qp->txfee && fabs(qp->maxprice) < SMALLVAL )
{ {
//qp->price = (double)qp->destsatoshis / (qp->satoshis = qp->txfee); qp->maxprice = (double)qp->destsatoshis / (qp->satoshis - qp->txfee);
} }
qp->destsatoshis = j64bits(argjson,"destsatoshis"); qp->destsatoshis = j64bits(argjson,"destsatoshis");
qp->desttxfee = j64bits(argjson,"desttxfee"); qp->desttxfee = j64bits(argjson,"desttxfee");
@ -738,10 +741,8 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg)); LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg));
} }
} }
if ( qp->mpnet != 0 && qp->gtc != 0 && qp->fill != 0 ) if ( strcmp(method,"connect") == 0 && qp->mpnet != 0 && qp->gtc == 0 )
{ LP_mpnet_send(0,msg,1,qp->coinaddr);
// send to mpnet
}
free(msg); free(msg);
} }

2
iguana/exchanges/LP_socket.c

@ -73,7 +73,7 @@ int32_t komodo_connect(int32_t sock,struct sockaddr *saddr,socklen_t addrlen)
closesocket(sock); closesocket(sock);
return(-1); return(-1);
} }
printf("continue with select ...\n"); //printf("continue with select ...\n");
FD_ZERO(&wfd); FD_ZERO(&wfd);
FD_SET(sock,&wfd); FD_SET(sock,&wfd);
FD_ZERO(&efd); FD_ZERO(&efd);

6
iguana/exchanges/mm.c

@ -346,6 +346,12 @@ int main(int argc, const char * argv[])
printf("couldnt write to (%s)\n",dirname); printf("couldnt write to (%s)\n",dirname);
exit(0); exit(0);
} }
sprintf(dirname,"%s/GTC",GLOBAL_DBDIR), OS_ensure_directory(dirname);
if ( ensure_writable(dirname) < 0 )
{
printf("couldnt write to (%s)\n",dirname);
exit(0);
}
sprintf(dirname,"%s/PRICES",GLOBAL_DBDIR), OS_ensure_directory(dirname); sprintf(dirname,"%s/PRICES",GLOBAL_DBDIR), OS_ensure_directory(dirname);
if ( ensure_writable(dirname) < 0 ) if ( ensure_writable(dirname) < 0 )
{ {

Loading…
Cancel
Save