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