Skip to content

Commit

Permalink
Fixed² balance display precision.
Browse files Browse the repository at this point in the history
████ ███  To request new features or in case this commit breaks something for you,
████ ███  please, create a new github issue with all possible information for me,
▓███▀█▄   but never share your API Keys!
▒▓██ ███
░▒▓█ ███  Signed-off-by: Carles Tubio <[email protected]>
 _________________________________________
/ Hello, WORLD!                           \
|                                         |
\ pssst.. 1.00000000 BTC = 61987.02 EUR.  /
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
  • Loading branch information
ctubio committed Oct 17, 2024
1 parent 2eb2786 commit 98e5a5b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ K ?= K.sh
MAJOR = 0
MINOR = 7
PATCH = 0
BUILD = 39
BUILD = 40

OBLIGATORY = DISCLAIMER: This is strict non-violent software: \n$\
if you hurt other living creatures, please stop; \n$\
Expand Down
12 changes: 7 additions & 5 deletions src/bin/+portfolios/+portfolios.client/Wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,13 @@ export class WalletsComponent {
this.selection = "";
}
else o.forEach(o => {
const amount = Shared.str(o.wallet.amount, 8);
const held = Shared.str(o.wallet.held, 8);
const total = Shared.str(o.wallet.amount + o.wallet.held, 8);
const balance = Shared.str(o.wallet.value, this.settings.currency == this.product.quote ? this.product.tickPrice : this.product.tickSize);
const price = Shared.str(o.price, 8);
var settingsPrecision = this.settings.currency == this.product.quote ? this.product.tickPrice : this.product.tickSize;
var has_settingsPrecision = this.settings.currency == o.wallet.currency;
const amount = Shared.str(o.wallet.amount, has_settingsPrecision ? settingsPrecision : -Math.log10(o.amountPrecision));
const held = Shared.str(o.wallet.held, has_settingsPrecision ? settingsPrecision : -Math.log10(o.amountPrecision));
const total = Shared.str(o.wallet.amount + o.wallet.held, has_settingsPrecision ? settingsPrecision : -Math.log10(o.amountPrecision));
const balance = Shared.str(o.wallet.value, settingsPrecision);
const price = Shared.str(o.price, has_settingsPrecision ? settingsPrecision : -Math.log10(o.pricePrecision));
sum += o.wallet.value;
var node: any = this.api.getRowNode(o.wallet.currency);
if (!node)
Expand Down
30 changes: 23 additions & 7 deletions src/bin/+portfolios/+portfolios.data.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ namespace analpaper {
struct Portfolio {
Wallet wallet;
unordered_map<string, Price> prices;
unordered_map<string, pair<Price, Amount>> precisions;
Price price;
pair<Price, Amount> precision;
};
static void to_json(json &j, const Portfolio &k) {
j = {
{"wallet", k.wallet},
{ "price", k.price }
{ "wallet", k.wallet },
{ "price", k.price },
{ "pricePrecision", k.precision.first },
{"amountPrecision", k.precision.second}
};
};

Expand Down Expand Up @@ -88,10 +92,13 @@ namespace analpaper {
return 0;
};
void calc() {
for (auto &it : portfolio)
for (auto &it : portfolio) {
portfolio.at(it.first).wallet.value = (
portfolio.at(it.first).price = calc(it.first)
) * portfolio.at(it.first).wallet.total;
portfolio.at(it.first).precision = portfolio.at(it.first).precisions.contains(settings.currency)
? portfolio.at(it.first).precisions.at(settings.currency) : (pair<Price, Amount>){1e-8, 1e-8};
}
if (ratelimit()) return;
broadcast();
K.repaint(true);
Expand Down Expand Up @@ -190,18 +197,26 @@ namespace analpaper {
struct Tickers: public Client::Clicked {
Markets markets;
private_ref:
Portfolios &portfolios;
const KryptoNinja &K;
Portfolios &portfolios;
public:
Tickers(const KryptoNinja &bot, Portfolios &p)
: Clicked(bot, {
{&p.settings, [&]() { calc(); }}
})
, markets(bot)
, K(bot)
, portfolios(p)
{};
void read_from_gw(const Ticker &raw) {
add(raw.base, raw.quote, raw.price);
add(raw.quote, raw.base, 1 / raw.price);
Price pricePrecision = 1e-8;
Amount amountPrecision = 1e-8;
if (K.gateway->precisions.contains(raw.symbol)) {
pricePrecision = K.gateway->precisions.at(raw.symbol).first;
amountPrecision = K.gateway->precisions.at(raw.symbol).second;
}
add(raw.base, raw.quote, raw.price, pricePrecision, amountPrecision);
add(raw.quote, raw.base, 1 / raw.price, amountPrecision, pricePrecision);
portfolios.calc();
markets.add(raw);
markets.calc(
Expand All @@ -224,8 +239,9 @@ namespace analpaper {
);
}
};
void add(const string &base, const string &quote, const Price &price) {
void add(const string &base, const string &quote, const Price &price, const Price &pricePrecision, const Amount &amountPrecision) {
portfolios.portfolio[base].prices[quote] = price;
portfolios.portfolio[base].precisions[quote] = {pricePrecision, amountPrecision};
if (portfolios.portfolio.at(base).wallet.currency.empty())
portfolios.portfolio.at(base).wallet.currency = base;
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Krypto.ninja-apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ namespace ₿ {
int maxLevel = 0;
bool debug = false;
Connectivity adminAgreement = Connectivity::Disconnected;
unordered_map<string, pair<double, double>> precisions;
unordered_map<string, pair<Price, Amount>> precisions;
void handshakes(const bool &nocache) {
json reply;
const string cache = (K_HOME "/cache/handshakes")
Expand Down

0 comments on commit 98e5a5b

Please sign in to comment.