Browse Source

Merge pull request #914 from jl777/dev

bug fixes, mpnet
patch-6
jl777 7 years ago
committed by GitHub
parent
commit
c8c41b1fd7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      crypto777/OS_portable.h
  2. 177
      crypto777/bitcoind_RPC.c
  3. 2
      iguana/coins/rfox_7776
  4. 2
      iguana/coins/vrsc_7776
  5. 1
      iguana/coins/zilla_7776
  6. 2
      iguana/dpow/dpow_fsm.c
  7. 2
      iguana/dpow/dpow_tx.c
  8. 21
      iguana/exchanges/LP_bitcoin.c
  9. 3
      iguana/exchanges/LP_coins.c
  10. 39
      iguana/exchanges/LP_commands.c
  11. 18
      iguana/exchanges/LP_include.h
  12. 254
      iguana/exchanges/LP_mpnet.c
  13. 46
      iguana/exchanges/LP_nativeDEX.c
  14. 334
      iguana/exchanges/LP_ordermatch.c
  15. 20
      iguana/exchanges/LP_portfolio.c
  16. 87
      iguana/exchanges/LP_prices.c
  17. 14
      iguana/exchanges/LP_rpc.c
  18. 38
      iguana/exchanges/LP_signatures.c
  19. 121
      iguana/exchanges/LP_socket.c
  20. 2
      iguana/exchanges/LP_statemachine.c
  21. 13
      iguana/exchanges/LP_swap.c
  22. 2
      iguana/exchanges/LP_tradebots.c
  23. 53
      iguana/exchanges/LP_transaction.c
  24. 2
      iguana/exchanges/LP_utxo.c
  25. 4
      iguana/exchanges/coins
  26. 6
      iguana/exchanges/mm.c
  27. 2
      iguana/iguana777.c
  28. 2
      iguana/iguana_notary.c
  29. 2
      iguana/iguana_ramchain.c
  30. 9
      iguana/iguana_wallet.c
  31. 3
      iguana/m_notary_run
  32. 1
      iguana/m_splitfund

1
crypto777/OS_portable.h

@ -425,6 +425,7 @@ bits256 bits256_sha256(bits256 data);
void bits256_rmd160(uint8_t rmd160[20],bits256 data);
void bits256_rmd160_sha256(uint8_t rmd160[20],bits256 data);
double get_theoretical(double *avebidp,double *aveaskp,double *highbidp,double *lowaskp,double *CMC_averagep,double changes[3],char *name,char *base,char *rel,double *USD_averagep);
char *bitcoind_RPCnew(void *curl_handle,char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params,int32_t timeout);
extern char *Iguana_validcommands[];
extern bits256 GENESIS_PUBKEY,GENESIS_PRIVKEY;

177
crypto777/bitcoind_RPC.c

@ -75,7 +75,7 @@ char *post_process_bitcoind_RPC(char *debugstr,char *command,char *rpcstr,char *
char *retstr = 0;
cJSON *json,*result,*error;
#ifndef FROM_MARKETMAKER
usleep(1000);
//usleep(1000);
#endif
//printf("<<<<<<<<<<< bitcoind_RPC: %s post_process_bitcoind_RPC.%s.[%s]\n",debugstr,command,rpcstr);
if ( command == 0 || rpcstr == 0 || rpcstr[0] == 0 )
@ -132,7 +132,7 @@ char *post_process_bitcoind_RPC(char *debugstr,char *command,char *rpcstr,char *
*
************************************************************************/
static int32_t USE_JAY;
//static int32_t USE_JAY;
char *Jay_NXTrequest(char *command,char *params)
{
@ -142,26 +142,28 @@ char *Jay_NXTrequest(char *command,char *params)
return(retstr);
}
static void bitcoind_init()
{
static int32_t didinit;
if ( didinit == 0 )
{
didinit = 1;
curl_global_init(CURL_GLOBAL_ALL); //init the curl session
}
}
char *bitcoind_RPC(char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params,int32_t timeout)
{
#ifdef KEEPALIVE
static
#endif
CURL *curl_handle = 0;
static int didinit,count,count2; static double elapsedsum,elapsedsum2; extern int32_t USE_JAY;
static int count,count2; static double elapsedsum,elapsedsum2; extern int32_t USE_JAY;
struct MemoryStruct chunk;
struct curl_slist *headers = NULL; struct return_string s; CURLcode res;
char *bracket0,*bracket1,*retstr,*databuf = 0; long len; int32_t specialcase,numretries; double starttime;
if ( didinit == 0 )
{
didinit = 1;
curl_global_init(CURL_GLOBAL_ALL); //init the curl session
}
if ( (0) && (USE_JAY != 0 && (strncmp(url,"http://127.0.0.1:7876/nxt",strlen("http://127.0.0.1:7876/nxt")) == 0 || strncmp(url,"https://127.0.0.1:7876/nxt",strlen("https://127.0.0.1:7876/nxt")) == 0)) )
{
if ( (databuf= Jay_NXTrequest(command,params)) != 0 )
return(databuf);
}
bitcoind_init();
numretries = 0;
if ( debugstr != 0 && strcmp(debugstr,"BTCD") == 0 && command != 0 && strcmp(command,"SuperNET") == 0 )
specialcase = 1;
@ -287,7 +289,7 @@ try_again:
count++;
elapsedsum += (OS_milliseconds() - starttime);
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 )
{
*retstrp = retstr;
@ -309,6 +311,153 @@ try_again:
}
}
char *bitcoind_RPCnew(void *curl_handle,char **retstrp,char *debugstr,char *url,char *userpass,char *command,char *params,int32_t timeout)
{
static int count,count2; static double elapsedsum,elapsedsum2; extern int32_t USE_JAY;
struct MemoryStruct chunk;
struct curl_slist *headers = NULL; struct return_string s; CURLcode res;
char *bracket0,*bracket1,*retstr,*databuf = 0; long len; int32_t flag=0,specialcase,numretries; double starttime;
bitcoind_init();
numretries = 0;
if ( url[0] == 0 )
strcpy(url,"http://127.0.0.1:7776");
try_again:
if ( curl_handle == 0 )
{
curl_handle = curl_easy_init();
flag = 1;
}
if ( retstrp != 0 )
*retstrp = 0;
starttime = OS_milliseconds();
headers = curl_slist_append(0,"Expect:");
curl_easy_setopt(curl_handle,CURLOPT_USERAGENT,"mozilla/4.0");//"Mozilla/4.0 (compatible; )");
curl_easy_setopt(curl_handle,CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl_handle,CURLOPT_URL, url);
if ( (0) )
{
init_string(&s);
curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTION, (void *)accumulate); // send all data to this function
curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA, &s); // we pass our 's' struct to the callback
}
else
{
memset(&chunk,0,sizeof(chunk));
curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);
curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA,(void *)&chunk);
}
curl_easy_setopt(curl_handle,CURLOPT_NOSIGNAL, 1L); // supposed to fix "Alarm clock" and long jump crash
curl_easy_setopt(curl_handle,CURLOPT_NOPROGRESS, 1L); // no progress callback
if ( timeout > 0 )
{
if ( bitcoind_RPC_inittime != 0 )
{
#ifndef _WIN32
curl_easy_setopt(curl_handle,CURLOPT_TIMEOUT,1);
#else
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, timeout*100);
#endif
} else curl_easy_setopt(curl_handle,CURLOPT_TIMEOUT,timeout); // causes problems with iguana timeouts
}
if ( strncmp(url,"https",5) == 0 )
{
curl_easy_setopt(curl_handle,CURLOPT_SSL_VERIFYPEER,0);
curl_easy_setopt(curl_handle,CURLOPT_SSL_VERIFYHOST,0);
}
if ( userpass != 0 )
curl_easy_setopt(curl_handle,CURLOPT_USERPWD, userpass);
databuf = 0;
if ( params != 0 )
{
if ( command != 0 )
{
len = strlen(params);
if ( len > 0 && params[0] == '[' && params[len-1] == ']' ) {
bracket0 = bracket1 = (char *)"";
}
else
{
bracket0 = (char *)"[";
bracket1 = (char *)"]";
}
char agentstr[64];
databuf = (char *)malloc(256 + strlen(command) + strlen(params));
if ( debugstr[0] != 0 )
sprintf(agentstr,"\"agent\":\"%s\",",debugstr);
else agentstr[0] = 0;
sprintf(databuf,"{\"id\":\"jl777\",%s\"method\":\"%s\",\"params\":%s%s%s}",agentstr,command,bracket0,params,bracket1);
//printf("url.(%s) userpass.(%s) databuf.(%s)\n",url,userpass,databuf);
//
} //else if ( specialcase != 0 ) fprintf(stderr,"databuf.(%s)\n",params);
curl_easy_setopt(curl_handle,CURLOPT_POST,1L);
if ( databuf != 0 )
curl_easy_setopt(curl_handle,CURLOPT_POSTFIELDS,databuf);
else curl_easy_setopt(curl_handle,CURLOPT_POSTFIELDS,params);
}
res = curl_easy_perform(curl_handle);
curl_slist_free_all(headers);
if ( databuf != 0 ) // clean up temporary buffer
{
free(databuf);
databuf = 0;
}
if ( flag != 0 )
{
curl_easy_cleanup(curl_handle);
curl_handle = 0;
}
retstr = chunk.memory; // retstr = s.ptr;
if ( res != CURLE_OK )
{
numretries++;
if ( timeout != 0 )
{
//printf("<<<<<<<<<<< bitcoind_RPC.(%s): BTCD.%s timeout params.(%s) s.ptr.(%s) err.%d\n",url,command,params,retstr,res);
free(retstr);
return(0);
}
else if ( numretries >= 4 )
{
printf( "curl_easy_perform() failed: %s %s.(%s %s), retries: %d\n",curl_easy_strerror(res),debugstr,url,command,numretries);
//printf("Maximum number of retries exceeded!\n");
free(retstr);
return(0);
}
free(retstr);
sleep((1<<numretries));
goto try_again;
}
else
{
if ( command != 0 )
{
count++;
elapsedsum += (OS_milliseconds() - starttime);
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);
if ( retstrp != 0 )
{
*retstrp = retstr;
return(retstr);
}
//printf("%s <- %s\n",url,command);
return(post_process_bitcoind_RPC(debugstr,command,retstr,params));
}
else
{
if ( (0) && specialcase != 0 )
fprintf(stderr,"<<<<<<<<<<< bitcoind_RPC: BTCD.(%s) -> (%s)\n",params,retstr);
count2++;
elapsedsum2 += (OS_milliseconds() - starttime);
if ( (count2 % 10000) == 0)
printf("%d: ave %9.6f | elapsed %.3f millis | NXT calls.(%s) cmd.(%s)\n",count2,elapsedsum2/count2,(double)(OS_milliseconds() - starttime),url,command);
return(retstr);
}
}
}
/************************************************************************
*
* Initialize the string handler so that it is thread safe

2
iguana/coins/rfox_7776

@ -0,0 +1,2 @@
curl --url "http://127.0.0.1:7776" --data "{\"conf\":\"RFOX.conf\",\"path\":\"${HOME#"/"}/.komodo/RFOX\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"RFOX\",\"name\":\"RFOX\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"4cad0644\",\"p2p\":32268,\"rpc\":32269,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"78.47.196.146\"}"

2
iguana/coins/vrsc_7776

@ -0,0 +1,2 @@
curl --url "http://127.0.0.1:7776" --data "{\"conf\":\"VRSC.conf\",\"path\":\"${HOME#"/"}/.komodo/VRSC\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"VRSC\",\"name\":\"Verus\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"91db4d20\",\"p2p\":27485,\"rpc\":27486,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"54.39.23.248\"}"

1
iguana/coins/zilla_7776

@ -0,0 +1 @@
curl --url "http://127.0.0.1:7776" --data "{\"conf\":\"ZILLA.conf\",\"path\":\"${HOME#"/"}/.komodo/ZILLA\",\"unitval\":\"20\",\"zcash\":1,\"RELAY\":-1,\"VALIDATE\":0,\"prefetchlag\":-1,\"poll\":100,\"active\":1,\"agent\":\"iguana\",\"method\":\"addcoin\",\"startpend\":4,\"endpend\":4,\"services\":129,\"maxpeers\":8,\"newcoin\":\"ZILLA\",\"name\":\"ZILLA\",\"hasheaders\":1,\"useaddmultisig\":0,\"netmagic\":\"91db4d20\",\"p2p\":10040,\"rpc\":10041,\"pubval\":60,\"p2shval\":85,\"wifval\":188,\"txfee_satoshis\":\"10000\",\"isPoS\":0,\"minoutput\":10000,\"minconfirms\":2,\"genesishash\":\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\",\"protover\":170002,\"genesisblock\":\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\",\"debug\":0,\"seedipaddr\":\"54.39.23.248\"}"

2
iguana/dpow/dpow_fsm.c

@ -235,7 +235,7 @@ bits256 dpow_calcMoM(uint32_t *MoMdepthp,struct supernet_info *myinfo,struct igu
bits256 MoM; cJSON *MoMjson,*infojson; int32_t prevMoMheight;
*MoMdepthp = 0;
memset(MoM.bytes,0,sizeof(MoM));
if ( strcmp(coin->symbol,"GAME") == 0 ) // 80 byte OP_RETURN limit
if ( strcmp(coin->symbol,"GAME") == 0 || strcmp(coin->symbol,"HUSH") == 0 ) // 80 byte OP_RETURN limit
return(MoM);
if ( (infojson= dpow_getinfo(myinfo,coin)) != 0 )
{

2
iguana/dpow/dpow_tx.c

@ -329,7 +329,7 @@ bits256 dpow_notarytx(struct supernet_info *myinfo,char *signedtx,int32_t *numsi
memcpy(&serialized[len],sig,siglen);
len += siglen;
numsigs++;
} else printf("Missing sig from k.%d\n",k);
} else printf("%s -> %s src_or_dest.%d Missing sig from k.%d\n",bp->srccoin->symbol,bp->destcoin->symbol,src_or_dest,k);
} else serialized[len++] = 0;
len += iguana_rwnum(1,&serialized[len],sizeof(sequenceid),&sequenceid);
//printf("height.%d mod.%d VINI.%d <- i.%d j.%d\n",height,height % numnotaries,m,i,j);

21
iguana/exchanges/LP_bitcoin.c

@ -2011,16 +2011,25 @@ int32_t bitcoin_p2shscript(uint8_t *script,int32_t n,const uint8_t *p2shscript,c
return(n);
}
char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params)
char *bitcoind_passthrut(char *coinstr,char *serverport,char *userpass,char *method,char *params,int32_t timeout)
{
if ( userpass[0] == 0 )
return(clonestr("{\"error\":\"no rpcusername rpcpassword in coin.conf\"}"));
return(bitcoind_RPC(0,coinstr,serverport,userpass,method,params,4));
/*struct iguana_info *coin; char *retstr;
if ( (coin= LP_coinfind(coinstr)) != 0 )
{
portable_mutex_lock(&coin->curl_mutex);
retstr = bitcoind_RPCnew(coin->curl_handle,0,coinstr,serverport,userpass,method,params,timeout);
portable_mutex_unlock(&coin->curl_mutex);
return(retstr);
}*/
return(bitcoind_RPC(0,coinstr,serverport,userpass,method,params,timeout));
}
char *bitcoind_passthrut(char *coinstr,char *serverport,char *userpass,char *method,char *params,int32_t timeout)
char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *method,char *params)
{
return(bitcoind_RPC(0,coinstr,serverport,userpass,method,params,timeout));
if ( userpass[0] == 0 )
return(clonestr("{\"error\":\"no rpcusername rpcpassword in coin.conf\"}"));
return(bitcoind_passthrut(coinstr,serverport,userpass,method,params,4));
//return(bitcoind_RPC(0,coinstr,serverport,userpass,method,params,4));
}
bits256 bits256_calcaddrhash(char *symbol,uint8_t *serialized,int32_t len)

