From bf1843be9e352aa39207ea98b40709f66f8da1be Mon Sep 17 00:00:00 2001 From: David Schwartz Date: Tue, 5 Nov 2013 16:07:17 -0800 Subject: [PATCH] Add "account_currencies" command. --- src/ripple_app/rpc/RPCHandler.cpp | 60 +++++++++++++++++++++++++++++++ src/ripple_app/rpc/RPCHandler.h | 1 + src/ripple_net/rpc/RPCCall.cpp | 6 ++++ 3 files changed, 67 insertions(+) diff --git a/src/ripple_app/rpc/RPCHandler.cpp b/src/ripple_app/rpc/RPCHandler.cpp index d27995374c..568a04d5af 100644 --- a/src/ripple_app/rpc/RPCHandler.cpp +++ b/src/ripple_app/rpc/RPCHandler.cpp @@ -575,6 +575,65 @@ Json::Value RPCHandler::accountFromString (Ledger::ref lrLedger, RippleAddress& return Json::Value (Json::objectValue); } +Json::Value RPCHandler::doAccountCurrencies (Json::Value params, LoadType* loadType, Application::ScopedLockType& masterLockHolder) +{ + Ledger::pointer lpLedger; + Json::Value jvResult = lookupLedger (params, lpLedger); + + if (!lpLedger) + return jvResult; + if (lpLedger->isImmutable ()) + masterLockHolder.unlock (); + + if (!params.isMember ("account") && !params.isMember ("ident")) + return rpcError (rpcINVALID_PARAMS); + + std::string strIdent = params.isMember ("account") ? params["account"].asString () : params["ident"].asString (); + bool bIndex; + int iIndex = params.isMember ("account_index") ? params["account_index"].asUInt () : 0; + bool bStrict = params.isMember ("strict") && params["strict"].asBool (); + RippleAddress naAccount; + + // Get info on account. + + Json::Value jvAccepted = accountFromString (lpLedger, naAccount, bIndex, strIdent, iIndex, bStrict); + + if (!jvAccepted.empty ()) + return jvAccepted; + + std::set send, receive; + AccountItems rippleLines (naAccount.getAccountID (), lpLedger, AccountItem::pointer (new RippleState ())); + BOOST_FOREACH(AccountItem::ref item, rippleLines.getItems ()) + { + RippleState* rspEntry = (RippleState*) item.get (); + const STAmount& saBalance = rspEntry->getBalance (); + + if (saBalance < rspEntry->getLimit ()) + receive.insert (saBalance.getCurrency ()); + if ((-saBalance) < rspEntry->getLimitPeer ()) + send.insert (saBalance.getCurrency ()); + } + + + send.erase (CURRENCY_BAD); + receive.erase (CURRENCY_BAD); + + Json::Value& sendCurrencies = (jvResult["send_currencies"] = Json::arrayValue); + BOOST_FOREACH(uint160 const& c, send) + { + sendCurrencies.append (STAmount::createHumanCurrency (c)); + } + + Json::Value& recvCurrencies = (jvResult["receive_currencies"] = Json::arrayValue); + BOOST_FOREACH(uint160 const& c, receive) + { + recvCurrencies.append (STAmount::createHumanCurrency (c)); + } + + + return jvResult; +} + // { // account: , // account_index : // optional @@ -3830,6 +3889,7 @@ Json::Value RPCHandler::doCommand (const Json::Value& params, int iRole, LoadTyp { // Request-response methods { "account_info", &RPCHandler::doAccountInfo, false, optCurrent }, + { "account_currencies", &RPCHandler::doAccountCurrencies, false, optCurrent }, { "account_lines", &RPCHandler::doAccountLines, false, optCurrent }, { "account_offers", &RPCHandler::doAccountOffers, false, optCurrent }, { "account_tx", &RPCHandler::doAccountTxSwitch, false, optNetwork }, diff --git a/src/ripple_app/rpc/RPCHandler.h b/src/ripple_app/rpc/RPCHandler.h index 98341554e8..3854db1170 100644 --- a/src/ripple_app/rpc/RPCHandler.h +++ b/src/ripple_app/rpc/RPCHandler.h @@ -95,6 +95,7 @@ private: const int iIndex, const bool bStrict); + Json::Value doAccountCurrencies (Json::Value params, LoadType* loadType, Application::ScopedLockType& mlh); Json::Value doAccountInfo (Json::Value params, LoadType* loadType, Application::ScopedLockType& mlh); Json::Value doAccountLines (Json::Value params, LoadType* loadType, Application::ScopedLockType& mlh); Json::Value doAccountOffers (Json::Value params, LoadType* loadType, Application::ScopedLockType& mlh); diff --git a/src/ripple_net/rpc/RPCCall.cpp b/src/ripple_net/rpc/RPCCall.cpp index 1993cb19c0..ac744edda3 100644 --- a/src/ripple_net/rpc/RPCCall.cpp +++ b/src/ripple_net/rpc/RPCCall.cpp @@ -497,6 +497,11 @@ private: return parseAccountRaw (jvParams, false); } + Json::Value parseAccountCurrencies (const Json::Value& jvParams) + { + return parseAccountRaw (jvParams, false); + } + // account_lines |"" [] Json::Value parseAccountLines (const Json::Value& jvParams) { @@ -834,6 +839,7 @@ public: // Request-response methods // - Returns an error, or the request. // - To modify the method, provide a new method in the request. + { "account_currencies", &RPCParser::parseAccountCurrencies, 1, 2 }, { "account_info", &RPCParser::parseAccountItems, 1, 2 }, { "account_lines", &RPCParser::parseAccountLines, 1, 3 }, { "account_offers", &RPCParser::parseAccountItems, 1, 2 },