Skip to content

Commit

Permalink
Interest per block value and amounts on getvault (#1314)
Browse files Browse the repository at this point in the history
* Add interest per block value and amounts on getvault

* Update src/masternodes/rpc_vault.cpp

Co-authored-by: Prasanna Loganathar <[email protected]>

* Restores accidentally removed variable

* Refactors

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
dcorral and prasannavl authored May 31, 2022
1 parent dfedbed commit 9a570e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
18 changes: 17 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,14 @@ 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};
CAmount totalInterestsPerBlock{0};
TAmounts interestsPerBlock{};

for (const auto& loan : loanTokens->balances) {
auto token = pcustomcsview->GetLoanTokenByID(loan.first);
Expand All @@ -169,7 +172,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 +188,10 @@ namespace {
interestValue = ValueFromAmount(totalInterests);
loanBalances = AmountsToJSON(totalBalances);
interestAmounts = AmountsToJSON(interestBalances);
if (verbose) {
interestsPerBlockBalances = AmountsToJSON(interestsPerBlock);
totalInterestsPerBlockValue = ValueFromAmount(totalInterestsPerBlock);
}
}

result.pushKV("vaultId", vaultId.GetHex());
Expand All @@ -194,6 +207,7 @@ namespace {
interestValue = -1;
ratioValue = -1;
collateralRatio = -1;
totalInterestsPerBlockValue = -1;
}
result.pushKV("collateralValue", collValue);
result.pushKV("loanValue", loanValue);
Expand All @@ -207,6 +221,8 @@ namespace {
nextCollateralRatio = int(rate.val->ratio());
result.pushKV("nextCollateralRatio", nextCollateralRatio);
}
result.pushKV("interestPerBlockValue", totalInterestsPerBlockValue);
result.pushKV("interestsPerBlock", 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["interestsPerBlock"][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["interestsPerBlock"][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['interestPerBlockValue'], -1)
assert_equal(result['interestsPerBlock'], [])

# 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

0 comments on commit 9a570e7

Please sign in to comment.