3
iguana/exchanges/LP_coins.c

@ -373,6 +373,7 @@ struct iguana_info *LP_coinadd(struct iguana_info *cdata)
return(coin);
}
void *curl_easy_init();
uint16_t LP_coininit(struct iguana_info *coin,char *symbol,char *name,char *assetname,int32_t isPoS,uint16_t port,uint8_t pubtype,uint8_t p2shtype,uint8_t wiftype,uint64_t txfee,double estimatedrate,int32_t longestchain,uint8_t wiftaddr,uint8_t taddr,uint16_t busport,char *confpath)
{
static void *ctx;
@ -434,6 +435,8 @@ uint16_t LP_coininit(struct iguana_info *coin,char *symbol,char *name,char *asse
coin->zcash = LP_IS_BITCOINCASH;
//printf("set coin.%s <- LP_IS_BITCOINCASH %d\n",symbol,coin->zcash);
}
coin->curl_handle = curl_easy_init();
portable_mutex_init(&coin->curl_mutex);
return(port);
}

39
iguana/exchanges/LP_commands.c

@ -173,6 +173,7 @@ unlockedspend(coin, txid)\n\
opreturndecrypt(coin, txid, passphrase)\n\
getendpoint(port=5555)\n\
getfee(coin)\n\
mpnet(onoff)\n\
sleep(seconds=60)\n\
listtransactions(coin, address, count=10, skip=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\"}"));
}
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 )
{
int32_t err,mode; uint16_t wsport = 5555; char endpoint[64],bindpoint[64];
@ -486,14 +493,14 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
double price,bid,ask;
if ( strcmp(method,"getprice") == 0 )
{
ask = LP_price(base,rel);
if ( (bid= LP_price(rel,base)) > SMALLVAL )
ask = LP_price(1,base,rel);
if ( (bid= LP_price(1,rel,base)) > SMALLVAL )
bid = 1./bid;
}
else
{
ask = LP_getmyprice(base,rel);
if ( (bid= LP_getmyprice(rel,base)) > SMALLVAL )
ask = LP_getmyprice(1,base,rel);
if ( (bid= LP_getmyprice(1,rel,base)) > SMALLVAL )
bid = 1./bid;
}
price = _pairaved(bid,ask);
@ -507,24 +514,24 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
jaddnum(retjson,"price",price);
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 )
return(clonestr("{\"error\":\"at least one of coins disabled\"}"));
price = jdouble(argjson,"price");
if ( strcmp(method,"setprice") == 0 )
{
if ( LP_mypriceset(&changed,base,rel,price) < 0 )
if ( LP_mypriceset(1,&changed,base,rel,price) < 0 )
return(clonestr("{\"error\":\"couldnt set price\"}"));
//else if ( LP_mypriceset(&changed,rel,base,1./price) < 0 )
//else if ( LP_mypriceset(1,&changed,rel,base,1./price) < 0 )
// return(clonestr("{\"error\":\"couldnt set price\"}"));
else if ( price == 0. || jobj(argjson,"broadcast") == 0 || jint(argjson,"broadcast") != 0 )
return(LP_pricepings(ctx,myipaddr,LP_mypubsock,base,rel,price * LP_profitratio));
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 )
{
if ( LP_myprice(&bid,&ask,base,rel) > SMALLVAL )
if ( LP_myprice(1,&bid,&ask,base,rel) > SMALLVAL )
{
retjson = cJSON_CreateObject();
jaddstr(retjson,"base",base);
@ -545,7 +552,7 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
} else vol = jdouble(argjson,"relvolume");
if ( price > SMALLVAL )
{
return(LP_autobuy(ctx,fomo,myipaddr,pubsock,base,rel,price,vol,jint(argjson,"timeout"),jint(argjson,"duration"),jstr(argjson,"gui"),juint(argjson,"nonce"),jbits256(argjson,"destpubkey"),0,jstr(argjson,"uuid")));
return(LP_autobuy(ctx,fomo,myipaddr,pubsock,base,rel,price,vol,jint(argjson,"timeout"),jint(argjson,"duration"),jstr(argjson,"gui"),juint(argjson,"nonce"),jbits256(argjson,"destpubkey"),0,jstr(argjson,"uuid"),jint(argjson,"fill"),jint(argjson,"gtc")));
} else return(clonestr("{\"error\":\"no price set\"}"));
}
else if ( strcmp(method,"sell") == 0 )
@ -559,7 +566,7 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
} else vol = jdouble(argjson,"basevolume");
if ( price > SMALLVAL )
{
return(LP_autobuy(ctx,fomo,myipaddr,pubsock,rel,base,1./price,vol,jint(argjson,"timeout"),jint(argjson,"duration"),jstr(argjson,"gui"),juint(argjson,"nonce"),jbits256(argjson,"destpubkey"),0,jstr(argjson,"uuid")));
return(LP_autobuy(ctx,fomo,myipaddr,pubsock,rel,base,1./price,vol,jint(argjson,"timeout"),jint(argjson,"duration"),jstr(argjson,"gui"),juint(argjson,"nonce"),jbits256(argjson,"destpubkey"),0,jstr(argjson,"uuid"),jint(argjson,"fill"),jint(argjson,"gtc")));
} else return(clonestr("{\"error\":\"no price set\"}"));
}
}
@ -801,7 +808,7 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
return(jprint(retjson,1));
}
else if ( strcmp(method,"myprices") == 0 )
return(LP_myprices());
return(LP_myprices(1));
else if ( strcmp(method,"trust") == 0 )
return(LP_pubkey_trustset(jbits256(argjson,"pubkey"),jint(argjson,"trust")));
else if ( strcmp(method,"trusted") == 0 )
@ -862,14 +869,14 @@ jpg(srcfile, destfile, power2=7, password, data="", required, ind=0)\n\
double price,bid,ask;
if ( strcmp(method,"getprice") == 0 )
{
ask = LP_price(base,rel);
if ( (bid= LP_price(rel,base)) > SMALLVAL )
ask = LP_price(1,base,rel);
if ( (bid= LP_price(1,rel,base)) > SMALLVAL )
bid = 1./bid;
}
else
{
ask = LP_getmyprice(base,rel);
if ( (bid= LP_getmyprice(rel,base)) > SMALLVAL )
ask = LP_getmyprice(1,base,rel);
if ( (bid= LP_getmyprice(1,rel,base)) > SMALLVAL )
bid = 1./bid;
}
price = _pairaved(bid,ask);

18
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_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_RESERVETIME (LP_AUTOTRADE_TIMEOUT * 3)
#define ELECTRUM_TIMEOUT 13
@ -220,7 +220,7 @@ struct basilisk_request
bits256 desthash;
char src[68],dest[68];
uint64_t destamount;
int32_t optionhours,DEXselector;
uint32_t optionhours,DEXselector;
};
struct basilisk_rawtx
@ -303,7 +303,7 @@ struct iguana_info
{
UT_hash_handle hh;
portable_mutex_t txmutex,addrmutex,addressutxo_mutex; struct LP_transaction *transactions; struct LP_address *addresses;
uint64_t txfee;
uint64_t txfee,do_autofill_merge;
int32_t numutxos,notarized,longestchain,firstrefht,firstscanht,lastscanht,height; uint16_t busport,did_addrutxo_reset;
uint32_t txversion,dPoWtime,lastautosplit,lastresetutxo,loadedcache,electrumlist,lastunspent,importedprivkey,lastpushtime,lastutxosync,addr_listunspent_requested,lastutxos,updaterate,counter,inactive,lastmempool,lastgetinfo,ratetime,heighttime,lastmonitor,obooktime;
uint8_t pubtype,p2shtype,isPoS,wiftype,wiftaddr,taddr,noimportprivkey_flag,userconfirms,isassetchain,maxconfirms;
@ -311,9 +311,10 @@ struct iguana_info
// portfolio
double price_kmd,force,perc,goal,goalperc,relvolume,rate;
void *electrum; void *ctx;
uint64_t maxamount,kmd_equiv,balanceA,balanceB,valuesumA,valuesumB;
uint64_t maxamount,kmd_equiv,balanceA,balanceB,valuesumA,valuesumB,fillsatoshis;
uint8_t pubkey33[33],zcash;
int32_t privkeydepth;
int32_t privkeydepth,bobfillheight;
void *curl_handle; portable_mutex_t curl_mutex;
bits256 cachedtxid,notarizationtxid; uint8_t *cachedtxiddata; int32_t cachedtxidlen;
bits256 cachedmerkle,notarizedhash; int32_t cachedmerkleheight;
};
@ -377,9 +378,10 @@ struct LP_quoteinfo
{
struct basilisk_request R;
bits256 srchash,desthash,txid,txid2,desttxid,feetxid,privkey;
double maxprice;
int64_t othercredits;
uint64_t satoshis,txfee,destsatoshis,desttxfee,aliceid;
uint32_t timestamp,quotetime,tradeid;
uint32_t timestamp,quotetime,tradeid,gtc,fill,mpnet;
int32_t vout,vout2,destvout,feevout,pair;
char srccoin[65],coinaddr[64],destcoin[65],destaddr[64],gui[64],etomicsrc[65],etomicdest[65],uuidstr[65];
};
@ -427,7 +429,7 @@ struct LP_swapstats
struct LP_pubswap { struct LP_pubswap *next,*prev; struct LP_swapstats *swap; };
#define LP_MAXPRICEINFOS 256
#define LP_MAXPRICEINFOS 255
struct LP_pubkey_info
{
UT_hash_handle hh;
@ -531,6 +533,7 @@ void HashKeccak(uint8_t *hash,void *data,size_t len);
void test_validate(struct iguana_info *coin,char *signedtx);
void LP_instantdex_depositadd(char *coinaddr,bits256 txid);
int64_t LP_instantdex_creditcalc(struct iguana_info *coin,int32_t dispflag,bits256 txid,char *refaddr,char *origcoinaddr);
char *LP_autofillbob(struct iguana_info *coin,uint64_t satoshis);
void LP_ports(uint16_t *pullportp,uint16_t *pubportp,uint16_t *busportp,uint16_t netid);
int32_t LP_destaddr(char *destaddr,cJSON *item);
int32_t LP_waitmempool(char *symbol,char *coinaddr,bits256 txid,int32_t vout,int32_t duration);
@ -580,6 +583,7 @@ int32_t LP_trades_canceluuid(char *uuidstr);
int _decreasing_uint64(const void *a,const void *b);
int32_t LP_alice_eligible(uint32_t quotetime);
int32_t LP_is_slowcoin(char *symbol);
void LP_alicequery_clear();
void LP_listunspent_query(char *symbol,char *coinaddr);
int32_t bitcoin_priv2wif(char *symbol,uint8_t wiftaddr,char *wifstr,bits256 privkey,uint8_t addrtype);

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);
}
}

