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

686 lines
30 KiB

8 years ago
/******************************************************************************
* Copyright © 2014-2017 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
// included from basilisk.c
8 years ago
// deposit address <coin> -> corresponding KMD address, if KMD deposit starts JUMBLR
// jumblr address <coin> is the destination of JUMBLR and JUMBLR BTC (would need tracking to map back to non-BTC)
// <symbol> address <coin> is DEX'ed for <SYMBOL>
// return value convention: -1 error, 0 partial match, >= 1 exact match
int32_t smartaddress_type(char *typestr)
{
char upper[64];
8 years ago
if ( strcmp(typestr,"deposit") != 0 && strcmp(typestr,"jumblr") != 0 && strcmp(typestr,"dividend") != 0 && strcmp(typestr,"pangea") != 0 )
8 years ago
{
upper[sizeof(upper)-1] = 0;
strncpy(upper,typestr,sizeof(upper)-1);
touppercase(upper);
if ( iguana_coinfind(upper) != 0 )
return(0);
8 years ago
else return(-1);
8 years ago
}
8 years ago
return(1);
8 years ago
}
bits256 jumblr_privkey(struct supernet_info *myinfo,char *coinaddr,uint8_t pubtype,char *KMDaddr,char *prefix)
{
bits256 privkey,pubkey; uint8_t pubkey33[33]; char passphrase[sizeof(myinfo->jumblr_passphrase) + 64];
sprintf(passphrase,"%s%s",prefix,myinfo->jumblr_passphrase);
if ( myinfo->jumblr_passphrase[0] == 0 )
strcpy(myinfo->jumblr_passphrase,"password");
conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase));
bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey);
bitcoin_address(coinaddr,pubtype,pubkey33,33);
bitcoin_address(KMDaddr,60,pubkey33,33);
8 years ago
//printf("(%s) -> (%s %s)\n",passphrase,coinaddr,KMDaddr);
8 years ago
return(privkey);
}
8 years ago
cJSON *smartaddress_extrajson(struct smartaddress *ap)
{
cJSON *retjson = cJSON_CreateObject();
if ( strcmp(ap->typestr,"dividend") == 0 )
{
}
return(retjson);
}
8 years ago
cJSON *smartaddress_json(struct smartaddress *ap)
{
char coinaddr[64],symbol[64]; uint8_t desttype,tmp,rmd160[20]; int32_t j,n; struct iguana_info *coin,*dest; cJSON *array,*item,*retjson; double amount;
8 years ago
retjson = cJSON_CreateObject();
jaddstr(retjson,"type",ap->typestr);
8 years ago
strcpy(symbol,ap->typestr), touppercase(symbol);
if ( (dest= iguana_coinfind(symbol)) != 0 )
desttype = dest->chain->pubtype;
else desttype = 60;
8 years ago
if ( (n= ap->numsymbols) > 0 )
8 years ago
{
8 years ago
array = cJSON_CreateArray();
8 years ago
for (j=0; j<n; j++)
8 years ago
{
8 years ago
if ( (coin= iguana_coinfind(ap->symbols[j].symbol)) != 0 )
8 years ago
{
8 years ago
bitcoin_address(coinaddr,coin->chain->pubtype,ap->pubkey33,33);
item = cJSON_CreateObject();
jaddstr(item,"coin",coin->symbol);
jaddstr(item,"address",coinaddr);
if ( (amount= ap->symbols[j].srcavail) != 0 )
jaddnum(item,"sourceamount",amount);
8 years ago
if ( dest != 0 )
{
bitcoin_addr2rmd160(&tmp,rmd160,coinaddr);
bitcoin_address(coinaddr,desttype,rmd160,20);
jaddstr(item,"dest",coinaddr);
if ( (amount= ap->symbols[j].destamount) != 0 )
jaddnum(item,"destamount",amount);
8 years ago
if ( coin->DEXinfo.depositaddr[0] != 0 )
{
jaddstr(item,"jumblr_deposit",coin->DEXinfo.depositaddr);
jaddnum(item,"deposit_avail",coin->DEXinfo.avail);
}
if ( coin->DEXinfo.jumblraddr[0] != 0 )
{
jaddstr(item,"jumblr",coin->DEXinfo.jumblraddr);
jaddnum(item,"jumblr_avail",coin->DEXinfo.jumblravail);
}
if ( ap->symbols[j].maxbid != 0. )
jaddnum(item,"maxbid",ap->symbols[j].maxbid);
if ( ap->symbols[j].minask != 0. )
jaddnum(item,"minask",ap->symbols[j].minask);
8 years ago
}
8 years ago
jadd(item,"extra",smartaddress_extrajson(ap));
8 years ago
jaddi(array,item);
8 years ago
}
}
8 years ago
jadd(retjson,"coins",array);
8 years ago
}
8 years ago
return(retjson);
8 years ago
}
8 years ago
void smartaddress_symboladd(struct smartaddress *ap,char *symbol,double maxbid,double minask)
{
8 years ago
char tmp[64]; struct smartaddress_symbol *sp;
strcpy(tmp,ap->typestr), touppercase(tmp);
if ( strcmp(tmp,symbol) != 0 )
{
ap->symbols = realloc(ap->symbols,(ap->numsymbols+1) * sizeof(*ap->symbols));
sp = &ap->symbols[ap->numsymbols++];
memset(sp,0,sizeof(*sp));
safecopy(sp->symbol,symbol,sizeof(sp->symbol));
sp->maxbid = maxbid;
sp->minask = minask;
8 years ago
printf("symboladd.%d (%s) <- (%s %f %f)\n",ap->numsymbols,ap->typestr,symbol,maxbid,minask);
8 years ago
}
8 years ago
}
struct smartaddress *smartaddressptr(struct smartaddress_symbol **ptrp,struct supernet_info *myinfo,char *_type,char *_symbol)
8 years ago
{
char type[64],symbol[64]; int32_t i,j,n; struct smartaddress *ap;
if ( ptrp != 0 )
*ptrp = 0;
8 years ago
strcpy(type,_type), tolowercase(type);
strcpy(symbol,_symbol), touppercase(symbol);
for (i=0; i<myinfo->numsmartaddrs; i++)
{
ap = &myinfo->smartaddrs[i];
if ( strcmp(type,ap->typestr) == 0 )
{
n = ap->numsymbols;
for (j=0; j<n; j++)
{
if ( strcmp(ap->symbols[j].symbol,symbol) == 0 )
{
if ( ptrp != 0 )
*ptrp = &ap->symbols[j];
return(ap);
8 years ago
}
}
}
}
return(0);
}
void smartaddress_minmaxupdate(struct supernet_info *myinfo,char *_type,char *_symbol,double maxbid,double minask)
{
struct smartaddress *ap; struct smartaddress_symbol *sp;
if ( (ap= smartaddressptr(&sp,myinfo,_type,_symbol)) != 0 && sp != 0 )
{
dxblend(&sp->maxbid,maxbid,0.5);
dxblend(&sp->minask,minask,0.5);
}
}
void smartaddress_availupdate(struct supernet_info *myinfo,char *_type,char *_symbol,double srcavail,double destamount)
{
struct smartaddress *ap; struct smartaddress_symbol *sp;
if ( (ap= smartaddressptr(&sp,myinfo,_type,_symbol)) != 0 && sp != 0 )
{
if ( srcavail > SMALLVAL )
sp->srcavail = srcavail;
if ( destamount > SMALLVAL )
sp->destamount = destamount;
}
8 years ago
}
8 years ago
int32_t _smartaddress_add(struct supernet_info *myinfo,bits256 privkey,char *type,char *symbol,double maxbid,double minask)
8 years ago
{
8 years ago
char coinaddr[64]; uint8_t addrtype,rmd160[20]; struct smartaddress *ap; int32_t i,j,n;
8 years ago
if ( myinfo->numsmartaddrs < sizeof(myinfo->smartaddrs)/sizeof(*myinfo->smartaddrs) )
{
for (i=0; i<myinfo->numsmartaddrs; i++)
8 years ago
{
ap = &myinfo->smartaddrs[i];
if ( strcmp(type,ap->typestr) == 0 && bits256_cmp(ap->privkey,privkey) == 0 )
8 years ago
{
8 years ago
n = ap->numsymbols;
for (j=0; j<n; j++)
8 years ago
{
8 years ago
if ( strcmp(ap->symbols[j].symbol,symbol) == 0 )
8 years ago
{
8 years ago
ap->symbols[j].maxbid = maxbid;
ap->symbols[j].minask = minask;
8 years ago
if ( maxbid > SMALLVAL && minask > SMALLVAL && smartaddress_type(type) == 0 )
smartaddress_minmaxupdate(myinfo,symbol,type,1./minask,1./maxbid);
8 years ago
return(0);
8 years ago
}
}
8 years ago
smartaddress_symboladd(ap,symbol,maxbid,minask);
8 years ago
return(i+1);
}
8 years ago
}
8 years ago
ap = &myinfo->smartaddrs[myinfo->numsmartaddrs];
if ( smartaddress_type(symbol) < 0 )
return(-1);
8 years ago
strcpy(ap->typestr,type);
smartaddress_symboladd(ap,"KMD",0.,0.);
smartaddress_symboladd(ap,"BTC",0.,0.);
8 years ago
ap->privkey = privkey;
bitcoin_pubkey33(myinfo->ctx,ap->pubkey33,privkey);
8 years ago
calc_rmd160_sha256(ap->rmd160,ap->pubkey33,33);
8 years ago
ap->pubkey = curve25519(privkey,curve25519_basepoint9());
char str[65]; printf("pubkey.(%s) ",bits256_str(str,ap->pubkey));
8 years ago
bitcoin_address(coinaddr,0,ap->pubkey33,33);
8 years ago
for (i=0; i<20; i++)
printf("%02x",ap->rmd160[i]);
8 years ago
bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
printf(", ");
for (i=0; i<20; i++)
printf("%02x",rmd160[i]);
8 years ago
printf (" <- rmd160 for %d %s\n",myinfo->numsmartaddrs,coinaddr);
return(++myinfo->numsmartaddrs + 1);
8 years ago
}
8 years ago
printf("too many smartaddresses %d vs %d\n",myinfo->numsmartaddrs,(int32_t)(sizeof(myinfo->smartaddrs)/sizeof(*myinfo->smartaddrs)));
8 years ago
return(-1);
}
8 years ago
int32_t smartaddress_add(struct supernet_info *myinfo,bits256 privkey,char *type,char *symbol,double maxbid,double minask)
8 years ago
{
int32_t retval;
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
retval = _smartaddress_add(myinfo,privkey,type,symbol,maxbid,minask);
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
return(retval);
}
int32_t smartaddress_symbolmatch(char *typestr,double *bidaskp,struct smartaddress *ap,char *symbol)
8 years ago
{
8 years ago
int32_t j,n;
strcpy(typestr,ap->typestr);
if ( (n= ap->numsymbols) > 0 )
8 years ago
{
8 years ago
for (j=0; j<n; j++)
8 years ago
{
8 years ago
if ( strcmp(ap->symbols[j].symbol,symbol) == 0 )
8 years ago
{
8 years ago
bidaskp[0] = ap->symbols[j].maxbid;
bidaskp[1] = ap->symbols[j].minask;
8 years ago
return(j);
8 years ago
}
8 years ago
}
}
return(-1);
}
8 years ago
int32_t smartaddress(struct supernet_info *myinfo,char *typestr,double *bidaskp,bits256 *privkeyp,char *symbol,char *coinaddr)
8 years ago
{
8 years ago
int32_t i,j,retval = -1; uint8_t addrtype,rmd160[20]; struct smartaddress *ap;
8 years ago
memset(privkeyp,0,sizeof(*privkeyp));
8 years ago
memset(bidaskp,0,sizeof(*bidaskp) * 2);
8 years ago
bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
8 years ago
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<myinfo->numsmartaddrs; i++)
if ( memcmp(myinfo->smartaddrs[i].rmd160,rmd160,20) == 0 )
{
8 years ago
ap = &myinfo->smartaddrs[i];
*privkeyp = ap->privkey;
8 years ago
if ( (j= smartaddress_symbolmatch(typestr,bidaskp,ap,symbol)) >= 0 )
retval = 0;
else retval = (i+1);
break;
8 years ago
}
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<20; i++)
printf("%02x",rmd160[i]);
printf(" <- rmd160 smartaddress cant find (%s) of %d\n",coinaddr,myinfo->numsmartaddrs);
8 years ago
return(retval);
8 years ago
}
8 years ago
int32_t smartaddress_pubkey(struct supernet_info *myinfo,char *typestr,double *bidaskp,bits256 *privkeyp,char *symbol,bits256 pubkey)
8 years ago
{
8 years ago
int32_t i,j,retval = -1; struct smartaddress *ap;
8 years ago
memset(privkeyp,0,sizeof(*privkeyp));
8 years ago
memset(bidaskp,0,sizeof(*bidaskp) * 2);
8 years ago
if ( bits256_cmp(myinfo->myaddr.persistent,pubkey) == 0 )
{
*privkeyp = myinfo->persistent_priv;
return(myinfo->numsmartaddrs);
}
8 years ago
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<myinfo->numsmartaddrs; i++)
if ( bits256_cmp(myinfo->smartaddrs[i].pubkey,pubkey) == 0 )
{
8 years ago
ap = &myinfo->smartaddrs[i];
8 years ago
*privkeyp = ap->privkey;
8 years ago
if ( (j= smartaddress_symbolmatch(typestr,bidaskp,ap,symbol)) >= 0 )
retval = 0;
else retval = (i+1);
break;
8 years ago
}
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
8 years ago
//char str[65]; if ( retval < 0 )
// printf("smartaddress_pubkey no match for %s\n",bits256_str(str,pubkey));
8 years ago
return(retval);
8 years ago
}
8 years ago
int32_t smartaddress_pubkey33(struct supernet_info *myinfo,char *typestr,double *bidaskp,bits256 *privkeyp,char *symbol,uint8_t *pubkey33)
8 years ago
{
8 years ago
int32_t i,j,retval = -1; struct smartaddress *ap;
8 years ago
memset(privkeyp,0,sizeof(*privkeyp));
8 years ago
memset(bidaskp,0,sizeof(*bidaskp) * 2);
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<myinfo->numsmartaddrs; i++)
if ( memcmp(myinfo->smartaddrs[i].pubkey33,pubkey33,33) == 0 )
{
8 years ago
ap = &myinfo->smartaddrs[i];
*privkeyp = ap->privkey;
8 years ago
if ( (j= smartaddress_symbolmatch(typestr,bidaskp,ap,symbol)) >= 0 )
retval = 0;
else retval = (i+1);
break;
8 years ago
}
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
return(retval);
8 years ago
}
8 years ago
void smartaddress_CMCname(char *CMCname,char *symbol)
{
if ( strcmp(symbol,"KMD") == 0 )
strcpy(CMCname,"komodo");
else if ( strcmp(symbol,"BTC") == 0 )
strcpy(CMCname,"bitcoin");
}
void smartaddress_coinupdate(struct supernet_info *myinfo,char *symbol,double BTC2KMD,double KMDavail,double KMD2USD)
{
int32_t r; double avebid,aveask,highbid,lowask,CMC_average,changes[3]; struct iguana_info *coin; struct DEXcoin_info *ptr;
if ( (coin= iguana_coinfind(symbol)) != 0 )
{
ptr = &coin->DEXinfo;
ptr->coin = coin;
if ( coin->CMCname[0] == 0 )
smartaddress_CMCname(coin->CMCname,symbol);
r = (((symbol[0]^symbol[1]^symbol[2])&0x7f) % 15) - 7; // 53 to 67 seconds
if ( time(NULL) > (ptr->lasttime + 60 + r) )
{
if ( strcmp(symbol,ptr->symbol) != 0 )
{
safecopy(ptr->symbol,symbol,sizeof(ptr->symbol));
safecopy(ptr->CMCname,coin->CMCname,sizeof(ptr->CMCname));
}
ptr->deposit_privkey = jumblr_privkey(myinfo,ptr->depositaddr,coin->chain->pubtype,ptr->KMDdepositaddr,JUMBLR_DEPOSITPREFIX);
ptr->jumblr_privkey = jumblr_privkey(myinfo,ptr->jumblraddr,coin->chain->pubtype,ptr->KMDjumblraddr,"");
ptr->avail = dstr(jumblr_balance(myinfo,coin,ptr->depositaddr));
ptr->jumblravail = dstr(jumblr_balance(myinfo,ptr->coin,ptr->jumblraddr));
if ( strcmp(symbol,"USD") == 0 )
{
if ( KMD2USD > SMALLVAL )
{
ptr->kmdprice = 1./ KMD2USD;
if ( (ptr->BTC2KMD= BTC2KMD) > SMALLVAL )
ptr->btcprice = ptr->kmdprice * BTC2KMD;
}
printf("USD btcprice %.8f kmdprice %.8f\n",ptr->btcprice,ptr->kmdprice);
}
else
{
if ( strcmp(symbol,"BTC") == 0 )
ptr->btcprice = 1.;
else if ( coin->CMCname[0] != 0 && (ptr->btcprice == 0. || (ptr->counter++ % 10) == 0) )
ptr->btcprice = get_theoretical(&avebid,&aveask,&highbid,&lowask,&CMC_average,changes,coin->CMCname,symbol,"BTC",&ptr->USD_average);
if ( strcmp("KMD",symbol) == 0 )
ptr->kmdprice = 1.;
else if ( (ptr->BTC2KMD= BTC2KMD) > SMALLVAL )
ptr->kmdprice = ptr->btcprice / BTC2KMD;
}
ptr->lasttime = (uint32_t)time(NULL);
printf("%s avail %.8f KMDavail %.8f btcprice %.8f deposit.(%s %s) -> jumblr.(%s %s)\n",symbol,ptr->avail,KMDavail,ptr->btcprice,ptr->depositaddr,ptr->KMDdepositaddr,ptr->jumblraddr,ptr->KMDjumblraddr);
}
} // else printf("skip\n");
}
void smartaddress_dex(struct supernet_info *myinfo,char *type,int32_t selector,struct iguana_info *basecoin,char *coinaddr,double maxavail,struct iguana_info *relcoin,double maxbid,double minask,cJSON *extraobj,double maxvol)
8 years ago
{
double minamount,minbtc,price,avail,vol,btc2kmd,basebtc,relbtc,baseusd,relusd; char *retstr; cJSON *vals; bits256 hash; struct smartaddress *ap;
8 years ago
basebtc = basecoin->DEXinfo.btcprice;
relbtc = relcoin->DEXinfo.btcprice;
baseusd = basecoin->DEXinfo.USD_average;
relusd = relcoin->DEXinfo.USD_average;
if ( (btc2kmd= basecoin->DEXinfo.BTC2KMD) < SMALLVAL && (btc2kmd= relcoin->DEXinfo.BTC2KMD) < SMALLVAL )
return;
minamount = price = 0.;
if ( basebtc < SMALLVAL && relbtc < SMALLVAL )
return;
8 years ago
if ( myinfo->DEXratio < .95 || myinfo->DEXratio > 1.01 )
myinfo->DEXratio = 0.995;
8 years ago
if ( basebtc < SMALLVAL || relbtc < SMALLVAL )
{
if ( (price= maxbid) > SMALLVAL )
{
if ( basebtc < SMALLVAL )
basebtc = price * relbtc, printf("calculated basebtc %.8f from (%.8f * %.8f)\n",basebtc,price,relbtc);
else if ( relbtc < SMALLVAL )
relbtc = basebtc / price, printf("calculated relbtc %.8f from (%.8f / %.8f)\n",relbtc,basebtc,price); // price * relbtc == basebtc
}
8 years ago
} else price = myinfo->DEXratio * (basebtc / relbtc);
8 years ago
minbtc = btc2kmd * (JUMBLR_INCR + 3*(JUMBLR_INCR * JUMBLR_FEE + JUMBLR_TXFEE));
if ( minamount == 0. && basebtc > SMALLVAL )
minamount = (minbtc / basebtc);
printf("DEX %s/%s maxavail %.8f minbtc %.8f btcprice %.8f -> minamount %.8f price %.8f vs maxbid %.8f DEXratio %.5f DEXpending %.8f\n",basecoin->symbol,relcoin->symbol,maxavail,minbtc,basecoin->DEXinfo.btcprice,minamount,price,maxbid,myinfo->DEXratio,basecoin->DEXinfo.DEXpending);
8 years ago
if ( minamount > SMALLVAL && maxavail > minamount + basecoin->DEXinfo.DEXpending && (maxbid == 0. || price <= maxbid) )
{
avail = (maxavail - basecoin->DEXinfo.DEXpending);
8 years ago
/*if ( avail >= (100. * minamount) )
8 years ago
vol = (100. * minamount);
else if ( avail >= (10. * minamount) )
vol = (10. * minamount);
8 years ago
else*/ if ( avail >= minamount )
8 years ago
vol = minamount;
else vol = 0.;
if ( vol > 0. )
{
vals = cJSON_CreateObject();
jaddstr(vals,"source",basecoin->symbol);
jaddstr(vals,"dest",relcoin->symbol);
jaddnum(vals,"amount",vol);
jaddnum(vals,"minprice",price);
if ( (ap= smartaddressptr(0,myinfo,type,basecoin->symbol)) != 0 )
jaddbits256(vals,"srchash",ap->pubkey);
8 years ago
if ( selector != 0 )
{
jaddnum(vals,"usejumblr",selector);
jaddnum(vals,"DEXselector",selector);
}
memset(hash.bytes,0,sizeof(hash));
basecoin->DEXinfo.DEXpending += vol;
if ( (retstr= InstantDEX_request(myinfo,basecoin,0,0,hash,vals,"")) != 0 )
{
printf("request.(%s) -> (%s)\n",jprint(vals,0),retstr);
free(retstr);
}
free_json(vals);
} else printf("avail %.8f < minamount %.8f\n",avail,minamount);
} //else printf("failed if check %d %d %d %d\n",minamount > SMALLVAL,maxavail > minamount + basecoin->DEXinfo.DEXpending,maxbid == 0,price <= maxbid);
8 years ago
/*
minbtc = (basecoin->DEXinfo.btcprice * 1.2) * (JUMBLR_INCR + 3*(JUMBLR_INCR * JUMBLR_FEE + JUMBLR_TXFEE));
btcavail = dstr(jumblr_balance(myinfo,coinbtc,kmdcoin->DEXinfo.depositaddr));
avail = (btcavail - coinbtc->DEXinfo.DEXpending);
printf("BTC.%d deposits %.8f, min %.8f avail %.8f pending %.8f\n",toKMD,btcavail,minbtc,avail,coinbtc->DEXinfo.DEXpending);
if ( toKMD == 0 && coinbtc != 0 && btcavail > (minbtc + coinbtc->DEXinfo.DEXpending) )
{
if ( vol > 0. )
{
vals = cJSON_CreateObject();
jaddstr(vals,"source","BTC");
jaddstr(vals,"dest","KMD");
jaddnum(vals,"amount",vol);
jaddnum(vals,"minprice",0.985/kmdcoin->DEXinfo.btcprice);
jaddnum(vals,"usejumblr",1);
jaddnum(vals,"DEXselector",1);
memset(hash.bytes,0,sizeof(hash));
coinbtc->DEXinfo.DEXpending += vol;
if ( (retstr= InstantDEX_request(myinfo,coinbtc,0,0,hash,vals,"")) != 0 )
{
printf("request.(%s) -> (%s)\n",jprint(vals,0),retstr);
free(retstr);
}
free_json(vals);
// curl --url "http://127.0.0.1:7778" --data "{\"agent\":\"InstantDEX\",\"method\":\"request\",\"vals\":{\"source\":\"KMD\",\"amount\":20,\"dest\":\"USD\",\"minprice\":0.08}}"
}
} //else printf("btcavail %.8f pending %.8f\n",btcavail,pending);
minkmd = 100.;
avail = (kmdcoin->DEXinfo.KMDavail - kmdcoin->DEXinfo.DEXpending);
printf("KMD.%d deposits %.8f, min %.8f, avail %.8f pending %.8f\n",toKMD,kmdcoin->DEXinfo.KMDavail,minkmd,avail,kmdcoin->DEXinfo.DEXpending);
if ( toKMD != 0 && coinbtc != 0 && kmdcoin->DEXinfo.KMDavail > (minkmd + kmdcoin->DEXinfo.DEXpending) )
{
if ( avail > 100.*JUMBLR_INCR )
vol = 100.*JUMBLR_INCR;
else if ( avail > 10.*JUMBLR_INCR )
vol = 10.*JUMBLR_INCR;
else if ( avail >= JUMBLR_INCR )
vol = JUMBLR_INCR;
else vol = 0.;
if ( vol > 0. )
{
vals = cJSON_CreateObject();
jaddstr(vals,"source","KMD");
jaddstr(vals,"dest","BTC");
jaddnum(vals,"amount",vol);
//jaddnum(vals,"destamount",JUMBLR_INCR*kmdcoin->DEXinfo.btcprice);
jaddnum(vals,"minprice",0.985 * kmdcoin->DEXinfo.btcprice);
jaddnum(vals,"usejumblr",2);
memset(hash.bytes,0,sizeof(hash));
kmdcoin->DEXinfo.DEXpending += vol;
jaddnum(vals,"DEXselector",2);
if ( (retstr= InstantDEX_request(myinfo,coinbtc,0,0,hash,vals,"")) != 0 )
{
printf("request.(%s) -> (%s)\n",jprint(vals,0),retstr);
free(retstr);
}
free_json(vals);
}
} else printf("kmdavail %.8f pending %.8f\n",kmdcoin->DEXinfo.avail,kmdcoin->DEXinfo.DEXpending);*/
}
void smartaddress_depositjumblr(struct supernet_info *myinfo,char *symbol,char *coinaddr,double maxbid,double minask,cJSON *extraobj)
{
struct iguana_info *basecoin,*relcoin;
if ( (basecoin= iguana_coinfind(symbol)) != 0 && (relcoin= iguana_coinfind("KMD")) != 0 )
{
if ( strcmp(coinaddr,basecoin->DEXinfo.depositaddr) == 0 )
smartaddress_dex(myinfo,"deposit",1,basecoin,coinaddr,basecoin->DEXinfo.avail,relcoin,maxbid,minask,extraobj,0.);
8 years ago
else printf("smartaddress_jumblr.%s: mismatch deposit address (%s) vs (%s)\n",symbol,coinaddr,basecoin->DEXinfo.depositaddr);
8 years ago
}
}
double smartaddress_jumblrcredit(struct supernet_info *myinfo,char *symbol)
{
return(0.); // default to BTC conversion for now
}
void smartaddress_jumblr(struct supernet_info *myinfo,char *symbol,char *coinaddr,double maxbid,double minask,cJSON *extraobj)
{
struct iguana_info *basecoin,*relcoin; double credits = 0.;
if ( strcmp("BTC",symbol) != 0 )
{
if ( (credits= smartaddress_jumblrcredit(myinfo,symbol)) <= 0. )
return;
}
if ( (basecoin= iguana_coinfind("KMD")) != 0 && (relcoin= iguana_coinfind(symbol)) != 0 )
{
if ( strcmp(coinaddr,basecoin->DEXinfo.jumblraddr) == 0 )
smartaddress_dex(myinfo,"jumblr",2,basecoin,coinaddr,basecoin->DEXinfo.jumblravail,relcoin,maxbid,minask,extraobj,credits);
8 years ago
else printf("smartaddress_jumblr.%s: mismatch jumblr address (%s) vs (%s)\n",symbol,coinaddr,basecoin->DEXinfo.jumblraddr);
8 years ago
}
}
void smartaddress_dividend(struct supernet_info *myinfo,char *symbol,char *coinaddr,double maxbid,double minask,cJSON *extraobj)
{
// support list of weighted addresses, including snapshots
}
void smartaddress_pangea(struct supernet_info *myinfo,char *symbol,char *coinaddr,double maxbid,double minask,cJSON *extraobj)
{
// table deposit
}
void smartaddress_action(struct supernet_info *myinfo,int32_t selector,char *typestr,char *symbol,char *coinaddr,double maxbid,double minask,cJSON *extraobj)
{
char rel[64]; struct iguana_info *basecoin,*relcoin; double avail;
if ( strcmp(typestr,"deposit") == 0 && selector == 0 )
smartaddress_depositjumblr(myinfo,symbol,coinaddr,maxbid,minask,extraobj);
else if ( strcmp(typestr,"jumblr") == 0 && selector == 0 )
smartaddress_jumblr(myinfo,symbol,coinaddr,maxbid,minask,extraobj);
else if ( strcmp(typestr,"dividend") == 0 && selector == 0 )
smartaddress_dividend(myinfo,symbol,coinaddr,maxbid,minask,extraobj);
else if ( strcmp(typestr,"pangea") == 0 && selector == 0 )
smartaddress_pangea(myinfo,symbol,coinaddr,maxbid,minask,extraobj);
else
{
safecopy(rel,typestr,sizeof(rel));
touppercase(rel);
if ( (relcoin= iguana_coinfind(rel)) != 0 && (basecoin= iguana_coinfind(symbol)) != 0 )
{
if ( myinfo->numswaps == 0 )//|| (basecoin->FULLNODE < 0 && relcoin->FULLNODE < 0) )
8 years ago
{
if ( (avail= dstr(jumblr_balance(myinfo,basecoin,coinaddr))) > SMALLVAL )
{
smartaddress_availupdate(myinfo,typestr,symbol,avail,SMALLVAL*0.99);
smartaddress_dex(myinfo,typestr,0,basecoin,coinaddr,avail,relcoin,maxbid,minask,extraobj,0.);
}
8 years ago
}
}
}
}
void smartaddress_update(struct supernet_info *myinfo,int32_t selector)
{
8 years ago
double maxbid,minask; uint8_t addrtype,rmd160[20]; char *smartstr,*typestr,*symbol,*address,coinaddr[64]; cJSON *smartarray,*extraobj,*item,*array,*coinitem; int32_t iter,i,n,j,m; struct iguana_info *kmdcoin,*coinbtc = 0;
8 years ago
//printf("smartaddress_update numswaps.%d notary.%d IAMLP.%d %p %p %f\n",myinfo->numswaps,myinfo->IAMNOTARY,myinfo->IAMLP,kmdcoin,coinbtc,kmdcoin->DEXinfo.btcprice);
if ( myinfo->IAMNOTARY != 0 || myinfo->IAMLP != 0 || myinfo->secret[0] == 0 )
return;
kmdcoin = iguana_coinfind("KMD");
coinbtc = iguana_coinfind("BTC");
if ( kmdcoin == 0 || coinbtc == 0 )
return;
smartaddress_coinupdate(myinfo,"KMD",0.,0.,0.); // must be first
if ( kmdcoin->DEXinfo.btcprice > SMALLVAL )
{
if ( (smartstr= InstantDEX_smartaddresses(myinfo,0,0,0)) != 0 )
{
if ( (smartarray= cJSON_Parse(smartstr)) != 0 )
{
if ( (n= cJSON_GetArraySize(smartarray)) > 0 )
{
for (iter=0; iter<2; iter++)
{
for (i=0; i<n; i++)
{
item = jitem(smartarray,i);
if ( (typestr= jstr(item,"type")) != 0 && (array= jarray(&m,item,"coins")) != 0 )
{
for (j=0; j<m; j++)
{
coinitem = jitem(array,j);
8 years ago
if ( (symbol= jstr(coinitem,"coin")) == 0 )
continue;
8 years ago
if ( iter == 0 )
smartaddress_coinupdate(myinfo,symbol,kmdcoin->DEXinfo.btcprice,kmdcoin->DEXinfo.avail,kmdcoin->DEXinfo.USD_average);
else
{
8 years ago
printf("Action.%s (%s)\n",typestr,jprint(coinitem,0));
8 years ago
if ( (address= jstr(coinitem,"address")) != 0 )
{
8 years ago
if ( strcmp(typestr,"jumblr") == 0 )
{
bitcoin_addr2rmd160(&addrtype,rmd160,address);
bitcoin_address(coinaddr,kmdcoin->chain->pubtype,rmd160,20);
} else strcpy(coinaddr,address);
8 years ago
maxbid = jdouble(coinitem,"maxbid");
minask = jdouble(coinitem,"minask");
extraobj = jobj(coinitem,"extra");
smartaddress_action(myinfo,selector,typestr,symbol,coinaddr,maxbid,minask,extraobj);
}
8 years ago
}
}
}
}
}
}
free_json(smartarray);
}
free(smartstr);
}
}
}
8 years ago
#include "../includes/iguana_apidefs.h"
#include "../includes/iguana_apideclares.h"
#include "../includes/iguana_apideclares2.h"
ZERO_ARGS(InstantDEX,smartaddresses)
{
int32_t i; cJSON *retjson = cJSON_CreateArray();
8 years ago
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<myinfo->numsmartaddrs; i++)
jaddi(retjson,smartaddress_json(&myinfo->smartaddrs[i]));
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
8 years ago
return(jprint(retjson,1));
}
8 years ago
TWO_STRINGS_AND_TWO_DOUBLES(InstantDEX,smartaddress,type,symbol,maxbid,minask)
8 years ago
{
8 years ago
char prefix[64],coinaddr[64],KMDaddr[64],typestr[64]; double bidask[2]; uint8_t pubkey33[33]; bits256 privkey;
8 years ago
if ( smartaddress_type(type) < 0 )
return(clonestr("{\"error\":\"non-supported smartaddress type\"}"));
if ( iguana_coinfind(symbol) == 0 )
return(clonestr("{\"error\":\"non-supported smartaddress symbol\"}"));
if ( strcmp(type,"deposit") == 0 || strcmp(type,"jumblr") == 0 )
{
8 years ago
if ( smartaddress_pubkey(myinfo,typestr,bidask,&privkey,symbol,strcmp(type,"deposit") == 0 ? myinfo->jumblr_depositkey : myinfo->jumblr_pubkey) < 0 )
8 years ago
return(clonestr("{\"error\":\"unexpected missing smartaddress deposit/jumblr\"}"));
}
else
{
strcpy(prefix,type);
tolowercase(prefix);
if ( strcmp(prefix,"btc") == 0 || strcmp(prefix,"kmd") == 0 )
return(clonestr("{\"success\":\"no need add BTC or KMD to smartaddress\"}"));
strcat(prefix," ");
privkey = jumblr_privkey(myinfo,coinaddr,0,KMDaddr,prefix);
}
if ( (coin= iguana_coinfind(symbol)) == 0 )
return(clonestr("{\"error\":\"non-supported smartaddress symbol\"}"));
bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey);
bitcoin_address(coinaddr,coin->chain->pubtype,pubkey33,33);
8 years ago
smartaddress_add(myinfo,privkey,type,symbol,maxbid,minask);
8 years ago
return(InstantDEX_smartaddresses(myinfo,0,0,0));
8 years ago
}
8 years ago
#include "../includes/iguana_apiundefs.h"