diff --git a/src/masternodes/rpc_vault.cpp b/src/masternodes/rpc_vault.cpp index 7dc9e8bcb25..0dc5fbf420d 100644 --- a/src/masternodes/rpc_vault.cpp +++ b/src/masternodes/rpc_vault.cpp @@ -129,6 +129,7 @@ namespace { if (!collaterals) collaterals = CBalances{}; + auto blockTime = ::ChainActive().Tip()->GetBlockTime(); bool useNextPrice = false, requireLivePrice = vaultState != VaultState::Frozen; LogPrint(BCLog::LOAN,"%s():\n", __func__); @@ -141,6 +142,14 @@ namespace { collateralRatio = int(rate.val->ratio()); } + bool isVaultTokenLocked {false}; + for (const auto& collateral : collaterals->balances) { + if(pcustomcsview->AreTokensLocked({collateral.first.v})){ + isVaultTokenLocked = true; + break; + } + } + UniValue loanBalances{UniValue::VARR}; UniValue interestAmounts{UniValue::VARR}; @@ -163,6 +172,9 @@ namespace { } totalBalances.insert({loan.first, value}); interestBalances.insert({loan.first, totalInterest}); + if (pcustomcsview->AreTokensLocked({loan.first.v})){ + isVaultTokenLocked = true; + } } interestValue = ValueFromAmount(totalInterests); loanBalances = AmountsToJSON(totalBalances); @@ -176,6 +188,13 @@ namespace { result.pushKV("collateralAmounts", AmountsToJSON(collaterals->balances)); result.pushKV("loanAmounts", loanBalances); result.pushKV("interestAmounts", interestAmounts); + if (isVaultTokenLocked){ + collValue = -1; + loanValue = -1; + interestValue = -1; + ratioValue = -1; + collateralRatio = -1; + } result.pushKV("collateralValue", collValue); result.pushKV("loanValue", loanValue); result.pushKV("interestValue", interestValue); diff --git a/test/functional/feature_token_lock.py b/test/functional/feature_token_lock.py index 20f9d05b6e5..30515e27d20 100755 --- a/test/functional/feature_token_lock.py +++ b/test/functional/feature_token_lock.py @@ -266,13 +266,13 @@ def vault_lock(self): assert_raises_rpc_error(-32600, "Cannot take loan while any of the asset's price in the vault is not live", self.nodes[0].takeloan, {'vaultId': self.vault, 'amounts': f'1@{self.symbolTSLA}'}) assert_raises_rpc_error(-32600, "Cannot take loan while any of the asset's price in the vault is not live", self.nodes[0].takeloan, {'vaultId': self.vault, 'amounts': f'1@{self.symbolGOOGL}'}) - # Vault amounts should be zero while token locked + # Vault amounts should be -1 while token locked result = self.nodes[0].getvault(self.vault) - assert_equal(result['collateralValue'], 0) - assert_equal(result['loanValue'], 0) - assert_equal(result['interestValue'], Decimal('0.00000000')) - assert_equal(result['informativeRatio'], 0) - assert_equal(result['collateralRatio'], 0) + assert_equal(result['collateralValue'], -1) + assert_equal(result['loanValue'], -1) + assert_equal(result['interestValue'], -1) + assert_equal(result['informativeRatio'], -1) + assert_equal(result['collateralRatio'], -1) # Deposit to vault should fail assert_raises_rpc_error(-32600, "Fixed interval price currently disabled due to locked token", self.nodes[0].deposittovault, self.vault, self.address, f'100000@{self.symbolDUSD}') diff --git a/test/functional/feature_token_split_usd_value.py b/test/functional/feature_token_split_usd_value.py index 11e64eb3b04..2544235dbd6 100755 --- a/test/functional/feature_token_split_usd_value.py +++ b/test/functional/feature_token_split_usd_value.py @@ -4,16 +4,10 @@ # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. -# TODO -# Multiple pools with split stock OK -# Both sides with split token -# Two splits same height -# MINIMUM_LIQUIDITY 1000sats -# Merge with one side bigger in value max_limit """Test token split""" from test_framework.test_framework import DefiTestFramework -from test_framework.util import assert_equal +from test_framework.util import assert_equal, assert_greater_than_or_equal from decimal import Decimal import time @@ -172,9 +166,7 @@ def oracle_split(self): self.nodes[0].setoracledata(self.oracle, int(time.time()), oracle_prices) self.nodes[0].generate(10) - # Make the split and return split height for revert if needed def split(self, tokenId, keepLocked=False, oracleSplit=False, multiplier=2): - tokenSymbol = self.get_token_symbol_from_id(tokenId) self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{tokenId}':'true'}}) self.nodes[0].generate(1) @@ -186,14 +178,13 @@ def split(self, tokenId, keepLocked=False, oracleSplit=False, multiplier=2): self.nodes[0].setgov({"ATTRIBUTES":{f'v0/oracles/splits/{str(splitHeight)}':f'{tokenId}/{multiplier}'}}) self.nodes[0].generate(2) - tokenId = list(self.nodes[0].gettoken(tokenSymbol).keys())[0] + self.idT1old = tokenId + self.idT1 = list(self.nodes[0].gettoken(self.symbolT1).keys())[0] if not keepLocked: - self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{tokenId}':'false'}}) + self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idT1}':'false'}}) self.nodes[0].generate(1) - return splitHeight - def remove_from_pool(self, account): amountLP = self.get_amount_from_account(account, "T1-DUSD") self.nodes[0].removepoolliquidity(account, amountLP+"@T1-DUSD", []) @@ -262,14 +253,13 @@ def get_amount_from_account(self, account, symbol): def compare_usd_account_value_on_split(self, revert=False): revertHeight = self.nodes[0].getblockcount() value_accounts_pre_split = self.accounts_usd_values() - new_idT1 = self.split(self.idT1, oracleSplit=True, multiplier=20) + self.split(self.idT1, oracleSplit=True, multiplier=20) value_accounts_post_split = self.accounts_usd_values() # TODO fail self.compare_value_list(value_accounts_pre_split, value_accounts_post_split) if revert: self.revert(revertHeight) - else: - self.idT1=new_idT1 + self.idT1=self.idT1old def setup_vaults(self, collateralSplit=False): self.nodes[0].createloanscheme(200, 0.01, 'LOAN_0') @@ -305,26 +295,51 @@ def get_vaults_usd_values(self): vault_values.append(vaultInfo) return vault_values - def compare_usd_vaults_values_on_split(self, revert=False): revertHeight = self.nodes[0].getblockcount() - self.setup_vaults() + self.setup_vaults(collateralSplit=False) vault_values_pre_split = self.get_vaults_usd_values() - new_idT1 = self.split(self.idT1, oracleSplit=True, multiplier=20) + self.split(self.idT1, oracleSplit=True, multiplier=20) self.nodes[0].generate(40) vault_values_post_split = self.get_vaults_usd_values() self.compare_vaults_list(vault_values_pre_split,vault_values_post_split) if revert: self.revert(revertHeight) - else: - self.idT1=new_idT1 + self.idT1=self.idT1old + + def test_values_non_zero_with_token_locked(self): + self.setup_vaults() + self.split(self.idT1, keepLocked=True) + vaults_values = self.get_vaults_usd_values() + for vault in vaults_values: + assert_equal(vault["state"], "frozen") + assert_equal(vault["collateralValue"], -1) + assert_equal(vault["loanValue"], -1) + assert_equal(vault["interestValue"], -1) + assert_equal(vault["informativeRatio"], -1) + assert_equal(vault["collateralRatio"], -1) + + def test_values_after_token_unlock(self): + # Unlock token + self.nodes[0].setgov({"ATTRIBUTES":{f'v0/locks/token/{self.idT1}':'false'}}) + self.nodes[0].generate(1) + vaults_values = self.get_vaults_usd_values() + for vault in vaults_values: + assert_equal(vault["state"], "active") + assert_greater_than_or_equal(vault["collateralValue"], 0) + assert_greater_than_or_equal(vault["loanValue"], 0) + assert_greater_than_or_equal(vault["interestValue"], 0) + assert_greater_than_or_equal(vault["informativeRatio"], 0) + assert_greater_than_or_equal(vault["collateralRatio"], 0) def run_test(self): self.setup() assert_equal(1,1) # Make linter happy for now self.compare_usd_account_value_on_split(revert=True) - self.compare_usd_vaults_values_on_split() + self.compare_usd_vaults_values_on_split(revert=True) + self.test_values_non_zero_with_token_locked() + self.test_values_after_token_unlock() if __name__ == '__main__': TokenSplitUSDValueTest().main()