46
iguana/exchanges/LP_nativeDEX.c

@ -90,7 +90,7 @@ void LP_millistats_update(struct LP_millistats *mp)
#include "LP_etomic.h"
#endif
portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex,LP_portfoliomutex,LP_electrummutex,LP_butxomutex,LP_reservedmutex,LP_nanorecvsmutex,LP_tradebotsmutex,LP_gcmutex,LP_inusemutex,LP_cJSONmutex,LP_logmutex,LP_statslogmutex,LP_tradesmutex,LP_commandQmutex,LP_blockinit_mutex,LP_pendswap_mutex,LP_listmutex;
portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex,LP_portfoliomutex,LP_electrummutex,LP_butxomutex,LP_reservedmutex,LP_nanorecvsmutex,LP_tradebotsmutex,LP_gcmutex,LP_inusemutex,LP_cJSONmutex,LP_logmutex,LP_statslogmutex,LP_tradesmutex,LP_commandQmutex,LP_blockinit_mutex,LP_pendswap_mutex,LP_listmutex,LP_gtcmutex;
int32_t LP_canbind;
char *Broadcaststr,*Reserved_msgs[2][1000];
int32_t num_Reserved_msgs[2],max_Reserved_msgs[2];
@ -120,7 +120,7 @@ struct LP_globals
uint64_t LP_skipstatus[10000];
uint16_t netid;
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;
char seednode[64],USERPASS[65],USERPASS_WIFSTR[64],LP_myrmd160str[41],gui[65],LP_NXTaddr[64];
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_coins.c"
#include "LP_rpc.c"
#include "LP_mpnet.c"
#include "LP_NXT.c"
#include "LP_cache.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;
if ( jobj(argjson,"result") != 0 || jobj(argjson,"error") != 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 )
{
@ -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 )
{
//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 )
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);
}
portable_mutex_unlock(&LP_nanorecvsmutex);
//if ( G.mpnet != 0 )
LP_mpnet_check(ctx,origipaddr,LP_mypubsock);
return(nonz);
}
@ -513,21 +516,21 @@ void command_rpcloop(void *ctx)
void LP_coinsloop(void *_coins)
{
static int32_t didfilescreate;
struct LP_address *ap=0; struct LP_transaction *tx; cJSON *retjson; struct LP_address_utxo *up,*tmp; struct iguana_info *coin,*ctmp; char str[65]; struct electrum_info *ep,*backupep=0; bits256 zero; int32_t notarized,oldht,j,nonz; char *coins = _coins;
struct LP_address *ap=0; struct LP_transaction *tx; cJSON *retjson; struct LP_address_utxo *up,*tmp; struct iguana_info *coin,*ctmp; char str[65],*retstr,*hexstr,*txidstr; struct electrum_info *ep,*backupep=0; bits256 zero; int32_t notarized,oldht,j,nonz; char *coins = _coins;
if ( strcmp("BTC",coins) == 0 )
{
strcpy(LP_coinsloopBTC_stats.name,"BTC coin loop");
LP_coinsloopBTC_stats.threshold = 20000.;
LP_coinsloopBTC_stats.threshold = 200000.;
}
else if ( strcmp("KMD",coins) == 0 )
{
strcpy(LP_coinsloopKMD_stats.name,"KMD coin loop");
LP_coinsloopKMD_stats.threshold = 10000.;
LP_coinsloopKMD_stats.threshold = 100000.;
}
else
{
strcpy(LP_coinsloop_stats.name,"other coins loop");
LP_coinsloop_stats.threshold = 5000.;
LP_coinsloop_stats.threshold = 50000.;
}
while ( LP_STOP_RECEIVED == 0 )
{
@ -576,6 +579,29 @@ void LP_coinsloop(void *_coins)
LP_address_utxo_reset(&num,coin);
coin->did_addrutxo_reset = 1;
}
//free_json(LP_address_balance(coin,coin->smartaddr,1)); expensive invoking gettxout
if ( coin->do_autofill_merge != 0 )
{
if ( (retstr= LP_autofillbob(coin,coin->do_autofill_merge*1.02)) != 0 )
{
if ( (retjson= cJSON_Parse(retstr)) != 0 )
{
if ( (hexstr= jstr(retjson,"hex")) != 0 )
{
if ( (txidstr= LP_sendrawtransaction(coin->symbol,hexstr,0)) != 0 )
{
printf("autofill created %s\n",txidstr);
free(txidstr);
coin->fillsatoshis = coin->do_autofill_merge;
coin->do_autofill_merge = 0;
coin->bobfillheight = LP_getheight(&notarized,coin);
}
}
free_json(retjson);
}
free(retstr);
}
}
if ( coin->longestchain == 1 ) // special init value
coin->longestchain = LP_getheight(&notarized,coin);
if ( (ep= coin->electrum) != 0 )
@ -656,6 +682,7 @@ void LP_coinsloop(void *_coins)
continue;
}
//if ( strcmp(coin->symbol,"BTC") != 0 && strcmp(coin->symbol,"KMD") != 0 ) // SPV as backup
if ( coin->lastscanht < coin->longestchain )
{
nonz++;
if ( strcmp("BTC",coins) == 0 )//&& coin->lastscanht < coin->longestchain-3 )
@ -1110,6 +1137,7 @@ void LP_swapsloop(void *ctx)
sleep(6);
}
}
LP_gtc_iteration(ctx,LP_myipaddr,LP_mypubsock);
}
}
@ -1473,6 +1501,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
portable_mutex_init(&LP_blockinit_mutex);
portable_mutex_init(&LP_pendswap_mutex);
portable_mutex_init(&LP_listmutex);
portable_mutex_init(&LP_gtcmutex);
myipaddr = clonestr("127.0.0.1");
#ifndef _WIN32
#ifndef FROM_JS
@ -1621,6 +1650,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu
int32_t nonz,didremote=0;
LP_statslog_parse();
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 )
{
nonz = 0;

334
iguana/exchanges/LP_ordermatch.c

@ -18,6 +18,14 @@
// LP_ordermatch.c
// marketmaker
//
struct LP_gtcorder
{
struct LP_gtcorder *next,*prev;
struct LP_quoteinfo Q;
uint32_t cancelled,pending;
} *GTCorders;
struct LP_quoteinfo LP_Alicequery,LP_Alicereserved;
double LP_Alicemaxprice;
bits256 LP_Alicedestpubkey,LP_bobs_reserved;
@ -494,7 +502,7 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
}
if ( bits256_nonz(privkey) != 0 && bits256_cmp(G.LP_mypub25519,qp->srchash) == 0 )
{
LP_requestinit(&qp->R,qp->srchash,qp->desthash,base,qp->satoshis-qp->txfee,rel,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector);
LP_requestinit(&qp->R,qp->srchash,qp->desthash,base,qp->satoshis-qp->txfee,rel,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector,qp->fill,qp->gtc);
dtrust = LP_dynamictrust(qp->othercredits,qp->desthash,LP_kmdvalue(qp->destcoin,qp->destsatoshis));
if ( (swap= LP_swapinit(1,0,privkey,&qp->R,qp,dtrust > 0)) == 0 )
{
@ -517,27 +525,35 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
LP_importaddress(qp->destcoin,qp->destaddr);
LP_otheraddress(qp->srccoin,otheraddr,qp->destcoin,qp->destaddr);
LP_importaddress(qp->srccoin,otheraddr);
bits256 zero;
memset(zero.bytes,0,sizeof(zero));
for (i=0; i<1; i++)
{
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0));
break;
sleep(10);
if ( swap->received != 0 )
bits256 zero;
memset(zero.bytes,0,sizeof(zero));
for (i=0; i<1; i++)
{
printf("swap %u-%u has started t%u\n",swap->I.req.requestid,swap->I.req.quoteid,swap->received);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0));
break;
sleep(10);
if ( swap->received != 0 )
{
printf("swap %u-%u has started t%u\n",swap->I.req.requestid,swap->I.req.quoteid,swap->received);
break;
}
printf("bob tries %u-%u again i.%d\n",swap->I.req.requestid,swap->I.req.quoteid,i);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0));
}
printf("bob tries %u-%u again i.%d\n",swap->I.req.requestid,swap->I.req.quoteid,i);
sleep(1);
printf("send CONNECT for %u-%u\n",qp->R.requestid,qp->R.quoteid);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0));
if ( IPC_ENDPOINT >= 0 )
LP_queuecommand(0,jprint(reqjson,0),IPC_ENDPOINT,-1,0);
}
sleep(1);
printf("send CONNECT for %u-%u\n",qp->R.requestid,qp->R.quoteid);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0));
if ( IPC_ENDPOINT >= 0 )
LP_queuecommand(0,jprint(reqjson,1),IPC_ENDPOINT,-1,0);
else free_json(reqjson);
if ( qp->mpnet != 0 && qp->gtc == 0 )
{
char *msg = jprint(reqjson,0);
LP_mpnet_send(0,msg,1,qp->destaddr);
free(msg);
}
free_json(reqjson);
retval = 0;
}
else
@ -567,10 +583,60 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
return(retval);
}
void LP_gtc_iteration(void *ctx,char *myipaddr,int32_t mypubsock)
{
struct LP_gtcorder *gtc,*tmp; struct LP_quoteinfo *qp; uint64_t destvalue,destvalue2; uint32_t oldest = 0;
if ( Alice_expiration != 0 )
return;
DL_FOREACH_SAFE(GTCorders,gtc,tmp)
{
qp = &gtc->Q;
if ( gtc->cancelled == 0 && (oldest == 0 || gtc->pending < oldest) )
oldest = gtc->pending;
}
DL_FOREACH_SAFE(GTCorders,gtc,tmp)
{
qp = &gtc->Q;
if ( gtc->cancelled == 0 && LP_iseligible(&destvalue,&destvalue2,0,qp->destcoin,qp->desttxid,qp->destvout,qp->destsatoshis,qp->feetxid,qp->feevout) == 0 )
{
gtc->cancelled = (uint32_t)time(NULL);
LP_failedmsg(qp->R.requestid,qp->R.quoteid,-9997,qp->uuidstr);
}
if ( gtc->cancelled != 0 )
{
portable_mutex_lock(&LP_gtcmutex);
DL_DELETE(GTCorders,gtc);
free(gtc);
portable_mutex_unlock(&LP_gtcmutex);
}
else
{
if ( gtc->pending <= oldest+60 && time(NULL) > gtc->pending+LP_AUTOTRADE_TIMEOUT*10 )
{
gtc->pending = qp->timestamp = (uint32_t)time(NULL);
LP_query(ctx,myipaddr,mypubsock,"request",qp);
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->Q.maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc);
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)
{
double price;
price = 0.;
struct LP_gtcorder *gtc;
memset(qp->txid.bytes,0,sizeof(qp->txid));
qp->txid2 = qp->txid;
qp->aliceid = LP_aliceid_calc(qp->desttxid,qp->destvout,qp->feetxid,qp->feevout);
@ -578,9 +644,25 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q
qp->tradeid = LP_rand();
qp->srchash = destpubkey;
strncpy(qp->uuidstr,uuidstr,sizeof(qp->uuidstr)-1);
LP_query(ctx,myipaddr,mypubsock,"request",qp);
LP_Alicequery = *qp, LP_Alicemaxprice = maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = destpubkey;
char str[65]; printf("LP_trade %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s\n",qp->srccoin,qp->destcoin,dstr(qp->satoshis),dstr(qp->destsatoshis),bits256_str(str,LP_Alicedestpubkey),maxprice,qp->etomicdest,qp->uuidstr);
qp->maxprice = maxprice;
qp->timestamp = (uint32_t)time(NULL);
if ( qp->gtc != 0 )
{
strcpy(&qp->uuidstr[strlen(qp->uuidstr)-6],"cccccc");
LP_gtc_addorder(qp);
}
{
LP_query(ctx,myipaddr,mypubsock,"request",qp);
LP_Alicequery = *qp, LP_Alicemaxprice = qp->maxprice, Alice_expiration = qp->timestamp + timeout, LP_Alicedestpubkey = qp->srchash;
}
if ( qp->gtc == 0 )
{
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),qp->maxprice,qp->etomicdest,qp->uuidstr,qp->fill,qp->gtc);
return(LP_recent_swaps(0,uuidstr));
}
@ -628,16 +710,45 @@ char *LP_cancel_order(char *uuidstr)
int32_t num = 0; cJSON *retjson;
if ( uuidstr != 0 )
{
num = LP_trades_canceluuid(uuidstr);
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
jaddnum(retjson,"numentries",num);
if ( strcmp(LP_Alicequery.uuidstr,uuidstr) == 0 )
if ( uuidstr[0] == 'G' )
{
LP_failedmsg(LP_Alicequery.R.requestid,LP_Alicequery.R.quoteid,-9998,LP_Alicequery.uuidstr);
LP_alicequery_clear();
jaddstr(retjson,"status","uuid canceled");
} else jaddstr(retjson,"status","will stop trade negotiation, but if swap started it wont cancel");
struct LP_gtcorder *gtc,*tmp;
DL_FOREACH_SAFE(GTCorders,gtc,tmp)
{
if ( strcmp(gtc->Q.uuidstr,uuidstr) == 0 )
{
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
jaddstr(retjson,"cancelled",uuidstr);
jaddnum(retjson,"pending",gtc->pending);
if ( gtc->cancelled == 0 )
{
gtc->cancelled = (uint32_t)time(NULL);
jaddstr(retjson,"status","uuid canceled");
LP_failedmsg(gtc->Q.R.requestid,gtc->Q.R.quoteid,-9997,gtc->Q.uuidstr);
}
else
{
jaddstr(retjson,"status","uuid already canceled");
LP_failedmsg(gtc->Q.R.requestid,gtc->Q.R.quoteid,-9996,gtc->Q.uuidstr);
}
}
}
return(clonestr("{\"error\":\"gtc uuid not found\"}"));
}
else
{
num = LP_trades_canceluuid(uuidstr);
retjson = cJSON_CreateObject();
jaddstr(retjson,"result","success");
jaddnum(retjson,"numentries",num);
if ( strcmp(LP_Alicequery.uuidstr,uuidstr) == 0 )
{
LP_failedmsg(LP_Alicequery.R.requestid,LP_Alicequery.R.quoteid,-9998,LP_Alicequery.uuidstr);
LP_alicequery_clear();
jaddstr(retjson,"status","uuid canceled");
} else jaddstr(retjson,"status","will stop trade negotiation, but if swap started it wont cancel");
}
return(jprint(retjson,1));
}
return(clonestr("{\"error\":\"uuid not cancellable\"}"));
@ -645,16 +756,22 @@ char *LP_cancel_order(char *uuidstr)
char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice
{
cJSON *retjson; char otheraddr[64],*msg; double bid,ask,price,qprice; int32_t pairsock = -1; int32_t DEXselector = 0; struct LP_utxoinfo *autxo,A,B,*butxo; struct basilisk_swap *swap; struct iguana_info *coin;
cJSON *retjson; char otheraddr[64],*msg; double bid,ask,price,qprice; int32_t changed,pairsock = -1; int32_t DEXselector = 0; struct LP_utxoinfo *autxo,A,B,*butxo; struct basilisk_swap *swap; struct iguana_info *coin;
if ( bits256_cmp(qp->desthash,G.LP_mypub25519) != 0 )
{
LP_aliceid(qp->tradeid,qp->aliceid,"error1",0,0);
LP_failedmsg(qp->R.requestid,qp->R.quoteid,-4000,qp->uuidstr);
return(clonestr("{\"result\",\"update stats\"}"));
}
printf("CONNECTED numpending.%d tradeid.%u requestid.%u quoteid.%u pairstr.%s\n",G.LP_pendingswaps,qp->tradeid,qp->R.requestid,qp->R.quoteid,pairstr!=0?pairstr:"");
LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis-qp->txfee,qp->destcoin,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector);
printf("CONNECTED mpnet.%d fill.%d gtc.%d numpending.%d tradeid.%u requestid.%u quoteid.%u pairstr.%s\n",qp->mpnet,qp->fill,qp->gtc,G.LP_pendingswaps,qp->tradeid,qp->R.requestid,qp->R.quoteid,pairstr!=0?pairstr:"");
LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis-qp->txfee,qp->destcoin,qp->destsatoshis-qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector,qp->fill,qp->gtc);
//printf("calculated requestid.%u quoteid.%u\n",qp->R.requestid,qp->R.quoteid);
if ( LP_Alicequery.srccoin[0] != 0 && LP_Alicequery.destcoin[0] != 0 )
{
LP_mypriceset(0,&changed,LP_Alicequery.destcoin,LP_Alicequery.srccoin,0.);
LP_mypriceset(0,&changed,LP_Alicequery.srccoin,LP_Alicequery.destcoin,0.);
}
LP_alicequery_clear();
memset(&LP_Alicereserved,0,sizeof(LP_Alicereserved));
LP_aliceid(qp->tradeid,qp->aliceid,"connected",qp->R.requestid,qp->R.quoteid);
autxo = &A;
@ -671,7 +788,7 @@ char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice
printf("quote %s/%s validate error %.0f\n",qp->srccoin,qp->destcoin,qprice);
return(clonestr("{\"error\":\"quote validation error\"}"));
}
if ( LP_myprice(&bid,&ask,qp->srccoin,qp->destcoin) <= SMALLVAL || bid <= SMALLVAL )
if ( LP_myprice(0,&bid,&ask,qp->srccoin,qp->destcoin) <= SMALLVAL || bid <= SMALLVAL )
{
printf("this node has no price for %s/%s (%.8f %.8f)\n",qp->destcoin,qp->srccoin,bid,ask);
LP_availableset(qp->desttxid,qp->vout);
@ -862,7 +979,7 @@ void LP_reserved(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo
double LP_trades_bobprice(double *bidp,double *askp,struct LP_quoteinfo *qp)
{
double price; struct iguana_info *coin; char str[65];
price = LP_myprice(bidp,askp,qp->srccoin,qp->destcoin);
price = LP_myprice(1,bidp,askp,qp->srccoin,qp->destcoin);
if ( (coin= LP_coinfind(qp->srccoin)) == 0 || price <= SMALLVAL || *askp <= SMALLVAL )
{
//printf("this node has no price for %s/%s\n",qp->srccoin,qp->destcoin);
@ -916,10 +1033,11 @@ double LP_trades_pricevalidate(struct LP_quoteinfo *qp,struct iguana_info *coin,
struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp,char *pairstr)
{
double price=0.,p=0.,qprice,myprice,bestprice,range,bid,ask; struct iguana_info *coin,*othercoin; struct LP_utxoinfo A,B,*autxo,*butxo; cJSON *reqjson; char str[65]; struct LP_address_utxo *utxos[4096]; int32_t i,r,counter,max = (int32_t)(sizeof(utxos)/sizeof(*utxos));
int32_t voliters=10,priceiters=33;
double price=0.,p=0.,qprice,myprice,bestprice,range,bid,ask; uint64_t satoshis; struct iguana_info *coin,*othercoin; struct LP_utxoinfo A,B,*autxo,*butxo; cJSON *reqjson,*retjson; char str[65],*retstr,*txidstr,*hexstr; struct LP_address_utxo *utxos[4096]; int32_t i,j,notarized,r,num,counter,max = (int32_t)(sizeof(utxos)/sizeof(*utxos));
*newqp = *qp;
qp = newqp;
//printf("bob %s received REQUEST.(%s)\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32);
printf("bob %s received REQUEST.(%s) mpnet.%d fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32,qp->mpnet,qp->fill,qp->gtc);
if ( (coin= LP_coinfind(qp->srccoin)) == 0 || (othercoin= LP_coinfind(qp->destcoin)) == 0 )
return(0);
if ( (myprice= LP_trades_bobprice(&bid,&ask,qp)) == 0. )
@ -970,7 +1088,7 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
}
else
{
//printf("ignore as qprice %.8f vs myprice %.8f\n",qprice,myprice);
printf("%s/%s ignore as qprice %.8f vs myprice %.8f\n",qp->srccoin,qp->destcoin,qprice,myprice);
return(0);
}
//LP_RTmetrics_update(qp->srccoin,qp->destcoin);
@ -998,45 +1116,79 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
}
}
i = 0;
while ( i < 33 && price >= myprice )
{
if ( (butxo= LP_address_myutxopair(butxo,1,utxos,max,LP_coinfind(qp->srccoin),qp->coinaddr,qp->txfee,dstr(qp->destsatoshis),price,qp->desttxfee)) != 0 )
{
strcpy(qp->gui,G.gui);
strcpy(qp->coinaddr,coin->smartaddr);
qp->srchash = G.LP_mypub25519;
qp->txid = butxo->payment.txid;
qp->vout = butxo->payment.vout;
qp->txid2 = butxo->deposit.txid;
qp->vout2 = butxo->deposit.vout;
qp->satoshis = butxo->swap_satoshis;// + qp->txfee;
qp->quotetime = (uint32_t)time(NULL);
while ( i < priceiters && price >= myprice )
{
for (j=0; j<voliters; j++)
{
printf("priceiter.%d voliter.%d price %.8f vol %.8f\n",i,j,price,dstr(qp->destsatoshis));
if ( (butxo= LP_address_myutxopair(&B,1,utxos,max,LP_coinfind(qp->srccoin),qp->coinaddr,qp->txfee,dstr(qp->destsatoshis),price,qp->desttxfee)) != 0 )
{
strcpy(qp->gui,G.gui);
strcpy(qp->coinaddr,coin->smartaddr);
qp->srchash = G.LP_mypub25519;
qp->txid = butxo->payment.txid;
qp->vout = butxo->payment.vout;
qp->txid2 = butxo->deposit.txid;
qp->vout2 = butxo->deposit.vout;
qp->satoshis = butxo->swap_satoshis;// + qp->txfee;
qp->quotetime = (uint32_t)time(NULL);
break;
}
if ( qp->fill != 0 )
break;
qp->destsatoshis = (qp->destsatoshis * 2) / 3;
}
else
if ( butxo != 0 && j < voliters )
{
printf("i.%d cant find utxopair aliceid.%llu %s/%s %.8f -> relvol %.8f\n",i,(long long)qp->aliceid,qp->srccoin,qp->destcoin,dstr(LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee)),dstr(qp->destsatoshis));
return(0);
if ( qp->satoshis <= qp->txfee )
return(0);
p = (double)qp->destsatoshis / (qp->satoshis - qp->txfee);
if ( LP_trades_pricevalidate(qp,coin,p) < 0. )
{
if ( qp->fill != 0 )
return(0);
price *= 0.995;
i++;
continue;
}
if ( i == 0 && p < myprice )
{
price = qprice;
printf("reset price <- qprice %.8f\n",qprice);
}
else
{
if ( qprice >= p || qp->fill != 0 )
break;
price *= 0.995;
}
if ( qp->fill != 0 )
break;
i++;
}
if ( qp->satoshis <= qp->txfee )
return(0);
p = (double)qp->destsatoshis / (qp->satoshis - qp->txfee);
if ( LP_trades_pricevalidate(qp,coin,p) < 0. )
return(0);
if ( i == 0 && p < myprice )
else if ( qp->fill != 0 || i == priceiters )
{
price = qprice;
printf("reset price <- qprice %.8f\n",qprice);
printf("i.%d cant find utxopair aliceid.%llu %s/%s %.8f -> relvol %.8f txfee %.8f\n",i,(long long)qp->aliceid,qp->srccoin,qp->destcoin,dstr(LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee)),dstr(qp->destsatoshis),dstr(qp->txfee));
if ( qp->gtc != 0 && qp->fill != 0 && coin != 0 )
{
LP_address_utxo_reset(&num,coin);
satoshis = LP_basesatoshis(dstr(qp->destsatoshis),price,qp->txfee,qp->desttxfee) + 3*qp->txfee;
if ( LP_getheight(&notarized,coin) > coin->bobfillheight+3 && fabs((double)coin->fillsatoshis - satoshis)/satoshis > 0.1 )
{
printf("queue up do_autofill_merge %.8f\n",dstr(satoshis));
coin->do_autofill_merge = satoshis;
}
}
return(0);
}
else
{
if ( qprice >= p )
break;
price *= 0.995;
i++;
}
i++;
}
printf("%s/%s i.%d qprice %.8f myprice %.8f price %.8f [%.8f]\n",qp->srccoin,qp->destcoin,i,qprice,myprice,price,p);
if ( LP_allocated(qp->txid,qp->vout) == 0 && LP_allocated(qp->txid2,qp->vout2) == 0 )
printf("%s/%s i.%d j.%d qprice %.8f myprice %.8f price %.8f [%.8f]\n",qp->srccoin,qp->destcoin,i,j,qprice,myprice,price,p);
if ( butxo != 0 && bits256_nonz(qp->txid) != 0 && bits256_nonz(qp->txid2) != 0 && LP_allocated(qp->txid,qp->vout) == 0 && LP_allocated(qp->txid2,qp->vout2) == 0 )
{
//printf("found unallocated txids\n");
reqjson = LP_quotejson(qp);
@ -1047,10 +1199,18 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
jaddnum(reqjson,"quotetime",qp->quotetime);
jaddnum(reqjson,"pending",qp->timestamp + LP_RESERVETIME);
jaddstr(reqjson,"method","reserved");
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->desthash,jprint(reqjson,0));
bits256 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,qp->desthash,jprint(reqjson,0));
bits256 zero;
memset(zero.bytes,0,sizeof(zero));
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,jprint(reqjson,0));
}
if ( qp->mpnet != 0 && qp->gtc == 0 )
{
char *msg = jprint(reqjson,0);
LP_mpnet_send(0,msg,1,qp->destaddr);
free(msg);
}
free_json(reqjson);
//printf("Send RESERVED id.%llu\n",(long long)qp->aliceid);
return(qp);
@ -1061,7 +1221,7 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
struct LP_quoteinfo *LP_trades_gotreserved(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp)
{
char *retstr; double qprice;
//char str[65]; printf("alice %s received RESERVED.(%s) %.8f\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32,(double)qp->destsatoshis/(qp->satoshis+1));
char str[65]; printf("alice %s received RESERVED.(%s) %.8f mpnet.%d fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32,(double)qp->destsatoshis/(qp->satoshis+1),qp->mpnet,qp->fill,qp->gtc);
*newqp = *qp;
qp = newqp;
if ( (qprice= LP_trades_alicevalidate(ctx,qp)) > 0. )
@ -1103,7 +1263,7 @@ struct LP_quoteinfo *LP_trades_gotconnect(void *ctx,struct LP_quoteinfo *qp,stru
struct LP_quoteinfo *LP_trades_gotconnected(void *ctx,struct LP_quoteinfo *qp,struct LP_quoteinfo *newqp,char *pairstr)
{
char *retstr; int32_t changed; double val;
//char str[65]; printf("alice %s received CONNECTED.(%llu)\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid);
char str[65]; printf("alice %s received CONNECTED.(%llu) mpnet.%d fill.%d gtc.%d\n",bits256_str(str,G.LP_mypub25519),(long long)qp->aliceid,qp->mpnet,qp->fill,qp->gtc);
*newqp = *qp;
qp = newqp;
if ( (val= LP_trades_alicevalidate(ctx,qp)) > 0. )
@ -1112,7 +1272,7 @@ struct LP_quoteinfo *LP_trades_gotconnected(void *ctx,struct LP_quoteinfo *qp,st
LP_aliceid(qp->tradeid,qp->aliceid,"connected",0,0);
if ( (retstr= LP_connectedalice(qp,pairstr)) != 0 )
free(retstr);
LP_mypriceset(&changed,qp->destcoin,qp->srccoin,0.);
LP_mypriceset(0,&changed,qp->destcoin,qp->srccoin,0.);
LP_alicequery_clear();
return(qp);
} else LP_failedmsg(qp->R.requestid,qp->R.quoteid,val,qp->uuidstr);
@ -1367,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);
}
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;
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;
@ -1380,7 +1540,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
}
if ( Q.satoshis < Q.txfee )
return(1);
LP_requestinit(&Q.R,Q.srchash,Q.desthash,Q.srccoin,Q.satoshis-Q.txfee,Q.destcoin,Q.destsatoshis-Q.desttxfee,Q.timestamp,Q.quotetime,DEXselector);
LP_requestinit(&Q.R,Q.srchash,Q.desthash,Q.srccoin,Q.satoshis-Q.txfee,Q.destcoin,Q.destsatoshis-Q.desttxfee,Q.timestamp,Q.quotetime,DEXselector,Q.fill,Q.gtc);
rq = ((uint64_t)Q.R.requestid << 32) | Q.R.quoteid;
if ( Q.uuidstr[0] == 0 || (Q.timestamp > 0 && time(NULL) > Q.timestamp + LP_AUTOTRADE_TIMEOUT*20) ) // eat expired packets, some old timestamps floating about?
{
@ -1390,7 +1550,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
LP_tradecommand_log(argjson);
qprice = (double)Q.destsatoshis / (Q.satoshis - Q.txfee); //jdouble(argjson,"price");
//printf("%s\n",jprint(argjson,0));
printf("%-4d uuid.%32s %12s %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d\n",(uint32_t)time(NULL) % 3600,Q.uuidstr+32,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;
aliceid = j64bits(argjson,"aliceid");
if ( strcmp(method,"reserved") == 0 )
@ -1446,7 +1606,9 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
else LP_tradecommandQ(&Q,jstr(argjson,"pair"),LP_CONNECTED);
}
}
price = LP_myprice(&bid,&ask,Q.srccoin,Q.destcoin);
if ( strcmp(method,"request") == 0 || strcmp(method,"connect") == 0 ) // bob
price = LP_myprice(1,&bid,&ask,Q.srccoin,Q.destcoin);
else price = LP_myprice(0,&bid,&ask,Q.srccoin,Q.destcoin); // alice
if ( (coin= LP_coinfind(Q.srccoin)) == 0 || coin->inactive != 0 )
{
//printf("%s is not active\n",Q.srccoin);
@ -1459,7 +1621,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
}
if ( LP_aliceonly(Q.srccoin) > 0 )
{
printf("{\"error\":\"GAME can only be alice coin\"}\n");
printf("{\"error\":\"alice only coins can only be alice coin\"}\n");
return(retval);
}
if ( strcmp(method,"request") == 0 ) // bob
@ -1469,6 +1631,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
if ( Q.destvout != Q.feevout || bits256_cmp(Q.desttxid,Q.feetxid) != 0 )
{
bestprice = LP_bob_competition(&counter,aliceid,qprice,-1);
//printf("bestprice %.8f\n",bestprice);
if ( Qtrades == 0 )//|| (bits256_cmp(Q.srchash,G.LP_mypub25519) == 0 && bits256_cmp(G.LP_mypub25519,Q.desthash) != 0) )
LP_trades_gotrequest(ctx,&Q,&Q2,jstr(argjson,"pair"));
else LP_tradecommandQ(&Q,jstr(argjson,"pair"),LP_REQUEST);
@ -1502,7 +1665,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
return(retval);
}
char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,char *base,char *rel,double maxprice,double relvolume,int32_t timeout,int32_t duration,char *gui,uint32_t nonce,bits256 destpubkey,uint32_t tradeid,char *uuidstr)
char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,char *base,char *rel,double maxprice,double relvolume,int32_t timeout,int32_t duration,char *gui,uint32_t nonce,bits256 destpubkey,uint32_t tradeid,char *uuidstr,int32_t fillflag,int32_t gtcflag)
{
uint64_t desttxfee,txfee,balance; uint32_t lastnonce; int64_t bestsatoshis=0,destsatoshis; struct iguana_info *basecoin,*relcoin; struct LP_utxoinfo *autxo,B,A; struct LP_quoteinfo Q; bits256 pubkeys[100]; struct LP_address_utxo *utxos[4096]; int32_t num=0,maxiters=100,i,max=(int32_t)(sizeof(utxos)/sizeof(*utxos)); char _uuidstr[65];
basecoin = LP_coinfind(base);
@ -1589,6 +1752,10 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha
memset(&A,0,sizeof(A));
if ( (autxo= LP_address_myutxopair(&A,0,utxos,max,relcoin,relcoin->smartaddr,txfee,dstr(destsatoshis),maxprice,desttxfee)) != 0 )
break;
if ( fillflag != 0 )
{
return(clonestr("{\"error\":\"cant find a deposit that is big enough in size. make another deposit that is just a bit larger than what you want to trade\"}"));
}
destsatoshis *= 0.98;
if ( destsatoshis < desttxfee*LP_MINSIZE_TXFEEMULT )
break;
@ -1640,8 +1807,11 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha
}
}
int32_t changed;
LP_mypriceset(&changed,rel,base,1. / maxprice);
LP_mypriceset(&changed,base,rel,0.);
Q.mpnet = G.mpnet;
Q.fill = fillflag;
Q.gtc = gtcflag;
LP_mypriceset(0,&changed,rel,base,1. / maxprice);
LP_mypriceset(0,&changed,base,rel,0.);
if ( uuidstr == 0 || uuidstr[0] == 0 )
{
uint8_t uuidhash[256]; bits256 hash; uint64_t millis; int32_t len = 0;

20
iguana/exchanges/LP_portfolio.c

@ -152,7 +152,7 @@ char *LP_portfolio()
coin->balanceA = LP_balance(&coin->valuesumA,0,coin->symbol,coin->smartaddr);
coin->balanceB = LP_balance(&coin->valuesumB,1,coin->symbol,coin->smartaddr);
if ( strcmp(coin->symbol,"KMD") != 0 )
coin->price_kmd = LP_price(coin->symbol,"KMD");
coin->price_kmd = LP_price(1,coin->symbol,"KMD");
else coin->price_kmd = 1.;
coin->maxamount = coin->valuesumA;
if ( coin->valuesumB > coin->maxamount )
@ -274,7 +274,7 @@ void LP_autopriceset(int32_t ind,void *ctx,int32_t dir,struct LP_priceinfo *base
oppomargin = basepp->buymargins[relpp->ind];
if ( (fixedprice= basepp->fixedprices[relpp->ind]) > SMALLVAL )
{
LP_mypriceset(&changed,relpp->symbol,basepp->symbol,fixedprice);
LP_mypriceset(1,&changed,relpp->symbol,basepp->symbol,fixedprice);
//printf("autoprice FIXED %s/%s <- %.8f\n",basepp->symbol,relpp->symbol,fixedprice);
LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,relpp->symbol,basepp->symbol,fixedprice);
return;
@ -285,7 +285,7 @@ void LP_autopriceset(int32_t ind,void *ctx,int32_t dir,struct LP_priceinfo *base
factor = basepp->factors[relpp->ind];
if ( fabs(price) < SMALLVAL && refbase != 0 && refrel != 0 )
{
price = LP_myprice(&bid,&ask,refbase,refrel);
price = LP_myprice(1,&bid,&ask,refbase,refrel);
//printf("%s/%s USE ref %s/%s %.8f factor %.8f offset %.8f margin %.8f/%.8f\n",basepp->symbol,relpp->symbol,refbase,refrel,price,factor,offset,oppomargin,margin);
}
if ( LP_pricevalid(price) > 0 )
@ -312,7 +312,7 @@ void LP_autopriceset(int32_t ind,void *ctx,int32_t dir,struct LP_priceinfo *base
newprice = LP_autorefs[ind].lastask;
//printf("autopriceset %s/%s <- %.8f %.8f (%.8f %.8f)\n",basepp->symbol,relpp->symbol,price,newprice,LP_autorefs[ind].lastbid,LP_autorefs[ind].lastask);
}
LP_mypriceset(&changed,relpp->symbol,basepp->symbol,newprice);
LP_mypriceset(1,&changed,relpp->symbol,basepp->symbol,newprice);
if ( changed != 0 || time(NULL) > lasttime+LP_ORDERBOOK_DURATION*.777)
{
lasttime = (uint32_t)time(NULL);
@ -582,9 +582,9 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
LP_autorefs[i].lastask = askprice;
else LP_autorefs[i].lastask = (LP_autorefs[i].lastask * 0.9) + (0.1 * askprice);
askprice = LP_autorefs[i].lastask;
LP_mypriceset(&changed,rel,base,bidprice);
LP_mypriceset(1,&changed,rel,base,bidprice);
LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,rel,base,bidprice);
LP_mypriceset(&changed,base,rel,askprice);
LP_mypriceset(1,&changed,base,rel,askprice);
LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,base,rel,askprice);
//printf("price %.8f -> %.8f %.8f\n",price,bidprice,askprice);
}
@ -626,7 +626,7 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
LP_autorefs[i].lastbid = newprice;
else LP_autorefs[i].lastbid = (LP_autorefs[i].lastbid * 0.99) + (0.01 * newprice);
newprice = LP_autorefs[i].lastbid;
LP_mypriceset(&changed,rel,base,newprice);
LP_mypriceset(1,&changed,rel,base,newprice);
LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,rel,base,newprice);
//printf("%s/%s price %.8f margin %.8f/%.8f newprice %.8f %.8f\n",base,rel,price,buymargin,sellmargin,newprice,(1. / newprice) * (1. + sellmargin));
newprice = (1. / price) * (1. + sellmargin);
@ -634,7 +634,7 @@ void LP_autoprice_iter(void *ctx,struct LP_priceinfo *btcpp)
LP_autorefs[i].lastask = newprice;
else LP_autorefs[i].lastask = (LP_autorefs[i].lastask * 0.99) + (0.01 * newprice);
newprice = LP_autorefs[i].lastask;
LP_mypriceset(&changed,base,rel,newprice);
LP_mypriceset(1,&changed,base,rel,newprice);
LP_pricepings(ctx,LP_myipaddr,LP_mypubsock,base,rel,newprice);
} //else printf("null return from CMC\n");
}
@ -782,7 +782,7 @@ int32_t LP_portfolio_trade(void *ctx,uint32_t *requestidp,uint32_t *quoteidp,str
char *retstr2; uint64_t txfee,desttxfee; double bid,ask,maxprice; bits256 zero; uint32_t requestid,quoteid,iter,i; cJSON *retjson2; struct LP_utxoinfo A; struct LP_address_utxo *utxos[1000]; int32_t max=(int32_t)(sizeof(utxos)/sizeof(*utxos));
LP_txfees(&txfee,&desttxfee,buy->symbol,sell->symbol);
requestid = quoteid = 0;
LP_myprice(&bid,&ask,buy->symbol,sell->symbol);
LP_myprice(1,&bid,&ask,buy->symbol,sell->symbol);
maxprice = ask;
if ( setbaserel != 0 )
{
@ -802,7 +802,7 @@ int32_t LP_portfolio_trade(void *ctx,uint32_t *requestidp,uint32_t *quoteidp,str
//if ( LP_utxo_bestfit(sell->symbol,SATOSHIDEN * relvolume) != 0 )
{
memset(zero.bytes,0,sizeof(zero));
if ( (retstr2= LP_autobuy(ctx,0,"127.0.0.1",-1,buy->symbol,sell->symbol,maxprice,relvolume,60,24*3600,gui,LP_lastnonce+1,zero,1,0)) != 0 )
if ( (retstr2= LP_autobuy(ctx,0,"127.0.0.1",-1,buy->symbol,sell->symbol,maxprice,relvolume,60,24*3600,gui,LP_lastnonce+1,zero,1,0,0,0)) != 0 )
{
if ( (retjson2= cJSON_Parse(retstr2)) != 0 )
{

87
iguana/exchanges/LP_prices.c

@ -35,7 +35,7 @@ struct LP_priceinfo
int32_t ind,pad;
double diagval,high[2],low[2],last[2],bid[2],ask[2];
double relvals[LP_MAXPRICEINFOS];
double myprices[LP_MAXPRICEINFOS];
double myprices[2][LP_MAXPRICEINFOS];
double minprices[LP_MAXPRICEINFOS]; // autoprice
double fixedprices[LP_MAXPRICEINFOS]; // fixedprices
double buymargins[LP_MAXPRICEINFOS];
@ -451,16 +451,16 @@ void LP_priceinfoupdate(char *base,char *rel,double price)
}
}
double LP_myprice(double *bidp,double *askp,char *base,char *rel)
double LP_myprice(int32_t iambob,double *bidp,double *askp,char *base,char *rel)
{
struct LP_priceinfo *basepp,*relpp; double val;
*bidp = *askp = 0.;
if ( (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 )
{
*askp = basepp->myprices[relpp->ind];
*askp = basepp->myprices[iambob][relpp->ind];
if ( LP_pricevalid(*askp) > 0 )
{
val = relpp->myprices[basepp->ind];
val = relpp->myprices[iambob][basepp->ind];
if ( LP_pricevalid(val) > 0 )
{
*bidp = 1. / val;
@ -474,7 +474,7 @@ double LP_myprice(double *bidp,double *askp,char *base,char *rel)
}
else
{
val = relpp->myprices[basepp->ind];
val = relpp->myprices[iambob][basepp->ind];
if ( LP_pricevalid(val) > 0 )
{
*bidp = 1. / val;
@ -486,7 +486,7 @@ double LP_myprice(double *bidp,double *askp,char *base,char *rel)
return(0.);
}
char *LP_myprices()
char *LP_myprices(int32_t iambob)
{
int32_t baseid,relid; double bid,ask; char *base,*rel; cJSON *item,*array;
array = cJSON_CreateArray();
@ -496,7 +496,7 @@ char *LP_myprices()
for (relid=0; relid<LP_numpriceinfos; relid++)
{
rel = LP_priceinfos[relid].symbol;
if ( LP_myprice(&bid,&ask,base,rel) > SMALLVAL )
if ( LP_myprice(iambob,&bid,&ask,base,rel) > SMALLVAL )
{
item = cJSON_CreateObject();
jaddstr(item,"base",base);
@ -510,7 +510,7 @@ char *LP_myprices()
return(jprint(array,1));
}
int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price)
int32_t LP_mypriceset(int32_t iambob,int32_t *changedp,char *base,char *rel,double price)
{
struct LP_priceinfo *basepp=0,*relpp=0; struct LP_pubkey_info *pubp; double minprice,maxprice,margin,buymargin,sellmargin;
*changedp = 0;
@ -519,43 +519,46 @@ int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price)
if ( base != 0 && rel != 0 && (basepp= LP_priceinfofind(base)) != 0 && (relpp= LP_priceinfofind(rel)) != 0 )
{
if ( price == 0. || fabs(basepp->myprices[relpp->ind] - price)/price > 0.001 )
if ( price == 0. || fabs(basepp->myprices[iambob][relpp->ind] - price)/price > 0.001 )
*changedp = 1;
sellmargin = relpp->sellmargins[basepp->ind];
buymargin = relpp->buymargins[basepp->ind];
margin = (sellmargin + buymargin) * 0.5;
if ( price == 0. )
if ( iambob != 0 )
{
relpp->minprices[basepp->ind] = 0.;
relpp->fixedprices[basepp->ind] = 0.;
relpp->buymargins[basepp->ind] = 0.;
relpp->sellmargins[basepp->ind] = 0.;
relpp->offsets[basepp->ind] = 0.;
relpp->factors[basepp->ind] = 0.;
LP_autoref_clear(base,rel);
margin = 0.;
}
else if ( (minprice= basepp->minprices[relpp->ind]) > SMALLVAL && price < minprice )
{
//printf("%s/%s price %.8f less than minprice %.8f\n",base,rel,price,minprice);
price = minprice * (1. - margin);
}
else if ( (maxprice= relpp->minprices[basepp->ind]) > SMALLVAL )
{
if ( price > (1. / maxprice) )
sellmargin = relpp->sellmargins[basepp->ind];
buymargin = relpp->buymargins[basepp->ind];
margin = (sellmargin + buymargin) * 0.5;
if ( price == 0. )
{
relpp->minprices[basepp->ind] = 0.;
relpp->fixedprices[basepp->ind] = 0.;
relpp->buymargins[basepp->ind] = 0.;
relpp->sellmargins[basepp->ind] = 0.;
relpp->offsets[basepp->ind] = 0.;
relpp->factors[basepp->ind] = 0.;
LP_autoref_clear(base,rel);
margin = 0.;
}
else if ( (minprice= basepp->minprices[relpp->ind]) > SMALLVAL && price < minprice )
{
//printf("%s/%s price %.8f less than minprice %.8f\n",base,rel,price,minprice);
price = minprice * (1. - margin);
}
else if ( (maxprice= relpp->minprices[basepp->ind]) > SMALLVAL )
{
//printf("%s/%s price %.8f less than maxprice %.8f, more than %.8f\n",base,rel,price,maxprice,1./maxprice);
price = (1. / maxprice) * (1. + margin);
if ( price > (1. / maxprice) )
{
//printf("%s/%s price %.8f less than maxprice %.8f, more than %.8f\n",base,rel,price,maxprice,1./maxprice);
price = (1. / maxprice) * (1. + margin);
}
}
}
/*else if ( basepp->myprices[relpp->ind] > SMALLVAL )
{
price = (basepp->myprices[relpp->ind] * 0.9) + (0.1 * price);
}*/
basepp->myprices[relpp->ind] = price; // ask
basepp->myprices[iambob][relpp->ind] = price; // ask
//printf("LP_mypriceset base.%s rel.%s <- price %.8f\n",base,rel,price);
//relpp->myprices[basepp->ind] = (1. / price); // bid, but best to do one dir at a time
if ( (pubp= LP_pubkeyadd(G.LP_mypub25519)) != 0 )
if ( iambob != 0 && (pubp= LP_pubkeyadd(G.LP_mypub25519)) != 0 )
{
pubp->timestamp = (uint32_t)time(NULL);
LP_pubkey_update(pubp,basepp->ind,relpp->ind,price,0,0,0,0,0);
@ -569,12 +572,12 @@ int32_t LP_mypriceset(int32_t *changedp,char *base,char *rel,double price)
return(-1);
}
double LP_price(char *base,char *rel)
double LP_price(int32_t iambob,char *base,char *rel)
{
struct LP_priceinfo *basepp; int32_t relind; double price = 0.;
if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 )
{
if ( (price= basepp->myprices[relind]) == 0. )
if ( (price= basepp->myprices[iambob][relind]) == 0. )
{
price = basepp->relvals[relind];
}
@ -582,19 +585,19 @@ double LP_price(char *base,char *rel)
return(price);
}
double LP_getmyprice(char *base,char *rel)
double LP_getmyprice(int32_t iambob,char *base,char *rel)
{
struct LP_priceinfo *basepp; int32_t relind; double price = 0.;
if ( (basepp= LP_priceinfoptr(&relind,base,rel)) != 0 )
{
if ( (price= basepp->myprices[relind]) == 0. )
if ( (price= basepp->myprices[iambob][relind]) == 0. )
{
}
}
return(price);
}
cJSON *LP_priceinfomatrix(int32_t usemyprices)
cJSON *LP_priceinfomatrix(int32_t iambob,int32_t usemyprices)
{
int32_t i,j,n,m; double total,sum,val; struct LP_priceinfo *pp; uint32_t now; struct LP_cacheinfo *ptr,*tmp; cJSON *vectorjson = cJSON_CreateObject();
now = (uint32_t)time(NULL);
@ -611,7 +614,7 @@ cJSON *LP_priceinfomatrix(int32_t usemyprices)
pp->diagval = sum = n = 0;
for (j=0; j<LP_numpriceinfos; j++)
{
if ( usemyprices == 0 || (val= pp->myprices[j]) == 0. )
if ( usemyprices == 0 || (val= pp->myprices[iambob][j]) == 0. )
val = pp->relvals[j];
if ( val > SMALLVAL )
{
@ -661,7 +664,7 @@ struct LP_priceinfo *LP_priceinfoadd(char *symbol)
pp->coinbits = stringbits(symbol);
pp->ind = LP_numpriceinfos++;
//LP_numpriceinfos++;
if ( (retjson= LP_priceinfomatrix(0)) != 0 )
if ( (retjson= LP_priceinfomatrix(1,0)) != 0 )
free_json(retjson);
return(pp);
}
@ -1016,7 +1019,7 @@ int64_t LP_KMDvalue(struct iguana_info *coin,int64_t balance)
KMDvalue = balance;
else
{
if ( (price= LP_price(coin->symbol,"KMD")) > SMALLVAL )
if ( (price= LP_price(1,coin->symbol,"KMD")) > SMALLVAL )
KMDvalue = price * balance;
}
}

14
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\"}"));
if ( bits256_nonz(txid) == 0 )
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 )
{
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
{
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 ( (txobj= electrum_transaction(&height,symbol,coin->electrum,&txobj,txid,0)) != 0 )
@ -1038,7 +1038,7 @@ cJSON *LP_blockjson(int32_t *heightp,char *symbol,char *blockhashstr,int32_t hei
coin = LP_coinfind(symbol);
if ( coin == 0 || coin->electrum != 0 )
{
printf("unexpected electrum path for %s\n",symbol);
//printf("unexpected electrum path for %s\n",symbol);
return(0);
}
if ( blockhashstr == 0 )

38
iguana/exchanges/LP_signatures.c

@ -19,7 +19,7 @@
// marketmaker
//
struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srchash,bits256 desthash,char *src,uint64_t srcsatoshis,char *dest,uint64_t destsatoshis,uint32_t timestamp,uint32_t quotetime,int32_t DEXselector)
struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srchash,bits256 desthash,char *src,uint64_t srcsatoshis,char *dest,uint64_t destsatoshis,uint32_t timestamp,uint32_t quotetime,int32_t DEXselector,int32_t fillflag,int32_t gtcflag)
{
struct basilisk_request R;
memset(rp,0,sizeof(*rp));
@ -45,6 +45,14 @@ cJSON *LP_quotejson(struct LP_quoteinfo *qp)
if ( jobj(retjson,"gui") == 0 )
jaddstr(retjson,"gui",qp->gui[0] != 0 ? qp->gui : LP_gui);
jaddstr(retjson,"uuid",qp->uuidstr);
if ( qp->maxprice != 0 )
jaddnum(retjson,"maxprice",qp->maxprice);
if ( qp->mpnet != 0 )
jaddnum(retjson,"mpnet",qp->mpnet);
if ( qp->gtc != 0 )
jaddnum(retjson,"gtc",qp->gtc);
if ( qp->fill != 0 )
jaddnum(retjson,"fill",qp->fill);
jadd64bits(retjson,"aliceid",qp->aliceid);
jaddnum(retjson,"tradeid",qp->tradeid);
jaddstr(retjson,"base",qp->srccoin);
@ -113,6 +121,10 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson)
{
uint32_t rid,qid; char etomic[64],activesymbol[65],*etomicstr;
memset(qp,0,sizeof(*qp));
qp->maxprice = jdouble(argjson,"maxprice");
qp->mpnet = juint(argjson,"mpnet");
qp->gtc = juint(argjson,"gtc");
qp->fill = juint(argjson,"fill");
safecopy(qp->gui,LP_gui,sizeof(qp->gui));
safecopy(qp->srccoin,jstr(argjson,"base"),sizeof(qp->srccoin));
safecopy(qp->uuidstr,jstr(argjson,"uuid"),sizeof(qp->uuidstr));
@ -152,9 +164,9 @@ int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson)
qp->destvout = jint(argjson,"destvout");
qp->desthash = jbits256(argjson,"desthash");
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->desttxfee = j64bits(argjson,"desttxfee");
@ -717,16 +729,20 @@ void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_
jadd(reqjson,"proof",LP_instantdex_txids(0,coin->smartaddr));
}
msg = jprint(reqjson,1);
//printf("QUERY.(%s)\n",msg);
if ( IPC_ENDPOINT >= 0 )
LP_queuecommand(0,msg,IPC_ENDPOINT,-1,0);
memset(&zero,0,sizeof(zero));
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg));
//if ( bits256_nonz(qp->srchash) != 0 )
{
sleep(1);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg));
//printf("QUERY.(%s)\n",msg);
if ( IPC_ENDPOINT >= 0 )
LP_queuecommand(0,msg,IPC_ENDPOINT,-1,0);
memset(&zero,0,sizeof(zero));
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg));
//if ( bits256_nonz(qp->srchash) != 0 )
{
sleep(1);
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg));
}
}
if ( strcmp(method,"connect") == 0 && qp->mpnet != 0 && qp->gtc == 0 )
LP_mpnet_send(0,msg,1,qp->coinaddr);
free(msg);
}

