forked from Layr-Labs/eigensdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
306 lines (266 loc) · 9.59 KB
/
reader.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package elcontracts
import (
"errors"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
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"
erc20 "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IERC20"
rewardscoordinator "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IRewardsCoordinator"
slasher "github.com/Layr-Labs/eigensdk-go/contracts/bindings/ISlasher"
strategy "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IStrategy"
strategymanager "github.com/Layr-Labs/eigensdk-go/contracts/bindings/StrategyManager"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/types"
"github.com/Layr-Labs/eigensdk-go/utils"
)
type Config struct {
DelegationManagerAddress common.Address
AvsDirectoryAddress common.Address
RewardsCoordinatorAddress common.Address
}
type ChainReader struct {
logger logging.Logger
slasher slasher.ContractISlasherCalls
delegationManager *delegationmanager.ContractDelegationManager
strategyManager *strategymanager.ContractStrategyManager
avsDirectory *avsdirectory.ContractIAVSDirectory
rewardsCoordinator *rewardscoordinator.ContractIRewardsCoordinator
ethClient eth.HttpBackend
}
func NewChainReader(
slasher slasher.ContractISlasherCalls,
delegationManager *delegationmanager.ContractDelegationManager,
strategyManager *strategymanager.ContractStrategyManager,
avsDirectory *avsdirectory.ContractIAVSDirectory,
rewardsCoordinator *rewardscoordinator.ContractIRewardsCoordinator,
logger logging.Logger,
ethClient eth.HttpBackend,
) *ChainReader {
logger = logger.With(logging.ComponentKey, "elcontracts/reader")
return &ChainReader{
slasher: slasher,
delegationManager: delegationManager,
strategyManager: strategyManager,
avsDirectory: avsDirectory,
rewardsCoordinator: rewardsCoordinator,
logger: logger,
ethClient: ethClient,
}
}
// BuildELChainReader creates a new ELChainReader
// Deprecated: Use BuildFromConfig instead
func BuildELChainReader(
delegationManagerAddr gethcommon.Address,
avsDirectoryAddr gethcommon.Address,
ethClient eth.HttpBackend,
logger logging.Logger,
) (*ChainReader, error) {
elContractBindings, err := NewEigenlayerContractBindings(
delegationManagerAddr,
avsDirectoryAddr,
ethClient,
logger,
)
if err != nil {
return nil, err
}
return NewChainReader(
elContractBindings.Slasher,
elContractBindings.DelegationManager,
elContractBindings.StrategyManager,
elContractBindings.AvsDirectory,
elContractBindings.RewardsCoordinator,
logger,
ethClient,
), nil
}
func NewReaderFromConfig(
cfg Config,
ethClient eth.HttpBackend,
logger logging.Logger,
) (*ChainReader, error) {
elContractBindings, err := NewBindingsFromConfig(
cfg,
ethClient,
logger,
)
if err != nil {
return nil, err
}
return NewChainReader(
elContractBindings.Slasher,
elContractBindings.DelegationManager,
elContractBindings.StrategyManager,
elContractBindings.AvsDirectory,
elContractBindings.RewardsCoordinator,
logger,
ethClient,
), nil
}
func (r *ChainReader) IsOperatorRegistered(opts *bind.CallOpts, operator types.Operator) (bool, error) {
if r.delegationManager == nil {
return false, errors.New("DelegationManager contract not provided")
}
isOperator, err := r.delegationManager.IsOperator(
opts,
gethcommon.HexToAddress(operator.Address),
)
if err != nil {
return false, err
}
return isOperator, nil
}
func (r *ChainReader) GetOperatorDetails(opts *bind.CallOpts, operator types.Operator) (types.Operator, error) {
if r.delegationManager == nil {
return types.Operator{}, errors.New("DelegationManager contract not provided")
}
operatorDetails, err := r.delegationManager.OperatorDetails(
opts,
gethcommon.HexToAddress(operator.Address),
)
if err != nil {
return types.Operator{}, err
}
return types.Operator{
Address: operator.Address,
StakerOptOutWindowBlocks: operatorDetails.StakerOptOutWindowBlocks,
DelegationApproverAddress: operatorDetails.DelegationApprover.Hex(),
}, nil
}
// GetStrategyAndUnderlyingToken returns the strategy contract and the underlying token address
func (r *ChainReader) GetStrategyAndUnderlyingToken(
opts *bind.CallOpts, strategyAddr gethcommon.Address,
) (*strategy.ContractIStrategy, gethcommon.Address, error) {
contractStrategy, err := strategy.NewContractIStrategy(strategyAddr, r.ethClient)
if err != nil {
return nil, common.Address{}, utils.WrapError("Failed to fetch strategy contract", err)
}
underlyingTokenAddr, err := contractStrategy.UnderlyingToken(opts)
if err != nil {
return nil, common.Address{}, utils.WrapError("Failed to fetch token contract", err)
}
return contractStrategy, underlyingTokenAddr, nil
}
// GetStrategyAndUnderlyingERC20Token returns the strategy contract, the erc20 bindings for the underlying token
// and the underlying token address
func (r *ChainReader) GetStrategyAndUnderlyingERC20Token(
opts *bind.CallOpts, strategyAddr gethcommon.Address,
) (*strategy.ContractIStrategy, erc20.ContractIERC20Methods, gethcommon.Address, error) {
contractStrategy, err := strategy.NewContractIStrategy(strategyAddr, r.ethClient)
if err != nil {
return nil, nil, common.Address{}, utils.WrapError("Failed to fetch strategy contract", err)
}
underlyingTokenAddr, err := contractStrategy.UnderlyingToken(opts)
if err != nil {
return nil, nil, common.Address{}, utils.WrapError("Failed to fetch token contract", err)
}
contractUnderlyingToken, err := erc20.NewContractIERC20(underlyingTokenAddr, r.ethClient)
if err != nil {
return nil, nil, common.Address{}, utils.WrapError("Failed to fetch token contract", err)
}
return contractStrategy, contractUnderlyingToken, underlyingTokenAddr, nil
}
func (r *ChainReader) ServiceManagerCanSlashOperatorUntilBlock(
opts *bind.CallOpts,
operatorAddr gethcommon.Address,
serviceManagerAddr gethcommon.Address,
) (uint32, error) {
if r.slasher == nil {
return uint32(0), errors.New("slasher contract not provided")
}
return r.slasher.ContractCanSlashOperatorUntilBlock(
opts, operatorAddr, serviceManagerAddr,
)
}
func (r *ChainReader) OperatorIsFrozen(opts *bind.CallOpts, operatorAddr gethcommon.Address) (bool, error) {
if r.slasher == nil {
return false, errors.New("slasher contract not provided")
}
return r.slasher.IsFrozen(opts, operatorAddr)
}
func (r *ChainReader) GetOperatorSharesInStrategy(
opts *bind.CallOpts,
operatorAddr gethcommon.Address,
strategyAddr gethcommon.Address,
) (*big.Int, error) {
if r.delegationManager == nil {
return &big.Int{}, errors.New("DelegationManager contract not provided")
}
return r.delegationManager.OperatorShares(
opts,
operatorAddr,
strategyAddr,
)
}
func (r *ChainReader) CalculateDelegationApprovalDigestHash(
opts *bind.CallOpts, staker gethcommon.Address, operator gethcommon.Address,
delegationApprover gethcommon.Address, approverSalt [32]byte, expiry *big.Int,
) ([32]byte, error) {
if r.delegationManager == nil {
return [32]byte{}, errors.New("DelegationManager contract not provided")
}
return r.delegationManager.CalculateDelegationApprovalDigestHash(
opts, staker, operator, delegationApprover, approverSalt, expiry,
)
}
func (r *ChainReader) CalculateOperatorAVSRegistrationDigestHash(
opts *bind.CallOpts, operator gethcommon.Address, avs gethcommon.Address, salt [32]byte, expiry *big.Int,
) ([32]byte, error) {
if r.avsDirectory == nil {
return [32]byte{}, errors.New("AVSDirectory contract not provided")
}
return r.avsDirectory.CalculateOperatorAVSRegistrationDigestHash(
opts, operator, avs, salt, expiry,
)
}
func (r *ChainReader) GetDistributionRootsLength(opts *bind.CallOpts) (*big.Int, error) {
if r.rewardsCoordinator == nil {
return nil, errors.New("RewardsCoordinator contract not provided")
}
return r.rewardsCoordinator.GetDistributionRootsLength(opts)
}
func (r *ChainReader) CurrRewardsCalculationEndTimestamp(opts *bind.CallOpts) (uint32, error) {
if r.rewardsCoordinator == nil {
return 0, errors.New("RewardsCoordinator contract not provided")
}
return r.rewardsCoordinator.CurrRewardsCalculationEndTimestamp(opts)
}
func (r *ChainReader) GetCurrentClaimableDistributionRoot(
opts *bind.CallOpts,
) (rewardscoordinator.IRewardsCoordinatorDistributionRoot, error) {
if r.rewardsCoordinator == nil {
return rewardscoordinator.IRewardsCoordinatorDistributionRoot{}, errors.New(
"RewardsCoordinator contract not provided",
)
}
return r.rewardsCoordinator.GetCurrentClaimableDistributionRoot(opts)
}
func (r *ChainReader) GetRootIndexFromHash(opts *bind.CallOpts, rootHash [32]byte) (uint32, error) {
if r.rewardsCoordinator == nil {
return 0, errors.New("RewardsCoordinator contract not provided")
}
return r.rewardsCoordinator.GetRootIndexFromHash(opts, rootHash)
}
func (r *ChainReader) GetCumulativeClaimed(
opts *bind.CallOpts,
earner gethcommon.Address,
token gethcommon.Address,
) (*big.Int, error) {
if r.rewardsCoordinator == nil {
return nil, errors.New("RewardsCoordinator contract not provided")
}
return r.rewardsCoordinator.CumulativeClaimed(opts, earner, token)
}
func (r *ChainReader) CheckClaim(
opts *bind.CallOpts,
claim rewardscoordinator.IRewardsCoordinatorRewardsMerkleClaim,
) (bool, error) {
if r.rewardsCoordinator == nil {
return false, errors.New("RewardsCoordinator contract not provided")
}
return r.rewardsCoordinator.CheckClaim(opts, claim)
}