Skip to content

Commit

Permalink
Verbose flag to getvault to get nextCollateralRatio (#1312)
Browse files Browse the repository at this point in the history
* Add verbose flag to getvault to get nextCollateralRatio

Add tests for nextCollateralRatio

* Add guard for rate when there is no live price

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
dcorral and prasannavl authored May 31, 2022
1 parent 3db917e commit dfedbed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/masternodes/rpc_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace {
return auctionObj;
}

UniValue VaultToJSON(const CVaultId& vaultId, const CVaultData& vault) {
UniValue VaultToJSON(const CVaultId& vaultId, const CVaultData& vault, const bool verbose = false) {
UniValue result{UniValue::VOBJ};
auto vaultState = GetVaultState(vaultId, vault);
auto height = ::ChainActive().Height();
Expand All @@ -123,7 +123,7 @@ namespace {
return result;
}

UniValue ratioValue{0}, collValue{0}, loanValue{0}, interestValue{0}, collateralRatio{0};
UniValue ratioValue{0}, collValue{0}, loanValue{0}, interestValue{0}, collateralRatio{0}, nextCollateralRatio{0};

auto collaterals = pcustomcsview->GetVaultCollaterals(vaultId);
if (!collaterals)
Expand Down Expand Up @@ -200,6 +200,14 @@ namespace {
result.pushKV("interestValue", interestValue);
result.pushKV("informativeRatio", ratioValue);
result.pushKV("collateralRatio", collateralRatio);
if (verbose) {
useNextPrice = true;
auto rate = pcustomcsview->GetLoanCollaterals(vaultId, *collaterals, height + 1, blockTime, useNextPrice, requireLivePrice);
if (rate) {
nextCollateralRatio = int(rate.val->ratio());
result.pushKV("nextCollateralRatio", nextCollateralRatio);
}
}
return result;
}
}
Expand Down Expand Up @@ -532,7 +540,8 @@ UniValue getvault(const JSONRPCRequest& request) {
RPCHelpMan{"getvault",
"Returns information about vault.\n",
{
{"vaultId", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "vault hex id",},
{"vaultId", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "vault hex id"},
{"verbose", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "Verbose vault information (default = false)"},
},
RPCResult{
"\"json\" (string) vault data in json form\n"
Expand All @@ -544,7 +553,7 @@ UniValue getvault(const JSONRPCRequest& request) {
}.Check(request);

RPCTypeCheck(request.params, {UniValue::VSTR}, false);

bool verbose = request.params[1].getBool();
CVaultId vaultId = ParseHashV(request.params[0], "vaultId");

LOCK(cs_main);
Expand All @@ -554,7 +563,7 @@ UniValue getvault(const JSONRPCRequest& request) {
throw JSONRPCError(RPC_DATABASE_ERROR, strprintf("Vault <%s> not found", vaultId.GetHex()));
}

return VaultToJSON(vaultId, *vault);
return VaultToJSON(vaultId, *vault, verbose);
}

UniValue updatevault(const JSONRPCRequest& request) {
Expand Down
27 changes: 27 additions & 0 deletions test/functional/feature_loan_priceupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,34 @@ def run_test(self):
fixedPriceList = self.nodes[0].listfixedintervalprices(pagination)
assert_equal(len(fixedPriceList), 2)

# Test verbose parameter in getvault to retrieve nextCollateralRatio

# Update price
oracle1_prices = [
{"currency": "USD", "tokenAmount": "70@TSLA"},
{"currency": "USD", "tokenAmount": "100@DFI"},
{"currency": "USD", "tokenAmount": "100@BTC"}]
timestamp = calendar.timegm(time.gmtime())
self.nodes[0].setoracledata(oracle_id1, timestamp, oracle1_prices)
self.nodes[0].generate(6)

# Check nextPrice and active price are correct
fixedPrice = self.nodes[0].getfixedintervalprice("TSLA/USD")
assert_equal(fixedPrice['isLive'], True)
assert_equal(fixedPrice['activePrice'], Decimal('57.50000000'))
assert_equal(fixedPrice['nextPrice'], Decimal('42.50000000'))

# Check vault nextCollateralRatio and collateralRatio
vaultBeforeUpdate = self.nodes[0].getvault(vaultId1, True)
assert_equal(vaultBeforeUpdate["collateralRatio"], 2375)
assert_equal(vaultBeforeUpdate["nextCollateralRatio"], 3213)

# Let price update and check vault again
self.nodes[0].generate(6)

vaultAfterUpdate = self.nodes[0].getvault(vaultId1, True)
assert_equal(vaultAfterUpdate["collateralRatio"], vaultBeforeUpdate["nextCollateralRatio"])
assert_equal(vaultAfterUpdate["nextCollateralRatio"], 3213)

if __name__ == '__main__':
PriceUpdateTest().main()

0 comments on commit dfedbed

Please sign in to comment.