121
iguana/exchanges/LP_socket.c

@ -25,6 +25,83 @@
#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#endif
#ifdef _WIN32
#include <WinSock2.h>
#endif
int32_t set_blocking_mode(int32_t sock,int32_t is_blocking) // from https://stackoverflow.com/questions/2149798/how-to-reset-a-socket-back-to-blocking-mode-after-i-set-it-to-nonblocking-mode?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
{
int32_t ret;
#ifdef _WIN32
/// @note windows sockets are created in blocking mode by default
// currently on windows, there is no easy way to obtain the socket's current blocking mode since WSAIsBlocking was deprecated
u_long non_blocking = is_blocking ? 0 : 1;
ret = (NO_ERROR == ioctlsocket(sock,FIONBIO,&non_blocking));
#else
const int flags = fcntl(sock, F_GETFL, 0);
if ((flags & O_NONBLOCK) && !is_blocking) { fprintf(stderr,"set_blocking_mode(): socket was already in non-blocking mode\n"); return ret; }
if (!(flags & O_NONBLOCK) && is_blocking) { fprintf(stderr,"set_blocking_mode(): socket was already in blocking mode\n"); return ret; }
ret = (0 == fcntl(sock, F_SETFL, is_blocking ? (flags ^ O_NONBLOCK) : (flags | O_NONBLOCK)));
#endif
if ( ret == 0 )
return(-1);
else return(0);
}
int32_t komodo_connect(int32_t sock,struct sockaddr *saddr,socklen_t addrlen)
{
struct timeval tv; fd_set wfd,efd; int32_t res,so_error; socklen_t len;
#ifdef _WIN32
set_blocking_mode(sock, 0);
#else
fcntl(sock, F_SETFL, O_NONBLOCK);
#endif // _WIN32
res = connect(sock,saddr,addrlen);
if ( res == -1 )
{
#ifdef _WIN32
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx - read about WSAEWOULDBLOCK return
errno = WSAGetLastError();
printf("[Decker] errno.%d --> ", errno);
if ( errno != EINPROGRESS && errno != WSAEWOULDBLOCK ) // connect failed, do something...
#else
if ( errno != EINPROGRESS ) // connect failed, do something...
#endif
{
printf("close socket ...\n");
closesocket(sock);
return(-1);
}
//printf("continue with select ...\n");
FD_ZERO(&wfd);
FD_SET(sock,&wfd);
FD_ZERO(&efd);
FD_SET(sock,&efd);
tv.tv_sec = 10;
tv.tv_usec = 0;
res = select(sock+1,NULL,&wfd,&efd,&tv);
if ( res == -1 ) // select failed, do something...
{
closesocket(sock);
return(-1);
}
if ( res == 0 ) // connect timed out...
{
closesocket(sock);
return(-1);
}
if ( FD_ISSET(sock,&efd) )
{
// connect failed, do something...
getsockopt(sock,SOL_SOCKET,SO_ERROR,&so_error,&len);
closesocket(sock);
return(-1);
}
}
set_blocking_mode(sock,1);
return(0);
}
int32_t LP_socket(int32_t bindflag,char *hostname,uint16_t port)
{
@ -124,25 +201,35 @@ int32_t LP_socket(int32_t bindflag,char *hostname,uint16_t port)
#endif
if ( bindflag == 0 )
{
//printf("call connect sock.%d\n",sock);
result = connect(sock,(struct sockaddr *)&saddr,addrlen);
//printf("called connect result.%d\n",result);
timeout.tv_sec = 2;
timeout.tv_usec = 0;
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(void *)&timeout,sizeof(timeout));
if ( result != 0 )
//#ifdef _WIN32
if ( 1 ) // connect using async to allow timeout, then switch to sync
{
if ( errno != ECONNRESET && errno != ENOTCONN && errno != ECONNREFUSED && errno != ETIMEDOUT && errno != EHOSTUNREACH )
uint32_t starttime = (uint32_t)time(NULL);
//printf("call connect sock.%d\n",sock);
result = komodo_connect(sock,(struct sockaddr *)&saddr,addrlen);
//printf("called connect result.%d lag.%d\n",result,(int32_t)(time(NULL) - starttime));
if ( result < 0 )
return(-1);
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(void *)&timeout,sizeof(timeout));
}
//#else
else
{
result = connect(sock,(struct sockaddr *)&saddr,addrlen);
if ( result != 0 )
{
//printf("%s(%s) port.%d failed: %s sock.%d. errno.%d\n",bindflag!=0?"bind":"connect",hostname,port,strerror(errno),sock,errno);
if ( errno != ECONNRESET && errno != ENOTCONN && errno != ECONNREFUSED && errno != ETIMEDOUT && errno != EHOSTUNREACH )
{
//printf("%s(%s) port.%d failed: %s sock.%d. errno.%d\n",bindflag!=0?"bind":"connect",hostname,port,strerror(errno),sock,errno);
}
if ( sock >= 0 )
closesocket(sock);
return(-1);
}
if ( sock >= 0 )
closesocket(sock);
return(-1);
}
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(void *)&timeout,sizeof(timeout));
//#endif
}
else
{
@ -422,7 +509,7 @@ cJSON *electrum_submit(char *symbol,struct electrum_info *ep,cJSON **retjsonp,ch
{
*retjsonp = 0;
sprintf(stratumreq,"{ \"jsonrpc\":\"2.0\", \"id\": %u, \"method\":\"%s\", \"params\": %s }\n",ep->stratumid,method,params);
//printf("%s %s",symbol,stratumreq);
//printf("timeout.%d exp.%d %s %s",timeout,(int32_t)(expiration-time(NULL)),symbol,stratumreq);
memset(ep->buf,0,ep->bufsize);
sitem = electrum_sitem(ep,stratumreq,timeout,retjsonp);
portable_mutex_lock(&ep->mutex); // this helps performance!
@ -489,7 +576,7 @@ cJSON *electrum_hasharg(char *symbol,struct electrum_info *ep,cJSON **retjsonp,c
char params[128],str[65]; cJSON *retjson;
if ( retjsonp == 0 )
retjsonp = &retjson;
sprintf(params,"[\"%s\"]",bits256_str(str,arg));
sprintf(params,"[\"%s\", true]",bits256_str(str,arg));
return(electrum_submit(symbol,ep,retjsonp,method,params,timeout));
}

2
iguana/exchanges/LP_statemachine.c

@ -3645,7 +3645,7 @@ if ( LP_pricevalid(price) > 0 )
{
LP_query(ctx,myipaddr,mypubsock,"connect",qp);
//price = LP_pricecache(qp,qp->srccoin,qp->destcoin,qp->txid,qp->vout);
LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis-2*qp->txfee,qp->destcoin,qp->destsatoshis-2*qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector);
LP_requestinit(&qp->R,qp->srchash,qp->desthash,qp->srccoin,qp->satoshis-2*qp->txfee,qp->destcoin,qp->destsatoshis-2*qp->desttxfee,qp->timestamp,qp->quotetime,DEXselector,qp->fill,qp->gtc);
while ( time(NULL) < expiration )
{
if ( aliceutxo->S.swap != 0 )

13
iguana/exchanges/LP_swap.c

@ -888,6 +888,15 @@ void LP_bobloop(void *_swap)
}
}
LP_unavailableset(swap->bobpayment.utxotxid,swap->bobpayment.utxovout,(uint32_t)time(NULL)+60,swap->I.otherhash);
m = swap->I.bobconfirms;
while ( (n= LP_numconfirms(bobstr,swap->bobdeposit.I.destaddr,swap->bobdeposit.I.signedtxid,0,1)) < m )
{
LP_swap_critical = (uint32_t)time(NULL);
LP_unavailableset(swap->bobpayment.utxotxid,swap->bobpayment.utxovout,(uint32_t)time(NULL)+60,swap->I.otherhash);
char str[65];printf("%d wait for bobdeposit %s numconfs.%d %s %s\n",n,swap->bobdeposit.I.destaddr,m,bobstr,bits256_str(str,swap->bobdeposit.I.signedtxid));
sleep(10);
}
printf("wait for alicepayment\n");
if ( error == 0 && LP_waitfor(swap->N.pair,swap,bobwaittimeout + alicewaittimeout,LP_verify_alicepayment) < 0 )
{
error = 1;
@ -901,6 +910,7 @@ void LP_bobloop(void *_swap)
else
{
m = swap->I.aliceconfirms;
LP_unavailableset(swap->bobpayment.utxotxid,swap->bobpayment.utxovout,(uint32_t)time(NULL)+60,swap->I.otherhash);
while ( (n= LP_numconfirms(alicestr,swap->alicepayment.I.destaddr,swap->alicepayment.I.signedtxid,0,1)) < m ) // sync with alice
{
LP_unavailableset(swap->bobpayment.utxotxid,swap->bobpayment.utxovout,(uint32_t)time(NULL)+60,swap->I.otherhash);
@ -939,6 +949,7 @@ void LP_bobloop(void *_swap)
void LP_aliceloop(void *_swap)
{
uint8_t *data; char bobstr[65],alicestr[65]; int32_t bobwaittimeout,alicewaittimeout,maxlen,n,m,err=0; uint32_t expiration; struct basilisk_swap *swap = _swap;
LP_alicequery_clear();
G.LP_pendingswaps++;
LP_etomicsymbol(bobstr,swap->I.bobtomic,swap->I.bobstr);
LP_etomicsymbol(alicestr,swap->I.alicetomic,swap->I.alicestr);
@ -973,6 +984,7 @@ void LP_aliceloop(void *_swap)
while ( (n= LP_numconfirms(bobstr,swap->bobdeposit.I.destaddr,swap->bobdeposit.I.signedtxid,0,1)) < m )
{
LP_swap_critical = (uint32_t)time(NULL);
LP_unavailableset(swap->alicepayment.utxotxid,swap->alicepayment.utxovout,(uint32_t)time(NULL)+60,swap->I.otherhash);
char str[65];printf("%d wait for bobdeposit %s numconfs.%d %s %s\n",n,swap->bobdeposit.I.destaddr,m,bobstr,bits256_str(str,swap->bobdeposit.I.signedtxid));
sleep(10);
}
@ -999,6 +1011,7 @@ void LP_aliceloop(void *_swap)
char str[65];printf("%d wait for bobpayment %s numconfs.%d %s %s\n",n,swap->bobpayment.I.destaddr,swap->I.bobconfirms,bobstr,bits256_str(str,swap->bobpayment.I.signedtxid));
sleep(10);
}
char str[65];printf("%d waited for bobpayment %s numconfs.%d %s %s\n",n,swap->bobpayment.I.destaddr,swap->I.bobconfirms,bobstr,bits256_str(str,swap->bobpayment.I.signedtxid));
if ( swap->N.pair >= 0 )
nn_close(swap->N.pair), swap->N.pair = -1;
}

2
iguana/exchanges/LP_tradebots.c

@ -338,7 +338,7 @@ void LP_tradebot_timeslice(void *ctx,struct LP_tradebot *bot)
{
if ( remaining < 0.001 )
break;
if ( (retstr= LP_autobuy(ctx,0,LP_myipaddr,LP_mypubsock,bot->base,bot->rel,bot->maxprice,remaining/i,0,0,G.gui,0,destpubkey,tradeid,0)) != 0 )
if ( (retstr= LP_autobuy(ctx,0,LP_myipaddr,LP_mypubsock,bot->base,bot->rel,bot->maxprice,remaining/i,0,0,G.gui,0,destpubkey,tradeid,0,0,0)) != 0 )
{
if ( (retjson2= cJSON_Parse(retstr)) != 0 )
{

53
iguana/exchanges/LP_transaction.c

@ -1887,7 +1887,7 @@ char *LP_withdraw(struct iguana_info *coin,cJSON *argjson)
if ( autofee != 0 && iter == 0 && strcmp(coin->symbol,"BTC") == 0 )
{
txfee = newtxfee = LP_txfeecalc(coin,0,datalen);
printf("txfee %.8f -> newtxfee %.8f, numvins.%d\n",dstr(txfee),dstr(newtxfee),numvins);
printf("txfee %.8f -> newtxfee %.8f, numvins.%d datalen.%d\n",dstr(txfee),dstr(newtxfee),numvins,datalen);
for (i=0; i<numvins; i++)
{
item = jitem(vins,i);
@ -1950,7 +1950,7 @@ char *LP_withdraw(struct iguana_info *coin,cJSON *argjson)
char *LP_autosplit(struct iguana_info *coin)
{
char *retstr; cJSON *argjson,*withdrawjson,*outputs,*item; int64_t total,balance,halfval,txfee;
char *retstr; cJSON *argjson,*withdrawjson,*outputs,*item; int64_t total,balance,txfee;
if ( coin->etomic[0] == 0 )
{
if ( coin->electrum != 0 )
@ -1962,17 +1962,21 @@ char *LP_autosplit(struct iguana_info *coin)
//printf("balance %.8f, txfee %.8f, threshold %.8f\n",dstr(balance),dstr(txfee),dstr((1000000 - (txfee + 100000))));
if ( balance > txfee && balance >= (1000000 - (txfee + 100000)) )
{
halfval = (balance / 100) * 45;
// .95 / .02 / .02 / 0.005
//halfval = (balance / 100) * 45;
argjson = cJSON_CreateObject();
outputs = cJSON_CreateArray();
item = cJSON_CreateObject();
jaddnum(item,coin->smartaddr,dstr(halfval));
jaddnum(item,coin->smartaddr,dstr(balance/100) * 95);
jaddi(outputs,item);
item = cJSON_CreateObject();
jaddnum(item,coin->smartaddr,dstr(halfval));
jaddnum(item,coin->smartaddr,dstr(balance/50));
jaddi(outputs,item);
item = cJSON_CreateObject();
jaddnum(item,coin->smartaddr,dstr(balance - 2*halfval));
jaddnum(item,coin->smartaddr,dstr(balance/50));
jaddi(outputs,item);
item = cJSON_CreateObject();
jaddnum(item,coin->smartaddr,0.0001);
jaddi(outputs,item);
jadd(argjson,"outputs",outputs);
jaddnum(argjson,"broadcast",1);
@ -1986,6 +1990,43 @@ char *LP_autosplit(struct iguana_info *coin)
return(clonestr("{\"error\":\"couldnt autosplit\"}"));
}
char *LP_autofillbob(struct iguana_info *coin,uint64_t satoshis)
{
char *retstr; cJSON *argjson,*withdrawjson,*outputs,*item; int64_t total,balance,txfee;
if ( coin->etomic[0] == 0 )
{
if ( coin->electrum != 0 )
balance = LP_unspents_load(coin->symbol,coin->smartaddr);
else balance = LP_RTsmartbalance(coin);
if ( strcmp("BTC",coin->symbol) == 0 )
txfee = LP_txfeecalc(coin,0,1000);
balance -= (txfee + 1000000);
if ( balance < (satoshis<<2) )
return(clonestr("{\"error\":\"couldnt autofill balance too small\"}"));
if ( balance > satoshis+3*txfee && balance >= (txfee + 1000000) )
{
argjson = cJSON_CreateObject();
outputs = cJSON_CreateArray();
item = cJSON_CreateObject();
jaddnum(item,coin->smartaddr,dstr(satoshis + 3000000));
jaddi(outputs,item);
item = cJSON_CreateObject();
jaddnum(item,coin->smartaddr,dstr(LP_DEPOSITSATOSHIS(satoshis) + 3000000));
jaddi(outputs,item);
item = cJSON_CreateObject();
jaddnum(item,coin->smartaddr,0.0001);
jaddi(outputs,item);
jadd(argjson,"outputs",outputs);
jaddnum(argjson,"broadcast",0);
jaddstr(argjson,"coin",coin->symbol);
retstr = LP_withdraw(coin,argjson);
free_json(argjson);
return(retstr);
} else return(clonestr("{\"error\":\"balance too small to autosplit, please make more deposits\"}"));
}
return(clonestr("{\"error\":\"couldnt autofill etomic needs separate support\"}"));
}
char *LP_movecoinbases(char *symbol)
{
static bits256 zero; bits256 utxotxid,txid; struct iguana_info *coin; cJSON *retjson,*outputs,*argjson,*txids,*unspents,*item,*gen,*output; int32_t i,n,utxovout; char *retstr,*hexstr;

2
iguana/exchanges/LP_utxo.c

@ -724,7 +724,7 @@ cJSON *LP_address_balance(struct iguana_info *coin,char *coinaddr,int32_t electr
}
else
{
if ( strcmp(coin->smartaddr,coinaddr) != 0 )
//if ( strcmp(coin->smartaddr,coinaddr) != 0 )
{
if ( (retjson= electrum_address_listunspent(coin->symbol,coin->electrum,&retjson,coinaddr,2,zero,zero)) != 0 )
free_json(retjson);

4
iguana/exchanges/coins

File diff suppressed because one or more lines are too long

6
iguana/exchanges/mm.c

@ -346,6 +346,12 @@ int main(int argc, const char * argv[])
printf("couldnt write to (%s)\n",dirname);
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);
if ( ensure_writable(dirname) < 0 )
{

2
iguana/iguana777.c

@ -1131,7 +1131,7 @@ struct iguana_info *iguana_setcoin(char *symbol,void *launched,int32_t maxpeers,
if ( coin->MAXMEM == 0 )
coin->MAXMEM = IGUANA_DEFAULTRAM;
coin->MAXMEM *= (1024L * 1024 * 1024);
coin->enableCACHE = 0;//(strcmp("BTCD",coin->symbol) == 0);
coin->enableCACHE = 1;//0;//(strcmp("BTCD",coin->symbol) == 0);
if ( jobj(json,"cache") != 0 )
coin->enableCACHE = juint(json,"cache");
if ( (coin->polltimeout= juint(json,"poll")) <= 0 )

2
iguana/iguana_notary.c

@ -535,7 +535,7 @@ STRING_ARG(iguana,addnotary,ipaddr)
}
char NOTARY_CURRENCIES[][65] = {
"REVS", "SUPERNET", "DEX", "PANGEA", "JUMBLR", "BET", "CRYPTO", "HODL", "BOTS", "MGW", "COQUI", "WLC", "KV", "CEAL", "MESH", "MNZ", "CHIPS", "MSHARK", "AXO", "ETOMIC", "BTCH", "VOTE2018", "NINJA", "OOT", "CHAIN", "BNTN", "PRLPAY", "DSEC", "GLXT", "EQL"
"REVS", "SUPERNET", "DEX", "PANGEA", "JUMBLR", "BET", "CRYPTO", "HODL", "BOTS", "MGW", "COQUI", "WLC", "KV", "CEAL", "MESH", "MNZ", "CHIPS", "MSHARK", "AXO", "ETOMIC", "BTCH", "VOTE2018", "NINJA", "OOT", "CHAIN", "BNTN", "PRLPAY", "DSEC", "GLXT", "EQL" "ZILLA"
};
// "LTC", "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", "CNY", "RUB", "MXN", "BRL", "INR", "HKD", "TRY", "ZAR", "PLN", "NOK", "SEK", "DKK", "CZK", "HUF", "ILS", "KRW", "MYR", "PHP", "RON", "SGD", "THB", "BGN", "IDR", "HRK",

2
iguana/iguana_ramchain.c

@ -1015,7 +1015,7 @@ int32_t iguana_ramchain_alloc(char *fname,struct iguana_info *coin,struct iguana
char str[65],str2[65]; fprintf(stderr,"ht.%d wait for allocated %s < MAXMEM %s | elapsed %.2f minutes hashsize.%ld allocsize.%ld\n",height,mbstr(str,myallocated(0,-1)+hashsize+allocsize),mbstr(str2,coin->MAXMEM),(double)(time(NULL)-coin->startutc)/60.,(long)hashsize,(long)allocsize);
sleep(13);
}
iguana_meminit(hashmem,"ramhashmem",0,2*hashsize + 65536,0);
iguana_meminit(hashmem,"ramhashmem",0,3*hashsize + 65536,0);
iguana_meminit(mem,"ramchain",0,allocsize + 65536,0);
mem->alignflag = sizeof(uint32_t);
hashmem->alignflag = sizeof(uint32_t);

9
iguana/iguana_wallet.c

@ -1402,7 +1402,7 @@ TWOSTRINGS_AND_INT(bitcoinrpc,walletpassphrase,password,permanentfile,timeout)
THREE_STRINGS(bitcoinrpc,encryptwallet,passphrase,password,permanentfile)
{
char *retstr,buf[128],wifstr[128]; cJSON *retjson; int32_t need_KMD = 0,need_BTC = 0,need_GAME = 0;
char *retstr,buf[128],wifstr[128]; cJSON *retjson; int32_t need_HUSH = 0,need_KMD = 0,need_BTC = 0,need_GAME = 0;
if ( remoteaddr != 0 || coin == 0 )
return(clonestr("{\"error\":\"no remote encrypt or no coin\"}"));
iguana_walletlock(myinfo,coin);
@ -1441,11 +1441,18 @@ THREE_STRINGS(bitcoinrpc,encryptwallet,passphrase,password,permanentfile)
need_BTC = 1;
if ( strcmp(coin->symbol,"GAME") != 0 )
need_GAME = 1;
if ( strcmp(coin->symbol,"HUSH") != 0 )
need_HUSH = 1;
if ( need_KMD != 0 && (coin= iguana_coinfind("KMD")) != 0 )
{
bitcoin_priv2wif(wifstr,waddr.privkey,coin->chain->wiftype);
jaddstr(retjson,"KMDwif",wifstr);
}
if ( need_HUSH != 0 && (coin= iguana_coinfind("HUSH")) != 0 )
{
bitcoin_priv2wif(wifstr,waddr.privkey,coin->chain->wiftype);
jaddstr(retjson,"HUSHwif",wifstr);
}
if ( (coin= iguana_coinfind("LTC")) != 0 )
{
bitcoin_priv2wif(wifstr,waddr.privkey,coin->chain->wiftype);

3
iguana/m_notary_run

@ -63,6 +63,9 @@ coins/prlpay_7776
coins/dsec_7776
coins/glxt_7776
coins/eql_7776
coins/zilla_7776
coins/vrsc_7776
coins/rfox_7776
#curl --url "http://127.0.0.1:7776" --data "{\"agent\":\"passthru\",\"method\":\"paxfiats\",\"timeout\":900000}"

1
iguana/m_splitfund

@ -35,3 +35,4 @@ curl --url "http://127.0.0.1:7776" --data "{\"coin\":\"CHAIN\",\"agent\":\"iguan
curl --url "http://127.0.0.1:7776" --data "{\"coin\":\"DSEC\",\"agent\":\"iguana\",\"method\":\"splitfunds\",\"satoshis\":\"10000\",\"sendflag\":1,\"duplicates\":50}"
curl --url "http://127.0.0.1:7776" --data "{\"coin\":\"GLXT\",\"agent\":\"iguana\",\"method\":\"splitfunds\",\"satoshis\":\"10000\",\"sendflag\":1,\"duplicates\":50}"
curl --url "http://127.0.0.1:7776" --data "{\"coin\":\"EQL\",\"agent\":\"iguana\",\"method\":\"splitfunds\",\"satoshis\":\"10000\",\"sendflag\":1,\"duplicates\":50}"
curl --url "http://127.0.0.1:7776" --data "{\"coin\":\"ZILLA\",\"agent\":\"iguana\",\"method\":\"splitfunds\",\"satoshis\":\"10000\",\"sendflag\":1,\"duplicates\":50}"

Loading…
Cancel
Save