Skip to content

Commit

Permalink
fix: handle price oracle by blocknum instead of version
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Sep 20, 2022
1 parent 343b965 commit e500974
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 27 deletions.
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.17

require (
github.com/Gearbox-protocol/sdk-go v0.0.0-20220907173001-30233ac80208
github.com/Gearbox-protocol/sdk-go v0.0.0-20220920174255-ddac5f5b289c
github.com/ethereum/go-ethereum v1.10.17
github.com/go-playground/validator/v10 v10.4.1
github.com/heroku/x v0.0.42
Expand Down Expand Up @@ -59,4 +59,4 @@ require (
sigs.k8s.io/yaml v1.1.0 // indirect
)

replace github.com/Gearbox-protocol/sdk-go v0.0.0-20220906113432-f7eaf25066e5 => ../sdk-go
// replace github.com/Gearbox-protocol/sdk-go v0.0.0-20220920174255-ddac5f5b289c => ../sdk-go
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/Gearbox-protocol/sdk-go v0.0.0-20220906114458-7c000f456efd h1:A45hAUX
github.com/Gearbox-protocol/sdk-go v0.0.0-20220906114458-7c000f456efd/go.mod h1:2jMzjWk66SUUXF5WG4SzMzfZ961B1nmKLJKeiIUCuXg=
github.com/Gearbox-protocol/sdk-go v0.0.0-20220907173001-30233ac80208 h1:AK+FtQRbMAQFuHW97NqsCfb1uQwNsI2FlKuCHBgiwRU=
github.com/Gearbox-protocol/sdk-go v0.0.0-20220907173001-30233ac80208/go.mod h1:2jMzjWk66SUUXF5WG4SzMzfZ961B1nmKLJKeiIUCuXg=
github.com/Gearbox-protocol/sdk-go v0.0.0-20220920174255-ddac5f5b289c h1:MWrj2k7DbrTsfCJ9+hlnAZZogtbshMeEKqN8uvRyAHU=
github.com/Gearbox-protocol/sdk-go v0.0.0-20220920174255-ddac5f5b289c/go.mod h1:NuHDJsFJw8e6Gwwca8QXpRji7xv60ovvf2Ro7YP1s+c=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down
60 changes: 60 additions & 0 deletions models/address_provider/model.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package address_provider

import (
"fmt"
"sort"
"strconv"

"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/third-eye/ds"
)

type blockAndOracle struct {
priceOracle string
blockNum int64
}
type AddressProvider struct {
*ds.SyncAdapter
priceOracles []blockAndOracle `json:"-"`
}

func NewAddressProvider(addr string, client core.ClientI, repo ds.RepositoryI) *AddressProvider {
Expand All @@ -21,3 +31,53 @@ func NewAddressProviderFromAdapter(adapter *ds.SyncAdapter) *AddressProvider {
}
return obj
}

func (mdl *AddressProvider) GetDetailsByKey(strBlockNum string) string {
blockNum, err := strconv.ParseInt(strBlockNum, 10, 64)
if err != nil { // if input is not number make call on the embedded struct
return mdl.SyncAdapter.GetDetailsByKey(strBlockNum)
}
priceOracles := mdl.getPriceOracleMap()
if mdl.priceOracles == nil {
for strBlockNum, oracle := range priceOracles {
oracleBlockNum, err := strconv.ParseInt(strBlockNum, 10, 64)
log.CheckFatal(err)
mdl.priceOracles = append(mdl.priceOracles, blockAndOracle{
blockNum: oracleBlockNum,
priceOracle: oracle.(string),
})
}
sort.SliceStable(mdl.priceOracles, func(i, j int) bool {
return mdl.priceOracles[i].blockNum < mdl.priceOracles[j].blockNum
})
}
ind := sort.Search(len(mdl.priceOracles), func(i int) bool {
return mdl.priceOracles[i].blockNum > blockNum
})
return mdl.priceOracles[ind-1].priceOracle
}

func (mdl *AddressProvider) addPriceOracle(blockNum int64, priceOracle string) {
priceOraclesMap := mdl.getPriceOracleMap()
priceOraclesMap[fmt.Sprintf("%d", blockNum)] = priceOracle
mdl.Details["priceOracles"] = priceOraclesMap
//
mdl.priceOracles = append(mdl.priceOracles, blockAndOracle{
blockNum: blockNum,
priceOracle: priceOracle,
})
}

func (mdl *AddressProvider) getPriceOracleMap() map[string]interface{} {
if mdl.Details == nil {
mdl.Details = make(map[string]interface{})
}
// price oracles
priceOracles, ok := mdl.Details["priceOracles"].(map[string]interface{})
if !ok {
if priceOracles == nil {
priceOracles = map[string]interface{}{}
}
}
return priceOracles
}
14 changes: 1 addition & 13 deletions models/address_provider/on_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,8 @@ func (mdl *AddressProvider) OnLog(txLog types.Log) {
mdl.Repo.AddSyncAdapter(cr)
case "PRICE_ORACLE":
//
if mdl.Details == nil {
mdl.Details = make(map[string]interface{})
}
// price oracles
priceOracles, ok := mdl.Details["priceOracles"].(map[string]interface{})
if !ok {
if priceOracles == nil {
priceOracles = map[string]interface{}{}
}
}
//
mdl.addPriceOracle(blockNum, address)
po := price_oracle.NewPriceOracle(address, blockNum, mdl.SyncAdapter.Client, mdl.Repo)
priceOracles[fmt.Sprintf("%d", po.GetVersion())] = address
mdl.Details["priceOracles"] = priceOracles
mdl.Repo.AddSyncAdapter(po)
case "ACCOUNT_FACTORY":
af := account_factory.NewAccountFactory(address, blockNum, mdl.SyncAdapter.Client, mdl.Repo)
Expand Down
16 changes: 5 additions & 11 deletions repository/handlers/sync_adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,12 @@ func (repo *SyncAdaptersRepo) GetActivePriceOracleByBlockNum(blockNum int64) (la
return
}

func (repo *SyncAdaptersRepo) GetPriceOracleByVersion(version int16) (string, error) {
func (repo *SyncAdaptersRepo) GetPriceOracleByDiscoveredAt(blockNum int64) (string, error) {
addrProviderAddr := repo.kit.GetAdapterAddressByName(ds.AddressProvider)
addrProvider := repo.GetAdapter(addrProviderAddr[0])
details := addrProvider.GetDetails()
if details != nil {
priceOracles, ok := details["priceOracles"].(map[string]interface{})
if ok {
value, ok := priceOracles[fmt.Sprintf("%d", version)].(string)
if ok {
return value, nil
}
}
priceOracle := addrProvider.GetDetailsByKey(fmt.Sprintf("%d", blockNum))
if priceOracle == "" {
return "", fmt.Errorf("Not Found")
}
return "", fmt.Errorf("Not Found")
return priceOracle, nil
}
2 changes: 1 addition & 1 deletion repository/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This function is used for getting the collateral value in usd and underlying
func (repo *Repository) GetValueInCurrency(blockNum int64, version int16, token, currency string, amount *big.Int) *big.Int {
oracle, err := repo.GetPriceOracleByVersion(version)
oracle, err := repo.GetPriceOracleByDiscoveredAt(blockNum)
if err != nil {
log.Fatalf("err %s version: %d", err, version)
}
Expand Down

0 comments on commit e500974

Please sign in to comment.