Skip to content

Commit 7ee6ddf

Browse files
committed
feat: force price if 1 hr difference for redstone
1 parent 74cab4b commit 7ee6ddf

File tree

8 files changed

+40
-13
lines changed

8 files changed

+40
-13
lines changed

ds/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ type RepositoryI interface {
150150
TokensValidAtBlock(string, int64) []*schemas.TokenOracle
151151
TokenAddrsValidAtBlock(string, int64) map[string]bool
152152
GetActivePriceOracleByBlockNum(blockNum int64) (schemas.PriceOracleT, core.VersionType, error)
153+
//
154+
GetPrevPriceFeed(feed string) *schemas.PriceFeed
153155
}
154156

155157
func IsTestnet(client core.ClientI) bool {

ds/repo_dummy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ func (DummyRepo) TokenAddrsValidAtBlock(string, int64) map[string]bool {
279279
return nil
280280
}
281281

282+
func (DummyRepo) GetPrevPriceFeed(feed string) *schemas.PriceFeed {
283+
return nil
284+
}
285+
282286
type QueryPriceFeedI interface {
283287
// TokensValidAtBlock(blockNum int64) []schemas.TokenAndMergedPFVersion
284288
GetPFType() string

models/aggregated_block_feed/composite_redstone_price_feed/model.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func (mdl *CompositeRedStonePriceFeed) GetCalls(blockNum int64) (calls []multica
8383
}}, true
8484
}
8585
func GetSpotPriceFeed(blockNum int64, token string, feedAddr string, repo ds.RepositoryI, client core.ClientI) *schemas.PriceFeed {
86+
if token == "" {
87+
return nil
88+
}
8689
chainId := core.GetBaseChainId(client)
8790
if chainId == 1 {
8891
pricespot, err := priceFetcher.GetPriceSpot(repo.GetToken(token), core.GetToken(chainId, "USDC"), client, blockNum)
@@ -117,7 +120,7 @@ func (mdl *CompositeRedStonePriceFeed) ProcessResult(blockNum int64, results []m
117120
}
118121
}
119122
validTokens := mdl.Repo.TokensValidAtBlock(mdl.Address, blockNum)
120-
if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), 0)) > time.Hour*24*30 {
123+
if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), 0)) > time.Hour*24*30 && token != "" {
121124
return GetSpotPriceFeed(blockNum, token, mdl.Address, mdl.Repo, mdl.Client)
122125
}
123126
// log.Info(mdl.Repo.SetAndGetBlock(blockNum).Timestamp, validTokens, utils.ToJson(mdl.DetailsDS))

models/aggregated_block_feed/dependency.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"github.com/Gearbox-protocol/third-eye/ds"
1212
)
1313

14-
type repoI interface {
15-
GetAdapter(addr string) ds.SyncAdapterI
16-
// if returned value is nil, it means that token oracle hasn't been added yet.
17-
GetTokens() []string
18-
GetToken(string) *schemas.Token
19-
}
14+
// type repoI interface {
15+
// GetAdapter(addr string) ds.SyncAdapterI
16+
// // if returned value is nil, it means that token oracle hasn't been added yet.
17+
// GetTokens() []string
18+
// GetToken(string) *schemas.Token
19+
// }
2020

