-
Notifications
You must be signed in to change notification settings - Fork 53
/
bindings.go
161 lines (145 loc) · 6.56 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
// Package elcontracts bindings.go contains functions that create contract bindings for the Eigenlayer Core contracts
// These functions are meant to be used by constructors of the chainio package.
package elcontracts
import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
delegationmanager "github.com/Layr-Labs/eigensdk-go/contracts/bindings/DelegationManager"
avsdirectory "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IAVSDirectory"
rewardscoordinator "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IRewardsCoordinator"
slasher "github.com/Layr-Labs/eigensdk-go/contracts/bindings/ISlasher"
strategymanager "github.com/Layr-Labs/eigensdk-go/contracts/bindings/StrategyManager"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/utils"
)
// ContractBindings contains the contract bindings for the EigenLayer Core contracts
//
// Unclear 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 {
SlasherAddr gethcommon.Address
StrategyManagerAddr gethcommon.Address
DelegationManagerAddr gethcommon.Address
AvsDirectoryAddr gethcommon.Address
RewardsCoordinatorAddress gethcommon.Address
Slasher *slasher.ContractISlasher
DelegationManager *delegationmanager.ContractDelegationManager
StrategyManager *strategymanager.ContractStrategyManager
AvsDirectory *avsdirectory.ContractIAVSDirectory
RewardsCoordinator *rewardscoordinator.ContractIRewardsCoordinator
}
func NewBindingsFromConfig(
cfg Config,
client eth.HttpBackend,
logger logging.Logger,
) (*ContractBindings, error) {
var (
err error
contractDelegationManager *delegationmanager.ContractDelegationManager
contractSlasher *slasher.ContractISlasher
contractStrategyManager *strategymanager.ContractStrategyManager
slasherAddr gethcommon.Address
strategyManagerAddr gethcommon.Address
avsDirectory *avsdirectory.ContractIAVSDirectory
rewardsCoordinator *rewardscoordinator.ContractIRewardsCoordinator
)
if isZeroAddress(cfg.DelegationManagerAddress) {
logger.Debug("DelegationManager address not provided, the calls to the contract will not work")
} else {
contractDelegationManager, err = delegationmanager.NewContractDelegationManager(cfg.DelegationManagerAddress, client)
if err != nil {
return nil, utils.WrapError("Failed to create DelegationManager contract", err)
}
slasherAddr, err = contractDelegationManager.Slasher(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch Slasher address", err)
}
contractSlasher, err = slasher.NewContractISlasher(slasherAddr, client)
if err != nil {
return nil, utils.WrapError("Failed to fetch Slasher contract", err)
}
strategyManagerAddr, err = contractDelegationManager.StrategyManager(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch StrategyManager address", err)
}
contractStrategyManager, err = strategymanager.NewContractStrategyManager(strategyManagerAddr, client)
if err != nil {
return nil, utils.WrapError("Failed to fetch StrategyManager contract", err)
}
}
if isZeroAddress(cfg.AvsDirectoryAddress) {
logger.Debug("AVSDirectory address not provided, the calls to the contract will not work")
} else {
avsDirectory, err = avsdirectory.NewContractIAVSDirectory(cfg.AvsDirectoryAddress, client)
if err != nil {
return nil, utils.WrapError("Failed to fetch AVSDirectory contract", err)
}
}
if isZeroAddress(cfg.RewardsCoordinatorAddress) {
logger.Debug("RewardsCoordinator address not provided, the calls to the contract will not work")
} else {
rewardsCoordinator, err = rewardscoordinator.NewContractIRewardsCoordinator(cfg.RewardsCoordinatorAddress, client)
if err != nil {
return nil, utils.WrapError("Failed to fetch RewardsCoordinator contract", err)
}
}
return &ContractBindings{
SlasherAddr: slasherAddr,
StrategyManagerAddr: strategyManagerAddr,
DelegationManagerAddr: cfg.DelegationManagerAddress,
AvsDirectoryAddr: cfg.AvsDirectoryAddress,
RewardsCoordinatorAddress: cfg.RewardsCoordinatorAddress,
Slasher: contractSlasher,
StrategyManager: contractStrategyManager,
DelegationManager: contractDelegationManager,
AvsDirectory: avsDirectory,
RewardsCoordinator: rewardsCoordinator,
}, nil
}
func isZeroAddress(address gethcommon.Address) bool {
return address == gethcommon.Address{}
}
// NewEigenlayerContractBindings creates a new ContractBindings struct with the provided contract addresses
// Deprecated: Use NewBindingsFromConfig instead
func NewEigenlayerContractBindings(
delegationManagerAddr gethcommon.Address,
avsDirectoryAddr gethcommon.Address,
ethclient eth.HttpBackend,
logger logging.Logger,
) (*ContractBindings, error) {
contractDelegationManager, err := delegationmanager.NewContractDelegationManager(delegationManagerAddr, ethclient)
if err != nil {
return nil, utils.WrapError("Failed to create DelegationManager contract", err)
}
slasherAddr, err := contractDelegationManager.Slasher(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch Slasher address", err)
}
contractSlasher, err := slasher.NewContractISlasher(slasherAddr, ethclient)
if err != nil {
return nil, utils.WrapError("Failed to fetch Slasher contract", err)
}
strategyManagerAddr, err := contractDelegationManager.StrategyManager(&bind.CallOpts{})
if err != nil {
return nil, utils.WrapError("Failed to fetch StrategyManager address", err)
}
contractStrategyManager, err := strategymanager.NewContractStrategyManager(strategyManagerAddr, ethclient)
if err != nil {
return nil, utils.WrapError("Failed to fetch StrategyManager contract", err)
}
avsDirectory, err := avsdirectory.NewContractIAVSDirectory(avsDirectoryAddr, ethclient)
if err != nil {
return nil, utils.WrapError("Failed to fetch AVSDirectory contract", err)
}
return &ContractBindings{
SlasherAddr: slasherAddr,
StrategyManagerAddr: strategyManagerAddr,
DelegationManagerAddr: delegationManagerAddr,
AvsDirectoryAddr: avsDirectoryAddr,
Slasher: contractSlasher,
StrategyManager: contractStrategyManager,
DelegationManager: contractDelegationManager,
AvsDirectory: avsDirectory,
}, nil
}