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

385 lines
15 KiB

8 years ago
/******************************************************************************
* Copyright © 2014-2017 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. *
* *
******************************************************************************/
//
// main.c
// marketmaker
//
// Copyright © 2017 SuperNET. All rights reserved.
//
7 years ago
7 years ago
void PNACL_message(char *arg,...)
7 years ago
{
}
8 years ago
#define FROM_MARKETMAKER
7 years ago
8 years ago
#include <stdio.h>
#include <stdint.h>
// #include "lib.h"
#ifndef NATIVE_WINDOWS
7 years ago
#include "OS_portable.h"
#else
7 years ago
#include "../../crypto777/OS_portable.h"
#endif // !_WIN_32
uint32_t DOCKERFLAG;
8 years ago
#define MAX(a,b) ((a) > (b) ? (a) : (b))
8 years ago
char *stats_JSON(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,char *remoteaddr,uint16_t port);
8 years ago
#include "stats.c"
8 years ago
void LP_priceupdate(char *base,char *rel,double price,double avebid,double aveask,double highbid,double lowask,double PAXPRICES[32]);
8 years ago
8 years ago
//defined(__APPLE__) ||
7 years ago
#ifdef FROM_JS // defined(WIN32) || defined(USE_STATIC_NANOMSG)
8 years ago
#include "../../crypto777/nanosrc/nn.h"
#include "../../crypto777/nanosrc/bus.h"
#include "../../crypto777/nanosrc/pubsub.h"
#include "../../crypto777/nanosrc/pipeline.h"
#include "../../crypto777/nanosrc/reqrep.h"
#include "../../crypto777/nanosrc/tcp.h"
8 years ago
#include "../../crypto777/nanosrc/pair.h"
8 years ago
#else
#if defined(WIN32) || defined(USE_STATIC_NANOMSG)
#include "../../crypto777/nanosrc/nn.h"
#include "../../crypto777/nanosrc/bus.h"
#include "../../crypto777/nanosrc/pubsub.h"
#include "../../crypto777/nanosrc/pipeline.h"
#include "../../crypto777/nanosrc/reqrep.h"
#include "../../crypto777/nanosrc/tcp.h"
7 years ago
#include "../../crypto777/nanosrc/pair.h"
#include "../../crypto777/nanosrc/ws.h"
#else
#include "/usr/local/include/nanomsg/nn.h"
#include "/usr/local/include/nanomsg/bus.h"
#include "/usr/local/include/nanomsg/pubsub.h"
#include "/usr/local/include/nanomsg/pipeline.h"
#include "/usr/local/include/nanomsg/reqrep.h"
#include "/usr/local/include/nanomsg/tcp.h"
7 years ago
#include "/usr/local/include/nanomsg/pair.h"
#include "/usr/local/include/nanomsg/ws.h"
#endif
8 years ago
#endif
#ifndef NN_WS_MSG_TYPE
#define NN_WS_MSG_TYPE 1
#endif
8 years ago
7 years ago
8 years ago
#include "LP_nativeDEX.c"
7 years ago
void LP_ports(uint16_t *pullportp,uint16_t *pubportp,uint16_t *busportp,uint16_t netid)
{
int32_t netmod,netdiv; uint16_t otherports;
*pullportp = *pubportp = *busportp = 0;
if ( netid < 0 )
netid = 0;
else if ( netid > (65535-40-LP_RPCPORT)/4 )
{
printf("netid.%d overflow vs max netid.%d 14420?\n",netid,(65535-40-LP_RPCPORT)/4);
exit(-1);
}
if ( netid != 0 )
{
netmod = (netid % 10);
netdiv = (netid / 10);
otherports = (netdiv * 40) + (LP_RPCPORT + netmod);
} else otherports = LP_RPCPORT;
*pullportp = otherports + 10;
*pubportp = otherports + 20;
*busportp = otherports + 30;
printf("RPCport.%d remoteport.%d, nanoports %d %d %d\n",RPC_port,RPC_port-1,*pullportp,*pubportp,*busportp);
}
8 years ago
void LP_main(void *ptr)
{
7 years ago
char *passphrase; double profitmargin; uint16_t netid=0,port,pullport,pubport,busport; cJSON *argjson = ptr;
8 years ago
if ( (passphrase= jstr(argjson,"passphrase")) != 0 )
{
profitmargin = jdouble(argjson,"profitmargin");
8 years ago
LP_profitratio += profitmargin;
7 years ago
if ( (port= juint(argjson,"rpcport")) < 1000 )
7 years ago
port = LP_RPCPORT;
7 years ago
if ( jobj(argjson,"netid") != 0 )
netid = juint(argjson,"netid");
7 years ago
LP_ports(&pullport,&pubport,&busport,netid);
LPinit(port,pullport,pubport,busport,passphrase,jint(argjson,"client"),jstr(argjson,"userhome"),argjson);
8 years ago
}
8 years ago
}
8 years ago
int32_t ensure_writable(char *dirname)
8 years ago
{
char fname[512],str[65],str2[65]; bits256 r,check; FILE *fp;
OS_randombytes(r.bytes,sizeof(r));
7 years ago
sprintf(fname,"%s/checkval",dirname), OS_compatible_path(fname);
if ( (fp= fopen(fname,"wb")) == 0 )
{
printf("FATAL ERROR cant create %s\n",fname);
fprintf(stderr,"FATAL ERROR cant create %s\n",fname);
return(-1);
}
else if ( fwrite(r.bytes,1,sizeof(r),fp) != sizeof(r) )
{
printf("FATAL ERROR error writing %s\n",fname);
fprintf(stderr,"FATAL ERROR writing %s\n",fname);
return(-1);
}
else
{
fclose(fp);
if ( (fp= fopen(fname,"rb")) == 0 )
{
printf("FATAL ERROR cant open %s\n",fname);
fprintf(stderr,"FATAL ERROR cant open %s\n",fname);
return(-1);
}
else if ( fread(check.bytes,1,sizeof(check),fp) != sizeof(check) )
{
printf("FATAL ERROR error reading %s\n",fname);
fprintf(stderr,"FATAL ERROR reading %s\n",fname);
return(-1);
}
else if ( memcmp(check.bytes,r.bytes,sizeof(r)) != 0 )
{
printf("FATAL ERROR error comparint %s %s vs %s\n",fname,bits256_str(str,r),bits256_str(str2,check));
fprintf(stderr,"FATAL ERROR error comparint %s %s vs %s\n",fname,bits256_str(str,r),bits256_str(str2,check));
return(-1);
}
fclose(fp);
}
return(0);
7 years ago
}
int main(int argc, const char * argv[])
{
char dirname[512]; double incr; cJSON *retjson;
7 years ago
OS_init();
7 years ago
if ( strstr(argv[0],"btc2kmd") != 0 && argv[1] != 0 )
7 years ago
{
7 years ago
bits256 privkey,checkkey; uint8_t tmptype; char kmdwif[64],str[65],str2[65],*retstr;
if ( LP_wifstr_valid("BTC",(char *)argv[1]) > 0 )
7 years ago
{
bitcoin_wif2priv("BTC",0,&tmptype,&privkey,(char *)argv[1]);
bitcoin_priv2wif("KMD",0,kmdwif,privkey,188);
bitcoin_wif2priv("KMD",0,&tmptype,&checkkey,kmdwif);
if ( bits256_cmp(privkey,checkkey) == 0 )
printf("BTC %s -> KMD %s: privkey %s\n",argv[1],kmdwif,bits256_str(str,privkey));
else printf("ERROR BTC %s %s != KMD %s %s\n",argv[1],bits256_str(str,privkey),kmdwif,bits256_str(str2,checkkey));
7 years ago
}
else
7 years ago
{
7 years ago
if ( (retstr= LP_convaddress("BTC",(char *)argv[1],"KMD")) != 0 )
printf("%s\n",retstr);
7 years ago
}
7 years ago
exit(0);
}
7 years ago
else if ( argv[1] != 0 && strcmp(argv[1],"events") == 0 )
{
int32_t len,bufsize = 1000000; void *ptr; char *buf;
7 years ago
if ( (IPC_ENDPOINT= nn_socket(AF_SP,NN_PAIR)) >= 0 )
{
if ( nn_connect(IPC_ENDPOINT,"ws://127.0.0.1:5555") >= 0 )
{
buf = calloc(1,bufsize);
7 years ago
while ( 1 )
{
if ( (len= nn_recv(IPC_ENDPOINT,&ptr,NN_MSG,0)) > 0 )
{
if ( len < bufsize )
{
memcpy(buf,ptr,len);
buf[len] = 0;
printf("%s\n",(char *)buf);
}
7 years ago
nn_freemsg(ptr);
}
}
7 years ago
} else printf("nn_connect error to IPC_ENDPOINT\n");
} else printf("error opening IPC_ENDPOINT\n");
7 years ago
}
7 years ago
else if ( argv[1] != 0 && strcmp(argv[1],"hush") == 0 )
7 years ago
{
7 years ago
uint32_t timestamp; char str[65],wifstr[128]; bits256 privkey; int32_t i;
7 years ago
timestamp = (uint32_t)time(NULL);
7 years ago
//printf("start hush vanitygen t.%u\n",timestamp);
7 years ago
for (i=0; i<1000000000; i++)
7 years ago
{
7 years ago
OS_randombytes(privkey.bytes,sizeof(privkey));
7 years ago
privkey.bytes[0] = 0x0e;
privkey.bytes[1] = 0x5b;
privkey.bytes[2] = 0xf9;
7 years ago
privkey.bytes[3] = 0xc6;
privkey.bytes[4] = 0x06;
7 years ago
privkey.bytes[5] = 0xdd;
privkey.bytes[6] = 0xbb;
7 years ago
bitcoin_priv2wiflong("HUSH",0xab,wifstr,privkey,0x36);
7 years ago
if ( wifstr[2] == 'x' && wifstr[4] == 'H' && wifstr[5] == 'u' && wifstr[6] == 's' )//&& wifstr[3] == 'x' )
7 years ago
{
7 years ago
if ( wifstr[7] == 'h' && wifstr[8] == 'L' && wifstr[9] == 'i' )
7 years ago
{
7 years ago
//printf("i.%d %s -> wif.%s\n",i,bits256_str(str,privkey),wifstr);
7 years ago
if ( wifstr[10] == 's' && wifstr[11] == 't' )
{
7 years ago
printf("{\"iters\":%d,\"privkey\":\"%s\",\"wif\":\"%s\"}\n",i,bits256_str(str,privkey),wifstr);
7 years ago
break;
}
7 years ago
}
7 years ago
} //else printf("failed %s\n",wifstr);
7 years ago
}
7 years ago
//printf("done hush vanitygen done %u elapsed %d\n",(uint32_t)time(NULL),(uint32_t)time(NULL) - timestamp);
7 years ago
exit(0);
}
else if ( argv[1] != 0 && strcmp(argv[1],"vanity") == 0 && argv[2] != 0 )
{
7 years ago
uint32_t timestamp; uint8_t pubkey33[33]; char str[65],coinaddr[64],wifstr[128]; bits256 privkey; int32_t i,len; void *ctx;
ctx = bitcoin_ctx();
len = (int32_t)strlen(argv[2]);
7 years ago
timestamp = (uint32_t)time(NULL);
7 years ago
printf("start vanitygen (%s).%d t.%u\n",argv[2],len,timestamp);
7 years ago
for (i=0; i<1000000000; i++)
{
OS_randombytes(privkey.bytes,sizeof(privkey));
bitcoin_priv2pub(ctx,"KMD",pubkey33,coinaddr,privkey,0,60);
7 years ago
if ( strncmp(coinaddr+1,argv[2],len-1) == 0 )
7 years ago
{
7 years ago
bitcoin_priv2wif("KMD",0,wifstr,privkey,188);
7 years ago
printf("i.%d %s -> %s wif.%s\n",i,bits256_str(str,privkey),coinaddr,wifstr);
7 years ago
if ( coinaddr[1+len-1] == argv[2][len-1] )
7 years ago
break;
7 years ago
} //else printf("failed %s\n",wifstr);
7 years ago
}
7 years ago
printf("done vanitygen.(%s) done %u elapsed %d\n",argv[2],(uint32_t)time(NULL),(uint32_t)time(NULL) - timestamp);
7 years ago
exit(0);
}
7 years ago
else if ( argv[1] != 0 && strcmp(argv[1],"airdropH") == 0 && argv[2] != 0 )
{
7 years ago
FILE *fp; double val,total = 0.; uint8_t checktype,addrtype,rmd160[21],checkrmd160[21]; char *floatstr,*addrstr,buf[256],checkaddr[64],coinaddr[64],manystrs[64][128],cmd[64*128]; int32_t n,i,num; char *flag;
7 years ago
if ( (fp= fopen(argv[2],"rb")) != 0 )
{
7 years ago
num = 0;
7 years ago
while ( fgets(buf,sizeof(buf),fp) > 0 )
{
if ( (n= (int32_t)strlen(buf)) > 0 )
buf[--n] = 0;
flag = 0;
for (i=0; i<n; i++)
{
if ( buf[i] == ',' )
{
buf[i] = 0;
flag = &buf[i+1];
7 years ago
break;
7 years ago
}
}
7 years ago
if ( flag != 0 )
{
7 years ago
addrstr = flag, floatstr = buf;
7 years ago
//addrstr = buf, floatstr = flag;
7 years ago
//bitcoin_addr2rmd160("HUSH",28,&addrtype,rmd160,buf);
7 years ago
bitcoin_addr2rmd160("BTC",0,&addrtype,rmd160,addrstr);
7 years ago
bitcoin_address("KMD",coinaddr,0,addrtype == 0 ? 60 : 85,rmd160,20);
7 years ago
bitcoin_addr2rmd160("KMD",0,&checktype,checkrmd160,coinaddr);
7 years ago
//bitcoin_address("HUSH",checkaddr,28,checktype == 60 ? 184 : 189,checkrmd160,20);
bitcoin_address("BTC",checkaddr,0,checktype == 60 ? 0 : 5,checkrmd160,20);
7 years ago
if ( memcmp(rmd160,checkrmd160,20) != 0 || strcmp(addrstr,checkaddr) != 0 )
7 years ago
{
for (i=0; i<20; i++)
printf("%02x",rmd160[i]);
printf(" vs. ");
for (i=0; i<20; i++)
printf("%02x",checkrmd160[i]);
7 years ago
printf(" address calc error (%s).%d -> (%s).%d -> (%s) %.8f?\n",addrstr,addrtype,coinaddr,checktype,checkaddr,atof(floatstr));
7 years ago
}
else
{
7 years ago
val = atof(floatstr);
7 years ago
sprintf(manystrs[num++],"\\\"%s\\\":%0.8f",coinaddr,val);
if ( num >= sizeof(manystrs)/sizeof(*manystrs) )
{
7 years ago
sprintf(cmd,"fiat/btch sendmany \\\"\\\" \"{");
7 years ago
for (i=0; i<num; i++)
7 years ago
sprintf(cmd + strlen(cmd),"%s%s",manystrs[i],i<num-1?",":"");
7 years ago
strcat(cmd,"}\" 0");
7 years ago
printf("%s\nsleep 3\n",cmd);
7 years ago
num = 0;
memset(manystrs,0,sizeof(manystrs));
}
total += val;
//printf("(%s).%d (%s) <- %.8f (%s) total %.8f\n",addrstr,addrtype,coinaddr,val,floatstr,total);
7 years ago
}
7 years ago
} else printf("parse error for (%s)\n",buf);
7 years ago
}
7 years ago
if ( num > 0 )
{
7 years ago
sprintf(cmd,"fiat/btch sendmany \\\"\\\" \"{");
7 years ago
for (i=0; i<num; i++)
7 years ago
sprintf(cmd + strlen(cmd),"%s%s",manystrs[i],i<num-1?",":"");
7 years ago
strcat(cmd,"}\" 0");
7 years ago
printf("%s\n",cmd);
num = 0;
memset(manystrs,0,sizeof(manystrs));
}
printf("close (%s) total %.8f\n",argv[2],total);
7 years ago
fclose(fp);
7 years ago
} else printf("couldnt open (%s)\n",argv[2]);
7 years ago
exit(0);
}
8 years ago
sprintf(dirname,"%s",GLOBAL_DBDIR), OS_ensure_directory(dirname);
if ( ensure_writable(dirname) < 0 )
exit(0);
8 years ago
sprintf(dirname,"%s/SWAPS",GLOBAL_DBDIR), OS_ensure_directory(dirname);
if ( ensure_writable(dirname) < 0 )
exit(0);
8 years ago
sprintf(dirname,"%s/PRICES",GLOBAL_DBDIR), OS_ensure_directory(dirname);
if ( ensure_writable(dirname) < 0 )
exit(0);
7 years ago
sprintf(dirname,"%s/UNSPENTS",GLOBAL_DBDIR), OS_ensure_directory(dirname);
if ( ensure_writable(dirname) < 0 )
exit(0);
7 years ago
#ifdef FROM_JS
argc = 2;
retjson = cJSON_Parse("{\"client\":1,\"passphrase\":\"test\"}");
printf("calling LP_main(%s)\n",jprint(retjson,0));
LP_main(retjson);
7 years ago
emscripten_set_main_loop(LP_fromjs_iter,1,0);
7 years ago
#else
7 years ago
if ( argc == 1 )
{
7 years ago
//LP_privkey_tests();
7 years ago
LP_NXT_redeems();
sleep(3);
return(0);
}
8 years ago
if ( argc > 1 && (retjson= cJSON_Parse(argv[1])) != 0 )
{
7 years ago
if ( jint(retjson,"docker") == 1 )
DOCKERFLAG = 1;
else if ( jstr(retjson,"docker") != 0 )
DOCKERFLAG = (uint32_t)calc_ipbits(jstr(retjson,"docker"));
//if ( jobj(retjson,"passphrase") != 0 )
// jdelete(retjson,"passphrase");
//if ( (passphrase= jstr(retjson,"passphrase")) == 0 )
// jaddstr(retjson,"passphrase","default");
8 years ago
if ( OS_thread_create(malloc(sizeof(pthread_t)),NULL,(void *)LP_main,(void *)retjson) != 0 )
8 years ago
{
8 years ago
printf("error launching LP_main (%s)\n",jprint(retjson,0));
8 years ago
exit(-1);
7 years ago
} //else printf("(%s) launched.(%s)\n",argv[1],passphrase);
8 years ago
incr = 100.;
7 years ago
while ( LP_STOP_RECEIVED == 0 )
7 years ago
sleep(100000);
8 years ago
}
7 years ago
#endif
8 years ago
return 0;
}