2121
// ignoring dependencies for v1
2222
type QueryPFDependencies struct {
@@ -30,7 +30,7 @@ type QueryPFDependencies struct {
3030
//
3131
aqf *AQFWrapper
3232
//
33-
repo repoI
33+
repo ds.RepositoryI
3434
mu *sync.Mutex
3535
client core.ClientI
3636
}
@@ -179,7 +179,7 @@ func (q *QueryPFDependencies) fetchRoundData(blockNum int64, tokens map[string]b
179179
for i := 0; i < entry.nocalls; i++ {
180180
results = append(results, iterator.Next())
181181
}
182-
prices := processRoundDataWithAdapterTokens(blockNum, entry.adapter, results)
182+
prices := processRoundDataWithAdapterTokens(blockNum, entry.adapter, results, getForceForAdapter(q.repo, entry.adapter))
183183
q.updateQueryPrices(prices)
184184
}
185185
// sync control

models/aggregated_block_feed/query.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (mdl *AQFWrapper) QueryData(blockNum int64) []*schemas.PriceFeed {
113113
for i := 0; i < entry.nocalls; i++ {
114114
results = append(results, iterator.Next())
115115
}
116-
pf := processRoundDataWithAdapterTokens(blockNum, entry.adapter, results)
116+
pf := processRoundDataWithAdapterTokens(blockNum, entry.adapter, results, getForceForAdapter(mdl.Repo, entry.adapter))
117117
queryFeedPrices = append(queryFeedPrices, pf...)
118118
}
119119
//
@@ -145,15 +145,27 @@ func (mdl *AQFWrapper) getRoundDataCalls(blockNum int64) (calls []multicall.Mult
145145
return
146146
}
147147

148-
func processRoundDataWithAdapterTokens(blockNum int64, adapter ds.QueryPriceFeedI, entries []multicall.Multicall2Result) []*schemas.PriceFeed {
148+
func processRoundDataWithAdapterTokens(blockNum int64, adapter ds.QueryPriceFeedI, entries []multicall.Multicall2Result, force bool) []*schemas.PriceFeed {
149149

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

152-
priceData := adapter.ProcessResult(blockNum, entries, "")
152+
priceData := adapter.ProcessResult(blockNum, entries, "", force)
153153
if priceData == nil {
154154
return nil
155155
}
156156
priceData.Feed = adapter.GetAddress()
157157
priceData.BlockNumber = blockNum
158158
return []*schemas.PriceFeed{priceData}
159159
}
160+
161+
func getForceForAdapter(repo ds.RepositoryI, adapter ds.QueryPriceFeedI) bool {
162+
var force bool
163+
if price := repo.GetPrevPriceFeed(adapter.GetAddress()); price != nil {
164+
165+
force = time.Since(time.Unix(int64(repo.SetAndGetBlock(price.BlockNumber).Timestamp), 0)) > time.Hour
166+
if force {
167+
log.Info(adapter.GetAddress(), "last price at ", price.BlockNumber)
168+
}
169+
}
170+
return force
171+
}

models/aggregated_block_feed/redstone_price_feed/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (mdl *RedstonePriceFeed) ProcessResult(blockNum int64, results []multicall.
8484
}
8585
}
8686
}
87-
if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), 0)) > time.Hour*24*30 {
87+
if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), 0)) > time.Hour*24*30 && token != "" {
8888
return composite_redstone_price_feed.GetSpotPriceFeed(blockNum, token, mdl.Address, mdl.Repo, mdl.Client)
8989
}
9090
{

repository/handlers/blocks.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func NewBlocksRepo(db *gorm.DB, client core.ClientI, cfg *config.Config, tokensR
3838
return blocksRepo
3939
}
4040

41+
func (repo *BlocksRepo) GetPrevPriceFeed(feed string) *schemas.PriceFeed {
42+
return repo.prevStore.GetPrevPriceFeed(feed)
43+
}
4144
func (repo *BlocksRepo) LoadBlocks(from, to int64) {
4245
log.Infof("Loaded %d to %d blocks for debt", from, to)
4346
data := []*schemas.Block{}

repository/handlers/prev_price.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ func (repo *PrevPriceStore) isPFAdded(pf *schemas.PriceFeed) bool {
6565
repo.prevPriceFeeds[pf.Feed] = pf
6666
return true
6767
}
68+
func (repo *PrevPriceStore) GetPrevPriceFeed(feed string) *schemas.PriceFeed {
69+
return repo.prevPriceFeeds[feed]
70+
}
6871

6972
func (repo *PrevPriceStore) getCurrentPrice(tokenOracleToFeed map[schemas.PriceOracleT]map[string]*schemas.TokenOracle) (ans []*schemas.TokenCurrentPrice) {
7073
for priceOracle, tokenToFeed := range tokenOracleToFeed {

0 commit comments

Comments
 (0)