-
Notifications
You must be signed in to change notification settings - Fork 17
/
source_proxy.go
67 lines (56 loc) · 1.5 KB
/
source_proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package monitoring
import (
"context"
"fmt"
"math/big"
"github.com/NethermindEth/juno/core/felt"
starknetutils "github.com/NethermindEth/starknet.go/utils"
relayMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring"
"github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/ocr2"
)
type ProxyData struct {
Answer *big.Int
}
func NewProxySourceFactory(
ocr2Reader ocr2.OCR2Reader,
) relayMonitoring.SourceFactory {
return &proxySourceFactory{
ocr2Reader,
}
}
type proxySourceFactory struct {
ocr2Reader ocr2.OCR2Reader
}
func (s *proxySourceFactory) NewSource(
_ relayMonitoring.ChainConfig,
feedConfig relayMonitoring.FeedConfig,
) (relayMonitoring.Source, error) {
starknetFeedConfig, ok := feedConfig.(StarknetFeedConfig)
if !ok {
return nil, fmt.Errorf("expected feedConfig to be of type StarknetFeedConfig not %T", feedConfig)
}
contractAddress, err := starknetutils.HexToFelt(starknetFeedConfig.ProxyAddress)
if err != nil {
return nil, err
}
return &proxySource{
contractAddress,
s.ocr2Reader,
}, nil
}
func (s *proxySourceFactory) GetType() string {
return "proxy"
}
type proxySource struct {
contractAddress *felt.Felt
ocr2Reader ocr2.OCR2Reader
}
func (s *proxySource) Fetch(ctx context.Context) (interface{}, error) {
latestRoundData, err := s.ocr2Reader.LatestRoundData(ctx, s.contractAddress)
if err != nil {
return nil, fmt.Errorf("couldn't fetch latest_round_data: %w", err)
}
return ProxyData{
Answer: latestRoundData.Answer,
}, nil
}