Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interest per block value and amounts on getvault #1314

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/masternodes/rpc_vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace {
return result;
}

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

auto collaterals = pcustomcsview->GetVaultCollaterals(vaultId);
if (!collaterals)
Expand Down Expand Up @@ -152,11 +152,13 @@ namespace {

UniValue loanBalances{UniValue::VARR};
UniValue interestAmounts{UniValue::VARR};
UniValue interestsPerBlockBalances{UniValue::VARR};

if (auto loanTokens = pcustomcsview->GetLoanTokens(vaultId)) {
TAmounts totalBalances{};
TAmounts interestBalances{};
CAmount totalInterests{0};
TAmounts interestsPerBlock{};

for (const auto& loan : loanTokens->balances) {
auto token = pcustomcsview->GetLoanTokenByID(loan.first);
Expand All @@ -169,7 +171,13 @@ namespace {
if (auto priceFeed = pcustomcsview->GetFixedIntervalPrice(token->fixedIntervalPriceId)) {
auto price = priceFeed.val->priceRecord[0];
totalInterests += MultiplyAmounts(price, totalInterest);
if (verbose) {
auto interestPerBlock = rate->interestPerBlock.GetLow64();
interestsPerBlock.insert({loan.first, interestPerBlock});
totalInterestsPerBlock += MultiplyAmounts(price, static_cast<CAmount>(interestPerBlock));
}
}

totalBalances.insert({loan.first, value});
interestBalances.insert({loan.first, totalInterest});
if (pcustomcsview->AreTokensLocked({loan.first.v})){
Expand All @@ -179,6 +187,10 @@ namespace {
interestValue = ValueFromAmount(totalInterests);
loanBalances = AmountsToJSON(totalBalances);
interestAmounts = AmountsToJSON(interestBalances);
if(verbose) {
dcorral marked this conversation as resolved.
Show resolved Hide resolved
interestsPerBlockBalances = AmountsToJSON(interestsPerBlock);
totalInterestsPerBlockValue = ValueFromAmount(totalInterestsPerBlock);
}
}

result.pushKV("vaultId", vaultId.GetHex());
Expand All @@ -194,6 +206,7 @@ namespace {
interestValue = -1;
ratioValue = -1;
collateralRatio = -1;
totalInterestsPerBlockValue = -1;
}
result.pushKV("collateralValue", collValue);
result.pushKV("loanValue", loanValue);
Expand All @@ -207,6 +220,8 @@ namespace {
nextCollateralRatio = int(rate.val->ratio());
result.pushKV("nextCollateralRatio", nextCollateralRatio);
}
result.pushKV("totalInterestPerBlockValue", totalInterestsPerBlockValue);
dcorral marked this conversation as resolved.
Show resolved Hide resolved
result.pushKV("interestPerBlock", interestsPerBlockBalances);
}
return result;
}
Expand Down
17 changes: 15 additions & 2 deletions test/functional/feature_loan_priceupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,26 @@ def run_test(self):
vaultBeforeUpdate = self.nodes[0].getvault(vaultId1, True)
assert_equal(vaultBeforeUpdate["collateralRatio"], 2375)
assert_equal(vaultBeforeUpdate["nextCollateralRatio"], 3213)

interestPerBlockTSLA = vaultBeforeUpdate["interestPerBlock"][0].split('@')[0]
amountInterestTSLA = vaultBeforeUpdate["interestAmounts"][0].split('@')[0]
self.nodes[0].generate(1)
vaultBeforeUpdate = self.nodes[0].getvault(vaultId1, True)
expectedInterestAfterOneBlock = Decimal(amountInterestTSLA) + Decimal(interestPerBlockTSLA)
realInteresAfterOneBlock = Decimal(vaultBeforeUpdate["interestAmounts"][0].split('@')[0])
assert_equal(realInteresAfterOneBlock, expectedInterestAfterOneBlock)
# Let price update and check vault again
self.nodes[0].generate(6)
self.nodes[0].generate(5)

vaultAfterUpdate = self.nodes[0].getvault(vaultId1, True)
assert_equal(vaultAfterUpdate["collateralRatio"], vaultBeforeUpdate["nextCollateralRatio"])
assert_equal(vaultAfterUpdate["nextCollateralRatio"], 3213)
interestPerBlockTSLA = vaultAfterUpdate["interestPerBlock"][0].split('@')[0]
amountInterestTSLA = vaultAfterUpdate["interestAmounts"][0].split('@')[0]
self.nodes[0].generate(1)
vaultAfterUpdate = self.nodes[0].getvault(vaultId1, True)
expectedInterestAfterOneBlock = Decimal(amountInterestTSLA) + Decimal(interestPerBlockTSLA)
realInteresAfterOneBlock = Decimal(vaultAfterUpdate["interestAmounts"][0].split('@')[0])
assert_equal(realInteresAfterOneBlock, expectedInterestAfterOneBlock)

if __name__ == '__main__':
PriceUpdateTest().main()
8 changes: 8 additions & 0 deletions test/functional/feature_token_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ def vault_lock(self):
assert_equal(result['interestValue'], -1)
assert_equal(result['informativeRatio'], -1)
assert_equal(result['collateralRatio'], -1)
result = self.nodes[0].getvault(self.vault, True)
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)
assert_equal(result['totalInterestPerBlockValue'], -1)
assert_equal(result['interestPerBlock'], [])

# 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