Skip to content

Commit

Permalink
Add logstoredinterests (#1429)
Browse files Browse the repository at this point in the history
* Add logstoredinterests
  • Loading branch information
prasannavl authored Aug 30, 2022
1 parent b4e7953 commit 7a90326
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/masternodes/rpc_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,72 @@ UniValue getstoredinterest(const JSONRPCRequest& request) {
return GetRPCResultCache().Set(request, ret);
}

UniValue logstoredinterests(const JSONRPCRequest& request) {
RPCHelpMan{"logstoredinterests",
"Logs all stored interests.\n",
{

},
RPCResult{
"[\"vaultId\": {\n"
" \"token\" : n, Token ID\n"
" \"amount\" : n, (amount) Token Amount\n"
" \"interestHeight\" : n, Height stored interest last updated\n"
" \"interestToHeight\" : n.nnnnnnnn, Interest stored to the point of the hight value\n"
" \"interestPerBlock\" : n.nnnnnnnn, nterest per block\n"
"}] \n"
},
RPCExamples{
HelpExampleCli("logstoredinterests", "") +
HelpExampleRpc("logstoredinterests", "")
},
}.Check(request);

if (auto res = GetRPCResultCache().TryGet(request)) return *res;

LOCK(cs_main);

auto height = ::ChainActive().Height();
using VaultInfo = std::tuple<DCT_ID,CAmount,CInterestRateV3>;
std::map<std::string, std::vector<VaultInfo>> items;

pcustomcsview->ForEachVault([&](const CVaultId& vaultId, const CVaultData& vaultData) {
auto vaultTokens = pcustomcsview->GetLoanTokens(vaultId);
if (!vaultTokens) return true;

std::vector<VaultInfo> infoItems;

for (const auto &[tokenId, tokenAmount]: vaultTokens->balances) {
const auto interestRate = pcustomcsview->GetInterestRate(vaultId, tokenId, height);
if (interestRate) {
infoItems.push_back({tokenId, tokenAmount, *interestRate});
}
}
items[vaultId.ToString()] = infoItems;
return true;
});

UniValue ret(UniValue::VARR);
for (const auto &[vaultId, infoItems]: items) {
UniValue v(UniValue::VOBJ);
v.pushKV("vaultId", vaultId);
UniValue vItems(UniValue::VARR);
for (const auto& [tokenId, amount, rate] : infoItems) {
UniValue i(UniValue::VOBJ);
i.pushKV("token", tokenId.ToString());
i.pushKV("amount", ValueFromAmount(amount));
i.pushKV("interestHeight", static_cast<uint64_t>(rate.height));
i.pushKV("interestToHeight", GetInterestPerBlockHighPrecisionString(rate.interestToHeight));
i.pushKV("interestPerBlock", GetInterestPerBlockHighPrecisionString(rate.interestPerBlock));
vItems.push_back(i);
}
v.pushKV("items", vItems);
ret.push_back(v);
}

return GetRPCResultCache().Set(request, ret);
}


UniValue getloantokens(const JSONRPCRequest& request) {

Expand Down Expand Up @@ -1962,6 +2028,7 @@ static const CRPCCommand commands[] =
{"vault", "estimatecollateral", &estimatecollateral, {"loanAmounts", "targetRatio", "tokens"}},
{"vault", "estimatevault", &estimatevault, {"collateralAmounts", "loanAmounts"}},
{"hidden", "getstoredinterest", &getstoredinterest, {"vaultId", "token"}},
{"hidden", "logstoredinterests", &logstoredinterests, {}},
{"hidden", "getloantokens", &getloantokens, {"vaultId"}},
};

Expand Down

0 comments on commit 7a90326

Please sign in to comment.