// for inbox only, allows you to lookup the total value of pending transfers within the inbox. // (And it really loads the items to check the amount, but does all this ONLY for pending transfers.) // long OTLedger::GetTotalPendingValue() { long lTotalPendingValue = 0; if (OTLedger::inbox != GetType()) { OTLog::Error("OTLedger::GetTotalPendingValue: Wrong ledger type (expected inbox).\n"); return 0; } OTTransaction * pTransaction = NULL; for (mapOfTransactions::iterator ii = m_mapTransactions.begin(); ii != m_mapTransactions.end(); ++ii) { pTransaction = (*ii).second; OT_ASSERT(NULL != pTransaction); if (pTransaction->GetType() == OTTransaction::pending) lTotalPendingValue += pTransaction->GetReceiptAmount(); // this actually loads up the original item and reads the amount. } return lTotalPendingValue; }
/// If transaction #87, in reference to #74, is in the inbox, you can remove it /// by calling this function and passing in 74. /// bool OTLedger::RemovePendingTransaction(long lTransactionNum) // if false, transaction wasn't found. { // loop through the items that make up this transaction. OTTransaction * pTransaction = NULL; mapOfTransactions::iterator it; for (mapOfTransactions::iterator ii = m_mapTransactions.begin(); ii != m_mapTransactions.end(); ++ii) { it = ii; pTransaction = (*ii).second; OT_ASSERT(NULL != pTransaction); bool bCorrectType = false; switch (pTransaction->GetType()) { case OTTransaction::pending: case OTTransaction::transferReceipt: case OTTransaction::chequeReceipt: bCorrectType = true; break; default: break; } if (bCorrectType && pTransaction->GetReferenceToNum() == lTransactionNum) break; else pTransaction = NULL; } // If it's not already on the list, then there's nothing to remove. if ( NULL == pTransaction ) { OTLog::vError("OTLedger::RemovePendingTransaction: Attempt to remove Transaction from ledger,\n" "when not already there: (the number in reference to) %ld\n", lTransactionNum); return false; } // Otherwise, if it WAS already there, remove it properly. else { m_mapTransactions.erase(it); delete pTransaction; return true; } return false; }
// While processing a transaction, you may wish to query it for items of a certain type. OTTransaction * OTLedger::GetTransaction(const OTTransaction::transactionType theType) { // loop through the items that make up this transaction for (mapOfTransactions::iterator ii = m_mapTransactions.begin(); ii != m_mapTransactions.end(); ++ii) { OTTransaction * pTransaction = (*ii).second; OT_ASSERT(NULL != pTransaction); if (theType == pTransaction->GetType()) return pTransaction; } return NULL; }
// Find the finalReceipt in this Inbox, that has lTransactionNum as its "in reference to". // This is useful for cases where a marketReceipt or paymentReceipt has been found, // yet the transaction # for that receipt isn't on my issued list... it's been closed. // Normally this would be a problem: why is it in my inbox then? Because those receipts // are still valid as long as there is a "FINAL RECEIPT" in the same inbox, that references // the same original transaction that they do. The below function makes it easy to find that // final receipt, if it exists. // OTTransaction * OTLedger::GetFinalReceipt(long lReferenceNum) { // loop through the items that make up this transaction. OTTransaction * pTransaction = NULL; for (mapOfTransactions::iterator ii = m_mapTransactions.begin(); ii != m_mapTransactions.end(); ++ii) { pTransaction = (*ii).second; OT_ASSERT(NULL != pTransaction); if (OTTransaction::finalReceipt != pTransaction->GetType()) // <======= continue; // --------------------------------- if (pTransaction->GetReferenceToNum() == lReferenceNum) return pTransaction; } return NULL; }
// If my outbox has a pending transfer, #1901, referencing 1884, and then the // recipient accepts it with his #781, referencing 1884, then it will pop into my inbox // as a transfer receipt, #1902 (say) and referencing 781. Attached to that // transfer receipt is a copy of the actual #781, which is in reference to 1884. // // Why does this matter? Because when I am verifying a balance agreement, and an // outbox item 1901/1884 is missing, that means there is probably a corresponding // transferReceipt in the Inbox. In that case, I START with #1901 referencing 1884 (from // the outbox) and I need to FIND #1902, in reference to 781, referencing 1884 in the inbox. // // Therefore, loop through all items and filter by transfer receipt. For each, load its // Reference string (containing the acceptPending) and get ITS ReferenceNum() to compare // to the one passed in. // // Therefore 1884 would be passed in, and the appropriate transferReceipt will be returned. // OTTransaction * OTLedger::GetTransferReceipt(long lTransactionNum) { // loop through the items that make up this transaction. OTTransaction * pTransaction = NULL; for (mapOfTransactions::iterator ii = m_mapTransactions.begin(); ii != m_mapTransactions.end(); ++ii) { pTransaction = (*ii).second; OT_ASSERT(NULL != pTransaction); if (OTTransaction::transferReceipt == pTransaction->GetType()) { OTString strReference; pTransaction->GetReferenceString(strReference); OTItem * pOriginalItem = OTItem::CreateItemFromString(strReference, pTransaction->GetPurportedServerID(), pTransaction->GetReferenceToNum()); OT_ASSERT(NULL != pOriginalItem); OTCleanup<OTItem> theItemAngel(*pOriginalItem); if (pOriginalItem->GetType() != OTItem::acceptPending) { OTLog::Error("OTLedger::GetTransferReceipt: Wrong item type attached to transferReceipt!\n"); return NULL; } else { if (pOriginalItem->GetReferenceToNum() == lTransactionNum) return pTransaction; // FOUND IT! } } } return NULL; }