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, QObject *parent) :
13 QObject(parent), wallet(wallet), optionsModel(0), 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 optionsModel = new OptionsModel(wallet, this);
24 addressTableModel = new AddressTableModel(wallet, this);
25 transactionTableModel = new TransactionTableModel(wallet, this);
28 qint64 WalletModel::getBalance() const
30 return wallet->GetBalance();
33 qint64 WalletModel::getUnconfirmedBalance() const
35 return wallet->GetUnconfirmedBalance();
38 int WalletModel::getNumTransactions() const
40 int numTransactions = 0;
41 CRITICAL_BLOCK(wallet->cs_mapWallet)
43 numTransactions = wallet->mapWallet.size();
45 return numTransactions;
48 void WalletModel::update()
50 qint64 newBalance = getBalance();
51 qint64 newUnconfirmedBalance = getUnconfirmedBalance();
52 int newNumTransactions = getNumTransactions();
54 if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance)
55 emit balanceChanged(newBalance, newUnconfirmedBalance);
57 if(cachedNumTransactions != newNumTransactions)
58 emit numTransactionsChanged(newNumTransactions);
60 cachedBalance = newBalance;
61 cachedUnconfirmedBalance = newUnconfirmedBalance;
62 cachedNumTransactions = newNumTransactions;
64 addressTableModel->update();
67 bool WalletModel::validateAddress(const QString &address)
69 CBitcoinAddress addressParsed(address.toStdString());
70 return addressParsed.IsValid();
73 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
76 QSet<QString> setAddress;
79 if(recipients.empty())
84 // Pre-check input data for validity
85 foreach(const SendCoinsRecipient &rcp, recipients)
89 if(!validateAddress(rcp.address))
91 return InvalidAddress;
93 setAddress.insert(rcp.address);
102 if(recipients.size() > setAddress.size())
104 return DuplicateAddress;
107 if(total > getBalance())
109 return AmountExceedsBalance;
112 if((total + nTransactionFee) > getBalance())
114 return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
117 CRITICAL_BLOCK(cs_main)
118 CRITICAL_BLOCK(wallet->cs_mapWallet)
121 std::vector<std::pair<CScript, int64> > vecSend;
122 foreach(const SendCoinsRecipient &rcp, recipients)
124 CScript scriptPubKey;
125 scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
126 vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
130 CReserveKey keyChange(wallet);
131 int64 nFeeRequired = 0;
132 bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
136 if((total + nFeeRequired) > wallet->GetBalance())
138 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
140 return TransactionCreationFailed;
142 if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString(), NULL))
146 if(!wallet->CommitTransaction(wtx, keyChange))
148 return TransactionCommitFailed;
150 hex = QString::fromStdString(wtx.GetHash().GetHex());
153 // Add addresses that we've sent to to the address book
154 foreach(const SendCoinsRecipient &rcp, recipients)
156 std::string strAddress = rcp.address.toStdString();
157 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
159 if (!wallet->mapAddressBook.count(strAddress))
160 wallet->SetAddressBookName(strAddress, rcp.label.toStdString());
164 return SendCoinsReturn(OK, 0, hex);
167 OptionsModel *WalletModel::getOptionsModel()
172 AddressTableModel *WalletModel::getAddressTableModel()
174 return addressTableModel;
177 TransactionTableModel *WalletModel::getTransactionTableModel()
179 return transactionTableModel;