Browse Source

Merge pull request #458 from jl777/spvdex

Spvdex
etomic
jl777 7 years ago
committed by GitHub
parent
commit
3729cd3e9e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      iguana/exchanges/DEXstats.h
  2. 7
      iguana/exchanges/LP_nativeDEX.c
  3. 4
      iguana/exchanges/LP_network.c
  4. 4
      iguana/exchanges/LP_ordermatch.c
  5. 13
      iguana/exchanges/LP_socket.c
  6. 93
      iguana/exchanges/LP_tradebots.c
  7. 2
      iguana/exchanges/bot_settings
  8. 3
      iguana/exchanges/stats.c

2
iguana/exchanges/DEXstats.h

@ -927,6 +927,7 @@ char *stats_prices(char *symbol,char *dest,struct DEXstats_disp *prices,int32_t
}
#ifndef FROM_MARKETMAKER
#ifndef FROM_PRIVATEBET
char *stats_JSON(void *ctx,char *myipaddr,int32_t mypubsock,cJSON *argjson,char *remoteaddr,uint16_t port)
{
char *method,*agent,*retstr,*source,*dest; struct tai T; uint32_t endtimestamp; struct DEXstats_disp prices[365]; int32_t leftdatenum,seconds,numdates;
@ -953,5 +954,6 @@ char *stats_JSON(void *ctx,char *myipaddr,int32_t mypubsock,cJSON *argjson,char
return(clonestr(jprint(argjson,0)));
}
#endif
#endif
#endif /* DEXstats_h */

7
iguana/exchanges/LP_nativeDEX.c

@ -18,8 +18,11 @@
// LP_nativeDEX.c
// marketmaker
//
// bot status 1600% ?
// swap started, pending, locked, finished, ...
// aliceid
//there is still a pending one with `-1 wait for bobpayment bYoNxkfvwQ42Yufry8J5y8BYi6mQxokvW9 numconfs.1 MNZ c0ea4aa808a653222a15122d96692fecf734dbbacfb9a54cb4711306ea0c3cef`, but that tx is already spent including 6 confirmation
// bot safe to exit?
//
// BCH signing
// dPoW security -> 4: KMD notarized, 5: BTC notarized, after next notary elections
// bigendian architectures need to use little endian for sighash calcs

4
iguana/exchanges/LP_network.c

@ -409,7 +409,11 @@ void LP_broadcast_finish(int32_t pubsock,char *base,char *rel,uint8_t *msg,cJSON
msglen = (int32_t)strlen((char *)msg) + 1;
if ( crc32 == 0 )
crc32 = calc_crc32(0,&msg[2],msglen - 2);
#ifdef FROM_MARKETMAKER
if ( G.LP_IAMLP == 0 )
#else
if ( IAMLP == 0 )
#endif
{
free(msg);
//printf("broadcast %s\n",jstr(argjson,"method"));

4
iguana/exchanges/LP_ordermatch.c

@ -146,12 +146,12 @@ double LP_quote_validate(struct LP_utxoinfo *autxo,struct LP_utxoinfo *butxo,str
if ( strcmp(autxo->coinaddr,qp->destaddr) != 0 )
return(-10);
}
if ( autxo != 0 && destvalue < 2*qp->desttxfee+qp->destsatoshis )
if ( autxo != 0 && destvalue < qp->desttxfee+qp->destsatoshis )
{
printf("destvalue %.8f destsatoshis %.8f is too small txfee %.8f?\n",dstr(destvalue),dstr(qp->destsatoshis),dstr(qp->desttxfee));
return(-11);
}
if ( butxo != 0 && srcvalue < 2*qp->txfee+qp->satoshis )
if ( butxo != 0 && srcvalue < qp->txfee+qp->satoshis )
{
printf("srcvalue %.8f [%.8f] satoshis %.8f is too small txfee %.8f?\n",dstr(srcvalue),dstr(srcvalue) - dstr(qp->txfee+qp->satoshis),dstr(qp->satoshis),dstr(qp->txfee));
return(-33);

13
iguana/exchanges/LP_socket.c

@ -391,8 +391,7 @@ void electrum_initial_requests(struct electrum_info *ep)
int32_t electrum_kickstart(struct electrum_info *ep)
{
closesocket(ep->sock), ep->sock = -1;
sleep(1);
closesocket(ep->sock);//, ep->sock = -1;
if ( (ep->sock= LP_socket(0,ep->ipaddr,ep->port)) < 0 )
{
printf("error RE-connecting to %s:%u\n",ep->ipaddr,ep->port);
@ -408,6 +407,8 @@ int32_t electrum_kickstart(struct electrum_info *ep)
return(0);
}
int32_t zeroval() { return(0); }
cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,char *method,char *params,int32_t timeout)
{
// queue id and string and callback
@ -416,6 +417,12 @@ cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch
ep = electrum_server(symbol,0);
while ( ep != 0 )
{
if ( strcmp(ep->symbol,symbol) != 0 )
{
printf("ep.%p %s %s:%u called for [%s]???\n",ep,ep->symbol,ep->ipaddr,ep->port,symbol);
int32_t i = 1 / zeroval();
printf("it should be dead already.%d\n",i);
}
if ( ep != 0 && ep->sock >= 0 && retjsonp != 0 )
{
*retjsonp = 0;
@ -605,7 +612,7 @@ cJSON *electrum_address_listunspent(char *symbol,struct electrum_info *ep,cJSON
{
if ( jobj(retjson,"error") == 0 && is_cJSON_Array(retjson) != 0 )
{
if ( 0 && electrumflag > 1 )
if ( 1 && electrumflag > 1 )
printf("%s.%d u.%u/%d t.%ld %s LISTUNSPENT.(%d)\n",coin->symbol,height,ap->unspenttime,ap->unspentheight,time(NULL),addr,(int32_t)strlen(jprint(retjson,0)));
updatedflag = 0;
if ( electrum_process_array(coin,ep,addr,retjson,electrumflag) != 0 )

93
iguana/exchanges/LP_tradebots.c

@ -18,15 +18,15 @@
// marketmaker
//
#define TRADEBOTS_GAPTIME 60
#define LP_TRADEBOTS_MAXTRADES 100
#define TRADEBOTS_GAPTIME 120
#define LP_TRADEBOTS_MAXTRADES 10
struct LP_tradebot_trade
{
double maxprice,totalrelvolume,basevol,relvol;
uint64_t aliceid;
int32_t dispdir;
uint32_t started,finished,requestid,quoteid,tradeid;
uint32_t started,finished,requestid,quoteid,tradeid,completed;
char base[32],rel[32],event[32];
};
@ -36,7 +36,7 @@ struct LP_tradebot
char name[128],base[32],rel[32];
int32_t numtrades,numpending,completed,dispdir;
double maxprice,totalrelvolume,totalbasevolume,basesum,relsum,pendbasesum,pendrelsum;
uint32_t dead,pause,started,id;
uint32_t lasttime,dead,pause,userpause,started,id;
struct LP_tradebot_trade *trades[LP_TRADEBOTS_MAXTRADES];
} *LP_tradebots;
@ -134,8 +134,8 @@ cJSON *LP_tradebot_json(struct LP_tradebot *bot)
jaddstr(json,"name",bot->name);
jaddnum(json,"botid",bot->id);
jaddnum(json,"started",bot->started);
if ( bot->pause != 0 )
jaddnum(json,"paused",bot->pause);
if ( bot->pause != 0 || bot->userpause != 0 )
jaddnum(json,"paused",bot->userpause != 0 ? bot->userpause : bot->pause);
if ( bot->dead != 0 )
jaddnum(json,"stopped",bot->dead);
if ( bot->dispdir > 0 )
@ -255,9 +255,9 @@ struct LP_tradebot_trade *LP_tradebot_pending(struct LP_tradebot *bot,cJSON *pen
tp->basevol = jdouble(pending,"basevalue");
tp->relvol = jdouble(pending,"relvalue");
printf("tradebot pending basevol %.8f relvol %.8f\n",tp->basevol,tp->relvol);
bot->pendrelsum += tp->relvol;
bot->pendbasesum += tp->basevol;
bot->numpending++;
//bot->pendrelsum += tp->relvol;
//bot->pendbasesum += tp->basevol;
//bot->numpending++;
return(tp);
}
@ -291,9 +291,9 @@ double LP_orderbook_maxrel(char *base,char *rel,double maxprice)
void LP_tradebot_timeslice(void *ctx,struct LP_tradebot *bot)
{
double remaining,maxrel; int32_t i,maxiters = 10; uint32_t tradeid; bits256 destpubkey; char *retstr,*liststr; cJSON *retjson,*retjson2,*pending;
double remaining,maxrel; struct LP_tradebot_trade *tp; int32_t i,maxiters = 10; uint32_t tradeid; bits256 destpubkey; char *retstr,*liststr; cJSON *retjson,*retjson2,*pending;
memset(destpubkey.bytes,0,sizeof(destpubkey));
if ( bot->dead == 0 && bot->pause == 0 && bot->numtrades < sizeof(bot->trades)/sizeof(*bot->trades) )
if ( bot->dead == 0 && bot->pause == 0 && bot->userpause == 0 && bot->numtrades < sizeof(bot->trades)/sizeof(*bot->trades) )
{
if ( (liststr= LP_recent_swaps(0)) != 0 )
{
@ -317,11 +317,7 @@ void LP_tradebot_timeslice(void *ctx,struct LP_tradebot *bot)
{
if ( (pending= jobj(retjson2,"pending")) != 0 && juint(pending,"tradeid") == tradeid )
{
bot->trades[bot->numtrades++] = LP_tradebot_pending(bot,pending,tradeid);
if ( bot->relsum >= 0.99*bot->totalrelvolume-SMALLVAL || bot->basesum >= 0.99*bot->totalbasevolume-SMALLVAL )
bot->dead = (uint32_t)time(NULL);
else if ( (bot->pendrelsum+bot->relsum) >= 0.99*bot->totalrelvolume-SMALLVAL || (bot->basesum+bot->pendbasesum) >= 0.99*bot->totalbasevolume-SMALLVAL )
bot->pause = (uint32_t)time(NULL);
bot->trades[bot->numtrades++] = tp = LP_tradebot_pending(bot,pending,tradeid);
printf("issued bot trade.%u %s\n",tradeid,retstr);
free_json(retjson2);
free(retstr);
@ -344,7 +340,9 @@ void LP_tradebot_timeslice(void *ctx,struct LP_tradebot *bot)
void LP_aliceid(uint32_t tradeid,uint64_t aliceid,char *event,uint32_t requestid,uint32_t quoteid)
{
struct LP_tradebot *bot,*tmp; int32_t i; struct LP_tradebot_trade *tp;
struct LP_tradebot *bot,*tmp; int32_t i,matched = 0; struct LP_tradebot_trade *tp;
if ( tradeid == 0 )
return;
DL_FOREACH_SAFE(LP_tradebots,bot,tmp)
{
for (i=0; i<bot->numtrades; i++)
@ -352,16 +350,22 @@ void LP_aliceid(uint32_t tradeid,uint64_t aliceid,char *event,uint32_t requestid
if ( (tp= bot->trades[i]) != 0 && tp->finished == 0 && tp->tradeid == tradeid )
{
tp->aliceid = aliceid;
printf("bot event tradeid.%u aliceid.%llu (%s) r.%u q.%u\n",tradeid,(long long)aliceid,event,requestid,quoteid);
if ( requestid != 0 && quoteid != 0 )
{
tp->requestid = requestid;
tp->quoteid = quoteid;
}
strcpy(tp->event,event);
matched = 0;
break;
}
}
if ( matched != 0 )
break;
}
if ( matched == 0 )
printf("NO MATCH: bot event tradeid.%u aliceid.%llu (%s) r.%u q.%u\n",tradeid,(long long)aliceid,event,requestid,quoteid);
}
void LP_tradebot_finished(uint32_t tradeid,uint32_t requestid,uint32_t quoteid)
@ -375,11 +379,12 @@ void LP_tradebot_finished(uint32_t tradeid,uint32_t requestid,uint32_t quoteid)
{
tp->requestid = requestid;
tp->quoteid = quoteid;
bot->pendbasesum -= tp->basevol, bot->basesum += tp->basevol;
bot->pendrelsum -= tp->relvol, bot->relsum += tp->relvol;
bot->numpending--, bot->completed++;
//bot->pendbasesum -= tp->basevol, bot->basesum += tp->basevol;
//bot->pendrelsum -= tp->relvol, bot->relsum += tp->relvol;
//bot->numpending--, bot->completed++;
printf("bot.%u detected completion tradeid.%u aliceid.%llx r.%u q.%u, numpending.%d completed.%d\n",bot->id,tp->tradeid,(long long)tp->aliceid,tp->requestid,tp->quoteid,bot->numpending,bot->completed);
tp->finished = (uint32_t)time(NULL);
tp->completed = tp->finished = (uint32_t)time(NULL);
strcpy(tp->event,"finished");
break;
}
}
@ -397,23 +402,47 @@ void LP_tradebots_timeslice(void *ctx)
if ( bot->numpending > 0 && LP_numfinished > lastnumfinished )
{
// expire pending trades and see if any still need their requestid/quoteid
bot->basesum = bot->pendbasesum = 0.;//-= tp->basevol;
bot->relsum = bot->pendrelsum = 0.;//-= tp->relvol;
bot->completed = bot->numpending = 0;//--;
for (i=0; i<bot->numtrades; i++)
{
if ( (tp= bot->trades[i]) != 0 && tp->finished == 0 )
if ( (tp= bot->trades[i]) != 0 )
{
if ( time(NULL) > tp->started+INSTANTDEX_LOCKTIME*2 )
if (tp->finished == 0 )
{
if ( time(NULL) > tp->started+INSTANTDEX_LOCKTIME*2 )
{
tp->finished = (uint32_t)time(NULL);
printf("%s trade.%d of %d expired\n",bot->name,i,bot->numtrades);
}
}
if ( tp->finished != 0 && tp->completed != 0 )
{
bot->pendbasesum -= tp->basevol;
bot->pendrelsum -= tp->relvol;
bot->numpending--;
tp->finished = (uint32_t)time(NULL);
printf("%s trade.%d of %d expired\n",bot->name,i,bot->numtrades);
bot->basesum += tp->basevol;
bot->relsum += tp->relvol;
bot->completed++;
}
else if ( tp->finished == 0 && tp->requestid != 0 && tp->quoteid != 0 )
{
bot->pendbasesum += tp->basevol;
bot->pendrelsum += tp->relvol;
bot->numpending++;
}
}
}
}
else if ( bot->numpending == 0 )
if ( bot->relsum >= 0.99*bot->totalrelvolume-SMALLVAL || bot->basesum >= 0.99*bot->totalbasevolume-SMALLVAL )
bot->dead = (uint32_t)time(NULL);
else if ( (bot->pendrelsum+bot->relsum) >= 0.99*bot->totalrelvolume-SMALLVAL || (bot->basesum+bot->pendbasesum) >= 0.99*bot->totalbasevolume-SMALLVAL )
bot->pause = (uint32_t)time(NULL);
else if ( bot->userpause == 0 )
bot->pause = 0;
if ( bot->numpending == 0 && time(NULL) > bot->lasttime+TRADEBOTS_GAPTIME )
{
LP_tradebot_timeslice(ctx,bot);
bot->lasttime = (uint32_t)time(NULL);
}
}
lastnumfinished = LP_numfinished;
}
@ -613,7 +642,7 @@ char *LP_tradebot_pause(void *ctx,int32_t pubsock,cJSON *argjson,uint32_t botid)
{
if ( bot->dead != 0 )
return(clonestr("{\"error\":\"botid aleady stopped\"}"));
bot->pause = (uint32_t)time(NULL);
bot->userpause = (uint32_t)time(NULL);
return(clonestr("{\"result\":\"success\"}"));
}
return(clonestr("{\"error\":\"couldnt find botid\"}"));
@ -626,9 +655,9 @@ char *LP_tradebot_resume(void *ctx,int32_t pubsock,cJSON *argjson,uint32_t botid
{
if ( bot->dead != 0 )
return(clonestr("{\"error\":\"botid aleady stopped\"}"));
if ( bot->pause == 0 )
if ( bot->userpause == 0 )
return(clonestr("{\"result\":\"success\",\"status\":\"botid not paused\"}"));
bot->pause = 0;
bot->userpause = 0;
return(clonestr("{\"result\":\"success\"}"));
}
return(clonestr("{\"error\":\"couldnt find botid\"}"));

2
iguana/exchanges/bot_settings

@ -1,3 +1,3 @@
#!/bin/bash
source userpass
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"bot_settings\",\"botid\":$1,\"newprice\":$1,\"newvolume\":$2}"
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"bot_settings\",\"botid\":$1,\"newprice\":$2,\"newvolume\":$3}"

3
iguana/exchanges/stats.c

@ -822,6 +822,7 @@ continue;
#ifndef FROM_MARKETMAKER
portable_mutex_t LP_commandmutex;
uint16_t LP_RPCPORT;
void stats_kvjson(FILE *logfp,int32_t height,int32_t savedheight,uint32_t timestamp,char *key,cJSON *kvjson,bits256 pubkey,bits256 sigprev)
{
@ -1133,6 +1134,7 @@ char *stats_update(FILE *logfp,char *destdir,char *statefname,char *komodofname)
return(jprint(retjson,1));
}
#ifndef FROM_PRIVATEBET
int main(int argc, const char * argv[])
{
struct tai T; uint32_t timestamp; struct DEXstats_disp prices[365]; int32_t i,n,seconds,leftdatenum; FILE *fp,*logfp; char *filestr,*retstr,*statefname,logfname[512],komodofile[512]; uint16_t port = LP_RPCPORT;
@ -1183,3 +1185,4 @@ int main(int argc, const char * argv[])
return 0;
}
#endif
#endif

Loading…
Cancel
Save