Skip to content

Commit

Permalink
[GUI] ColdStaking widget: fix "total staking" amount
Browse files Browse the repository at this point in the history
sum amounts only from owned delegations or received delegations that
have been whitelisted (thus actually staking).
Github-Pull: #1194
Rebased-From: 2f622b0
  • Loading branch information
random-zebra authored and Fuzzbawls committed Dec 14, 2019
1 parent 7ad5385 commit fa1987b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/qt/addresstablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ int AddressTableModel::lookupAddress(const QString& address) const
}
}

bool AddressTableModel::isWhitelisted(const std::string& address) const
{
return purposeForAddress(address).compare(AddressBook::AddressBookPurpose::DELEGATOR) == 0;
}

/**
* Return last created unused address --> TODO: complete "unused" and "last".. basically everything..
* @return
Expand Down
5 changes: 5 additions & 0 deletions src/qt/addresstablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class AddressTableModel : public QAbstractTableModel
*/
std::string purposeForAddress(const std::string& address) const;

/**
* Checks if the address is whitelisted
*/
bool isWhitelisted(const std::string& address) const;

/**
* Return last unused address
*/
Expand Down
49 changes: 38 additions & 11 deletions src/qt/pivx/coldstakingmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ void ColdStakingModel::refresh() {
del.delegatedUtxo.unite(delegation.delegatedUtxo);
del.cachedTotalAmount += delegation.cachedTotalAmount;
}

// add amount to cachedAmount if either:
// - this is a owned delegation
// - this is a staked delegation, and the owner is whitelisted
if (!delegation.isSpendable && !addressTableModel->isWhitelisted(delegation.ownerAddress)) continue;
cachedAmount += delegation.cachedTotalAmount;
}
}
Expand Down Expand Up @@ -135,22 +140,44 @@ QVariant ColdStakingModel::data(const QModelIndex &index, int role) const
return QVariant();
}

bool ColdStakingModel::whitelist(const QModelIndex& modelIndex) {
bool ColdStakingModel::whitelist(const QModelIndex& modelIndex)
{
QString address = modelIndex.data(Qt::DisplayRole).toString();
int idx = modelIndex.row();
beginRemoveRows(QModelIndex(), idx, idx);
bool ret = model->whitelistAddressFromColdStaking(address);
endRemoveRows();
emit dataChanged(index(idx, 0, QModelIndex()), index(idx, COLUMN_COUNT, QModelIndex()) );
return ret;
if (addressTableModel->isWhitelisted(address.toStdString())) {
return error("trying to whitelist already whitelisted address");
}

if (!model->whitelistAddressFromColdStaking(address)) return false;

// address whitelisted - update cached amount and row data
const int idx = modelIndex.row();
cachedAmount += cachedDelegations[idx].cachedTotalAmount;
removeRowAndEmitDataChanged(idx);

return true;
}

bool ColdStakingModel::blacklist(const QModelIndex& modelIndex) {
bool ColdStakingModel::blacklist(const QModelIndex& modelIndex)
{
QString address = modelIndex.data(Qt::DisplayRole).toString();
int idx = modelIndex.row();
if (!addressTableModel->isWhitelisted(address.toStdString())) {
return error("trying to blacklist already blacklisted address");
}

if (!model->blacklistAddressFromColdStaking(address)) return false;

// address blacklisted - update cached amount and row data
const int idx = modelIndex.row();
cachedAmount -= cachedDelegations[idx].cachedTotalAmount;
removeRowAndEmitDataChanged(idx);

return true;
}

void ColdStakingModel::removeRowAndEmitDataChanged(const int idx)
{
beginRemoveRows(QModelIndex(), idx, idx);
bool ret = model->blacklistAddressFromColdStaking(address);
endRemoveRows();
emit dataChanged(index(idx, 0, QModelIndex()), index(idx, COLUMN_COUNT, QModelIndex()) );
return ret;
}

3 changes: 2 additions & 1 deletion src/qt/pivx/coldstakingmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class ColdStakingModel : public QAbstractTableModel
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool whitelist(const QModelIndex& modelIndex);
bool blacklist(const QModelIndex& index);
void removeRowAndEmitDataChanged(const int idx);
void updateCSList();
CAmount getTotalAmount() { return cachedAmount; }
CAmount getTotalAmount() const { return cachedAmount; }

void refresh();

Expand Down
16 changes: 11 additions & 5 deletions src/qt/pivx/coldstakingwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,7 @@ void ColdStakingWidget::onDelegationsRefreshed() {
isLoading = false;
bool hasDel = csModel->rowCount() > 0;

// Update the total value.
CAmount total = csModel->getTotalAmount();
ui->labelStakingTotal->setText(tr("Total Staking: %1").arg(
(total == 0) ? "0.00 PIV" : GUIUtil::formatBalance(total, nDisplayUnit))
);
updateStakingTotalLabel();

// Update list if we are showing that section.
if (!isInDelegation) {
Expand Down Expand Up @@ -646,6 +642,7 @@ void ColdStakingWidget::onEditClicked() {
if (label.isEmpty()) {
label = index.data(Qt::DisplayRole).toString();
}
updateStakingTotalLabel();
inform(label + tr(" staking!"));
}

Expand All @@ -659,6 +656,7 @@ void ColdStakingWidget::onDeleteClicked() {
if (label.isEmpty()) {
label = index.data(Qt::DisplayRole).toString();
}
updateStakingTotalLabel();
inform(label + tr(" blacklisted from staking"));
}

Expand Down Expand Up @@ -731,6 +729,14 @@ void ColdStakingWidget::changeTheme(bool isLightTheme, QString& theme){
ui->listView->update();
}

void ColdStakingWidget::updateStakingTotalLabel()
{
const CAmount& total = csModel->getTotalAmount();
ui->labelStakingTotal->setText(tr("Total Staking: %1").arg(
(total == 0) ? "0.00 PIV" : GUIUtil::formatBalance(total, nDisplayUnit))
);
}

ColdStakingWidget::~ColdStakingWidget(){
if (sendMultiRow)
delete sendMultiRow;
Expand Down
1 change: 1 addition & 0 deletions src/qt/pivx/coldstakingwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private slots:
void tryRefreshDelegations();
bool refreshDelegations();
void onLabelClicked(QString dialogTitle, const QModelIndex &index, const bool& isMyColdStakingAddresses);
void updateStakingTotalLabel();
};

#endif // COLDSTAKINGWIDGET_H

0 comments on commit fa1987b

Please sign in to comment.