Skip to content

Commit

Permalink
refactor to more config based approach
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Jun 21, 2024
1 parent a6f77e8 commit 846ab5b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
58 changes: 58 additions & 0 deletions chainio/clients/elcontracts/reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package elcontracts

import (
"errors"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -73,6 +74,7 @@ type ELChainReader struct {
// forces EthReader to implement the chainio.Reader interface
var _ ELReader = (*ELChainReader)(nil)

// TODO(madhur): make this private. All clients should use build functions
func NewELChainReader(
slasher slasher.ContractISlasherCalls,
delegationManager delegationmanager.ContractDelegationManagerCalls,
Expand Down Expand Up @@ -116,7 +118,39 @@ func BuildELChainReader(
), nil
}

type ElChainReaderConfig struct {
delegationManagerAddress gethcommon.Address

Check failure on line 122 in chainio/clients/elcontracts/reader.go

View workflow job for this annotation

GitHub Actions / Lint

field `delegationManagerAddress` is unused (unused)
avsDirectoryAddress gethcommon.Address

Check failure on line 123 in chainio/clients/elcontracts/reader.go

View workflow job for this annotation

GitHub Actions / Lint

field `avsDirectoryAddress` is unused (unused)
}

func BuildELChainReaderFromConfig(
cfg types.ElChainReaderConfig,
ethClient eth.Client,
logger logging.Logger,
) (*ELChainReader, error) {
elContractBindings, err := chainioutils.NewEigenLayerContractBindingsFromConfig(
cfg,
ethClient,
logger,
)
if err != nil {
return nil, err
}
return NewELChainReader(
elContractBindings.Slasher,
elContractBindings.DelegationManager,
elContractBindings.StrategyManager,
elContractBindings.AvsDirectory,
logger,
ethClient,
), nil
}

func (r *ELChainReader) 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),
Expand All @@ -129,6 +163,10 @@ func (r *ELChainReader) IsOperatorRegistered(opts *bind.CallOpts, operator types
}

func (r *ELChainReader) 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),
Expand Down Expand Up @@ -185,6 +223,10 @@ func (r *ELChainReader) ServiceManagerCanSlashOperatorUntilBlock(
operatorAddr gethcommon.Address,
serviceManagerAddr gethcommon.Address,
) (uint32, error) {
if r.slasher == nil {
return uint32(0), errors.New("slasher contract not provided")
}

serviceManagerCanSlashOperatorUntilBlock, err := r.slasher.ContractCanSlashOperatorUntilBlock(
opts, operatorAddr, serviceManagerAddr,
)
Expand All @@ -195,6 +237,10 @@ func (r *ELChainReader) ServiceManagerCanSlashOperatorUntilBlock(
}

func (r *ELChainReader) OperatorIsFrozen(opts *bind.CallOpts, operatorAddr gethcommon.Address) (bool, error) {
if r.slasher == nil {
return false, errors.New("slasher contract not provided")
}

operatorIsFrozen, err := r.slasher.IsFrozen(opts, operatorAddr)
if err != nil {
return false, err
Expand All @@ -207,6 +253,10 @@ func (r *ELChainReader) GetOperatorSharesInStrategy(
operatorAddr gethcommon.Address,
strategyAddr gethcommon.Address,
) (*big.Int, error) {
if r.delegationManager == nil {
return &big.Int{}, errors.New("DelegationManager contract not provided")
}

operatorSharesInStrategy, err := r.delegationManager.OperatorShares(
opts,
operatorAddr,
Expand All @@ -222,6 +272,10 @@ func (r *ELChainReader) 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,
)
Expand All @@ -230,6 +284,10 @@ func (r *ELChainReader) CalculateDelegationApprovalDigestHash(
func (r *ELChainReader) 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,
)
Expand Down
62 changes: 62 additions & 0 deletions chainio/utils/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package utils

import (
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/types"
"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -34,6 +35,67 @@ type EigenlayerContractBindings struct {
AvsDirectory *avsdirectory.ContractAVSDirectory
}

func NewEigenLayerContractBindingsFromConfig(
cfg types.ElChainReaderConfig,
client eth.Client,
logger logging.Logger,
) (*EigenlayerContractBindings, error) {
var contractDelegationManager *delegationmanager.ContractDelegationManager
var contractSlasher *slasher.ContractISlasher
var contractStrategyManager *strategymanager.ContractStrategyManager
var slasherAddr gethcommon.Address
var strategyManagerAddr gethcommon.Address
var avsDirectory *avsdirectory.ContractAVSDirectory
var err error

if cfg.DelegationManagerAddress == gethcommon.HexToAddress("") {
logger.Warn("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 cfg.AvsDirectoryAddress == gethcommon.HexToAddress("") {
logger.Warn("AVSDirectory address not provided, the calls to the contract will not work")
} else {
avsDirectory, err = avsdirectory.NewContractAVSDirectory(cfg.AvsDirectoryAddress, client)
if err != nil {
return nil, utils.WrapError("Failed to fetch AVSDirectory contract", err)
}
}

return &EigenlayerContractBindings{
SlasherAddr: slasherAddr,
StrategyManagerAddr: strategyManagerAddr,
DelegationManagerAddr: cfg.DelegationManagerAddress,
AvsDirectoryAddr: cfg.AvsDirectoryAddress,
Slasher: contractSlasher,
StrategyManager: contractStrategyManager,
DelegationManager: contractDelegationManager,
AvsDirectory: avsDirectory,
}, nil
}

func NewEigenlayerContractBindings(
delegationManagerAddr gethcommon.Address,
avsDirectoryAddr gethcommon.Address,
Expand Down
8 changes: 8 additions & 0 deletions types/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

import "github.com/ethereum/go-ethereum/common"

type ElChainReaderConfig struct {
DelegationManagerAddress common.Address
AvsDirectoryAddress common.Address
}

0 comments on commit 846ab5b

Please sign in to comment.