From 98e5a5b2e63f7332a0660ed1883875244df30620 Mon Sep 17 00:00:00 2001 From: Carles Tubio Date: Thu, 17 Oct 2024 15:22:11 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=C2=B2=20balance=20display=20precision.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ████ ███ 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 _________________________________________ / Hello, WORLD! \ | | \ pssst.. 1.00000000 BTC = 61987.02 EUR. / ----------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || --- Makefile | 2 +- .../+portfolios/+portfolios.client/Wallets.ts | 12 ++++---- src/bin/+portfolios/+portfolios.data.h | 30 ++++++++++++++----- src/lib/Krypto.ninja-apis.h | 2 +- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 94f598289..48f655812 100644 --- a/Makefile +++ b/Makefile @@ -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$\ diff --git a/src/bin/+portfolios/+portfolios.client/Wallets.ts b/src/bin/+portfolios/+portfolios.client/Wallets.ts index 0b2fe2e5b..379f8f9e1 100644 --- a/src/bin/+portfolios/+portfolios.client/Wallets.ts +++ b/src/bin/+portfolios/+portfolios.client/Wallets.ts @@ -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) diff --git a/src/bin/+portfolios/+portfolios.data.h b/src/bin/+portfolios/+portfolios.data.h index aa79e9060..83a98d650 100644 --- a/src/bin/+portfolios/+portfolios.data.h +++ b/src/bin/+portfolios/+portfolios.data.h @@ -51,12 +51,16 @@ namespace analpaper { struct Portfolio { Wallet wallet; unordered_map prices; + unordered_map> precisions; Price price; + pair 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} }; }; @@ -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){1e-8, 1e-8}; + } if (ratelimit()) return; broadcast(); K.repaint(true); @@ -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( @@ -224,8 +239,9 @@ namespace analpaper { ); } }; - void add(const string &base, const string "e, const Price &price) { + void add(const string &base, const string "e, 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; }; diff --git a/src/lib/Krypto.ninja-apis.h b/src/lib/Krypto.ninja-apis.h index 13aa9dff1..4b9f229fc 100644 --- a/src/lib/Krypto.ninja-apis.h +++ b/src/lib/Krypto.ninja-apis.h @@ -325,7 +325,7 @@ namespace ₿ { int maxLevel = 0; bool debug = false; Connectivity adminAgreement = Connectivity::Disconnected; - unordered_map> precisions; + unordered_map> precisions; void handshakes(const bool &nocache) { json reply; const string cache = (K_HOME "/cache/handshakes")