diff --git a/src/masternodes/rpc_vault.cpp b/src/masternodes/rpc_vault.cpp index 0dc5fbf420d..7067c2dd914 100644 --- a/src/masternodes/rpc_vault.cpp +++ b/src/masternodes/rpc_vault.cpp @@ -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(); @@ -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) @@ -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; } } @@ -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" @@ -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); @@ -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) { diff --git a/test/functional/feature_loan_priceupdate.py b/test/functional/feature_loan_priceupdate.py index ae6a452ba1c..88355f7c178 100755 --- a/test/functional/feature_loan_priceupdate.py +++ b/test/functional/feature_loan_priceupdate.py @@ -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()