Skip to content

Commit

Permalink
feat: retry with podsign forgetting account state
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Sep 8, 2024
1 parent 5b8f114 commit fd33197
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 81 deletions.
37 changes: 18 additions & 19 deletions debts/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (
"sort"

"github.com/Gearbox-protocol/sdk-go/artifacts/multicall"
"github.com/Gearbox-protocol/sdk-go/artifacts/yearnPriceFeed"
"github.com/Gearbox-protocol/sdk-go/calc"
"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/core/schemas"
"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/sdk-go/pkg/dc"
"github.com/Gearbox-protocol/sdk-go/utils"
"github.com/Gearbox-protocol/third-eye/ds"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -298,15 +296,18 @@ func (eng *DebtEngine) SessionDebtHandler(blockNum int64, session *schemas.Credi
// yearn price feed might be stale as a result difference btw dc and calculated values
// solution: fetch price again for all stale yearn feeds
if profile != nil {
yearnFeeds := eng.repo.GetRetryFeedForDebts()
retryFeeds := eng.repo.GetRetryFeedForDebts()
for tokenAddr, details := range *sessionSnapshot.Balances {
if details.IsEnabled && details.HasBalanceMoreThanOne() {
lastPriceEvent := eng.getTokenPriceFeed(tokenAddr, schemas.VersionToPFVersion(session.Version, false)) // don't use reserve
//
if tokenAddr != eng.repo.GetWETHAddr() && lastPriceEvent.BlockNumber != blockNum {
feed := lastPriceEvent.Feed
if utils.Contains(yearnFeeds, feed) {
eng.requestPriceFeed(blockNum, feed, tokenAddr, lastPriceEvent.MergedPFVersion)
feedAddr := lastPriceEvent.Feed
for _, retryFeed:= range retryFeeds {
if retryFeed.GetAddress() == feedAddr {
eng.requestPriceFeed(blockNum, retryFeed, tokenAddr, lastPriceEvent.MergedPFVersion)

}
}
}
}
Expand Down Expand Up @@ -503,29 +504,27 @@ func (eng *DebtEngine) SessionDataFromDC(version core.VersionType, blockNum int6
return data
}

func (eng *DebtEngine) requestPriceFeed(blockNum int64, feed, token string, pfVersion schemas.MergedPFVersion) {
func (eng *DebtEngine) requestPriceFeed(blockNum int64, retryFeed ds.QueryPriceFeedI, token string, pfVersion schemas.MergedPFVersion) {
// defer func() {
// if err:= recover(); err != nil {
// log.Warn("err", err, "in getting yearn price feed in debt", feed, token, blockNum, pfVersion)
// }
// }()
// PFFIX
yearnPFContract, err := yearnPriceFeed.NewYearnPriceFeed(common.HexToAddress(feed), eng.client)
log.CheckFatal(err)
opts := &bind.CallOpts{
BlockNumber: big.NewInt(blockNum),
}
roundData, err := yearnPFContract.LatestRoundData(opts)
if err != nil {
log.Fatal(err)
calls, isQueryable := retryFeed.GetCalls(blockNum)
if !isQueryable {
return
}
log.Info("getting price for ", token, "at", blockNum)
results :=core.MakeMultiCall(eng.client, blockNum, false, calls)
price := retryFeed.ProcessResult(blockNum, results, true)
eng.AddTokenLastPrice(&schemas.PriceFeed{
BlockNumber: blockNum,
Token: token,
Feed: feed,
RoundId: roundData.RoundId.Int64(),
PriceBI: (*core.BigInt)(roundData.Answer),
Price: utils.GetFloat64Decimal(roundData.Answer, pfVersion.Decimals()),
Feed: retryFeed.GetAddress(),
RoundId: price.RoundId,
PriceBI: price.PriceBI,
Price: price.Price,
MergedPFVersion: pfVersion,
})
}
2 changes: 1 addition & 1 deletion ds/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type RepositoryI interface {
RecentMsgf(headers log.RiskHeader, msg string, args ...interface{})
//
// oracle and uni
GetRetryFeedForDebts() []string
GetRetryFeedForDebts() []QueryPriceFeedI
//
LoadLastDebtSync() int64
LoadLastAdapterSync() int64
Expand Down
15 changes: 14 additions & 1 deletion ds/repo_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"
"time"

"github.com/Gearbox-protocol/sdk-go/artifacts/multicall"
"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/core/schemas"
"github.com/Gearbox-protocol/sdk-go/core/schemas/schemas_v3"
Expand Down Expand Up @@ -208,7 +209,7 @@ func (DummyRepo) RecentMsgf(headers log.RiskHeader, msg string, args ...interfac
}

// oracle
func (DummyRepo) GetRetryFeedForDebts() []string {
func (DummyRepo) GetRetryFeedForDebts() []QueryPriceFeedI {
return nil
}

Expand Down Expand Up @@ -260,3 +261,15 @@ type DieselBalance struct {
func (DieselBalance) TableName() string {
return "diesel_balances"
}


type QueryPriceFeedI interface {
TokensValidAtBlock(blockNum int64) []schemas.TokenAndMergedPFVersion
GetPFType() string
SyncAdapterI
GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool)
ProcessResult(blockNum int64, results []multicall.Multicall2Result, force ...bool) *schemas.PriceFeed
DisableToken(token string, disabledAt int64, pfVersion schemas.PFVersion)
AddToken(token string, discoveredAt int64, pfVersion schemas.PFVersion)
GetTokens() map[string]map[schemas.PFVersion][]int64
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/Gearbox-protocol/third-eye
go 1.20

require (
github.com/Gearbox-protocol/sdk-go v0.0.0-20240903141325-ca91bc2d32d3
github.com/Gearbox-protocol/sdk-go v0.0.0-20240908055634-9130911ce752
github.com/ethereum/go-ethereum v1.13.14
github.com/go-playground/validator/v10 v10.4.1
github.com/google/go-cmp v0.5.9
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ contrib.go.opencensus.io/exporter/ocagent v0.6.0/go.mod h1:zmKjrJcdo0aYcVS7bmEeS
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
github.com/Gearbox-protocol/sdk-go v0.0.0-20240903070426-d7305e1702be h1:yUJgugM5LvH7Z+Q1UraSlTzjFwV7vmwcuI4OhhpAIBg=
github.com/Gearbox-protocol/sdk-go v0.0.0-20240903070426-d7305e1702be/go.mod h1:jRBSOG94bpGc5ci8EWIPUVXZdaGEaekMNmhajbmWFVU=
github.com/Gearbox-protocol/sdk-go v0.0.0-20240903133928-01c5eb46dcd9 h1:A1BUUcqNsxA8paO1FUk3iJDSCmyYp3ixOkgZJaE8JOU=
github.com/Gearbox-protocol/sdk-go v0.0.0-20240903133928-01c5eb46dcd9/go.mod h1:jRBSOG94bpGc5ci8EWIPUVXZdaGEaekMNmhajbmWFVU=
github.com/Gearbox-protocol/sdk-go v0.0.0-20240903141325-ca91bc2d32d3 h1:FwNBOH/pEn29i/Pvdr1mjkwYH4KXbzoTXiNV2M0n3mA=
github.com/Gearbox-protocol/sdk-go v0.0.0-20240903141325-ca91bc2d32d3/go.mod h1:jRBSOG94bpGc5ci8EWIPUVXZdaGEaekMNmhajbmWFVU=
github.com/Gearbox-protocol/sdk-go v0.0.0-20240908055634-9130911ce752 h1:NRCd1mawNwX57Q0jpGUyrWbPlRKSW92N7sSh6kAHRBk=
github.com/Gearbox-protocol/sdk-go v0.0.0-20240908055634-9130911ce752/go.mod h1:jRBSOG94bpGc5ci8EWIPUVXZdaGEaekMNmhajbmWFVU=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/OffchainLabs/go-ethereum v1.13.4-0.20240313010929-e5d8587e7227 h1:+/3TrD+q+BP36jGj2Bycdmrc/joKLNbc5ImePQzKRLM=
Expand Down
18 changes: 0 additions & 18 deletions models/aggregated_block_feed/base_price_feed/interface.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package composite_redstone_price_feed

import (
"math/big"
"time"

"github.com/Gearbox-protocol/sdk-go/artifacts/multicall"
"github.com/Gearbox-protocol/sdk-go/core"
Expand Down Expand Up @@ -68,7 +67,8 @@ func (mdl *CompositeRedStonePriceFeed) ProcessResult(blockNum int64, results []m
price := *abi.ConvertType(value[1], new(*big.Int)).(**big.Int)
log.Info("onchain price found for ", mdl.Address, "at", blockNum, price)
return parsePriceForRedStone(price, isPriceInUSD)
} else if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp),0)) > time.Hour {
// } else if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp),0)) > time.Hour {
} else {
if (len(force) ==0 || !force[0] ) {
return nil
}
Expand Down
7 changes: 3 additions & 4 deletions models/aggregated_block_feed/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/core/schemas"
"github.com/Gearbox-protocol/third-eye/ds"
"github.com/Gearbox-protocol/third-eye/models/aggregated_block_feed/base_price_feed"
"github.com/Gearbox-protocol/third-eye/models/aggregated_block_feed/composite_redstone_price_feed"
"github.com/Gearbox-protocol/third-eye/models/aggregated_block_feed/curve_price_feed"
"github.com/Gearbox-protocol/third-eye/models/aggregated_block_feed/redstone_price_feed"
"github.com/Gearbox-protocol/third-eye/models/aggregated_block_feed/yearn_price_feed"
)

func NewQueryPriceFeed(token, oracle string, pfType string, discoveredAt int64, client core.ClientI, repo ds.RepositoryI, pfVersion schemas.PFVersion) base_price_feed.QueryPriceFeedI {
func NewQueryPriceFeed(token, oracle string, pfType string, discoveredAt int64, client core.ClientI, repo ds.RepositoryI, pfVersion schemas.PFVersion) ds.QueryPriceFeedI {
switch pfType {
case ds.RedStonePF:
return redstone_price_feed.NewRedstonePriceFeed(token, oracle, pfType, discoveredAt, client, repo, pfVersion)
Expand All @@ -25,7 +24,7 @@ func NewQueryPriceFeed(token, oracle string, pfType string, discoveredAt int64,
return nil
}
}
func NewQueryPriceFeedFromAdapter(adapter *ds.SyncAdapter) base_price_feed.QueryPriceFeedI {
func NewQueryPriceFeedFromAdapter(adapter *ds.SyncAdapter) ds.QueryPriceFeedI {
switch adapter.GetDetailsByKey("pfType") {
case ds.RedStonePF:
return redstone_price_feed.NewRedstonePriceFeedFromAdapter(adapter)
Expand All @@ -40,7 +39,7 @@ func NewQueryPriceFeedFromAdapter(adapter *ds.SyncAdapter) base_price_feed.Query
return nil
}
}
func FromAdapter(obj ds.SyncAdapterI) base_price_feed.QueryPriceFeedI {
func FromAdapter(obj ds.SyncAdapterI) ds.QueryPriceFeedI {
switch adapter := obj.(type) {
case *curve_price_feed.CurvePriceFeed:
return adapter
Expand Down
15 changes: 7 additions & 8 deletions models/aggregated_block_feed/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/sdk-go/utils"
"github.com/Gearbox-protocol/third-eye/ds"
"github.com/Gearbox-protocol/third-eye/models/aggregated_block_feed/base_price_feed"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -21,7 +20,7 @@ type AQFWrapper struct {
*ds.SyncAdapter
mu *sync.Mutex
// yearn feed
QueryFeeds map[string]base_price_feed.QueryPriceFeedI
QueryFeeds map[string]ds.QueryPriceFeedI

// for dependency based fetching price
queryPFdeps *QueryPFDependencies
Expand Down Expand Up @@ -53,21 +52,21 @@ func NewAQFWrapper(client core.ClientI, repo ds.RepositoryI, interval int64) *AQ
SyncAdapter: syncAdapter,
Interval: interval,
mu: &sync.Mutex{},
QueryFeeds: map[string]base_price_feed.QueryPriceFeedI{},
QueryFeeds: map[string]ds.QueryPriceFeedI{},
queryPFdeps: NewQueryPFDepenencies(repo, client),
}
wrapper.queryPFdeps.aqf = wrapper
return wrapper
}

// only called by priceoracle
func (mdl *AQFWrapper) AddQueryPriceFeed(adapter base_price_feed.QueryPriceFeedI) {
func (mdl *AQFWrapper) AddQueryPriceFeed(adapter ds.QueryPriceFeedI) {
mdl.LastSync = utils.Min(adapter.GetLastSync(), mdl.LastSync)
mdl.QueryFeeds[adapter.GetAddress()] = adapter
}

func (mdl *AQFWrapper) GetQueryFeeds() []base_price_feed.QueryPriceFeedI {
feeds := make([]base_price_feed.QueryPriceFeedI, 0, len(mdl.QueryFeeds))
func (mdl *AQFWrapper) GetQueryFeeds() []ds.QueryPriceFeedI {
feeds := make([]ds.QueryPriceFeedI, 0, len(mdl.QueryFeeds))
for _, feed := range mdl.QueryFeeds {
feeds = append(feeds, feed)
}
Expand Down Expand Up @@ -104,7 +103,7 @@ func mergePFVersionAt(blockNum int64, details map[schemas.PFVersion][]int64) sch
}
return pfVersion
}
func createPriceFeedOnInit(qpf base_price_feed.QueryPriceFeedI, client core.ClientI, token string, discoveredAt int64) []*schemas.PriceFeed {
func createPriceFeedOnInit(qpf ds.QueryPriceFeedI, client core.ClientI, token string, discoveredAt int64) []*schemas.PriceFeed {
mainPFContract, err := priceFeed.NewPriceFeed(common.HexToAddress(qpf.GetAddress()), client)
log.CheckFatal(err)
data, err := mainPFContract.LatestRoundData(&bind.CallOpts{BlockNumber: big.NewInt(discoveredAt)})
Expand Down Expand Up @@ -135,7 +134,7 @@ func (mdl *AQFWrapper) OnLog(txLog types.Log) {

// no need to check version of feed, as while adding from chainlink we make sure that the version is more than 1
// and we can't have version 2 and 3 feed active at the same time.
func (mdl AQFWrapper) getFeedAdapters(blockNum int64, neededTokens map[string]bool) (result []base_price_feed.QueryPriceFeedI) {
func (mdl AQFWrapper) getFeedAdapters(blockNum int64, neededTokens map[string]bool) (result []ds.QueryPriceFeedI) {
for _, adapter := range mdl.QueryFeeds {
if !adapter.GetVersion().MoreThan(core.NewVersion(1)) {
continue
Expand Down
6 changes: 3 additions & 3 deletions models/aggregated_block_feed/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/core/schemas"
"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/third-eye/models/aggregated_block_feed/base_price_feed"
"github.com/Gearbox-protocol/third-eye/ds"
// "fmt"
)

Expand Down Expand Up @@ -121,7 +121,7 @@ func (mdl *AQFWrapper) QueryData(blockNum int64) []*schemas.PriceFeed {
}

type adapterAndNoCall struct {
adapter base_price_feed.QueryPriceFeedI
adapter ds.QueryPriceFeedI
nocalls int
}

Expand All @@ -144,7 +144,7 @@ func (mdl *AQFWrapper) getRoundDataCalls(blockNum int64) (calls []multicall.Mult
return
}

func processRoundDataWithAdapterTokens(blockNum int64, adapter base_price_feed.QueryPriceFeedI, entries []multicall.Multicall2Result) []*schemas.PriceFeed {
func processRoundDataWithAdapterTokens(blockNum int64, adapter ds.QueryPriceFeedI, entries []multicall.Multicall2Result) []*schemas.PriceFeed {

// } else if utils.Contains([]string{"0xCbeCfA4017965939805Da5a2150E3DB1BeDD0364", "0x814E6564e8cda436c1ab25041C10bfdb21dEC519"},

Expand Down
4 changes: 2 additions & 2 deletions models/aggregated_block_feed/redstone_price_feed/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package redstone_price_feed
import (
"encoding/hex"
"math/big"
"time"

"github.com/Gearbox-protocol/sdk-go/artifacts/multicall"
"github.com/Gearbox-protocol/sdk-go/core"
Expand Down Expand Up @@ -54,7 +53,8 @@ func (mdl *RedstonePriceFeed) ProcessResult(blockNum int64, results []multicall.
price := *abi.ConvertType(value[1], new(*big.Int)).(**big.Int)
log.Info("onchain price found for ", mdl.Address, "at", blockNum, price)
return parsePriceForRedStone(price, isPriceInUSD)
} else if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp),0)) > time.Hour {
// } else if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp),0)) > time.Hour {
} else {
if (len(force) ==0 || !force[0] ) {
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions models/credit_manager/cm_common/credit_session_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func (mdl *CommonCMAdapter) liqv3SessionCallAndResultFn(liquidatedAt int64, sess
log.Fatalf("Failing GetAccount for CM:%s Borrower:%s at %d: %v", mdl.GetAddress(), session.Borrower, liquidatedAt-1, result.ReturnData)
}
dcAccountData, err := resultFn(result.ReturnData)
if err == nil && !dcAccountData.IsSuccessful && mdl.GetVersion() == core.NewVersion(300) {
dcAccountData, err = mdl.retry(dcAccountData, liquidatedAt-1)
}
if err != nil {
log.Fatalf("For blockNum %d CM:%s Borrower:%s %v", liquidatedAt, mdl.GetAddress(), session.Borrower, err)
}
Expand Down Expand Up @@ -144,6 +147,9 @@ func (mdl *CommonCMAdapter) closeSessionCallAndResultFn(closedAt int64, sessionI
log.Fatalf("Failing GetAccount for CM:%s Borrower:%s at %d: %v, dc: %s(%s)", mdl.GetAddress(), session.Borrower, closedAt-1, result.ReturnData, key, dc)
}
dcAccountData, err := resultFn(result.ReturnData)
if err == nil && !dcAccountData.IsSuccessful && mdl.GetVersion() == core.NewVersion(300) {
dcAccountData, err = mdl.retry(dcAccountData, closedAt-1)
}
if err != nil {
log.Fatalf("For blockNum %d CM:%s Borrower:%s %v", closedAt, mdl.GetAddress(), session.Borrower, err)
}
Expand Down Expand Up @@ -252,6 +258,7 @@ func (mdl *CommonCMAdapter) poolRepay(blockNum int64, session *schemas.CreditSes

}


func (mdl *CommonCMAdapter) updateSessionCallAndProcessFn(sessionId string, blockNum int64) (
multicall.Multicall2Call, func(multicall.Multicall2Result)) {
if mdl.DontGetSessionFromDCForTest {
Expand Down
Loading

0 comments on commit fd33197

Please sign in to comment.