Skip to content

Commit

Permalink
#336 fixed problem with sending to expired own address
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-strilets committed Jan 11, 2019
1 parent cd04488 commit c282e62
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 11 deletions.
22 changes: 22 additions & 0 deletions ui/model/wallet_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,26 @@ void WalletModel::sendMoney(const beam::WalletID& receiver, const std::string& c
{
try
{
auto receiverAddr = _walletDB->getAddress(receiver);

if (receiverAddr)
{
if (receiverAddr->isExpired())
{
emit cantSendToExpired();
return;
}
}
else
{
WalletAddress peerAddr;
peerAddr.m_walletID = receiver;
peerAddr.m_createTime = getTimestamp();
peerAddr.m_label = comment;

saveAddress(peerAddr, false);
}

WalletAddress senderAddress = wallet::createAddress(_walletDB);
senderAddress.m_label = comment;
saveAddress(senderAddress, true); // should update the wallet_network
Expand All @@ -428,6 +448,8 @@ void WalletModel::sendMoney(const beam::WalletID& receiver, const std::string& c
{
s->transfer_money(senderAddress.m_walletID, receiver, move(amount), move(fee), true, 120, move(message));
}

emit sendMoneyVerified();
}
catch (...)
{
Expand Down
2 changes: 2 additions & 0 deletions ui/model/wallet_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class WalletModel
void onChangeCurrentWalletIDs(beam::WalletID senderID, beam::WalletID receiverID);
void nodeConnectionChanged(bool isNodeConnected);
void onWalletError(beam::wallet::ErrorType error);
void sendMoneyVerified();
void cantSendToExpired();

private:
void onCoinsChanged() override;
Expand Down
60 changes: 57 additions & 3 deletions ui/view/wallet.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,64 @@ Item {
id: root
anchors.fill: parent

WalletViewModel {id: viewModel}
WalletViewModel {
id: viewModel
onSendMoneyVerified: {
walletView.enabled = true
console.log("onSendMoneyVerified");
walletView.pop();
}

onCantSendToExpired: {
walletView.enabled = true
console.log("can't send to expired address");
cantSendToExpiredDialog.open();
}
}

property bool toSend: false

Dialog {
id: cantSendToExpiredDialog

modal: true

width: 300
height: 160

x: (parent.width - width) / 2
y: (parent.height - height) / 2
visible: false

background: Rectangle {
radius: 10
color: Style.dark_slate_blue
anchors.fill: parent
}

contentItem: Column {
anchors.fill: parent
anchors.margins: 30

spacing: 40

SFText {
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Can't send to the expired address.")
color: Style.white
font.pixelSize: 14
font.styleName: "Bold"; font.weight: Font.Bold
}

PrimaryButton {
text: qsTr("ok")
anchors.horizontalCenter: parent.horizontalCenter
icon.source: "qrc:/assets/icon-done.svg"
onClicked: cantSendToExpiredDialog.close()
}
}
}

ConfirmationDialog {
id: confirmationDialog
okButtonColor: Style.heliotrope
Expand Down Expand Up @@ -120,8 +174,8 @@ Item {
}

onAccepted: {
viewModel.sendMoney()
walletView.pop();
viewModel.sendMoney();
walletView.enabled = false;
}
}

Expand Down
27 changes: 19 additions & 8 deletions ui/viewmodel/wallet_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ WalletViewModel::WalletViewModel()
connect(&_model, SIGNAL(onGeneratedNewAddress(const beam::WalletAddress&)),
SLOT(onGeneratedNewAddress(const beam::WalletAddress&)));

connect(&_model, SIGNAL(sendMoneyVerified()), SLOT(onSendMoneyVerified()));

connect(&_model, SIGNAL(cantSendToExpired()), SLOT(onCantSendToExpired()));

_model.getAsync()->getWalletStatus();
}

Expand Down Expand Up @@ -610,17 +614,12 @@ void WalletViewModel::sendMoney()
{
if (/*!_senderAddr.isEmpty() && */isValidReceiverAddress(getReceiverAddr()))
{
WalletAddress peerAddr;
peerAddr.m_walletID.FromHex(getReceiverAddr().toStdString());
peerAddr.m_createTime = getTimestamp();
peerAddr.m_label = _comment.toStdString();
WalletID walletID(Zero);

// TODO: implement UI for this situation
// TODO: don't save if you send to yourself
_model.getAsync()->saveAddress(peerAddr, false);
walletID.FromHex(getReceiverAddr().toStdString());

// TODO: show 'operation in process' animation here?
_model.getAsync()->sendMoney(peerAddr.m_walletID, _comment.toStdString(), calcSendAmount(), calcFeeAmount());
_model.getAsync()->sendMoney(walletID, _comment.toStdString(), calcSendAmount(), calcFeeAmount());
}
}

Expand Down Expand Up @@ -732,3 +731,15 @@ void WalletViewModel::onGeneratedNewAddress(const beam::WalletAddress& addr)

emit newReceiverAddrChanged();
}

void WalletViewModel::onSendMoneyVerified()
{
// retranslate to qml
emit sendMoneyVerified();
}

void WalletViewModel::onCantSendToExpired()
{
// retranslate to qml
emit cantSendToExpired();
}
4 changes: 4 additions & 0 deletions ui/viewmodel/wallet_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public slots:
void onChangeCurrentWalletIDs(beam::WalletID senderID, beam::WalletID receiverID);
void onAdrresses(bool own, const std::vector<beam::WalletAddress>& addresses);
void onGeneratedNewAddress(const beam::WalletAddress& addr);
void onSendMoneyVerified();
void onCantSendToExpired();

signals:
void stateChanged();
Expand All @@ -209,6 +211,8 @@ public slots:
void newReceiverNameChanged();
void commentChanged();
void expiresChanged();
void sendMoneyVerified();
void cantSendToExpired();

private:
beam::Amount calcSendAmount() const;
Expand Down

0 comments on commit c282e62

Please sign in to comment.