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)
71 return AddressToHash160(address.toStdString(), hash160);
74 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
77 QSet<QString> setAddress;
80 if(recipients.empty())
85 // Pre-check input data for validity
86 foreach(const SendCoinsRecipient &rcp, recipients)
90 if(!AddressToHash160(rcp.address.toUtf8().constData(), hash160))
92 return InvalidAddress;
94 setAddress.insert(rcp.address);
103 if(recipients.size() > setAddress.size())
105 return DuplicateAddress;
108 if(total > getBalance())
110 return AmountExceedsBalance;
113 if((total + nTransactionFee) > getBalance())
115 return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
118 CRITICAL_BLOCK(cs_main)
119 CRITICAL_BLOCK(wallet->cs_mapWallet)
122 std::vector<std::pair<CScript, int64> > vecSend;
123 foreach(const SendCoinsRecipient &rcp, recipients)
125 CScript scriptPubKey;
126 scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
127 vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
131 CReserveKey keyChange(wallet);
132 int64 nFeeRequired = 0;
133 bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
137 if((total + nFeeRequired) > wallet->GetBalance())
139 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
141 return TransactionCreationFailed;
143 if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString(), NULL))
147 if(!wallet->CommitTransaction(wtx, keyChange))
149 return TransactionCommitFailed;
151 hex = QString::fromStdString(wtx.GetHash().GetHex());
154 // Add addresses that we've sent to to the address book
155 foreach(const SendCoinsRecipient &rcp, recipients)
157 std::string strAddress = rcp.address.toStdString();
158 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
160 if (!wallet->mapAddressBook.count(strAddress))
161 wallet->SetAddressBookName(strAddress, rcp.label.toStdString());
165 return SendCoinsReturn(OK, 0, hex);
168 OptionsModel *WalletModel::getOptionsModel()
173 AddressTableModel *WalletModel::getAddressTableModel()
175 return addressTableModel;
178 TransactionTableModel *WalletModel::getTransactionTableModel()
180 return transactionTableModel;