From f032cf4139209c9fdf4ef81c9e6d2680d429f542 Mon Sep 17 00:00:00 2001 From: Dcorral Date: Mon, 30 May 2022 23:47:03 +0200 Subject: [PATCH 1/2] Add verbose flag to getvault to get nextCollateralRatio Add tests for nextCollateralRatio --- src/masternodes/rpc_vault.cpp | 17 +++++++++---- test/functional/feature_loan_priceupdate.py | 27 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/masternodes/rpc_vault.cpp b/src/masternodes/rpc_vault.cpp index 7dc9e8bcb2..ff3caf7857 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) @@ -181,6 +181,12 @@ 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); + nextCollateralRatio = int(rate.val->ratio()); + result.pushKV("nextCollateralRatio", nextCollateralRatio); + } return result; } } @@ -513,7 +519,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" @@ -525,7 +532,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); @@ -535,7 +542,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 ae6a452ba1..88355f7c17 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() From 43066e077edcf8d779a5294762d55a338fe5f55a Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 31 May 2022 13:00:19 +0530 Subject: [PATCH 2/2] Add guard for rate when there is no live price --- src/masternodes/rpc_vault.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/masternodes/rpc_vault.cpp b/src/masternodes/rpc_vault.cpp index ff3caf7857..43a7a5986c 100644 --- a/src/masternodes/rpc_vault.cpp +++ b/src/masternodes/rpc_vault.cpp @@ -184,8 +184,10 @@ namespace { if (verbose) { useNextPrice = true; auto rate = pcustomcsview->GetLoanCollaterals(vaultId, *collaterals, height + 1, blockTime, useNextPrice, requireLivePrice); - nextCollateralRatio = int(rate.val->ratio()); - result.pushKV("nextCollateralRatio", nextCollateralRatio); + if (rate) { + nextCollateralRatio = int(rate.val->ratio()); + result.pushKV("nextCollateralRatio", nextCollateralRatio); + } } return result; }