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.

105 lines
4.2 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. *
* *
******************************************************************************/
//
// LP_portfolio.c
// marketmaker
//
cJSON *LP_portfolio_entry(struct iguana_info *coin,uint64_t kmdsum)
{
cJSON *item = cJSON_CreateObject();
jaddstr(item,"coin",coin->symbol);
jaddnum(item,"amount",dstr(coin->maxamount));
jaddnum(item,"price",coin->price_kmd);
jaddnum(item,"kmd_equiv",dstr(coin->kmd_equiv));
jaddnum(item,"kmdsum",dstr(kmdsum));
8 years ago
jaddnum(item,"goal",coin->goal);
jaddnum(item,"perc",coin->perc);
jaddnum(item,"force",coin->force);
8 years ago
jaddnum(item,"balanceA",dstr(coin->balanceA));
jaddnum(item,"valuesumA",dstr(coin->valuesumA));
jaddnum(item,"aliceutil",100. * (double)coin->balanceA/coin->valuesumA);
jaddnum(item,"balanceB",dstr(coin->balanceB));
jaddnum(item,"valuesumB",dstr(coin->valuesumB));
jaddnum(item,"bobutil",100. * (double)coin->balanceB/coin->valuesumB);
return(item);
}
uint64_t LP_balance(uint64_t *valuep,int32_t iambob,char *symbol,char *coinaddr)
{
cJSON *array,*item; int32_t i,n; uint64_t valuesum,satoshisum;
valuesum = satoshisum = 0;
if ( (array= LP_inventory(symbol,iambob)) != 0 )
{
if ( (n= cJSON_GetArraySize(array)) > 0 && is_cJSON_Array(array) != 0 )
{
for (i=0; i<n; i++)
{
item = jitem(array,i);
8 years ago
valuesum += j64bits(item,"value") + j64bits(item,"value2");
8 years ago
satoshisum += j64bits(item,"satoshis");
}
}
free_json(array);
}
*valuep = valuesum;
return(satoshisum);
}
char *LP_portfolio()
{
8 years ago
double goalsum = 0.; uint64_t kmdsum = 0; int32_t iter; cJSON *retjson,*array; struct iguana_info *coin,*tmp;
8 years ago
array = cJSON_CreateArray();
retjson = cJSON_CreateObject();
for (iter=0; iter<2; iter++)
{
HASH_ITER(hh,LP_coins,coin,tmp)
{
8 years ago
if ( coin->inactive != 0 )
continue;
8 years ago
if ( iter == 0 )
{
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");
else coin->price_kmd = 1.;
coin->maxamount = coin->valuesumA;
if ( coin->valuesumB > coin->maxamount )
coin->maxamount = coin->valuesumB;
coin->kmd_equiv = coin->maxamount * coin->price_kmd;
kmdsum += coin->kmd_equiv;
8 years ago
goalsum += coin->goal;
8 years ago
}
else if ( coin->maxamount > 0 )
8 years ago
{
if ( goalsum > SMALLVAL && coin->goal > SMALLVAL )
{
coin->perc = 100. * coin->goal / goalsum;
coin->force = (coin->perc - coin->goal);
} else coin->perc = coin->force = 0.;
8 years ago
jaddi(array,LP_portfolio_entry(coin,kmdsum));
8 years ago
}
8 years ago
}
}
jaddstr(retjson,"result","success");
8 years ago
jaddnum(retjson,"kmd_equiv",dstr(kmdsum));
8 years ago
jadd(retjson,"portfolio",array);
return(jprint(retjson,1));
}