Skip to content

Commit

Permalink
Merge branch 'master' into pvl/consolidaterewards-on-start
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl authored May 31, 2022
2 parents a58c785 + 3db917e commit 6a0100e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
19 changes: 19 additions & 0 deletions src/masternodes/rpc_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand All @@ -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};

Expand All @@ -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);
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions test/functional/feature_token_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down
59 changes: 37 additions & 22 deletions test/functional/feature_token_split_usd_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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", [])
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()

0 comments on commit 6a0100e

Please sign in to comment.