Skip to content

Commit

Permalink
fix: recover querypricefeed getprice in debt
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh jain authored and harsh jain committed Oct 21, 2024
1 parent a8a2cf1 commit 9c17779
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
3 changes: 3 additions & 0 deletions debts/debt_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func (eng *DebtEngine) AddDebt(debt *schemas.Debt, forceAdd bool) {
core.DiffMoreThanFraction(lastDebt.CalDebtBI, debt.CalDebtBI, big.NewFloat(0.05)) ||
// add debt when the health factor is on different side of 10000 from the lastdebt
core.ValueDifferSideOf10000(debt.CalHealthFactor, lastDebt.CalHealthFactor) {
if lastDebt.CalHealthFactor.Cmp(debt.CalDebtBI) == 0 && lastDebt.CalTotalValueBI.Cmp(debt.CalTotalValueBI) == 0 {
return
}
eng.addDebt(debt)
}
} else {
Expand Down
15 changes: 9 additions & 6 deletions debts/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ func (eng *DebtEngine) SessionDebtHandler(blockNum int64, session *schemas.Credi
feedAddr := lastPriceEvent.Feed
for _, retryFeed := range retryFeeds {
if retryFeed.GetAddress() == feedAddr {
eng.priceHandler.requestPriceFeed(blockNum, eng.client, retryFeed, tokenAddr)

// log.Info("hf ", debt.CalHealthFactor.Convert(), "of", sessionId, "at", blockNum)
eng.priceHandler.requestPriceFeed(blockNum, eng.client, retryFeed, tokenAddr, profile != nil)
}
}
}
Expand Down Expand Up @@ -429,12 +429,15 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
notMatched = true
}
// even if data compressor matching is disabled check the calc values with session data at block where last credit snapshot was taken
} else if sessionSnapshot.BlockNum == blockNum && sessionSnapshot.HealthFactor.Convert().Cmp(new(big.Int)) != 0 { // it is 0 when the issuccessful is false for redstone credit accounts
// // 20563217 and 0xe8f5F52842D7AF4BbcF5Fe731A336147B51F09D5_19980779_297 on mainnet has creditsessionsnapshot but isSuccessful for dv3 is false.
} else if sessionSnapshot.BlockNum == blockNum && sessionSnapshot.HealthFactor.Convert().Cmp(new(big.Int)) != 0 &&
!(utils.Contains([]int64{20563217, 20671148}, blockNum) && sessionSnapshot.TotalValueBI.Convert().Cmp(new(big.Int)) == 0) {
// it is 0 when the issuccessful is false for redstone credit accounts
if IsChangeMoreThanFraction(debt.CalTotalValueBI, sessionSnapshot.TotalValueBI, big.NewFloat(0.001)) ||
// hf value calculated are on different side of 1
core.ValueDifferSideOf10000(debt.CalHealthFactor, sessionSnapshot.HealthFactor) ||
// if healhFactor diff by 4 %
core.DiffMoreThanFraction(debt.CalHealthFactor, sessionSnapshot.HealthFactor, big.NewFloat(0.04)) {
core.DiffMoreThanFraction(debt.CalHealthFactor, sessionSnapshot.HealthFactor, big.NewFloat(0.01)) {
// log.Info(debt.CalHealthFactor, sessionSnapshot.HealthFactor, blockNum)
// log.Info(debt.CalTotalValueBI, sessionSnapshot.TotalValueBI, blockNum)
if log.GetBaseNet(core.GetChainId(eng.client)) == "ARBITRUM" {
Expand All @@ -451,12 +454,12 @@ func (eng *DebtEngine) CalculateSessionDebt(blockNum int64, session *schemas.Cre
notMatched = true
}
log.Info(
debt.CalTotalValueBI, sessionSnapshot.TotalValueBI,
debt.CalTotalValueBI, sessionSnapshot.TotalValueBI, sessionId,
IsChangeMoreThanFraction(debt.CalTotalValueBI, sessionSnapshot.TotalValueBI, big.NewFloat(0.001)),
// hf value calculated are on different side of 1
core.ValueDifferSideOf10000(debt.CalHealthFactor, sessionSnapshot.HealthFactor),
// if healhFactor diff by 4 %
core.DiffMoreThanFraction(debt.CalHealthFactor, sessionSnapshot.HealthFactor, big.NewFloat(0.04)),
core.DiffMoreThanFraction(debt.CalHealthFactor, sessionSnapshot.HealthFactor, big.NewFloat(0.01)),
)
}
}
Expand Down
49 changes: 31 additions & 18 deletions debts/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (eng *PriceHandler) GetLastPriceFeed(cm string, token string, version core.
priceOracle = obj.GetPriceOracleByVersion(version)
}
feed := eng.poTotokenOracle[priceOracle][token]
if feed != nil { // has feed
if feed != nil && eng.feedLastPrice[feed.Feed] != nil { // has feed
return eng.feedLastPrice[feed.Feed]
// feed.Feed
}
Expand All @@ -111,9 +111,10 @@ func (eng *PriceHandler) GetLastPriceFeed(cm string, token string, version core.
// }
//
if len(dontFail) > 0 && dontFail[0] {
log.Infof("Price not found for %s pfversion: %d, priceoracle:%s, feed:%s", token, version, priceOracle, feed.Feed)
return nil
}
log.Fatal(fmt.Sprintf("Price not found for %s pfversion: %d", token, version))
log.Fatal(fmt.Sprintf("Price not found for %s pfversion: %d, priceoracle:%s, feed:%s", token, version, priceOracle, feed.Feed))
return nil
}
func (eng *PriceHandler) GetLastPriceFeedByOracle(priceOracle schemas.PriceOracleT, token string, version core.VersionType, dontFail ...bool) *schemas.PriceFeed {
Expand All @@ -133,31 +134,43 @@ func (eng *PriceHandler) GetLastPrice(cm string, token string, version core.Vers
if version.Eq(1) && eng.repo.GetWETHAddr() == token { // for mainnet on ethereum
return core.WETHPrice
}
a := eng.GetLastPriceFeed(cm, token, version).PriceBI.Convert()
return a
a := eng.GetLastPriceFeed(cm, token, version)
if a == nil {
log.Fatal(cm, token, version)
}

return a.PriceBI.Convert()
}

func (eng *PriceHandler) requestPriceFeed(blockNum int64, client core.ClientI, retryFeed ds.QueryPriceFeedI, token string) {
// defer func() {
// if err:= recover(); err != nil {
// log.Warn("err", err, "in getting yearn price feed in debt", feed, token, blockNum, pfVersion)
// }
// }()
func (eng *PriceHandler) requestPriceFeed(blockNum int64, client core.ClientI, retryFeed ds.QueryPriceFeedI, token string, misHFOrTValue bool) {
defer func() {
// if err := recover(); err != nil {
// log.Warn("err", err, "in getting query price feed in debt", token, blockNum)
// }
}()
// PFFIX
calls, isQueryable := retryFeed.GetCalls(blockNum)
if !isQueryable {
return
}
log.Info("getting price for ", token, "at", blockNum)
category := ""
if misHFOrTValue {
category = "due to missed hf/total_value"
} else {
category = "due to hf <1"
}
log.Info("getting price for ", token, "at", blockNum, category)
results := core.MakeMultiCall(client, blockNum, false, calls)
price := retryFeed.ProcessResult(blockNum, results, true)
eng.AddTokenLastPrice(&schemas.PriceFeed{
BlockNumber: blockNum,
Feed: retryFeed.GetAddress(),
RoundId: price.RoundId,
PriceBI: price.PriceBI,
Price: price.Price,
})
if price != nil {
eng.AddTokenLastPrice(&schemas.PriceFeed{
BlockNumber: blockNum,
Feed: retryFeed.GetAddress(),
RoundId: price.RoundId,
PriceBI: price.PriceBI,
Price: price.Price,
})
}
}

func (eng *PriceHandler) init(repo ds.RepositoryI) {
Expand Down
3 changes: 2 additions & 1 deletion models/chainlink_price_feed/on_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func (mdl *ChainlinkPriceFeed) OnLogs(txLogs []types.Log) {
PriceBI: (*core.BigInt)(answerBI),
Price: utils.GetFloat64Decimal(answerBI, mdl.GetVersion().Decimals()),
}
mdl.Repo.AddPriceFeed(priceFeed)
mdl.pfs = append(mdl.pfs, priceFeed)
// mdl.Repo.AddPriceFeed(priceFeed)
blockNums = append(blockNums, blockNum)
}
}
Expand Down

0 comments on commit 9c17779

Please sign in to comment.