Skip to content

Commit

Permalink
fix: get data from on chain
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Sep 2, 2024
1 parent 53404de commit 261643a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
8 changes: 4 additions & 4 deletions jsonnet/aqf_multiple_adapters/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
blockNum: 26,
feed: '#CompositeRedstone_1',
mergedPFVersion: 4,
price: 3140.31087,
priceBI: '314031087000',
price: 6e-8,
priceBI: '6',
roundId: 0,
token: '0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee',
},
Expand All @@ -22,8 +22,8 @@
blockNum: 26,
feed: '#Redstone_1',
mergedPFVersion: 4,
price: 59496.29243100,
priceBI: '5949629243100',
price: 20,
priceBI: '2000000000',
roundId: 0,
token: '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
},
Expand Down
3 changes: 2 additions & 1 deletion jsonnet/aqf_multiple_adapters/input.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local newCollateral = 1;
'385aee1b': { '#CompositeRedstone_1': '#PriceFeed_0' },
ab0ca0e1: { '#CompositeRedstone_1': '#PriceFeed_1' },
'313ce567': { '#PriceFeed_0': '8' },
feaf968c: { '#CompositeRedstone_1': '6' },
},
},
blocks: {
Expand All @@ -23,7 +24,7 @@ local newCollateral = 1;
'#YearnFeed_1': utils.bigIntTopic(1, 8),
'#CurvePriceFeed_1': utils.bigIntTopic(10000, 8),
'#SingleAsset_1': utils.bigIntTopic(1, 8),
'#Redstone_1': utils.bigIntTopic(0, 8),
'#Redstone_1': utils.bigIntTopic(20, 8),
'#PriceFeed_1': utils.bigIntTopic(3000, 8),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -47,15 +48,30 @@ func (mdl *CompositeRedStonePriceFeed) GetCalls(blockNum int64) (calls []multica
data, err := priceFeedABI.Pack("latestRoundData")
log.CheckFatal(err)
return []multicall.Multicall2Call{{
Target: common.HexToAddress(mdl.Address),
CallData: data,
},{
Target: mdl.priceFeed1,
CallData: data,
}}, true
}

func (mdl *CompositeRedStonePriceFeed) ProcessResult(blockNum int64, results []multicall.Multicall2Result) *schemas.PriceFeed {
if !results[0].Success {
if !results[1].Success {
return nil
}
isPriceInUSD := mdl.GetVersion().IsPriceInUSD() // should be always true
{
if results[0].Success {
value, err := core.GetAbi("YearnPriceFeed").Unpack("latestRoundData", results[0].ReturnData)
log.CheckFatal(err)
price := *abi.ConvertType(value[1], new(*big.Int)).(**big.Int)
return parsePriceForRedStone(price, isPriceInUSD)
} else if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp),0)) > time.Hour {
log.Info("onchain price missing for ", mdl.Address, "at", blockNum)
return nil
}
}
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)
Expand All @@ -78,7 +94,6 @@ func (mdl *CompositeRedStonePriceFeed) ProcessResult(blockNum int64, results []m
}
// calculate price
price := utils.GetInt64(new(big.Int).Mul(targetPrice, basePrice), mdl.Decimals)
isPriceInUSD := mdl.GetVersion().IsPriceInUSD() // should be always true
return parsePriceForRedStone(price, isPriceInUSD)
}

Expand Down
52 changes: 39 additions & 13 deletions models/aggregated_block_feed/redstone_price_feed/model.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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 All @@ -10,6 +12,8 @@ import (
"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"
"github.com/ethereum/go-ethereum/common"
)

type RedstonePriceFeed struct {
Expand All @@ -27,24 +31,46 @@ func NewRedstonePriceFeedFromAdapter(adapter *ds.SyncAdapter) *RedstonePriceFeed
}
}

func (*RedstonePriceFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
return nil, true
func (obj *RedstonePriceFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
data, _:=hex.DecodeString("feaf968c")
return []multicall.Multicall2Call{
{
Target: common.HexToAddress(obj.Address),
CallData: data,
},
}, true
}

func (mdl *RedstonePriceFeed) ProcessResult(blockNum int64, results []multicall.Multicall2Result) *schemas.PriceFeed {
validTokens := mdl.TokensValidAtBlock(blockNum)
priceBI := mdl.Repo.GetRedStonemgr().GetPrice(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), validTokens[0].Token, false)
//
isPriceInUSD := mdl.GetVersion().IsPriceInUSD() // should be always true
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
isPriceInUSD := mdl.GetVersion().IsPriceInUSD()
{
if len(results) != 1 {
log.Fatal("wrong result")
}
if results[0].Success {
value, err := core.GetAbi("YearnPriceFeed").Unpack("latestRoundData", results[0].ReturnData)
log.CheckFatal(err)
price := *abi.ConvertType(value[1], new(*big.Int)).(**big.Int)
return parsePriceForRedStone(price, isPriceInUSD)
} else if time.Since(time.Unix(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp),0)) > time.Hour {
log.Info("onchain price missing for ", mdl.Address, "at", blockNum)
return nil
}
}
{
//
priceBI := mdl.Repo.GetRedStonemgr().GetPrice(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), validTokens[0].Token, false)
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
}

priceData := parsePriceForRedStone(priceBI, isPriceInUSD)
log.Infof("RedStone price for %s at %d is %f", mdl.Repo.GetToken(validTokens[0].Token).Symbol, blockNum, priceData.Price)
//
return priceData
}

priceData := parsePriceForRedStone(priceBI, isPriceInUSD)
log.Infof("RedStone price for %s at %d is %f", mdl.Repo.GetToken(validTokens[0].Token).Symbol, blockNum, priceData.Price)
//
return priceData
}

func parsePriceForRedStone(price *big.Int, isPriceInUSD bool) *schemas.PriceFeed {
Expand Down

0 comments on commit 261643a

Please sign in to comment.