Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listtransactions feature #468

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1084,10 +1084,11 @@ void AcentryToJSON(const CAccountingEntry& acentry, const string& strAccount, Ar

Value listtransactions(const Array& params, bool fHelp)
{
if (fHelp || params.size() > 3)
if (fHelp || params.size() > 4)
throw runtime_error(
"listtransactions [account] [count=10] [from=0]\n"
"Returns up to [count] most recent transactions skipping the first [from] transactions for account [account].");
"listtransactions [account] [count=10] [from=0] [oldesttonewest=true]\n"
"Returns up to [count] most recent transactions skipping the first [from] transactions for account [account].\n"
"[oldesttonewest] order the transactions from oldest to newest.");

string strAccount = "*";
if (params.size() > 0)
Expand All @@ -1098,6 +1099,9 @@ Value listtransactions(const Array& params, bool fHelp)
int nFrom = 0;
if (params.size() > 2)
nFrom = params[2].get_int();
bool fOldestToNewest = true;
if (params.size() > 3)
fOldestToNewest = params[3].get_bool();

Array ret;
CWalletDB walletdb(pwalletMain->strWalletFile);
Expand All @@ -1121,17 +1125,19 @@ Value listtransactions(const Array& params, bool fHelp)

// Now: iterate backwards until we have nCount items to return:
TxItems::reverse_iterator it = txByTime.rbegin();
if (txByTime.size() > nFrom) std::advance(it, nFrom);
for (; it != txByTime.rend(); ++it)
if (nFrom < txByTime.size())
{
CWalletTx *const pwtx = (*it).second.first;
if (pwtx != 0)
ListTransactions(*pwtx, strAccount, 0, true, ret);
CAccountingEntry *const pacentry = (*it).second.second;
if (pacentry != 0)
AcentryToJSON(*pacentry, strAccount, ret);

if (ret.size() >= nCount) break;
for (std::advance(it, nFrom); it != txByTime.rend(); ++it)
{
CWalletTx *const pwtx = (*it).second.first;
if (pwtx != 0)
ListTransactions(*pwtx, strAccount, 0, true, ret);
CAccountingEntry *const pacentry = (*it).second.second;
if (pacentry != 0)
AcentryToJSON(*pacentry, strAccount, ret);

if (ret.size() >= nCount) break;
}
}
// ret is now newest to oldest

Expand All @@ -1142,7 +1148,10 @@ Value listtransactions(const Array& params, bool fHelp)
std::advance(last, nCount);
ret.erase(last, ret.end());
}
std::reverse(ret.begin(), ret.end()); // oldest to newest

// oldest to newest
if (fOldestToNewest)
std::reverse(ret.begin(), ret.end());

return ret;
}
Expand Down Expand Up @@ -2308,6 +2317,7 @@ int CommandLineRPC(int argc, char *argv[])
if (strMethod == "sendfrom" && n > 3) ConvertTo<boost::int64_t>(params[3]);
if (strMethod == "listtransactions" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "listtransactions" && n > 2) ConvertTo<boost::int64_t>(params[2]);
if (strMethod == "listtransactions" && n > 3) ConvertTo<bool>(params[3]);
if (strMethod == "listaccounts" && n > 0) ConvertTo<boost::int64_t>(params[0]);
if (strMethod == "walletpassphrase" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "sendmany" && n > 1)
Expand Down