Skip to content

Commit ba2dc4a

Browse files
committed
feat: add curve lp with redstone
1 parent 6e254f4 commit ba2dc4a

File tree

9 files changed

+109
-66
lines changed

9 files changed

+109
-66
lines changed

ds/repo_dummy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,5 +279,5 @@ type QueryPriceFeedI interface {
279279
DisableToken(token string, disabledAt int64, pfVersion schemas.PFVersion)
280280
AddToken(token string, discoveredAt int64, pfVersion schemas.PFVersion)
281281
GetTokens() map[string]map[schemas.PFVersion][]int64
282-
GetRedstonePF() *core.RedStonePF
282+
GetRedstonePF() []*core.RedStonePF
283283
}

models/aggregated_block_feed/base_price_feed/call_and_process.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package base_price_feed
22

33
import (
4+
"encoding/hex"
45
"math/big"
56

67
"github.com/Gearbox-protocol/sdk-go/artifacts/multicall"
8+
"github.com/Gearbox-protocol/sdk-go/artifacts/redstone"
79
"github.com/Gearbox-protocol/sdk-go/core"
810
"github.com/Gearbox-protocol/sdk-go/core/schemas"
911
"github.com/Gearbox-protocol/sdk-go/log"
12+
"github.com/Gearbox-protocol/sdk-go/pkg/priceFetcher"
1013
"github.com/Gearbox-protocol/sdk-go/utils"
1114
"github.com/ethereum/go-ethereum/accounts/abi"
1215
"github.com/ethereum/go-ethereum/common"
@@ -74,3 +77,42 @@ func (mdl *BasePriceFeed) ProcessResult(blockNum int64, results []multicall.Mult
7477
return ParseQueryRoundData(results[0].ReturnData, isPriceInUSD, mdl.GetAddress(), blockNum)
7578
//
7679
}
80+
81+
func (mdl *BasePriceFeed) GetUnderlyingCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
82+
updateABI := core.GetAbi("UpdatePriceFeed")
83+
for _, entry := range mdl.DetailsDS.Underlyings {
84+
contract, err := redstone.NewRedstone(common.HexToAddress(entry), mdl.Client)
85+
log.CheckFatal(err)
86+
var tokenDetails *core.RedStonePF
87+
if _, ok := mdl.DetailsDS.Info[entry]; ok {
88+
tokenDetails = mdl.DetailsDS.Info[entry]
89+
} else if _, err := contract.DataFeedId(nil); err == nil {
90+
feedToken, signThreshold, dataId := priceFetcher.RedstoneDetails(common.HexToAddress(entry), mdl.Client)
91+
//
92+
tokenDetails = &core.RedStonePF{
93+
Type: 15,
94+
DataServiceId: "redstone-primary-prod",
95+
DataId: dataId,
96+
SignersThreshold: signThreshold,
97+
UnderlyingToken: feedToken,
98+
}
99+
mdl.DetailsDS.Info[entry] = tokenDetails
100+
}
101+
if tokenDetails != nil {
102+
pod := mdl.Repo.GetRedStonemgr().GetPodSignWithRedstoneToken(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), *tokenDetails)
103+
update, err := updateABI.Pack("updatePrice", pod.CallData)
104+
log.CheckFatal(err)
105+
calls = append(calls, multicall.Multicall2Call{
106+
Target: common.HexToAddress(entry),
107+
CallData: update,
108+
})
109+
}
110+
}
111+
b, err := hex.DecodeString("feaf968c") // // latestRounData
112+
log.CheckFatal(err)
113+
calls = append(calls, multicall.Multicall2Call{
114+
Target: common.HexToAddress(mdl.Address),
115+
CallData: b,
116+
})
117+
return calls, true
118+
}

models/aggregated_block_feed/base_price_feed/yearn_price_feed.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ type BasePriceFeed struct {
1616
DetailsDS DetailsDS
1717
}
1818

