TransactionRecord *index(int idx)
    {
        if(idx >= 0 && idx < cachedWallet.size())
        {
            TransactionRecord *rec = &cachedWallet[idx];

            // Get required locks upfront. This avoids the GUI from getting
            // stuck if the core is holding the locks for a longer time - for
            // example, during a wallet rescan.
            //
            // If a status update is needed (blocks came in since last check),
            //  update the status of this transaction from the wallet. Otherwise,
            // simply re-use the cached status.
            TRY_LOCK(cs_main, lockMain);
            if(lockMain)
            {
                TRY_LOCK(wallet->cs_wallet, lockWallet);
                if(lockWallet && rec->statusUpdateNeeded())
                {
                    std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(rec->hash);

                    if(mi != wallet->mapWallet.end())
                    {
                        rec->updateStatus(mi->second);
                    }
                }
            }
            return rec;
        }
        return 0;
    }
    void balanceWallet(bool sortRequired = false)
    {
        if (sortRequired) {
            {
                LOCK(wallet->cs_wallet);
                // We need updated status for all the transaction records
                for (int i = 0 ; i < cachedWallet.size(); i++)
                {
                    TransactionRecord *rec = &cachedWallet[i];

                    rec->presort_i = i;
                    rec->balance = 0;
                    std::map<uint256, CWalletTx>::iterator mi = wallet->mapWallet.find(rec->hash);

                    if(mi != wallet->mapWallet.end())
                    {
                        rec->updateStatus(mi->second);
                    }
                }
            }
            keySortedWallet.clear();
            keySortedWallet = cachedWallet;
            qSort(keySortedWallet.begin(), keySortedWallet.end(), TxLessThanKey());
        }

        int64 balance=0;
        for( int i=0; i<keySortedWallet.count(); ++i )
        {
            TransactionRecord *rec = &keySortedWallet[i];
            TransactionRecord *wtx = &cachedWallet[rec->presort_i];
            if(!wtx->status.confirmed || wtx->status.maturity != TransactionStatus::Mature) {
                balance += wtx->debit;
            }
            else {
                balance += (wtx->credit + wtx->debit);
            }

            wtx->balance = balance;
            rec->balance = balance;
        }
    }
    TransactionRecord *index(interfaces::Wallet& wallet, int idx)
    {
        if(idx >= 0 && idx < cachedWallet.size())
        {
            TransactionRecord *rec = &cachedWallet[idx];

            // Get required locks upfront. This avoids the GUI from getting
            // stuck if the core is holding the locks for a longer time - for
            // example, during a wallet rescan.
            //
            // If a status update is needed (blocks came in since last check),
            //  update the status of this transaction from the wallet. Otherwise,
            // simply re-use the cached status.
            interfaces::WalletTxStatus wtx;
            int numBlocks;
            int64_t adjustedTime;
            if (wallet.tryGetTxStatus(rec->hash, wtx, numBlocks, adjustedTime) && rec->statusUpdateNeeded(numBlocks)) {
                rec->updateStatus(wtx, numBlocks, adjustedTime);
            }
            return rec;
        }
        return 0;
    }