Skip to content

Commit f3fbe99

Browse files
committed
GUI: TransactionRecord: Refactor to turn send-to-self into send+receive pairs
1 parent b9765ba commit f3fbe99

File tree

3 files changed

+55
-77
lines changed

3 files changed

+55
-77
lines changed

src/interfaces/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ struct WalletTx
387387
CTransactionRef tx;
388388
std::vector<wallet::isminetype> txin_is_mine;
389389
std::vector<wallet::isminetype> txout_is_mine;
390+
std::vector<bool> txout_is_change;
390391
std::vector<CTxDestination> txout_address;
391392
std::vector<wallet::isminetype> txout_address_is_mine;
392393
CAmount credit;

src/qt/transactionrecord.cpp

Lines changed: 53 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -54,90 +54,36 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interface
5454
}
5555
}
5656

57-
if (!any_from_me) {
58-
//
59-
// Credit
60-
//
61-
for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
62-
{
63-
const CTxOut& txout = wtx.tx->vout[i];
64-
isminetype mine = wtx.txout_is_mine[i];
65-
if(mine)
66-
{
67-
TransactionRecord sub(hash, nTime);
68-
sub.idx = i; // vout index
69-
sub.credit = txout.nValue;
70-
sub.involvesWatchAddress = mine & ISMINE_WATCH_ONLY;
71-
if (wtx.txout_address_is_mine[i])
72-
{
73-
// Received by Bitcoin Address
74-
sub.type = TransactionRecord::RecvWithAddress;
75-
sub.address = EncodeDestination(wtx.txout_address[i]);
76-
}
77-
else
78-
{
79-
// Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
80-
sub.type = TransactionRecord::RecvFromOther;
81-
sub.address = mapValue["from"];
82-
}
83-
if (wtx.is_coinbase)
84-
{
85-
// Generated
86-
sub.type = TransactionRecord::Generated;
87-
}
88-
89-
parts.append(sub);
90-
}
91-
}
92-
}
93-
else
94-
{
95-
isminetype fAllToMe = ISMINE_SPENDABLE;
57+
if (fAllFromMe || !any_from_me) {
9658
for (const isminetype mine : wtx.txout_is_mine)
9759
{
9860
if(mine & ISMINE_WATCH_ONLY) involvesWatchAddress = true;
99-
if(fAllToMe > mine) fAllToMe = mine;
10061
}
10162

102-
if (fAllFromMe && fAllToMe)
63+
CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
64+
65+
for(unsigned int i = 0; i < wtx.tx->vout.size(); i++)
10366
{
104-
// Payment to self
105-
std::string address;
106-
for (auto it = wtx.txout_address.begin(); it != wtx.txout_address.end(); ++it) {
107-
if (it != wtx.txout_address.begin()) address += ", ";
108-
address += EncodeDestination(*it);
67+
const CTxOut& txout = wtx.tx->vout[i];
68+
69+
if (wtx.txout_is_change[i]) {
70+
continue;
10971
}
11072

111-
CAmount nChange = wtx.change;
112-
parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, address, -(nDebit - nChange), nCredit - nChange));
113-
parts.last().involvesWatchAddress = involvesWatchAddress; // maybe pass to TransactionRecord as constructor argument
114-
}
115-
else if (fAllFromMe)
116-
{
117-
//
118-
// Debit
119-
//
120-
CAmount nTxFee = nDebit - wtx.tx->GetValueOut();
73+
if (fAllFromMe) {
74+
//
75+
// Debit
76+
//
12177

122-
for (unsigned int nOut = 0; nOut < wtx.tx->vout.size(); nOut++)
123-
{
124-
const CTxOut& txout = wtx.tx->vout[nOut];
12578
TransactionRecord sub(hash, nTime);
126-
sub.idx = nOut;
79+
sub.idx = i;
12780
sub.involvesWatchAddress = involvesWatchAddress;
12881

129-
if(wtx.txout_is_mine[nOut])
130-
{
131-
// Ignore parts sent to self, as this is usually the change
132-
// from a transaction sent back to our own address.
133-
continue;
134-
}
135-
136-
if (!std::get_if<CNoDestination>(&wtx.txout_address[nOut]))
82+
if (!std::get_if<CNoDestination>(&wtx.txout_address[i]))
13783
{
13884
// Sent to Bitcoin Address
13985
sub.type = TransactionRecord::SendToAddress;
140-
sub.address = EncodeDestination(wtx.txout_address[nOut]);
86+
sub.address = EncodeDestination(wtx.txout_address[i]);
14187
}
14288
else
14389
{
@@ -157,15 +103,45 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const interface
157103

158104
parts.append(sub);
159105
}
106+
107+
isminetype mine = wtx.txout_is_mine[i];
108+
if(mine)
109+
{
110+
//
111+
// Credit
112+
//
113+
114+
TransactionRecord sub(hash, nTime);
115+
sub.idx = i; // vout index
116+
sub.credit = txout.nValue;
117+
sub.involvesWatchAddress = mine & ISMINE_WATCH_ONLY;
118+
if (wtx.txout_address_is_mine[i])
119+
{
120+
// Received by Bitcoin Address
121+
sub.type = TransactionRecord::RecvWithAddress;
122+
sub.address = EncodeDestination(wtx.txout_address[i]);
123+
}
124+
else
125+
{
126+
// Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
127+
sub.type = TransactionRecord::RecvFromOther;
128+
sub.address = mapValue["from"];
129+
}
130+
if (wtx.is_coinbase)
131+
{
132+
// Generated
133+
sub.type = TransactionRecord::Generated;
134+
}
135+
136+
parts.append(sub);
137+
}
160138
}
161-
else
162-
{
163-
//
164-
// Mixed debit transaction, can't break down payees
165-
//
166-
parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
167-
parts.last().involvesWatchAddress = involvesWatchAddress;
168-
}
139+
} else {
140+
//
141+
// Mixed debit transaction, can't break down payees
142+
//
143+
parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0));
144+
parts.last().involvesWatchAddress = involvesWatchAddress;
169145
}
170146

171147
return parts;

src/wallet/interfaces.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
6464
result.txout_address_is_mine.reserve(wtx.tx->vout.size());
6565
for (const auto& txout : wtx.tx->vout) {
6666
result.txout_is_mine.emplace_back(wallet.IsMine(txout));
67+
result.txout_is_change.push_back(OutputIsChange(wallet, txout));
6768
result.txout_address.emplace_back();
6869
result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
6970
wallet.IsMine(result.txout_address.back()) :

0 commit comments

Comments
 (0)