19-
func (feed BasePriceFeed) GetRedstonePF() *core.RedStonePF {
20-
return nil
19+
func (feed BasePriceFeed) GetRedstonePF() (ans []*core.RedStonePF) {
20+
if len(feed.DetailsDS.Underlyings) == 0 {
21+
return nil
22+
}
23+
for _, d := range feed.DetailsDS.Info {
24+
ans = append(ans, d)
25+
}
26+
return ans
2127
}
2228

2329
// single querypricefeed can be valid for multiple tokens so we have to maintain tokens within the details

models/aggregated_block_feed/composite_redstone_price_feed/model.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ type CompositeRedStonePriceFeed struct {
2323
Decimals int8
2424
}
2525

26-
func (feed CompositeRedStonePriceFeed) GetRedstonePF() *core.RedStonePF {
27-
return feed.DetailsDS.Info[feed.GetAddress()]
26+
func (feed CompositeRedStonePriceFeed) GetRedstonePF() []*core.RedStonePF {
27+
return []*core.RedStonePF{
28+
feed.DetailsDS.Info[feed.GetAddress()],
29+
}
2830
}
2931

3032
func NewRedstonePriceFeed(token, oracle string, pfType string, discoveredAt int64, client core.ClientI, repo ds.RepositoryI, pfVersion schemas.PFVersion) *CompositeRedStonePriceFeed {

models/aggregated_block_feed/curve_price_feed/model.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ func NewCurvePriceFeedFromAdapter(adapter *ds.SyncAdapter) *CurvePriceFeed {
2828
}
2929
}
3030

31+
func (feed CurvePriceFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
32+
return feed.GetUnderlyingCalls(blockNum)
33+
}
34+
3135
// same as query price feed
3236
// func (*CurvePriceFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
3337

3438
var curvePFLatestRoundDataTimer = map[string]log.TimerFn{}
3539

3640
func (adapter *CurvePriceFeed) ProcessResult(blockNum int64, results []multicall.Multicall2Result, force ...bool) *schemas.PriceFeed {
37-
if !results[0].Success {
41+
result := results[len(results)-1]
42+
if !result.Success {
3843
if adapter.GetVersion().LessThan(core.NewVersion(300)) { // failed and
3944
// if virtualprice of pool for this oracle is not within lowerBound and upperBound , ignore the price
4045
oracleAddr := common.HexToAddress(adapter.GetAddress())
@@ -74,7 +79,7 @@ func (adapter *CurvePriceFeed) ProcessResult(blockNum int64, results []multicall
7479
}
7580
}
7681
isPriceInUSD := adapter.GetVersion().IsPriceInUSD()
77-
return base_price_feed.ParseQueryRoundData(results[0].ReturnData, isPriceInUSD, adapter.GetAddress(), blockNum)
82+
return base_price_feed.ParseQueryRoundData(result.ReturnData, isPriceInUSD, adapter.GetAddress(), blockNum)
7883
}
7984

8085
func GetCurveVirtualPrice(blockNum int64, oracleAddr common.Address, version core.VersionType, client core.ClientI) *big.Int {

models/aggregated_block_feed/redstone_price_feed/model.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ func NewRedstonePriceFeed(token, oracle string, pfType string, discoveredAt int6
2626
return NewRedstonePriceFeedFromAdapter(adapter.SyncAdapter)
2727
}
2828

29-
func (feed RedstonePriceFeed) GetRedstonePF() *core.RedStonePF {
30-
return feed.DetailsDS.Info[feed.GetAddress()]
29+
func (feed RedstonePriceFeed) GetRedstonePF() []*core.RedStonePF {
30+
return []*core.RedStonePF{
31+
feed.DetailsDS.Info[feed.GetAddress()],
32+
}
3133
}
3234

3335
func NewRedstonePriceFeedFromAdapter(adapter *ds.SyncAdapter) *RedstonePriceFeed {
@@ -50,7 +52,7 @@ func NewRedstonePriceFeedFromAdapter(adapter *ds.SyncAdapter) *RedstonePriceFeed
5052
}
5153

5254
func (obj *RedstonePriceFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
53-
data, _ := hex.DecodeString("feaf968c")
55+
data, _ := hex.DecodeString("feaf968c") // latestRounData
5456
return []multicall.Multicall2Call{
5557
{
5658
Target: common.HexToAddress(obj.Address),

models/aggregated_block_feed/single_asset_feed/main.go

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package single_asset_feed
22

33
import (
4-
"encoding/hex"
54
"fmt"
65
"time"
76

87
"github.com/Gearbox-protocol/sdk-go/artifacts/multicall"
9-
"github.com/Gearbox-protocol/sdk-go/artifacts/redstone"
108
"github.com/Gearbox-protocol/sdk-go/core"
119
"github.com/Gearbox-protocol/sdk-go/core/schemas"
1210
"github.com/Gearbox-protocol/sdk-go/log"
13-
"github.com/Gearbox-protocol/sdk-go/pkg/priceFetcher"
1411
"github.com/Gearbox-protocol/third-eye/ds"
1512
"github.com/Gearbox-protocol/third-eye/models/aggregated_block_feed/base_price_feed"
16-
"github.com/ethereum/go-ethereum/common"
1713
)
1814

1915
type SingleAssetFeed struct {
@@ -31,50 +27,8 @@ func NewSingleAssetFromAdapter(adapter *ds.SyncAdapter) *SingleAssetFeed {
3127
}
3228
}
3329

34-
func (feed SingleAssetFeed) GetRedstonePF() *core.RedStonePF {
35-
if len(feed.DetailsDS.Underlyings) == 0 {
36-
return nil
37-
}
38-
return feed.DetailsDS.Info[feed.DetailsDS.Underlyings[0]]
39-
}
40-
41-
func (mdl *SingleAssetFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
42-
updateABI := core.GetAbi("UpdatePriceFeed")
43-
for _, entry := range mdl.DetailsDS.Underlyings {
44-
contract, err := redstone.NewRedstone(common.HexToAddress(entry), mdl.Client)
45-
log.CheckFatal(err)
46-
var tokenDetails *core.RedStonePF
47-
if _, ok := mdl.DetailsDS.Info[entry]; ok {
48-
tokenDetails = mdl.DetailsDS.Info[entry]
49-
} else if _, err := contract.DataFeedId(nil); err == nil {
50-
feedToken, signThreshold, dataId := priceFetcher.RedstoneDetails(common.HexToAddress(entry), mdl.Client)
51-
//
52-
tokenDetails = &core.RedStonePF{
53-
Type: 15,
54-
DataServiceId: "redstone-primary-prod",
55-
DataId: dataId,
56-
SignersThreshold: signThreshold,
57-
UnderlyingToken: feedToken,
58-
}
59-
mdl.DetailsDS.Info[entry] = tokenDetails
60-
}
61-
if tokenDetails != nil {
62-
pod := mdl.Repo.GetRedStonemgr().GetPodSignWithRedstoneToken(int64(mdl.Repo.SetAndGetBlock(blockNum).Timestamp), *tokenDetails)
63-
update, err := updateABI.Pack("updatePrice", pod.CallData)
64-
log.CheckFatal(err)
65-
calls = append(calls, multicall.Multicall2Call{
66-
Target: common.HexToAddress(entry),
67-
CallData: update,
68-
})
69-
}
70-
}
71-
b, err := hex.DecodeString("feaf968c")
72-
log.CheckFatal(err)
73-
calls = append(calls, multicall.Multicall2Call{
74-
Target: common.HexToAddress(mdl.Address),
75-
CallData: b,
76-
})
77-
return calls, true
30+
func (feed SingleAssetFeed) GetCalls(blockNum int64) (calls []multicall.Multicall2Call, isQueryable bool) {
31+
return feed.GetUnderlyingCalls(blockNum)
7832
}
7933

8034
var counter = log.SendMsgIfCountMoreThan(24*time.Hour, 10)

models/credit_manager/cm_common/retry_redstone_get_account.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func (mdl *CommonCMAdapter) priceFeedNeeded(balances core.DBBalanceFormat) (ans
2525
}
2626
con = adapter.(ds.QueryPriceFeedI)
2727
}
28-
pfType := con.GetPFType()
29-
if pfType == ds.CompositeRedStonePF || pfType == ds.RedStonePF || pfType == ds.SingleAssetPF {
30-
red := con.GetRedstonePF()
31-
if red != nil {
32-
ans = append(ans, *red)
33-
}
28+
// pfType := con.GetPFType()
29+
// if pfType == ds.CompositeRedStonePF || pfType == ds.RedStonePF || pfType == ds.SingleAssetPF || pfType == ds.Cu {
30+
reds := con.GetRedstonePF()
31+
for _, red := range reds {
32+
ans = append(ans, *red)
3433
}
34+
// }
3535
}
3636
return
3737
}

models/price_oracle/on_log.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,41 @@ func (mdl *PriceOracle) V3PriceFeedType(opts *bind.CallOpts, oracle, token strin
243243
return ds.YearnPF, nil, false, nil
244244
case core.V3_CHAINLINK_ORACLE:
245245
return ds.ChainlinkPriceFeed, nil, true, nil
246-
case core.V3_CURVE_USD_ORACLE, core.V3_CURVE_CRYPTO_ORACLE, // usd and crypto
247-
core.V3_CURVE_2LP_ORACLE, core.V3_CURVE_3LP_ORACLE, core.V3_CURVE_4LP_ORACLE: // 2lp,3lp, 4lp
246+
case core.V3_CURVE_USD_ORACLE, core.V3_CURVE_CRYPTO_ORACLE:
248247
return ds.CurvePF, nil, false, nil
248+
// usd and crypto
249+
case core.V3_CURVE_2LP_ORACLE, core.V3_CURVE_3LP_ORACLE, core.V3_CURVE_4LP_ORACLE: // 2lp,3lp, 4lp
250+
nCoinBytes, err := core.CallFuncWithExtraBytes(mdl.Client, "c21ee162", common.HexToAddress(oracle), 0, nil)
251+
log.CheckFatal(err)
252+
fn := func(n int) string {
253+
var sig string
254+
if n == 0 {
255+
sig = "385aee1b"
256+
} else if n == 1 {
257+
sig = "ab0ca0e1"
258+
} else if n == 2 {
259+
sig = "e5693f41"
260+
} else if n == 3 {
261+
sig = "427cb6fe"
262+
}
263+
pfBytes, err := core.CallFuncWithExtraBytes(mdl.Client, sig, common.HexToAddress(oracle), 0, nil)
264+
log.CheckFatal(err)
265+
pf := common.BytesToAddress(pfBytes)
266+
pfTypeBytes, err := core.CallFuncWithExtraBytes(mdl.Client, "3fd0875f", pf, 0, nil) // priceFeedType
267+
log.CheckFatal(err)
268+
if new(big.Int).SetBytes(pfTypeBytes).Int64() == core.V3_REDSTONE_ORACLE {
269+
return pf.Hex()
270+
}
271+
return ""
272+
}
273+
nCoins := int(new(big.Int).SetBytes(nCoinBytes).Int64())
274+
underlyings := []string{}
275+
for i := 0; i < nCoins; i++ {
276+
if pf := fn(i); pf != "" {
277+
underlyings = append(underlyings, pf)
278+
}
279+
}
280+
return ds.CurvePF, underlyings, false, nil
249281
case core.V3_ZERO_ORACLE:
250282
return ds.ZeroPF, nil, false, nil
251283
// SingleAssetLPPriceFeed

0 commit comments

Comments
 (0)