Skip to content

Commit

Permalink
Calculate collateral ratio when there is actual price update
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Dec 29, 2021
1 parent 0b63d4f commit 466850d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class CCustomCSView
ByPoolLoanReward,
CGovView :: ByName, ByHeightVars,
CAnchorConfirmsView :: BtcTx,
COracleView :: ByName, FixedIntervalBlockKey, FixedIntervalPriceKey, PriceDeviation,
COracleView :: ByName, FixedIntervalBlockKey, FixedIntervalPriceKey, PriceDeviation, FixedIntervalHeightkKey,
CICXOrderView :: ICXOrderCreationTx, ICXMakeOfferCreationTx, ICXSubmitDFCHTLCCreationTx,
ICXSubmitEXTHTLCCreationTx, ICXClaimDFCHTLCCreationTx, ICXCloseOrderCreationTx,
ICXCloseOfferCreationTx, ICXOrderOpenKey, ICXOrderCloseKey, ICXMakeOfferOpenKey,
Expand Down
19 changes: 18 additions & 1 deletion src/masternodes/oracles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,21 @@ uint32_t COracleView::GetIntervalBlock() const

// Default
return 60 * 60 / Params().GetConsensus().pos.nTargetSpacing;
}
}

Res COracleView::SetLastFixedPriceBlock(const uint32_t height)
{
Write(FixedIntervalHeightkKey::prefix(), height);
return Res::Ok();
}

uint32_t COracleView::GetLastFixedPriceBlock() const
{
uint32_t height;
if (Read(FixedIntervalHeightkKey::prefix(), height)) {
return height;
}

// Default
return 0;
}
4 changes: 4 additions & 0 deletions src/masternodes/oracles.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ class COracleView : public virtual CStorageView
Res SetIntervalBlock(const uint32_t blockInterval);
uint32_t GetIntervalBlock() const;

Res SetLastFixedPriceBlock(const uint32_t height);
uint32_t GetLastFixedPriceBlock() const;

struct ByName { static constexpr uint8_t prefix() { return 'O'; } };
struct PriceDeviation { static constexpr uint8_t prefix() { return 'Y'; } };
struct FixedIntervalBlockKey { static constexpr uint8_t prefix() { return 'z'; } };
struct FixedIntervalPriceKey { static constexpr uint8_t prefix() { return 'y'; } };
struct FixedIntervalHeightkKey { static constexpr uint8_t prefix() { return 'm'; } };
};

#endif // DEFI_MASTERNODES_ORACLES_H
21 changes: 16 additions & 5 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3073,7 +3073,11 @@ void CChainState::ProcessLoanEvents(const CBlockIndex* pindex, CCustomCSView& ca
viewCache.Flush();
}

if (pindex->nHeight % chainparams.GetConsensus().blocksCollateralizationRatioCalculation() == 0) {
auto lastFixedPriceBlock = cache.GetLastFixedPriceBlock();
auto blockCollateralRatioCalculation = chainparams.GetConsensus().blocksCollateralizationRatioCalculation();

if (pindex->nHeight % blockCollateralRatioCalculation == 0
&& (lastFixedPriceBlock == 0 || lastFixedPriceBlock > pindex->nHeight - blockCollateralRatioCalculation)) {
bool useNextPrice = false, requireLivePrice = true;
LogPrint(BCLog::LOAN,"ProcessLoanEvents()->ForEachVaultCollateral():\n"); /* Continued */

Expand Down Expand Up @@ -3259,6 +3263,7 @@ void CChainState::ProcessOracleEvents(const CBlockIndex* pindex, CCustomCSView&
if (pindex->nHeight % blockInterval != 0) {
return;
}
bool actualPriceUpdate = false;
cache.ForEachFixedIntervalPrice([&](const CTokenCurrencyPair&, CFixedIntervalPrice fixedIntervalPrice){
// Ensure that we update active and next regardless of state of things
// And SetFixedIntervalPrice on each evaluation of this block.
Expand All @@ -3273,20 +3278,22 @@ void CChainState::ProcessOracleEvents(const CBlockIndex* pindex, CCustomCSView&

// Furthermore, the time stamp is always indicative of the
// last price time.
auto nextPrice = fixedIntervalPrice.priceRecord[1];
auto& nextPrice = fixedIntervalPrice.priceRecord[1];
if (nextPrice > 0) {
fixedIntervalPrice.priceRecord[0] = fixedIntervalPrice.priceRecord[1];
auto& currentPrice = fixedIntervalPrice.priceRecord[0];
actualPriceUpdate = actualPriceUpdate || nextPrice != currentPrice;
currentPrice = nextPrice;
}
// keep timestamp updated
fixedIntervalPrice.timestamp = pindex->nTime;
// Use -1 to indicate empty price
fixedIntervalPrice.priceRecord[1] = -1;
nextPrice = -1;
auto aggregatePrice = GetAggregatePrice(cache,
fixedIntervalPrice.priceFeedId.first,
fixedIntervalPrice.priceFeedId.second,
pindex->nTime);
if (aggregatePrice) {
fixedIntervalPrice.priceRecord[1] = aggregatePrice;
nextPrice = aggregatePrice;
} else {
LogPrint(BCLog::ORACLE,"ProcessOracleEvents(): No aggregate price available: %s\n", aggregatePrice.msg);
}
Expand All @@ -3296,6 +3303,10 @@ void CChainState::ProcessOracleEvents(const CBlockIndex* pindex, CCustomCSView&
}
return true;
});

if (actualPriceUpdate && pindex->nHeight >= chainparams.GetConsensus().FortCanningHillHeight) {
cache.SetLastFixedPriceBlock(pindex->nHeight);
}
}

bool CChainState::FlushStateToDisk(
Expand Down

0 comments on commit 466850d

Please sign in to comment.