88 "github.com/Gearbox-protocol/sdk-go/core"
99 "github.com/Gearbox-protocol/sdk-go/core/schemas"
1010 "github.com/Gearbox-protocol/sdk-go/log"
11+ "github.com/Gearbox-protocol/sdk-go/utils"
1112 "github.com/Gearbox-protocol/third-eye/ds"
1213 cpf "github.com/Gearbox-protocol/third-eye/models/chainlink_price_feed"
1314 "github.com/ethereum/go-ethereum/common"
@@ -28,16 +29,6 @@ type CompositeChainlinkPF struct {
2829// address is set as identifier(random), as same oracle can be added for different tokens.
2930func NewCompositeChainlinkPF (token , oracle string , discoveredAt int64 , client core.ClientI , repo ds.RepositoryI , version core.VersionType , priceOracle schemas.PriceOracleT ) * CompositeChainlinkPF {
3031 oracleAddr := common .HexToAddress (oracle )
31- tokenETHPF := getAddrFromRPC (client , "targetETH" , oracleAddr , discoveredAt )
32- // get decimals
33- decimalsToBasePF := getDecimals (client , tokenETHPF , discoveredAt )
34- mainAgg := cpf .NewMainAgg (client , tokenETHPF )
35-
36- ethUSDPF := getAddrFromRPC (client , "ETHUSD" , oracleAddr , discoveredAt )
37- baseTokenMainAgg := cpf .NewMainAgg (client , ethUSDPF )
38- //
39- mainPhaseAgg := mainAgg .GetPriceFeedAddr (discoveredAt )
40- basePhaseAgg := baseTokenMainAgg .GetPriceFeedAddr (discoveredAt )
4132 //
4233 adapter := & ds.SyncAdapter {
4334 SyncAdapterSchema : & schemas.SyncAdapterSchema {
@@ -49,16 +40,9 @@ func NewCompositeChainlinkPF(token, oracle string, discoveredAt int64, client co
4940 Client : client ,
5041 },
5142 Details : map [string ]interface {}{
52- "oracle" : oracle ,
53- "token" : token ,
54- "decimals" : decimalsToBasePF ,
55- "secAddrs" : map [string ]interface {}{
56- "target" : tokenETHPF .Hex (),
57- "base" : ethUSDPF .Hex (),
58- "targetPhase" : mainPhaseAgg .Hex (),
59- "basePhase" : basePhaseAgg .Hex (),
60- }},
61-
43+ "oracle" : oracle ,
44+ "token" : token ,
45+ },
6246 // since last_sync is set to discoveredAt not discoveredAt-1, setPrice will get tokenBase and baseUSD price at discoveredAt
6347 // so the db entry that is added at addPriceToDB will have the correct price while creating new compositeChainlinkPF
6448 LastSync : discoveredAt ,
@@ -70,6 +54,32 @@ func NewCompositeChainlinkPF(token, oracle string, discoveredAt int64, client co
7054 compositeMdl .addPriceToDB (discoveredAt )
7155 return compositeMdl
7256}
57+ func (mdl * CompositeChainlinkPF ) addSecAddrs (blockNum int64 ) {
58+ if mdl .Details ["decimals" ] == nil {
59+ return
60+ }
61+ oracleAddr := common .HexToAddress (mdl .Address )
62+ tokenETHPF := getAddrFromRPC (mdl .Client , "targetETH" , oracleAddr , blockNum )
63+ // get decimals
64+ decimalsToBasePF := getDecimals (mdl .Client , tokenETHPF , blockNum )
65+ mdl .Details ["decimals" ] = decimalsToBasePF
66+ if mdl .Details ["secAddrs" ] != nil {
67+ return
68+ }
69+ mainAgg := cpf .NewMainAgg (mdl .Client , tokenETHPF )
70+
71+ ethUSDPF := getAddrFromRPC (mdl .Client , "ETHUSD" , oracleAddr , blockNum )
72+ baseTokenMainAgg := cpf .NewMainAgg (mdl .Client , ethUSDPF )
73+ //
74+ mainPhaseAgg := mainAgg .GetPriceFeedAddr (blockNum )
75+ basePhaseAgg := baseTokenMainAgg .GetPriceFeedAddr (blockNum )
76+ mdl .Details ["secAddrs" ] = map [string ]interface {}{
77+ "target" : tokenETHPF .Hex (),
78+ "base" : ethUSDPF .Hex (),
79+ "targetPhase" : mainPhaseAgg .Hex (),
80+ "basePhase" : basePhaseAgg .Hex (),
81+ }
82+ }
7383
7484func toDecimals (decimals interface {}) int8 {
7585 switch ans := decimals .(type ) {
@@ -85,9 +95,10 @@ func toDecimals(decimals interface{}) int8 {
8595
8696func NewCompositeChainlinkPFFromAdapter (adapter * ds.SyncAdapter ) * CompositeChainlinkPF {
8797 compositeMdl := & CompositeChainlinkPF {
88- SyncAdapter : adapter ,
89- decimalsOfBasePF : toDecimals (adapter .GetDetails ()["decimals" ]),
98+ SyncAdapter : adapter ,
9099 }
100+ compositeMdl .addSecAddrs (utils .Max (adapter .LastSync , adapter .DiscoveredAt ))
101+ compositeMdl .decimalsOfBasePF = toDecimals (adapter .GetDetails ()["decimals" ])
91102 compositeMdl .BaseTokenMainAgg = cpf .NewMainAgg (adapter .Client , compositeMdl .getAddrFromDetails ("base" ))
92103 compositeMdl .MainAgg = cpf .NewMainAgg (adapter .Client , compositeMdl .getAddrFromDetails ("target" ))
93104 compositeMdl .DataProcessType = ds .ViaMultipleLogs
0 commit comments