Skip to content

Commit

Permalink
Refactor rpc oracles (#886)
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
bvbfan and prasannavl authored Nov 11, 2021
1 parent 97c5fc0 commit fef18b7
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/masternodes/rpc_oracles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ UniValue updateoracle(const JSONRPCRequest& request) {
int targetHeight = chainHeight(*pwallet->chain().lock()) + 1;

CUpdateOracleAppointMessage msg{
oracleId,
CAppointOracleMessage{std::move(script), static_cast<uint8_t>(weightage), std::move(allowedPairs)}
oracleId,
CAppointOracleMessage{std::move(script), uint8_t(weightage), std::move(allowedPairs)}
};

// encode
Expand Down Expand Up @@ -452,7 +452,7 @@ UniValue setoracledata(const JSONRPCRequest &request) {
auto currency = value[oraclefields::Currency].getValStr();
auto tokenAmount = value[oraclefields::TokenAmount].getValStr();
auto amounts = ParseTokenAmount(tokenAmount);
if (!amounts.ok) {
if (!amounts) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, amounts.msg);
}

Expand All @@ -476,7 +476,7 @@ UniValue setoracledata(const JSONRPCRequest &request) {
LOCK(cs_main);
// check if tx parameters are valid
auto oracleRes = pcustomcsview->GetOracleData(oracleId);
if (!oracleRes.ok) {
if (!oracleRes) {
throw JSONRPCError(RPC_INVALID_REQUEST, oracleRes.msg);
}
oracleAddress = oracleRes.val->oracleAddress;
Expand Down Expand Up @@ -611,7 +611,7 @@ UniValue getoracledata(const JSONRPCRequest &request) {
CCustomCSView mnview(*pcustomcsview); // don't write into actual DB

auto oracleRes = mnview.GetOracleData(oracleId);
if (!oracleRes.ok) {
if (!oracleRes) {
throw JSONRPCError(RPC_DATABASE_ERROR, oracleRes.msg);
}

Expand Down Expand Up @@ -688,8 +688,7 @@ UniValue listoracles(const JSONRPCRequest &request) {
return (true);
}
value.push_back(id.GetHex());
limit--;
return limit != 0;
return --limit != 0;
}, start);

return value;
Expand Down Expand Up @@ -805,7 +804,9 @@ UniValue listlatestrawprices(const JSONRPCRequest &request) {
auto state = diffInHour(timestamp, lastBlockTime) ? oraclefields::Alive : oraclefields::Expired;
value.pushKV(oraclefields::State, state);
result.push_back(value);
limit--;
if (--limit == 0) {
return false;
}
}
}
return limit != 0;
Expand Down Expand Up @@ -879,6 +880,9 @@ namespace {
if (!paginationObj["including_start"].isNull()) {
including_start = paginationObj["including_start"].getBool();
}
if (!including_start) {
++start;
}
}
if (limit == 0) {
limit = std::numeric_limits<decltype(limit)>::max();
Expand All @@ -888,14 +892,9 @@ namespace {
std::set<CTokenCurrencyPair> setTokenCurrency;
view.ForEachOracle([&](const COracleId&, COracle oracle) {
const auto& pairs = oracle.availablePairs;
if(start > pairs.size()-1)
return true;
const auto& startingPairIt = std::next(pairs.begin(), start);
if(!including_start){
setTokenCurrency.insert(std::next(pairs.begin(), start+1), pairs.end());
return true;
if (start < pairs.size()) {
setTokenCurrency.insert(std::next(pairs.begin(), start), pairs.end());
}
setTokenCurrency.insert(startingPairIt, pairs.end());
return true;
});
for (const auto& tokenCurrency : setTokenCurrency) {
Expand All @@ -912,9 +911,9 @@ namespace {
item.pushKV(oraclefields::ValidityFlag, aggregatePrice.msg);
}
result.push_back(item);
limit--;
if (limit == 0)
if (--limit == 0) {
break;
}
}
return result;
}
Expand Down Expand Up @@ -1043,14 +1042,17 @@ UniValue getfixedintervalprice(const JSONRPCRequest& request) {
"Invalid parameter, argument fixedIntervalPriceId must be non-null");

auto fixedIntervalStr = request.params[0].getValStr();

UniValue objPrice{UniValue::VOBJ};
objPrice.pushKV("fixedIntervalPriceId", fixedIntervalStr);
auto pairId = DecodePriceFeedUni(objPrice);

LOCK(cs_main);

LogPrint(BCLog::ORACLE,"%s()->", __func__); /* Continued */

auto fixedPrice = pcustomcsview->GetFixedIntervalPrice(pairId);
if(!fixedPrice)
if (!fixedPrice)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, fixedPrice.msg);

auto priceBlocks = GetFixedIntervalPriceBlocks(::ChainActive().Height(), *pcustomcsview);
Expand Down Expand Up @@ -1113,7 +1115,6 @@ UniValue listfixedintervalprices(const JSONRPCRequest& request) {

LOCK(cs_main);


UniValue listPrice{UniValue::VARR};
pcustomcsview->ForEachFixedIntervalPrice([&](const CTokenCurrencyPair&, CFixedIntervalPrice fixedIntervalPrice){
UniValue obj{UniValue::VOBJ};
Expand All @@ -1123,16 +1124,11 @@ UniValue listfixedintervalprices(const JSONRPCRequest& request) {
obj.pushKV("timestamp", fixedIntervalPrice.timestamp);
obj.pushKV("isLive", fixedIntervalPrice.isLive(pcustomcsview->GetPriceDeviation()));
listPrice.push_back(obj);
limit--;
return limit != 0;
return --limit != 0;
}, start);
return listPrice;
}





static const CRPCCommand commands[] =
{
// category name actor (function) params
Expand Down

0 comments on commit fef18b7

Please sign in to comment.