forked from Layr-Labs/eigensdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbindings.go
254 lines (231 loc) · 9.67 KB
/
bindings.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package avsregistry
import (
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
blsapkregistry "github.com/Layr-Labs/eigensdk-go/contracts/bindings/BLSApkRegistry"
indexregistry "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IndexRegistry"
opstateretriever "github.com/Layr-Labs/eigensdk-go/contracts/bindings/OperatorStateRetriever"
regcoordinator "github.com/Layr-Labs/eigensdk-go/contracts/bindings/RegistryCoordinator"
servicemanager "github.com/Layr-Labs/eigensdk-go/contracts/bindings/ServiceManagerBase"
stakeregistry "github.com/Layr-Labs/eigensdk-go/contracts/bindings/StakeRegistry"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"
)
// ContractBindings Unclear to me why geth bindings don't store and expose the contract address...
// so we also store them here in case the different constructors that use this struct need them
type ContractBindings struct {
// contract addresses
ServiceManagerAddr gethcommon.Address
RegistryCoordinatorAddr gethcommon.Address
StakeRegistryAddr gethcommon.Address
BlsApkRegistryAddr gethcommon.Address
OperatorStateRetrieverAddr gethcommon.Address
IndexRegistryAddr gethcommon.Address
DelegationManagerAddr gethcommon.Address
AvsDirectoryAddr gethcommon.Address
AllocationManagerAddr gethcommon.Address
// contract bindings
ServiceManager *servicemanager.ContractServiceManagerBase
RegistryCoordinator *regcoordinator.ContractRegistryCoordinator
StakeRegistry *stakeregistry.ContractStakeRegistry
BlsApkRegistry *blsapkregistry.ContractBLSApkRegistry
IndexRegistry *indexregistry.ContractIndexRegistry
OperatorStateRetriever *opstateretriever.ContractOperatorStateRetriever
}
// NewAVSRegistryContractBindings creates a new instance of ContractBindings
// Deprecated: Use NewBindingsFromConfig instead
func NewAVSRegistryContractBindings(
registryCoordinatorAddr gethcommon.Address,
operatorStateRetrieverAddr gethcommon.Address,
ethclient eth.HttpBackend,
logger logging.Logger,
) (*ContractBindings, error) {
contractBlsRegistryCoordinator, err := regcoordinator.NewContractRegistryCoordinator(
registryCoordinatorAddr,
ethclient,
)
if err != nil {
return nil, utils.WrapError("Failed to create BLSRegistryCoordinator contract", err)
}
serviceManagerAddr, err := contractBlsRegistryCoordinator.ServiceManager(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch ServiceManager address", err)
}
contractServiceManager, err := servicemanager.NewContractServiceManagerBase(
serviceManagerAddr,
ethclient,
)
if err != nil {
return nil, utils.WrapError("Failed to fetch ServiceManager contract", err)
}
stakeregistryAddr, err := contractBlsRegistryCoordinator.StakeRegistry(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch StakeRegistry address", err)
}
contractStakeRegistry, err := stakeregistry.NewContractStakeRegistry(
stakeregistryAddr,
ethclient,
)
if err != nil {
return nil, utils.WrapError("Failed to fetch StakeRegistry contract", err)
}
blsApkRegistryAddr, err := contractBlsRegistryCoordinator.BlsApkRegistry(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch BLSPubkeyRegistry address", err)
}
contractBlsApkRegistry, err := blsapkregistry.NewContractBLSApkRegistry(
blsApkRegistryAddr,
ethclient,
)
if err != nil {
return nil, utils.WrapError("Failed to fetch BLSPubkeyRegistry contract", err)
}
indexRegistryAddr, err := contractBlsRegistryCoordinator.IndexRegistry(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch IndexRegistry address", err)
}
contractIndexRegistry, err := indexregistry.NewContractIndexRegistry(indexRegistryAddr, ethclient)
if err != nil {
return nil, utils.WrapError("Failed to fetch IndexRegistry contract", err)
}
contractOperatorStateRetriever, err := opstateretriever.NewContractOperatorStateRetriever(
operatorStateRetrieverAddr,
ethclient,
)
if err != nil {
return nil, utils.WrapError("Failed to fetch OperatorStateRetriever contract", err)
}
return &ContractBindings{
ServiceManagerAddr: serviceManagerAddr,
RegistryCoordinatorAddr: registryCoordinatorAddr,
StakeRegistryAddr: stakeregistryAddr,
BlsApkRegistryAddr: blsApkRegistryAddr,
IndexRegistryAddr: indexRegistryAddr,
OperatorStateRetrieverAddr: operatorStateRetrieverAddr,
ServiceManager: contractServiceManager,
RegistryCoordinator: contractBlsRegistryCoordinator,
StakeRegistry: contractStakeRegistry,
BlsApkRegistry: contractBlsApkRegistry,
IndexRegistry: contractIndexRegistry,
OperatorStateRetriever: contractOperatorStateRetriever,
}, nil
}
// NewBindingsFromConfig creates a new instance of ContractBindings
func NewBindingsFromConfig(
cfg Config,
client eth.HttpBackend,
logger logging.Logger,
) (*ContractBindings, error) {
var (
err error
serviceManagerAddr gethcommon.Address
stakeRegistryAddr gethcommon.Address
blsApkRegistryAddr gethcommon.Address
indexRegistryAddr gethcommon.Address
delegationManagerAddr gethcommon.Address
avsDirectoryAddr gethcommon.Address
allocationManagerAddr gethcommon.Address
contractBlsRegistryCoordinator *regcoordinator.ContractRegistryCoordinator
contractServiceManager *servicemanager.ContractServiceManagerBase
contractStakeRegistry *stakeregistry.ContractStakeRegistry
contractBlsApkRegistry *blsapkregistry.ContractBLSApkRegistry
contractIndexRegistry *indexregistry.ContractIndexRegistry
contractOperatorStateRetriever *opstateretriever.ContractOperatorStateRetriever
)
if isZeroAddress(cfg.RegistryCoordinatorAddress) {
logger.Debug("RegistryCoordinator address not provided, the calls to the contract will not work")
} else {
contractBlsRegistryCoordinator, err = regcoordinator.NewContractRegistryCoordinator(
cfg.RegistryCoordinatorAddress,
client,
)
if err != nil {
return nil, utils.WrapError("Failed to create BLSRegistryCoordinator contract", err)
}
serviceManagerAddr, err = contractBlsRegistryCoordinator.ServiceManager(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch ServiceManager address", err)
}
contractServiceManager, err = servicemanager.NewContractServiceManagerBase(
serviceManagerAddr,
client,
)
if err != nil {
return nil, utils.WrapError("Failed to create ServiceManager contract", err)
}
stakeRegistryAddr, err = contractBlsRegistryCoordinator.StakeRegistry(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch StakeRegistry address", err)
}
contractStakeRegistry, err = stakeregistry.NewContractStakeRegistry(
stakeRegistryAddr,
client,
)
if err != nil {
return nil, utils.WrapError("Failed to create StakeRegistry contract", err)
}
blsApkRegistryAddr, err = contractBlsRegistryCoordinator.BlsApkRegistry(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch BLSPubkeyRegistry address", err)
}
contractBlsApkRegistry, err = blsapkregistry.NewContractBLSApkRegistry(
blsApkRegistryAddr,
client,
)
if err != nil {
return nil, utils.WrapError("Failed to create BLSPubkeyRegistry contract", err)
}
indexRegistryAddr, err = contractBlsRegistryCoordinator.IndexRegistry(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch IndexRegistry address", err)
}
contractIndexRegistry, err = indexregistry.NewContractIndexRegistry(indexRegistryAddr, client)
if err != nil {
return nil, utils.WrapError("Failed to create IndexRegistry contract", err)
}
delegationManagerAddr, err = contractStakeRegistry.Delegation(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to get DelegationManager address", err)
}
avsDirectoryAddr, err = contractServiceManager.AvsDirectory(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to get AvsDirectory address", err)
}
allocationManagerAddr, err = contractServiceManager.AllocationManager(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to get AllocationManager address", err)
}
}
if isZeroAddress(cfg.OperatorStateRetrieverAddress) {
logger.Debug("OperatorStateRetriever address not provided, the calls to the contract will not work")
} else {
contractOperatorStateRetriever, err = opstateretriever.NewContractOperatorStateRetriever(
cfg.OperatorStateRetrieverAddress,
client,
)
if err != nil {
return nil, utils.WrapError("Failed to fetch OperatorStateRetriever contract", err)
}
}
return &ContractBindings{
ServiceManagerAddr: serviceManagerAddr,
RegistryCoordinatorAddr: cfg.RegistryCoordinatorAddress,
StakeRegistryAddr: stakeRegistryAddr,
BlsApkRegistryAddr: blsApkRegistryAddr,
IndexRegistryAddr: indexRegistryAddr,
OperatorStateRetrieverAddr: cfg.OperatorStateRetrieverAddress,
DelegationManagerAddr: delegationManagerAddr,
AvsDirectoryAddr: avsDirectoryAddr,
AllocationManagerAddr: allocationManagerAddr,
ServiceManager: contractServiceManager,
RegistryCoordinator: contractBlsRegistryCoordinator,
StakeRegistry: contractStakeRegistry,
BlsApkRegistry: contractBlsApkRegistry,
IndexRegistry: contractIndexRegistry,
OperatorStateRetriever: contractOperatorStateRetriever,
}, nil
}
func isZeroAddress(address gethcommon.Address) bool {
return address == gethcommon.Address{}
}