forked from Layr-Labs/eigensdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.go
94 lines (84 loc) · 3.45 KB
/
subscriber.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package avsregistry
import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
blsapkreg "github.com/Layr-Labs/eigensdk-go/contracts/bindings/BLSApkRegistry"
regcoord "github.com/Layr-Labs/eigensdk-go/contracts/bindings/RegistryCoordinator"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/utils"
)
type ChainSubscriber struct {
logger logging.Logger
regCoord regcoord.ContractRegistryCoordinatorFilters
blsApkRegistry blsapkreg.ContractBLSApkRegistryFilters
}
// NewChainSubscriber creates a new instance of ChainSubscriber
// The bindings must be created using websocket ETH Client
func NewChainSubscriber(
regCoord regcoord.ContractRegistryCoordinatorFilters,
blsApkRegistry blsapkreg.ContractBLSApkRegistryFilters,
logger logging.Logger,
) *ChainSubscriber {
logger = logger.With(logging.ComponentKey, "avsregistry/ChainSubscriber")
return &ChainSubscriber{
regCoord: regCoord,
blsApkRegistry: blsApkRegistry,
logger: logger,
}
}
// BuildAvsRegistryChainSubscriber creates a new instance of ChainSubscriber
// Deprecated: Use NewSubscriberFromConfig instead
func BuildAvsRegistryChainSubscriber(
regCoordAddr common.Address,
ethWsClient eth.WsBackend,
logger logging.Logger,
) (*ChainSubscriber, error) {
regCoord, err := regcoord.NewContractRegistryCoordinator(regCoordAddr, ethWsClient)
if err != nil {
return nil, utils.WrapError("Failed to create RegistryCoordinator contract", err)
}
blsApkRegAddr, err := regCoord.BlsApkRegistry(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to get BLSApkRegistry address from RegistryCoordinator", err)
}
blsApkReg, err := blsapkreg.NewContractBLSApkRegistry(blsApkRegAddr, ethWsClient)
if err != nil {
return nil, utils.WrapError("Failed to create BLSApkRegistry contract", err)
}
return NewChainSubscriber(regCoord, blsApkReg, logger), nil
}
// NewSubscriberFromConfig creates a new instance of ChainSubscriber
// A websocket ETH Client must be provided
func NewSubscriberFromConfig(
cfg Config,
wsClient eth.WsBackend,
logger logging.Logger,
) (*ChainSubscriber, error) {
bindings, err := NewBindingsFromConfig(cfg, wsClient, logger)
if err != nil {
return nil, err
}
return NewChainSubscriber(bindings.RegistryCoordinator, bindings.BlsApkRegistry, logger), nil
}
func (s *ChainSubscriber) SubscribeToNewPubkeyRegistrations() (chan *blsapkreg.ContractBLSApkRegistryNewPubkeyRegistration, event.Subscription, error) {
newPubkeyRegistrationChan := make(chan *blsapkreg.ContractBLSApkRegistryNewPubkeyRegistration)
sub, err := s.blsApkRegistry.WatchNewPubkeyRegistration(
&bind.WatchOpts{}, newPubkeyRegistrationChan, nil,
)
if err != nil {
return nil, nil, utils.WrapError("Failed to subscribe to NewPubkeyRegistration events", err)
}
return newPubkeyRegistrationChan, sub, nil
}
func (s *ChainSubscriber) SubscribeToOperatorSocketUpdates() (chan *regcoord.ContractRegistryCoordinatorOperatorSocketUpdate, event.Subscription, error) {
operatorSocketUpdateChan := make(chan *regcoord.ContractRegistryCoordinatorOperatorSocketUpdate)
sub, err := s.regCoord.WatchOperatorSocketUpdate(
&bind.WatchOpts{}, operatorSocketUpdateChan, nil,
)
if err != nil {
return nil, nil, utils.WrapError("Failed to subscribe to OperatorSocketUpdate events", err)
}
return operatorSocketUpdateChan, sub, nil
}