diff --git a/lib/Cargo.lock b/lib/Cargo.lock index a325abe..6f0ae92 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -1051,7 +1051,7 @@ version = "0.1.0" dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", - "zecwalletlitelib 0.1.0 (git+https://github.com/adityapk00/zecwallet-light-cli?rev=b928d5f09646cc94023ea25f99951fcf1b43e90d)", + "zecwalletlitelib 0.1.0 (git+https://github.com/adityapk00/zecwallet-light-cli?rev=50d331b0cfe1b3c4b81e33fd6febb4b24264627a)", ] [[package]] @@ -2266,7 +2266,7 @@ dependencies = [ [[package]] name = "zecwalletlitelib" version = "0.1.0" -source = "git+https://github.com/adityapk00/zecwallet-light-cli?rev=b928d5f09646cc94023ea25f99951fcf1b43e90d#b928d5f09646cc94023ea25f99951fcf1b43e90d" +source = "git+https://github.com/adityapk00/zecwallet-light-cli?rev=50d331b0cfe1b3c4b81e33fd6febb4b24264627a#50d331b0cfe1b3c4b81e33fd6febb4b24264627a" dependencies = [ "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "bellman 0.1.0 (git+https://github.com/adityapk00/librustzcash.git?rev=188537ea025fcb7fbdfc11266f307a084a5451e4)", @@ -2562,4 +2562,4 @@ dependencies = [ "checksum zcash_client_backend 0.0.0 (git+https://github.com/adityapk00/librustzcash.git?rev=188537ea025fcb7fbdfc11266f307a084a5451e4)" = "" "checksum zcash_primitives 0.0.0 (git+https://github.com/adityapk00/librustzcash.git?rev=188537ea025fcb7fbdfc11266f307a084a5451e4)" = "" "checksum zcash_proofs 0.0.0 (git+https://github.com/adityapk00/librustzcash.git?rev=188537ea025fcb7fbdfc11266f307a084a5451e4)" = "" -"checksum zecwalletlitelib 0.1.0 (git+https://github.com/adityapk00/zecwallet-light-cli?rev=b928d5f09646cc94023ea25f99951fcf1b43e90d)" = "" +"checksum zecwalletlitelib 0.1.0 (git+https://github.com/adityapk00/zecwallet-light-cli?rev=50d331b0cfe1b3c4b81e33fd6febb4b24264627a)" = "" diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 31b1de2..8987dc1 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -11,4 +11,4 @@ crate-type = ["staticlib"] [dependencies] libc = "0.2.58" lazy_static = "1.4.0" -zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "92d3804a5c67ca7621b141518a1f72451ca6ff40" } +zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "3e1c61a4b0589be1ff7590cf4ddf025a9160c631" } diff --git a/src/controller.cpp b/src/controller.cpp index da5f522..392043c 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -272,6 +272,34 @@ void Controller::processUnspent(const json& reply, QMap* balan processFn(reply["pending_utxos"].get()); }; +void Controller::updateUIBalances() { + CAmount balT = getModel()->getBalT(); + CAmount balZ = getModel()->getBalZ(); + CAmount balVerified = getModel()->getBalVerified(); + + // Reduce the BalanceZ by the pending outgoing amount. We're adding + // here because totalPending is already negative for outgoing txns. + balZ = balZ + getModel()->getTotalPending(); + + CAmount balTotal = balT + balZ; + CAmount balAvailable = balT + balVerified; + + // Balances table + ui->balSheilded ->setText(balZ.toDecimalZECString()); + ui->balVerified ->setText(balVerified.toDecimalZECString()); + ui->balTransparent->setText(balT.toDecimalZECString()); + ui->balTotal ->setText(balTotal.toDecimalZECString()); + + ui->balSheilded ->setToolTip(balZ.toDecimalUSDString()); + ui->balVerified ->setToolTip(balVerified.toDecimalUSDString()); + ui->balTransparent->setToolTip(balT.toDecimalUSDString()); + ui->balTotal ->setToolTip(balTotal.toDecimalUSDString()); + + // Send tab + ui->txtAvailableZEC->setText(balAvailable.toDecimalZECString()); + ui->txtAvailableUSD->setText(balAvailable.toDecimalUSDString()); +} + void Controller::refreshBalances() { if (!zrpc->haveConnection()) return noConnection(); @@ -282,29 +310,18 @@ void Controller::refreshBalances() { CAmount balZ = CAmount::fromqint64(reply["zbalance"].get()); CAmount balVerified = CAmount::fromqint64(reply["verified_zbalance"].get()); - CAmount balTotal = balT + balZ; - CAmount balAvailable = balT + balVerified; + model->setBalT(balT); + model->setBalZ(balZ); + model->setBalVerified(balVerified); // This is for the websockets AppDataModel::getInstance()->setBalances(balT, balZ); // This is for the datamodel + CAmount balAvailable = balT + balVerified; model->setAvailableBalance(balAvailable); - // Balances table - ui->balSheilded ->setText(balZ.toDecimalZECString()); - ui->balVerified ->setText(balVerified.toDecimalZECString()); - ui->balTransparent->setText(balT.toDecimalZECString()); - ui->balTotal ->setText(balTotal.toDecimalZECString()); - - ui->balSheilded ->setToolTip(balZ.toDecimalUSDString()); - ui->balVerified ->setToolTip(balVerified.toDecimalUSDString()); - ui->balTransparent->setToolTip(balT.toDecimalUSDString()); - ui->balTotal ->setToolTip(balTotal.toDecimalUSDString()); - - // Send tab - ui->txtAvailableZEC->setText(balAvailable.toDecimalZECString()); - ui->txtAvailableUSD->setText(balAvailable.toDecimalUSDString()); + updateUIBalances(); }); // 2. Get the UTXOs @@ -409,6 +426,21 @@ void Controller::refreshTransactions() { } + // Calculate the total unspent amount that's pending. This will need to be + // shown in the UI so the user can keep track of pending funds + CAmount totalPending; + for (auto txitem : txdata) { + if (txitem.confirmations == 0) { + for (auto item: txitem.items) { + totalPending = totalPending + item.amount; + } + } + } + getModel()->setTotalPending(totalPending); + + // Update UI Balance + updateUIBalances(); + // Update model data, which updates the table view transactionsTableModel->replaceData(txdata); }); diff --git a/src/controller.h b/src/controller.h index e885876..c2eb326 100644 --- a/src/controller.h +++ b/src/controller.h @@ -103,6 +103,7 @@ private: void processUnspent (const json& reply, QMap* newBalances, QList* newUnspentOutputs); void updateUI (bool anyUnconfirmed); + void updateUIBalances (); void getInfoThenRefresh (bool force); diff --git a/src/datamodel.cpp b/src/datamodel.cpp index e043466..c1c4d66 100644 --- a/src/datamodel.cpp +++ b/src/datamodel.cpp @@ -24,6 +24,7 @@ DataModel::~DataModel() { } void DataModel::setLatestBlock(int blockHeight) { + QReadLocker locker(lock); this->latestBlock = blockHeight; } diff --git a/src/datamodel.h b/src/datamodel.h index e20261a..4d2ac06 100644 --- a/src/datamodel.h +++ b/src/datamodel.h @@ -26,7 +26,7 @@ public: void markAddressUsed(QString address); void setLatestBlock(int blockHeight); - int getLatestBlock() { return this->latestBlock; } + int getLatestBlock() { QReadLocker locker(lock); return this->latestBlock; } void setEncryptionStatus(bool encrypted, bool locked) { this->isEncrypted = encrypted; this->isLocked = locked; } QPair getEncryptionStatus() { return qMakePair(this->isEncrypted, this->isLocked); } @@ -37,8 +37,20 @@ public: const QMap getAllBalances() { QReadLocker locker(lock); return *balances; } const QMap getUsedAddresses() { QReadLocker locker(lock); return *usedAddresses; } - CAmount getAvailableBalance() { return availableBalance; } - void setAvailableBalance(CAmount a) { this->availableBalance = a; } + CAmount getAvailableBalance() { QReadLocker locker(lock); return availableBalance; } + void setAvailableBalance(CAmount a) { QReadLocker locker(lock); this->availableBalance = a; } + + CAmount getBalT() { QReadLocker locker(lock); return balT; } + void setBalT(CAmount a) { QReadLocker locker(lock); this->balT = a; } + + CAmount getBalZ() { QReadLocker locker(lock); return balZ; } + void setBalZ(CAmount a) { QReadLocker locker(lock); this->balZ = a; } + + CAmount getBalVerified() { QReadLocker locker(lock); return balVerified; } + void setBalVerified(CAmount a) { QReadLocker locker(lock); this->balVerified = a; } + + CAmount getTotalPending() { QReadLocker locker(lock); return totalPending; } + void setTotalPending(CAmount a) { QReadLocker locker(lock); this->totalPending = a; } DataModel(); ~DataModel(); @@ -55,9 +67,13 @@ private: QList* taddresses = nullptr; CAmount availableBalance; + CAmount totalPending; // Outgoing pending is -ve - QReadWriteLock* lock; + CAmount balT; + CAmount balZ; + CAmount balVerified; + QReadWriteLock* lock; }; #endif // DATAMODEL_H \ No newline at end of file