@ -18,6 +18,15 @@
// LP_ordermatch.c
// LP_ordermatch.c
// marketmaker
// marketmaker
//
//
struct LP_gtcorder
{
struct LP_gtcorder * next , * prev ;
struct LP_quoteinfo Q ;
double maxprice ;
uint32_t cancelled , pending ;
} * GTCorders ;
struct LP_quoteinfo LP_Alicequery , LP_Alicereserved ;
struct LP_quoteinfo LP_Alicequery , LP_Alicereserved ;
double LP_Alicemaxprice ;
double LP_Alicemaxprice ;
bits256 LP_Alicedestpubkey , LP_bobs_reserved ;
bits256 LP_Alicedestpubkey , LP_bobs_reserved ;
@ -494,7 +503,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 )
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 ) ) ;
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 )
if ( ( swap = LP_swapinit ( 1 , 0 , privkey , & qp - > R , qp , dtrust > 0 ) ) = = 0 )
{
{
@ -567,10 +576,43 @@ int32_t LP_connectstartbob(void *ctx,int32_t pubsock,char *base,char *rel,double
return ( retval ) ;
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 ;
if ( Alice_expiration ! = 0 )
return ;
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 ( time ( NULL ) > gtc - > pending + LP_AUTOTRADE_TIMEOUT * 10 )
{
qp - > timestamp = ( uint32_t ) time ( NULL ) ;
LP_query ( ctx , myipaddr , mypubsock , " request " , qp ) ;
LP_Alicequery = * qp , LP_Alicemaxprice = gtc - > maxprice , Alice_expiration = qp - > timestamp + 2 * LP_AUTOTRADE_TIMEOUT , LP_Alicedestpubkey = qp - > srchash ;
char str [ 65 ] ; printf ( " LP_gtc fill.%d gtc.%d %s/%s %.8f vol %.8f dest.(%s) maxprice %.8f etomicdest.(%s) uuid.%s fill.%d gtc.%d \n " , qp - > fill , qp - > gtc , qp - > srccoin , qp - > destcoin , dstr ( qp - > satoshis ) , dstr ( qp - > destsatoshis ) , bits256_str ( str , LP_Alicedestpubkey ) , gtc - > maxprice , qp - > etomicdest , qp - > uuidstr , qp - > fill , qp - > gtc ) ;
break ;
}
}
}
}
char * LP_trade ( void * ctx , char * myipaddr , int32_t mypubsock , struct LP_quoteinfo * qp , double maxprice , int32_t timeout , int32_t duration , uint32_t tradeid , bits256 destpubkey , char * uuidstr )
char * LP_trade ( void * ctx , char * myipaddr , int32_t mypubsock , struct LP_quoteinfo * qp , double maxprice , int32_t timeout , int32_t duration , uint32_t tradeid , bits256 destpubkey , char * uuidstr )
{
{
double price ;
struct LP_gtcorder * gtc ;
price = 0. ;
memset ( qp - > txid . bytes , 0 , sizeof ( qp - > txid ) ) ;
memset ( qp - > txid . bytes , 0 , sizeof ( qp - > txid ) ) ;
qp - > txid2 = qp - > txid ;
qp - > txid2 = qp - > txid ;
qp - > aliceid = LP_aliceid_calc ( qp - > desttxid , qp - > destvout , qp - > feetxid , qp - > feevout ) ;
qp - > aliceid = LP_aliceid_calc ( qp - > desttxid , qp - > destvout , qp - > feetxid , qp - > feevout ) ;
@ -578,9 +620,21 @@ char *LP_trade(void *ctx,char *myipaddr,int32_t mypubsock,struct LP_quoteinfo *q
qp - > tradeid = LP_rand ( ) ;
qp - > tradeid = LP_rand ( ) ;
qp - > srchash = destpubkey ;
qp - > srchash = destpubkey ;
strncpy ( qp - > uuidstr , uuidstr , sizeof ( qp - > uuidstr ) - 1 ) ;
strncpy ( qp - > uuidstr , uuidstr , sizeof ( qp - > uuidstr ) - 1 ) ;
qp - > timestamp = ( uint32_t ) time ( NULL ) ;
if ( qp - > gtc ! = 0 )
{
strcpy ( & qp - > uuidstr [ strlen ( qp - > uuidstr ) - 6 ] , " cccccc " ) ;
gtc = calloc ( 1 , sizeof ( * gtc ) ) ;
gtc - > Q = * qp ;
gtc - > maxprice = maxprice ;
gtc - > pending = ( uint32_t ) time ( NULL ) ;
portable_mutex_lock ( & LP_gtcmutex ) ;
DL_APPEND ( GTCorders , gtc ) ;
portable_mutex_unlock ( & LP_gtcmutex ) ;
}
LP_query ( ctx , myipaddr , mypubsock , " request " , qp ) ;
LP_query ( ctx , myipaddr , mypubsock , " request " , qp ) ;
LP_Alicequery = * qp , LP_Alicemaxprice = maxprice , Alice_expiration = qp - > timestamp + timeout , LP_Alicedestpubkey = destpubkey ;
LP_Alicequery = * qp , LP_Alicemaxprice = maxprice , Alice_expiration = qp - > timestamp + timeout , LP_Alicedestpubkey = qp - > srchash ;
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 ) ;
char str [ 65 ] ; printf ( " LP_trade 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 ) , maxprice , qp - > etomicdest , qp - > uuidstr , qp - > fill , qp - > gtc ) ;
return ( LP_recent_swaps ( 0 , uuidstr ) ) ;
return ( LP_recent_swaps ( 0 , uuidstr ) ) ;
}
}
@ -628,16 +682,45 @@ char *LP_cancel_order(char *uuidstr)
int32_t num = 0 ; cJSON * retjson ;
int32_t num = 0 ; cJSON * retjson ;
if ( uuidstr ! = 0 )
if ( uuidstr ! = 0 )
{
{
num = LP_trades_canceluuid ( uuidstr ) ;
if ( uuidstr [ 0 ] = = ' G ' )
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 ) ;
struct LP_gtcorder * gtc , * tmp ;
LP_alicequery_clear ( ) ;
DL_FOREACH_SAFE ( GTCorders , gtc , tmp )
jaddstr ( retjson , " status " , " uuid canceled " ) ;
{
} else jaddstr ( retjson , " status " , " will stop trade negotiation, but if swap started it wont cancel " ) ;
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 ( jprint ( retjson , 1 ) ) ;
}
}
return ( clonestr ( " { \" error \" : \" uuid not cancellable \" } " ) ) ;
return ( clonestr ( " { \" error \" : \" uuid not cancellable \" } " ) ) ;
@ -652,8 +735,8 @@ char *LP_connectedalice(struct LP_quoteinfo *qp,char *pairstr) // alice
LP_failedmsg ( qp - > R . requestid , qp - > R . quoteid , - 4000 , qp - > uuidstr ) ;
LP_failedmsg ( qp - > R . requestid , qp - > R . quoteid , - 4000 , qp - > uuidstr ) ;
return ( clonestr ( " { \" result \" , \" update stats \" } " ) ) ;
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 : " " ) ;
printf ( " CONNECTED fill.%d gtc.%d numpending.%d tradeid.%u requestid.%u quoteid.%u pairstr.%s \n " , 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 ) ;
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);
//printf("calculated requestid.%u quoteid.%u\n",qp->R.requestid,qp->R.quoteid);
memset ( & LP_Alicereserved , 0 , sizeof ( LP_Alicereserved ) ) ;
memset ( & LP_Alicereserved , 0 , sizeof ( LP_Alicereserved ) ) ;
LP_aliceid ( qp - > tradeid , qp - > aliceid , " connected " , qp - > R . requestid , qp - > R . quoteid ) ;
LP_aliceid ( qp - > tradeid , qp - > aliceid , " connected " , qp - > R . requestid , qp - > R . quoteid ) ;
@ -916,10 +999,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 )
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 ;
* newqp = * qp ;
qp = newqp ;
qp = newqp ;
//printf("bob %s received REQUEST.(%s)\n",bits256_str(str,G.LP_mypub25519),qp->uuidstr+32);
printf ( " bob %s received REQUEST.(%s) fill.%d gtc.%d \n " , bits256_str ( str , G . LP_mypub25519 ) , qp - > uuidstr + 32 , qp - > fill , qp - > gtc ) ;
if ( ( coin = LP_coinfind ( qp - > srccoin ) ) = = 0 | | ( othercoin = LP_coinfind ( qp - > destcoin ) ) = = 0 )
if ( ( coin = LP_coinfind ( qp - > srccoin ) ) = = 0 | | ( othercoin = LP_coinfind ( qp - > destcoin ) ) = = 0 )
return ( 0 ) ;
return ( 0 ) ;
if ( ( myprice = LP_trades_bobprice ( & bid , & ask , qp ) ) = = 0. )
if ( ( myprice = LP_trades_bobprice ( & bid , & ask , qp ) ) = = 0. )
@ -970,7 +1054,7 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
}
}
else
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 ) ;
return ( 0 ) ;
}
}
//LP_RTmetrics_update(qp->srccoin,qp->destcoin);
//LP_RTmetrics_update(qp->srccoin,qp->destcoin);
@ -998,45 +1082,91 @@ struct LP_quoteinfo *LP_trades_gotrequest(void *ctx,struct LP_quoteinfo *qp,stru
}
}
}
}
i = 0 ;
i = 0 ;
while ( i < 33 & & price > = myprice )
while ( i < priceiters & & 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 )
for ( j = 0 ; j < voliters ; j + + )
{
{
strcpy ( qp - > gui , G . gui ) ;
printf ( " priceiter.%d voliter.%d price %.8f vol %.8f \n " , i , j , price , dstr ( qp - > destsatoshis ) ) ;
strcpy ( qp - > coinaddr , coin - > smartaddr ) ;
if ( ( butxo = LP_address_myutxopair ( & B , 1 , utxos , max , LP_coinfind ( qp - > srccoin ) , qp - > coinaddr , qp - > txfee , dstr ( qp - > destsatoshis ) , price , qp - > desttxfee ) ) ! = 0 )
qp - > srchash = G . LP_mypub25519 ;
{
qp - > txid = butxo - > payment . txid ;
strcpy ( qp - > gui , G . gui ) ;
qp - > vout = butxo - > payment . vout ;
strcpy ( qp - > coinaddr , coin - > smartaddr ) ;
qp - > txid2 = butxo - > deposit . txid ;
qp - > srchash = G . LP_mypub25519 ;
qp - > vout2 = butxo - > deposit . vout ;
qp - > txid = butxo - > payment . txid ;
qp - > satoshis = butxo - > swap_satoshis ; // + qp->txfee;
qp - > vout = butxo - > payment . vout ;
qp - > quotetime = ( uint32_t ) time ( NULL ) ;
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 ) ) ;
if ( qp - > satoshis < = qp - > txfee )
return ( 0 ) ;
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 )
else if ( qp - > fill ! = 0 | | i = = priceiters )
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 )
{
{
price = 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 ) ) ;
printf ( " reset price <- qprice %.8f \n " , qprice ) ;
if ( qp - > gtc ! = 0 & & qp - > fill ! = 0 & & coin ! = 0 & & LP_getheight ( & notarized , coin ) > coin - > bobfillheight + 3 )
{
satoshis = LP_basesatoshis ( dstr ( qp - > destsatoshis ) , price , qp - > txfee , qp - > desttxfee ) + 3 * qp - > txfee ;
LP_address_utxo_reset ( & num , coin ) ;
if ( ( retstr = LP_autofillbob ( coin , satoshis * 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 - > bobfillheight = LP_getheight ( & notarized , coin ) ;
}
}
free_json ( retjson ) ;
}
free ( retstr ) ;
}
}
return ( 0 ) ;
}
}
else
else
{
{
if ( qprice > = p )
break ;
price * = 0.995 ;
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 ) ;
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 ( LP_allocated ( qp - > txid , qp - > vout ) = = 0 & & LP_allocated ( qp - > txid2 , qp - > vout2 ) = = 0 )
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");
//printf("found unallocated txids\n");
reqjson = LP_quotejson ( qp ) ;
reqjson = LP_quotejson ( qp ) ;
@ -1061,7 +1191,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 )
struct LP_quoteinfo * LP_trades_gotreserved ( void * ctx , struct LP_quoteinfo * qp , struct LP_quoteinfo * newqp )
{
{
char * retstr ; double qprice ;
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 fill.%d gtc.%d \n " , bits256_str ( str , G . LP_mypub25519 ) , qp - > uuidstr + 32 , ( double ) qp - > destsatoshis / ( qp - > satoshis + 1 ) , qp - > fill , qp - > gtc ) ;
* newqp = * qp ;
* newqp = * qp ;
qp = newqp ;
qp = newqp ;
if ( ( qprice = LP_trades_alicevalidate ( ctx , qp ) ) > 0. )
if ( ( qprice = LP_trades_alicevalidate ( ctx , qp ) ) > 0. )
@ -1103,7 +1233,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 )
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 * 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) fill.%d gtc.%d \n " , bits256_str ( str , G . LP_mypub25519 ) , ( long long ) qp - > aliceid , qp - > fill , qp - > gtc ) ;
* newqp = * qp ;
* newqp = * qp ;
qp = newqp ;
qp = newqp ;
if ( ( val = LP_trades_alicevalidate ( ctx , qp ) ) > 0. )
if ( ( val = LP_trades_alicevalidate ( ctx , qp ) ) > 0. )
@ -1380,7 +1510,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
}
}
if ( Q . satoshis < Q . txfee )
if ( Q . satoshis < Q . txfee )
return ( 1 ) ;
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 ;
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?
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 +1520,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
LP_tradecommand_log ( argjson ) ;
LP_tradecommand_log ( argjson ) ;
qprice = ( double ) Q . destsatoshis / ( Q . satoshis - Q . txfee ) ; //jdouble(argjson,"price");
qprice = ( double ) Q . destsatoshis / ( Q . satoshis - Q . txfee ) ; //jdouble(argjson,"price");
//printf("%s\n",jprint(argjson,0));
//printf("%s\n",jprint(argjson,0));
printf ( " %-4d uuid.%32s %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 g.%d f.%d %12s %5s/%-5s %12.8f -> %12.8f (%11.8f) | RT.%d %d n%d \n " , ( uint32_t ) time ( NULL ) % 3600 , Q . uuidstr + 32 , Q . gtc , Q . fill , method , Q . srccoin , Q . destcoin , dstr ( Q . satoshis ) , dstr ( Q . destsatoshis ) , qprice , LP_RTcount , LP_swapscount , G . netid ) ;
retval = 1 ;
retval = 1 ;
aliceid = j64bits ( argjson , " aliceid " ) ;
aliceid = j64bits ( argjson , " aliceid " ) ;
if ( strcmp ( method , " reserved " ) = = 0 )
if ( strcmp ( method , " reserved " ) = = 0 )
@ -1469,6 +1599,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 )
if ( Q . destvout ! = Q . feevout | | bits256_cmp ( Q . desttxid , Q . feetxid ) ! = 0 )
{
{
bestprice = LP_bob_competition ( & counter , aliceid , qprice , - 1 ) ;
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) )
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 " ) ) ;
LP_trades_gotrequest ( ctx , & Q , & Q2 , jstr ( argjson , " pair " ) ) ;
else LP_tradecommandQ ( & Q , jstr ( argjson , " pair " ) , LP_REQUEST ) ;
else LP_tradecommandQ ( & Q , jstr ( argjson , " pair " ) , LP_REQUEST ) ;
@ -1502,7 +1633,7 @@ int32_t LP_tradecommand(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,
return ( retval ) ;
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 ] ;
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 ) ;
basecoin = LP_coinfind ( base ) ;
@ -1589,6 +1720,10 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha
memset ( & A , 0 , sizeof ( A ) ) ;
memset ( & A , 0 , sizeof ( A ) ) ;
if ( ( autxo = LP_address_myutxopair ( & A , 0 , utxos , max , relcoin , relcoin - > smartaddr , txfee , dstr ( destsatoshis ) , maxprice , desttxfee ) ) ! = 0 )
if ( ( autxo = LP_address_myutxopair ( & A , 0 , utxos , max , relcoin , relcoin - > smartaddr , txfee , dstr ( destsatoshis ) , maxprice , desttxfee ) ) ! = 0 )
break ;
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 ;
destsatoshis * = 0.98 ;
if ( destsatoshis < desttxfee * LP_MINSIZE_TXFEEMULT )
if ( destsatoshis < desttxfee * LP_MINSIZE_TXFEEMULT )
break ;
break ;
@ -1640,6 +1775,8 @@ char *LP_autobuy(void *ctx,int32_t fomoflag,char *myipaddr,int32_t mypubsock,cha
}
}
}
}
int32_t changed ;
int32_t changed ;
Q . fill = fillflag ;
Q . gtc = gtcflag ;
LP_mypriceset ( & changed , rel , base , 1. / maxprice ) ;
LP_mypriceset ( & changed , rel , base , 1. / maxprice ) ;
LP_mypriceset ( & changed , base , rel , 0. ) ;
LP_mypriceset ( & changed , base , rel , 0. ) ;
if ( uuidstr = = 0 | | uuidstr [ 0 ] = = 0 )
if ( uuidstr = = 0 | | uuidstr [ 0 ] = = 0 )