Example #1
0
/*
 * Decompose CWallet transaction to model transaction records.
 */
QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
{
    QList<TransactionRecord> parts;
    int64 nTime = wtx.GetTxTime();
    int64 nCredit = wtx.GetCredit(true);
    int64 nDebit = wtx.GetDebit();
    int64 nNet = nCredit - nDebit;
    uint256 hash = wtx.GetHash();
    std::map<std::string, std::string> mapValue = wtx.mapValue;
    const bool combineOutputs = (wtx.cUnit == 'S');

    if (wtx.IsCoinStake()) // ppcoin: coinstake transaction
    {
        parts.append(TransactionRecord(hash, nTime, TransactionRecord::StakeMint, "", -nDebit, wtx.GetValueOut()));
    }
    else if (nNet > 0 || wtx.IsCoinBase())
    {
        //
        // Credit
        //
        QMap<CScript, TransactionRecord*> outputParts;
        BOOST_FOREACH(const CTxOut& txout, wtx.vout)
        {
            if(wallet->IsMine(txout))
            {
                TransactionRecord sub(hash, nTime);
                CTxDestination address;
                sub.idx = parts.size(); // sequence number
                sub.credit = txout.nValue;
                if (ExtractDestination(txout.scriptPubKey, address) && IsMine(*wallet, address))
                {
                    if (wtx.IsUnpark())
                        sub.type = TransactionRecord::Unpark;
                    else // Received by Bitcoin Address
                        sub.type = TransactionRecord::RecvWithAddress;
                    sub.address = CBitcoinAddress(address, wtx.cUnit).ToString();
                }
                else
                {
                    // Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
                    sub.type = TransactionRecord::RecvFromOther;
                    sub.address = mapValue["from"];
                }
                if (wtx.IsCoinBase())
                {
                    // Generated
                    sub.type = TransactionRecord::Generated;
                }

                if (combineOutputs)
                {
                    QMap<CScript, TransactionRecord*>::const_iterator it = outputParts.find(txout.scriptPubKey);
                    if (it != outputParts.end())
                    {
                        TransactionRecord& previous = *it.value();
                        previous.credit += sub.credit;
                        continue;
                    }
                }

                parts.append(sub);

                if (combineOutputs)
                    outputParts[txout.scriptPubKey] = &parts.back();
            }
        }
    }