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)
16 // Until signal notifications is built into the bitcoin core,
17 // simply update everything after polling using a timer.
18 QTimer *timer = new QTimer(this);
19 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
20 timer->start(MODEL_UPDATE_DELAY);
22 optionsModel = new OptionsModel(wallet, this);
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 // Plainly emit all signals for now. To be more efficient this should check
50 // whether the values actually changed first, although it'd be even better if these
51 // were events coming in from the bitcoin core.
52 emit balanceChanged(getBalance(), wallet->GetUnconfirmedBalance());
53 emit numTransactionsChanged(getNumTransactions());
55 addressTableModel->update();
58 bool WalletModel::validateAddress(const QString &address)
62 return AddressToHash160(address.toStdString(), hash160);
65 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
68 QSet<QString> setAddress;
71 if(recipients.empty())
76 // Pre-check input data for validity
77 foreach(const SendCoinsRecipient &rcp, recipients)
81 if(!AddressToHash160(rcp.address.toUtf8().constData(), hash160))
83 return InvalidAddress;
85 setAddress.insert(rcp.address);
94 if(recipients.size() > setAddress.size())
96 return DuplicateAddress;
99 if(total > getBalance())
101 return AmountExceedsBalance;
104 if((total + nTransactionFee) > getBalance())
106 return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
109 CRITICAL_BLOCK(cs_main)
110 CRITICAL_BLOCK(wallet->cs_mapWallet)
113 std::vector<std::pair<CScript, int64> > vecSend;
114 foreach(const SendCoinsRecipient &rcp, recipients)
116 CScript scriptPubKey;
117 scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
118 vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
122 CReserveKey keyChange(wallet);
123 int64 nFeeRequired = 0;
124 bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
128 if((total + nFeeRequired) > wallet->GetBalance())
130 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
132 return TransactionCreationFailed;
134 if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString(), NULL))
138 if(!wallet->CommitTransaction(wtx, keyChange))
140 return TransactionCommitFailed;
142 hex = QString::fromStdString(wtx.GetHash().GetHex());
145 // Add addresses that we've sent to to the address book
146 foreach(const SendCoinsRecipient &rcp, recipients)
148 std::string strAddress = rcp.address.toStdString();
149 CRITICAL_BLOCK(wallet->cs_mapAddressBook)
151 if (!wallet->mapAddressBook.count(strAddress))
152 wallet->SetAddressBookName(strAddress, rcp.label.toStdString());
156 return SendCoinsReturn(OK, 0, hex);
159 OptionsModel *WalletModel::getOptionsModel()
164 AddressTableModel *WalletModel::getAddressTableModel()
166 return addressTableModel;
169 TransactionTableModel *WalletModel::getTransactionTableModel()
171 return transactionTableModel;