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 Feb 28, 2022
1 parent 80fca65 commit b4e4ea0
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 @@ -415,7 +415,7 @@ class CCustomCSView
ByPoolLoanReward, ByTokenDexFeePct,
CGovView :: ByName, ByHeightVars,
CAnchorConfirmsView :: BtcTx,
COracleView :: ByName, FixedIntervalBlockKey, FixedIntervalPriceKey, PriceDeviation,
COracleView :: ByName, FixedIntervalBlockKey, FixedIntervalPriceKey, PriceDeviation, FixedIntervalHeightKey,
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(FixedIntervalHeightKey::prefix(), height);
return Res::Ok();
}

uint32_t COracleView::GetLastFixedPriceBlock() const
{
uint32_t height;
if (Read(FixedIntervalHeightKey::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 @@ -148,10 +148,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 FixedIntervalHeightKey { 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 @@ -3038,7 +3038,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 @@ -3220,6 +3224,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 @@ -3234,20 +3239,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 @@ -3257,6 +3264,10 @@ void CChainState::ProcessOracleEvents(const CBlockIndex* pindex, CCustomCSView&
}
return true;
});

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

void CChainState::ProcessGovEvents(const CBlockIndex* pindex, CCustomCSView& cache, const CChainParams& chainparams) {
Expand Down

0 comments on commit b4e4ea0

Please sign in to comment.