1 #include "walletmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
12 WalletModel::WalletModel(CWallet *wallet, OptionsModel *optionsModel, QObject *parent) :
13 QObject(parent), wallet(wallet), optionsModel(optionsModel), addressTableModel(0),
14 transactionTableModel(0),
15 cachedBalance(0), cachedUnconfirmedBalance(0), cachedNumTransactions(0)
17 // Until signal notifications is built into the bitcoin core,
18 // simply update everything after polling using a timer.
19 QTimer *timer = new QTimer(this);
20 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
21 timer->start(MODEL_UPDATE_DELAY);
23 addressTableModel = new AddressTableModel(wallet, this);
24 transactionTableModel = new TransactionTableModel(wallet, this);
27 qint64 WalletModel::getBalance() const
29 return wallet->GetBalance();
32 qint64 WalletModel::getUnconfirmedBalance() const
34 return wallet->GetUnconfirmedBalance();
37 int WalletModel::getNumTransactions() const
39 int numTransactions = 0;
40 CRITICAL_BLOCK(wallet->cs_mapWallet)
42 numTransactions = wallet->mapWallet.size();
44 return numTransactions;
47 void WalletModel::update()
49 qint64 newBalance = getBalance();
50 qint64 newUnconfirmedBalance = getUnconfirmedBalance();
51 int newNumTransactions = getNumTransactions();
53 if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance)
54 emit balanceChanged(newBalance, newUnconfirmedBalance);
56 if(cachedNumTransactions != newNumTransactions)
57 emit numTransactionsChanged(newNumTransactions);
59 cachedBalance = newBalance;
60 cachedUnconfirmedBalance = newUnconfirmedBalance;
61 cachedNumTransactions = newNumTransactions;
63 addressTableModel->update();
66 bool WalletModel::validateAddress(const QString &address)
68 CBitcoinAddress addressParsed(address.toStdString());
69 return addressParsed.IsValid();
72 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
75 QSet<QString> setAddress;
78 if(recipients.empty())
83 // Pre-check input data for validity
84 foreach(const SendCoinsRecipient &rcp, recipients)
86 if(!validateAddress(rcp.address))
88 return InvalidAddress;
90 setAddress.insert(rcp.address);
99 if(recipients.size() > setAddress.size())
101 return DuplicateAddress;
104 if(total > getBalance())
106 return AmountExceedsBalance;
109 if((total + nTransactionFee) > getBalance())
111 return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
114 CRITICAL_BLOCK(cs_main)
115 CRITICAL_BLOCK(wallet->cs_mapWallet)
118 std::vector<std::pair<CScript, int64> > vecSend;
119 foreach(const SendCoinsRecipient &rcp, recipients)
121 CScript scriptPubKey;
122 scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
123 vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
127 CReserveKey keyChange(wallet);
128 int64 nFeeRequired = 0;
129 bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
133 if((total + nFeeRequired) > wallet->GetBalance())
135 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
137 return TransactionCreationFailed;
139 if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString(), NULL))
143 if(!wallet->CommitTransaction(wtx, keyChange))
145 return TransactionCommitFailed;
147 hex = QString::fromStdString(wtx.GetHash().GetHex());
150 // Add addresses that we've sent to to the address book
151 foreach(const SendCoinsRecipient &rcp, recipients)
153 std::string strAddress = rcp.address.toStdString();
154 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
156 if (!wallet->mapAddressBook.count(strAddress))
157 wallet->SetAddressBookName(strAddress, rcp.label.toStdString());
161 // Update our model of the address table
162 addressTableModel->updateList();
164 return SendCoinsReturn(OK, 0, hex);
167 OptionsModel *WalletModel::getOptionsModel()
172 AddressTableModel *WalletModel::getAddressTableModel()
174 return addressTableModel;
177 TransactionTableModel *WalletModel::getTransactionTableModel()
179 return transactionTableModel;