Skip to content

Commit

Permalink
fix: token oracle should be stored by priceInUSD
Browse files Browse the repository at this point in the history
  • Loading branch information
5lliot committed Dec 22, 2023
1 parent efe87dc commit 91b229a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
24 changes: 23 additions & 1 deletion ds/repo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ds

import (
"fmt"
"math/big"
"strings"

"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/core/schemas"
Expand All @@ -10,6 +12,26 @@ import (
"github.com/Gearbox-protocol/third-eye/ds/dc_wrapper"
)

type PriceInUSDType bool

func (z *PriceInUSDType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%v", *z)), nil
}

func (z *PriceInUSDType) UnmarshalJSON(b []byte) error {
str := strings.Trim(string(b), "\"")
*z = (str == "true")
return nil

}
func (z PriceInUSDType) MarshalText() (text []byte, err error) {
return z.MarshalJSON()
}

func (s *PriceInUSDType) UnmarshalText(text []byte) error {
return s.UnmarshalJSON(text)
}

type EngineI interface {
SyncHandler()
Sync(syncTill int64)
Expand All @@ -36,7 +58,7 @@ type RepositoryI interface {
// for getting executeparser
GetExecuteParser() ExecuteParserI
// price feed/oracle funcs
GetTokenOracles() map[core.VersionType]map[string]*schemas.TokenOracle
GetTokenOracles() map[PriceInUSDType]map[string]*schemas.TokenOracle
DirectlyAddTokenOracle(tokenOracle *schemas.TokenOracle)
AddNewPriceOracleEvent(tokenOracle *schemas.TokenOracle, bounded bool)
//
Expand Down
2 changes: 1 addition & 1 deletion ds/repo_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (DummyRepo) SetAndGetBlock(blockNum int64) *schemas.Block {
func (DummyRepo) GetBlocks() map[int64]*schemas.Block {
return nil
}
func (DummyRepo) GetTokenOracles() map[core.VersionType]map[string]*schemas.TokenOracle {
func (DummyRepo) GetTokenOracles() map[PriceInUSDType]map[string]*schemas.TokenOracle {
return nil
}
func (DummyRepo) GetDisabledTokens() []*schemas.AllowedToken {
Expand Down
2 changes: 1 addition & 1 deletion jsonnet/oracle/token_oracle.jsonnet
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
'1': {
'false': {
'#Token_1': {
blockNum: 5,
feed: '#ChainlinkPriceFeed_3',
Expand Down
7 changes: 6 additions & 1 deletion models/account_manager/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

// for getting directtoken transfer
// AddAccount is called from account factory
// AddAccountTokenTransfer sets the data in the ds.DirectTransferManager
type AccountManager struct {
*ds.SyncAdapter
node *pkg.Node
Expand Down Expand Up @@ -89,7 +92,9 @@ func (mdl *AccountManager) Query(queryTill int64) {
}
logs, err := mdl.node.GetLogsForTransfer(queryFrom, queryTill, hexAddrs, mdl.AccountHashes)
log.Infof("len of logs: %d", len(logs))
log.CheckFatal(err)
if err != nil {
log.Fatal(err, "range ", queryFrom, queryTill, "tokenAddrs", tokenAddrs, "accountHashes", mdl.AccountHashes)
}
for _, log := range logs {
mdl.OnLog(log)
}
Expand Down
31 changes: 17 additions & 14 deletions repository/handlers/token_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

type TokenOracleRepo struct {
// version to token to oracle
tokensCurrentOracle map[core.VersionType]map[string]*schemas.TokenOracle // done
tokensCurrentOracle map[ds.PriceInUSDType]map[string]*schemas.TokenOracle // done
mu *sync.Mutex
adapters *SyncAdaptersRepo
blocks *BlocksRepo
Expand All @@ -27,7 +27,7 @@ type TokenOracleRepo struct {

func NewTokenOracleRepo(adapters *SyncAdaptersRepo, blocks *BlocksRepo, repo ds.RepositoryI, client core.ClientI) *TokenOracleRepo {
return &TokenOracleRepo{
tokensCurrentOracle: make(map[core.VersionType]map[string]*schemas.TokenOracle),
tokensCurrentOracle: make(map[ds.PriceInUSDType]map[string]*schemas.TokenOracle),
mu: &sync.Mutex{},
adapters: adapters,
blocks: blocks,
Expand All @@ -42,8 +42,8 @@ func (repo *TokenOracleRepo) LoadCurrentTokenOracle(db *gorm.DB) {
defer utils.Elapsed("loadCurrentTokenOracle")()
data := []*schemas.TokenOracle{}
query := `SELECT token_oracle.* FROM token_oracle
JOIN (SELECT max(block_num) AS bn, token FROM token_oracle GROUP BY token) AS max_to
ON max_to.bn = token_oracle.block_num AND max_to.token = token_oracle.token`
JOIN (SELECT max(block_num) AS bn, token FROM token_oracle GROUP BY token, version) AS max_to
ON max_to.bn = token_oracle.block_num AND max_to.token = token_oracle.token order by block_num`
err := db.Raw(query).Find(&data).Error
if err != nil {
log.Fatal(err)
Expand All @@ -66,18 +66,20 @@ func (repo *TokenOracleRepo) loadZeroPFs(db *gorm.DB) {
}

func (repo *TokenOracleRepo) addTokenCurrentOracle(oracle *schemas.TokenOracle) {
if repo.tokensCurrentOracle[oracle.Version] == nil {
repo.tokensCurrentOracle[oracle.Version] = map[string]*schemas.TokenOracle{}
priceInUSD := ds.PriceInUSDType(oracle.Version.IsPriceInUSD())
if repo.tokensCurrentOracle[priceInUSD] == nil {
repo.tokensCurrentOracle[priceInUSD] = map[string]*schemas.TokenOracle{}
}
repo.tokensCurrentOracle[oracle.Version][oracle.Token] = oracle
repo.tokensCurrentOracle[priceInUSD][oracle.Token] = oracle
}

// if same feed is active for current token and version
func (repo *TokenOracleRepo) alreadyActiveFeedForToken(newTokenOracle *schemas.TokenOracle) bool {
feedType := newTokenOracle.FeedType
if repo.tokensCurrentOracle[newTokenOracle.Version] != nil &&
repo.tokensCurrentOracle[newTokenOracle.Version][newTokenOracle.Token] != nil {
oldTokenOracle := repo.tokensCurrentOracle[newTokenOracle.Version][newTokenOracle.Token]
newPriceInUSD := ds.PriceInUSDType(newTokenOracle.Version.IsPriceInUSD())
if repo.tokensCurrentOracle[newPriceInUSD] != nil &&
repo.tokensCurrentOracle[newPriceInUSD][newTokenOracle.Token] != nil {
oldTokenOracle := repo.tokensCurrentOracle[newPriceInUSD][newTokenOracle.Token]

if oldTokenOracle.Feed == newTokenOracle.Feed {
log.Debugf("Same %s(%s) added for token(%s)", feedType, newTokenOracle.Feed, newTokenOracle.Token)
Expand All @@ -88,9 +90,10 @@ func (repo *TokenOracleRepo) alreadyActiveFeedForToken(newTokenOracle *schemas.T
}

func (repo *TokenOracleRepo) disablePrevAdapterAndAddNewTokenOracle(newTokenOracle *schemas.TokenOracle) {
if repo.tokensCurrentOracle[newTokenOracle.Version] != nil &&
repo.tokensCurrentOracle[newTokenOracle.Version][newTokenOracle.Token] != nil {
oldTokenOracle := repo.tokensCurrentOracle[newTokenOracle.Version][newTokenOracle.Token]
newPriceInUSD := ds.PriceInUSDType(newTokenOracle.Version.IsPriceInUSD())
if repo.tokensCurrentOracle[newPriceInUSD] != nil &&
repo.tokensCurrentOracle[newPriceInUSD][newTokenOracle.Token] != nil {
oldTokenOracle := repo.tokensCurrentOracle[newPriceInUSD][newTokenOracle.Token]
oldFeed := oldTokenOracle.Feed

adapter := repo.adapters.GetAdapter(oldFeed)
Expand Down Expand Up @@ -207,7 +210,7 @@ func (repo *TokenOracleRepo) AddNewPriceOracleEvent(newTokenOracle *schemas.Toke
}
}

func (repo *TokenOracleRepo) GetTokenOracles() map[core.VersionType]map[string]*schemas.TokenOracle {
func (repo *TokenOracleRepo) GetTokenOracles() map[ds.PriceInUSDType]map[string]*schemas.TokenOracle {
return repo.tokensCurrentOracle
}

Expand Down

0 comments on commit 91b229a

Please sign in to comment.