update readme
[novacoin.git] / src / qt / walletmodel.cpp
1 #include "walletmodel.h"
2 #include "guiconstants.h"
3 #include "optionsmodel.h"
4 #include "addresstablemodel.h"
5 #include "transactiontablemodel.h"
6
7 #include "headers.h"
8
9 #include <QTimer>
10 #include <QSet>
11
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)
16 {
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);
22
23     optionsModel = new OptionsModel(wallet, this);
24     addressTableModel = new AddressTableModel(wallet, this);
25     transactionTableModel = new TransactionTableModel(wallet, this);
26 }
27
28 qint64 WalletModel::getBalance() const
29 {
30     return wallet->GetBalance();
31 }
32
33 qint64 WalletModel::getUnconfirmedBalance() const
34 {
35     return wallet->GetUnconfirmedBalance();
36 }
37
38 int WalletModel::getNumTransactions() const
39 {
40     int numTransactions = 0;
41     CRITICAL_BLOCK(wallet->cs_mapWallet)
42     {
43         numTransactions = wallet->mapWallet.size();
44     }
45     return numTransactions;
46 }
47
48 void WalletModel::update()
49 {
50     qint64 newBalance = getBalance();
51     qint64 newUnconfirmedBalance = getUnconfirmedBalance();
52     int newNumTransactions = getNumTransactions();
53
54     if(cachedBalance != newBalance || cachedUnconfirmedBalance != newUnconfirmedBalance)
55         emit balanceChanged(newBalance, newUnconfirmedBalance);
56
57     if(cachedNumTransactions != newNumTransactions)
58         emit numTransactionsChanged(newNumTransactions);
59
60     cachedBalance = newBalance;
61     cachedUnconfirmedBalance = newUnconfirmedBalance;
62     cachedNumTransactions = newNumTransactions;
63
64     addressTableModel->update();
65 }
66
67 bool WalletModel::validateAddress(const QString &address)
68 {
69     uint160 hash160 = 0;
70
71     return AddressToHash160(address.toStdString(), hash160);
72 }
73
74 WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
75 {
76     qint64 total = 0;
77     QSet<QString> setAddress;
78     QString hex;
79
80     if(recipients.empty())
81     {
82         return OK;
83     }
84
85     // Pre-check input data for validity
86     foreach(const SendCoinsRecipient &rcp, recipients)
87     {
88         uint160 hash160 = 0;
89
90         if(!AddressToHash160(rcp.address.toUtf8().constData(), hash160))
91         {
92             return InvalidAddress;
93         }
94         setAddress.insert(rcp.address);
95
96         if(rcp.amount <= 0)
97         {
98             return InvalidAmount;
99         }
100         total += rcp.amount;
101     }
102
103     if(recipients.size() > setAddress.size())
104     {
105         return DuplicateAddress;
106     }
107
108     if(total > getBalance())
109     {
110         return AmountExceedsBalance;
111     }
112
113     if((total + nTransactionFee) > getBalance())
114     {
115         return SendCoinsReturn(AmountWithFeeExceedsBalance, nTransactionFee);
116     }
117
118     CRITICAL_BLOCK(cs_main)
119     CRITICAL_BLOCK(wallet->cs_mapWallet)
120     {
121         // Sendmany
122         std::vector<std::pair<CScript, int64> > vecSend;
123         foreach(const SendCoinsRecipient &rcp, recipients)
124         {
125             CScript scriptPubKey;
126             scriptPubKey.SetBitcoinAddress(rcp.address.toStdString());
127             vecSend.push_back(make_pair(scriptPubKey, rcp.amount));
128         }
129
130         CWalletTx wtx;
131         CReserveKey keyChange(wallet);
132         int64 nFeeRequired = 0;
133         bool fCreated = wallet->CreateTransaction(vecSend, wtx, keyChange, nFeeRequired);
134
135         if(!fCreated)
136         {
137             if((total + nFeeRequired) > wallet->GetBalance())
138             {
139                 return SendCoinsReturn(AmountWithFeeExceedsBalance, nFeeRequired);
140             }
141             return TransactionCreationFailed;
142         }
143         if(!ThreadSafeAskFee(nFeeRequired, tr("Sending...").toStdString(), NULL))
144         {
145             return Aborted;
146         }
147         if(!wallet->CommitTransaction(wtx, keyChange))
148         {
149             return TransactionCommitFailed;
150         }
151         hex = QString::fromStdString(wtx.GetHash().GetHex());
152     }
153
154     // Add addresses that we've sent to to the address book
155     foreach(const SendCoinsRecipient &rcp, recipients)
156     {
157         std::string strAddress = rcp.address.toStdString();
158         CRITICAL_BLOCK(wallet->cs_mapAddressBook)
159         {
160             if (!wallet->mapAddressBook.count(strAddress))
161                 wallet->SetAddressBookName(strAddress, rcp.label.toStdString());
162         }
163     }
164
165     return SendCoinsReturn(OK, 0, hex);
166 }
167
168 OptionsModel *WalletModel::getOptionsModel()
169 {
170     return optionsModel;
171 }
172
173 AddressTableModel *WalletModel::getAddressTableModel()
174 {
175     return addressTableModel;
176 }
177
178 TransactionTableModel *WalletModel::getTransactionTableModel()
179 {
180     return transactionTableModel;
181 }
182
183