Skip to content

Commit

Permalink
feat: remove ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Nov 27, 2024
1 parent 226b0d0 commit c85a22c
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 67 deletions.
3 changes: 3 additions & 0 deletions ds/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/sdk-go/pkg/redstone"
"github.com/Gearbox-protocol/third-eye/ds/dc_wrapper"
"github.com/ethereum/go-ethereum/common"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -140,6 +141,8 @@ type RepositoryI interface {
AddQuotaDetails(*schemas_v3.QuotaDetails)
GetAccountQuotaMgr() *AccountQuotaMgr
IsBlockRecent(block int64, dur time.Duration) bool
GetFeedToTicker(feed string) common.Address
AddFeedToTicker(feed string, ticker common.Address)
}

func IsTestnet(client core.ClientI) bool {
Expand Down
12 changes: 10 additions & 2 deletions ds/repo_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/sdk-go/pkg/redstone"
"github.com/Gearbox-protocol/third-eye/ds/dc_wrapper"
"github.com/ethereum/go-ethereum/common"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -92,6 +93,13 @@ func (DummyRepo) AddAllowedToken(logID uint, txHash, creditFilter string, atoken
func (DummyRepo) DisableAllowedToken(blockNum int64, logID uint, txHash string, creditManager, creditFilter, token string) {
}

func (DummyRepo) GetFeedToTicker(feed string) common.Address {
return core.NULL_ADDR
}

func (DummyRepo) AddFeedToTicker(feed string, ticker common.Address) {
}

// v2
func (DummyRepo) AddAllowedTokenV2(logID uint, txHash, creditFilter string, atoken *schemas.AllowedToken) {
}
Expand Down Expand Up @@ -262,7 +270,6 @@ func (DieselBalance) TableName() string {
return "diesel_balances"
}


type QueryPriceFeedI interface {
TokensValidAtBlock(blockNum int64) []schemas.TokenAndMergedPFVersion
GetPFType() string
Expand All @@ -272,4 +279,5 @@ type QueryPriceFeedI interface {
DisableToken(token string, disabledAt int64, pfVersion schemas.PFVersion)
AddToken(token string, discoveredAt int64, pfVersion schemas.PFVersion)
GetTokens() map[string]map[schemas.PFVersion][]int64
}
GetRedstonePF() *core.RedStonePF
}
4 changes: 2 additions & 2 deletions 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-20241126153159-4d2505d49944
github.com/Gearbox-protocol/sdk-go v0.0.0-20241126181124-95ecc656f974
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 Expand Up @@ -82,6 +82,6 @@ require (

replace github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.22.1

replace github.com/Gearbox-protocol/sdk-go v0.0.0-20241125124726-93c3aff17c9a => ../sdk-go
replace github.com/Gearbox-protocol/sdk-go v0.0.0-20241126153159-4d2505d49944 => ../sdk-go

replace github.com/ethereum/go-ethereum v1.13.14 => github.com/OffchainLabs/go-ethereum v1.13.4-0.20240313010929-e5d8587e7227
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +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-20241126153159-4d2505d49944 h1:Tp1v4mwQoyshVxQhlpkSpf9dfkSwPwPc/GH67ycEG0I=
github.com/Gearbox-protocol/sdk-go v0.0.0-20241126153159-4d2505d49944/go.mod h1:jRBSOG94bpGc5ci8EWIPUVXZdaGEaekMNmhajbmWFVU=
github.com/Gearbox-protocol/sdk-go v0.0.0-20241126181124-95ecc656f974 h1:XMHa9ymY7wGRmLei93nI5MKp9S7XHY8R4qOB/y0xmH0=
github.com/Gearbox-protocol/sdk-go v0.0.0-20241126181124-95ecc656f974/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: 18 additions & 0 deletions jsonnet/mocks/aqf_multiple_adapters.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
token: { '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599': [1] },
pfType: 'RedStonePF',
mergedPFVersion: 4,
info: {
'#Redstone_1': {
type: 15,
dataServiceId: 'redstone-primary-prod',
dataId: 'BTC',
signersThreshold: 5,
token: '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
},
},
},
lastSync: 1,
version: 2,
Expand All @@ -51,6 +60,15 @@
token: { '0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee': [1] },
pfType: 'CompositeRedStonePF',
mergedPFVersion: 4,
info: {
'#CompositeRedstone_1': {
type: 15,
dataServiceId: 'redstone-primary-prod',
dataId: 'weETH_FUNDAMENTAL',
signersThreshold: 5,
token: '0x8C23b9E4CB9884e807294c4b4C33820333cC613c',
},
},
},
lastSync: 1,
version: 2,
Expand Down
4 changes: 4 additions & 0 deletions migrations/000054_ticker.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create table tickers (
ticker varchar(42) PRIMARY KEY,
feed varchar(42));

2 changes: 1 addition & 1 deletion models/aggregated_block_feed/base_price_feed/token_ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (obj *DetailsDS) Load(in core.Json, version core.VersionType) {
redstoneMap := map[string]*core.RedStonePF{}
if a != nil {
str := utils.ToJson(a)
err := utils.ReadJsonReaderAndSetInterface(bytes.NewBufferString(str), redstoneMap)
err := utils.ReadJsonReaderAndSetInterface(bytes.NewBufferString(str), &redstoneMap)
log.CheckFatal(err)
}
obj.Info = redstoneMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type BasePriceFeed struct {
DetailsDS DetailsDS
}

func (feed BasePriceFeed) GetRedstonePF() *core.RedStonePF {
return nil
}

// single querypricefeed can be valid for multiple tokens so we have to maintain tokens within the details
// details->token is token map to start and end block
func NewBasePriceFeed(token, oracle string, pfType string, discoveredAt int64, client core.ClientI, repo ds.RepositoryI, pfVersion schemas.PFVersion) *BasePriceFeed {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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/sdk-go/pkg/priceFetcher"
"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"
Expand All @@ -22,6 +23,10 @@ type CompositeRedStonePriceFeed struct {
Decimals int8
}

func (feed CompositeRedStonePriceFeed) GetRedstonePF() *core.RedStonePF {
return feed.DetailsDS.Info[feed.GetAddress()]
}

func NewRedstonePriceFeed(token, oracle string, pfType string, discoveredAt int64, client core.ClientI, repo ds.RepositoryI, pfVersion schemas.PFVersion) *CompositeRedStonePriceFeed {
adapter := base_price_feed.NewBasePriceFeed(token, oracle, pfType, discoveredAt, client, repo, pfVersion)
return NewRedstonePriceFeedFromAdapter(adapter.SyncAdapter)
Expand All @@ -35,12 +40,25 @@ func NewRedstonePriceFeedFromAdapter(adapter *ds.SyncAdapter) *CompositeRedStone
//
decimals, err := core.CallFuncWithExtraBytes(adapter.Client, "313ce567", common.BytesToAddress(pf0), 0, nil) // decimals
log.CheckFatal(err)
return &CompositeRedStonePriceFeed{
obj := &CompositeRedStonePriceFeed{
BasePriceFeed: base_price_feed.NewBasePriceFeedFromAdapter(adapter),
priceFeed0: common.BytesToAddress(pf0),
priceFeed1: common.BytesToAddress(pf1),
Decimals: int8(new(big.Int).SetBytes(decimals).Int64()),
}
if obj.DetailsDS.Info[adapter.GetAddress()] == nil {
_, signThreshold, dataId := priceFetcher.RedstoneDetails(obj.priceFeed0, adapter.Client)
//
tokenDetails := &core.RedStonePF{
Type: 15,
DataServiceId: "redstone-primary-prod",
DataId: dataId,
SignersThreshold: signThreshold,
UnderlyingToken: obj.Repo.GetFeedToTicker(obj.priceFeed0.Hex()),
}
obj.DetailsDS.Info[adapter.GetAddress()] = tokenDetails
}
return obj
}

func (mdl *CompositeRedStonePriceFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
Expand Down Expand Up @@ -77,7 +95,7 @@ func (mdl *CompositeRedStonePriceFeed) ProcessResult(blockNum int64, results []m
}
validTokens := mdl.TokensValidAtBlock(blockNum)
// log.Info(mdl.Repo.SetAndGetBlock(blockNum).Timestamp, validTokens, utils.ToJson(mdl.DetailsDS))
targetPrice := mdl.Repo.GetRedStonemgr().GetPrice(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), validTokens[0].Token, true)
targetPrice := mdl.Repo.GetRedStonemgr().GetPrice(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), *mdl.DetailsDS.Info[mdl.GetAddress()])
if targetPrice.Cmp(new(big.Int)) == 0 {
log.Warnf("RedStone composite targetprice for %s at %d is %s", mdl.Repo.GetToken(validTokens[0].Token).Symbol, blockNum, targetPrice)
return nil
Expand Down
22 changes: 20 additions & 2 deletions models/aggregated_block_feed/redstone_price_feed/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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/sdk-go/pkg/priceFetcher"
"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"
Expand All @@ -25,10 +26,27 @@ func NewRedstonePriceFeed(token, oracle string, pfType string, discoveredAt int6
return NewRedstonePriceFeedFromAdapter(adapter.SyncAdapter)
}

func (feed RedstonePriceFeed) GetRedstonePF() *core.RedStonePF {
return feed.DetailsDS.Info[feed.GetAddress()]
}

func NewRedstonePriceFeedFromAdapter(adapter *ds.SyncAdapter) *RedstonePriceFeed {
return &RedstonePriceFeed{
obj := &RedstonePriceFeed{
BasePriceFeed: base_price_feed.NewBasePriceFeedFromAdapter(adapter),
}
if obj.DetailsDS.Info[adapter.GetAddress()] == nil {
feedToken, signThreshold, dataId := priceFetcher.RedstoneDetails(common.HexToAddress(adapter.GetAddress()), adapter.Client)
//
tokenDetails := &core.RedStonePF{
Type: 15,
DataServiceId: "redstone-primary-prod",
DataId: dataId,
SignersThreshold: signThreshold,
UnderlyingToken: feedToken,
}
obj.DetailsDS.Info[adapter.GetAddress()] = tokenDetails
}
return obj
}

func (obj *RedstonePriceFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
Expand Down Expand Up @@ -63,7 +81,7 @@ func (mdl *RedstonePriceFeed) ProcessResult(blockNum int64, results []multicall.
}
{
//
priceBI := mdl.Repo.GetRedStonemgr().GetPrice(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), validTokens[0].Token, false)
priceBI := mdl.Repo.GetRedStonemgr().GetPrice(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), *mdl.DetailsDS.Info[mdl.GetAddress()])
if priceBI.Cmp(new(big.Int)) == 0 {
log.Warnf("RedStone price for %s at %d is %f", mdl.Repo.GetToken(validTokens[0].Token).Symbol, blockNum, priceBI)
return nil
Expand Down
40 changes: 10 additions & 30 deletions models/aggregated_block_feed/single_asset_feed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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/sdk-go/pkg/priceFetcher"
"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/common"
Expand All @@ -28,51 +29,30 @@ func NewSingleAssetFromAdapter(adapter *ds.SyncAdapter) *SingleAssetFeed {
}
}

func (mdl *SingleAssetFeed) GetUnderlyings() (ans []string) {
underlyings := mdl.Details["underlyings"]
if underlyings != nil {
_underlyings, ok := underlyings.([]interface{})
if ok {
for _, entry := range _underlyings {
ans = append(ans, entry.(string))
}
}
func (feed SingleAssetFeed) GetRedstonePF() *core.RedStonePF {
if len(feed.DetailsDS.Underlyings) == 0 {
return nil
}
return
return feed.DetailsDS.Info[feed.DetailsDS.Underlyings[0]]
}

func (mdl *SingleAssetFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
updateABI := core.GetAbi("UpdatePriceFeed")
for _, entry := range mdl.GetUnderlyings() {
for _, entry := range mdl.DetailsDS.Underlyings {
contract, err := redstone.NewRedstone(common.HexToAddress(entry), mdl.Client)
log.CheckFatal(err)
var tokenDetails *core.RedStonePF
if _, ok := mdl.DetailsDS.Info[entry]; ok {
tokenDetails = mdl.DetailsDS.Info[entry]
} else if dataIdBytes, err := contract.DataFeedId(nil); err == nil {
dataId := func() string {
var s []byte
for _, b := range dataIdBytes {
if b != 0 {
s = append(s, b)
} else {
break
}
}
return string(s)

}()
} else if _, err := contract.DataFeedId(nil); err == nil {
feedToken, signThreshold, dataId := priceFetcher.RedstoneDetails(common.HexToAddress(entry), mdl.Client)
//
signThreshold, err := contract.GetUniqueSignersThreshold(nil)
log.CheckFatal(err)
token, err := contract.Token(nil)
log.CheckFatal(err)
tokenDetails = &core.RedStonePF{
Type: 15,
DataServiceId: "redstone-primary-prod",
DataId: dataId,
SignersThreshold: int(signThreshold),
UnderlyingToken: token,
SignersThreshold: signThreshold,
UnderlyingToken: feedToken,
}
mdl.DetailsDS.Info[entry] = tokenDetails
}
Expand Down
27 changes: 6 additions & 21 deletions models/credit_manager/cm_common/retry_redstone_get_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (
"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/pkg/redstone"

"github.com/Gearbox-protocol/third-eye/ds"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)

func (mdl *CommonCMAdapter) priceFeedNeeded(balances core.DBBalanceFormat) (ans []redstone.TokenAndFeedType) {
func (mdl *CommonCMAdapter) priceFeedNeeded(balances core.DBBalanceFormat) (ans []core.RedStonePF) {
feeds := mdl.Repo.GetTokenOracles()[schemas.V3PF_MAIN]
for token := range balances {
var con ds.QueryPriceFeedI
Expand All @@ -28,25 +26,12 @@ func (mdl *CommonCMAdapter) priceFeedNeeded(balances core.DBBalanceFormat) (ans
con = adapter.(ds.QueryPriceFeedI)
}
pfType := con.GetPFType()
t := 0
if pfType == ds.CompositeRedStonePF {
t = core.V3_BACKEND_COMPOSITE_REDSTONE_ORACLE
} else if pfType == ds.RedStonePF {
t = core.V3_REDSTONE_ORACLE
}
{ // ignore LBTC price on mainnet as the pf0 of composite is not updated, so can't provide the pod
client := mdl.Client
chainId := core.GetChainId(client)
addrToSym := core.GetTokenToSymbolByChainId(chainId)
if addrToSym[common.HexToAddress(token)] == "LBTC" && log.GetBaseNet(chainId) == "MAINNET" && t == core.V3_BACKEND_COMPOSITE_REDSTONE_ORACLE { // lbtc as redstone can be updated
continue
if pfType == ds.CompositeRedStonePF || pfType == ds.RedStonePF || pfType == ds.SingleAssetPF {
red := con.GetRedstonePF()
if red != nil {
ans = append(ans, *red)
}
}
ans = append(ans, redstone.TokenAndFeedType{
Token: common.HexToAddress(token),
Reversed: false,
PFType: t,
})
}
return
}
Expand All @@ -56,7 +41,7 @@ func (mdl *CommonCMAdapter) retry(oldaccount dc.CreditAccountCallData, blockNum
log.CheckFatal(err)
ts := mdl.Repo.SetAndGetBlock(blockNum).Timestamp
bal := moreThan1Balance(oldaccount.Balances)
pod := mdl.Repo.GetRedStonemgr().GetPodSign(int64(ts), mdl.priceFeedNeeded(bal), bal)
pod := mdl.Repo.GetRedStonemgr().GetPodSign(int64(ts), mdl.priceFeedNeeded(bal))
newaccountData, err := dcw.GetCreditAccountData(&bind.CallOpts{
BlockNumber: big.NewInt(blockNum),
},
Expand Down
Loading

0 comments on commit c85a22c

Please sign in to comment.