Skip to content

Commit

Permalink
refactor builder
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Jul 2, 2024
1 parent 626d21c commit 5616c1d
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 219 deletions.
68 changes: 68 additions & 0 deletions chainio/clients/avsregistry/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package avsregistry

import (
"github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
"github.com/Layr-Labs/eigensdk-go/chainio/txmgr"
"github.com/Layr-Labs/eigensdk-go/logging"
)

func BuildClients(
config Config,
client eth.Client,
txMgr txmgr.TxManager,
logger logging.Logger,
) (*ChainReader, *ChainSubscriber, *ChainWriter, *ContractBindings, error) {
avsBindings, err := NewBindingsFromConfig(
config,
client,
logger,
)

if err != nil {
return nil, nil, nil, nil, err
}

chainReader := NewChainReader(
avsBindings.RegistryCoordinatorAddr,
avsBindings.BlsApkRegistryAddr,
avsBindings.RegistryCoordinator,
avsBindings.OperatorStateRetriever,
avsBindings.StakeRegistry,
logger,
client,
)

chainSubscriber := NewChainSubscriber(
avsBindings.RegistryCoordinator,
avsBindings.BlsApkRegistry,
logger,
)

// This is ugly but we need elReader to be able to create the AVS writer
elChainReader, err := elcontracts.NewReaderFromConfig(
elcontracts.Config{
DelegationManagerAddress: avsBindings.DelegationManagerAddr,
AvsDirectoryAddress: avsBindings.AvsDirectoryAddr,
},
client,
logger,
)
if err != nil {
return nil, nil, nil, nil, err
}

chainWriter := NewChainWriter(
avsBindings.ServiceManagerAddr,
avsBindings.RegistryCoordinator,
avsBindings.OperatorStateRetriever,
avsBindings.StakeRegistry,
avsBindings.BlsApkRegistry,
elChainReader,
logger,
client,
txMgr,
)

return chainReader, chainSubscriber, chainWriter, avsBindings, nil
}
12 changes: 6 additions & 6 deletions chainio/clients/avsregistry/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ type ChainSubscriber struct {
var _ Subscriber = (*ChainSubscriber)(nil)

func NewChainSubscriber(
logger logging.Logger,
regCoord regcoord.ContractRegistryCoordinatorFilters,
blsApkRegistry blsapkreg.ContractBLSApkRegistryFilters,
) (*ChainSubscriber, error) {
logger logging.Logger,
) *ChainSubscriber {
logger = logger.With(logging.ComponentKey, "avsregistry/ChainSubscriber")

return &ChainSubscriber{
logger: logger,
regCoord: regCoord,
blsApkRegistry: blsApkRegistry,
}, nil
logger: logger,
}
}

func BuildAvsRegistryChainSubscriber(
Expand All @@ -57,7 +57,7 @@ func BuildAvsRegistryChainSubscriber(
if err != nil {
return nil, utils.WrapError("Failed to create BLSApkRegistry contract", err)
}
return NewChainSubscriber(logger, regCoord, blsApkReg)
return NewChainSubscriber(regCoord, blsApkReg, logger), nil
}

func NewSubscriberFromConfig(
Expand All @@ -70,7 +70,7 @@ func NewSubscriberFromConfig(
return nil, err
}

return NewChainSubscriber(logger, bindings.RegistryCoordinator, bindings.BlsApkRegistry)
return NewChainSubscriber(bindings.RegistryCoordinator, bindings.BlsApkRegistry, logger), nil
}

func (s *ChainSubscriber) SubscribeToNewPubkeyRegistrations() (chan *blsapkreg.ContractBLSApkRegistryNewPubkeyRegistration, event.Subscription, error) {
Expand Down
12 changes: 6 additions & 6 deletions chainio/clients/avsregistry/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type ChainWriter struct {
operatorStateRetriever *opstateretriever.ContractOperatorStateRetriever
stakeRegistry *stakeregistry.ContractStakeRegistry
blsApkRegistry *blsapkregistry.ContractBLSApkRegistry
elReader elcontracts.ELReader
elReader elcontracts.Reader
logger logging.Logger
ethClient eth.Client
txMgr txmgr.TxManager
Expand All @@ -108,11 +108,11 @@ func NewChainWriter(
operatorStateRetriever *opstateretriever.ContractOperatorStateRetriever,
stakeRegistry *stakeregistry.ContractStakeRegistry,
blsApkRegistry *blsapkregistry.ContractBLSApkRegistry,
elReader elcontracts.ELReader,
elReader elcontracts.Reader,
logger logging.Logger,
ethClient eth.Client,
txMgr txmgr.TxManager,
) (*ChainWriter, error) {
) *ChainWriter {
logger = logger.With(logging.ComponentKey, "avsregistry/ChainWriter")

return &ChainWriter{
Expand All @@ -125,7 +125,7 @@ func NewChainWriter(
logger: logger,
ethClient: ethClient,
txMgr: txMgr,
}, nil
}
}

// BuildAvsRegistryChainWriter creates a new ChainWriter instance from the provided contract addresses
Expand Down Expand Up @@ -194,7 +194,7 @@ func BuildAvsRegistryChainWriter(
logger,
ethClient,
txMgr,
)
), nil
}

// NewWriterFromConfig creates a new ChainWriter from the provided config
Expand Down Expand Up @@ -226,7 +226,7 @@ func NewWriterFromConfig(
logger,
client,
txMgr,
)
), nil
}

func (w *ChainWriter) RegisterOperatorInQuorumWithAVSRegistryCoordinator(
Expand Down
178 changes: 19 additions & 159 deletions chainio/clients/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/Layr-Labs/eigensdk-go/metrics"
"github.com/Layr-Labs/eigensdk-go/signerv2"
"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -28,6 +27,7 @@ type BuildAllConfig struct {
PromMetricsIpPortAddress string
}

// Clients is a struct that holds all the clients that are needed to interact with the AVS and EL contracts.
// TODO: this is confusing right now because clients are not instrumented clients, but
// we return metrics and prometheus reg, so user has to build instrumented clients at the call
// site if they need them. We should probably separate into two separate constructors, one
Expand All @@ -37,8 +37,8 @@ type Clients struct {
AvsRegistryChainReader *avsregistry.ChainReader
AvsRegistryChainSubscriber *avsregistry.ChainSubscriber
AvsRegistryChainWriter *avsregistry.ChainWriter
ElChainReader *elcontracts.ELChainReader
ElChainWriter *elcontracts.ELChainWriter
ElChainReader *elcontracts.ChainReader
ElChainWriter *elcontracts.ChainWriter
EthHttpClient eth.Client
EthWsClient eth.Client
Wallet wallet.Wallet
Expand Down Expand Up @@ -87,27 +87,34 @@ func BuildAll(
return nil, utils.WrapError("Failed to create transaction sender", err)
}
txMgr := txmgr.NewSimpleTxManager(pkWallet, ethHttpClient, logger, addr)
// creating EL clients: Reader, Writer and EigenLayer Contract Bindings
elChainReader, elChainWriter, elContractBindings, err := config.BuildELClients(

// creating AVS clients: Reader and Writer
avsRegistryChainReader, avsRegistryChainSubscriber, avsRegistryChainWriter, avsRegistryContractBindings, err := avsregistry.BuildClients(
avsregistry.Config{
RegistryCoordinatorAddress: gethcommon.HexToAddress(config.RegistryCoordinatorAddr),
OperatorStateRetrieverAddress: gethcommon.HexToAddress(config.OperatorStateRetrieverAddr),
},
ethHttpClient,
txMgr,
logger,
eigenMetrics,
)
if err != nil {
return nil, utils.WrapError("Failed to create EL Reader, Writer and Subscriber", err)
return nil, utils.WrapError("Failed to create AVS Registry Reader and Writer", err)
}

// creating AVS clients: Reader and Writer
avsRegistryChainReader, avsRegistryChainSubscriber, avsRegistryChainWriter, avsRegistryContractBindings, err := config.BuildAVSRegistryClients(
elChainReader,
// creating EL clients: Reader, Writer and EigenLayer Contract Bindings
elChainReader, elChainWriter, elContractBindings, err := elcontracts.BuildClients(
elcontracts.Config{
DelegationManagerAddress: avsRegistryContractBindings.DelegationManagerAddr,
AvsDirectoryAddress: avsRegistryContractBindings.AvsDirectoryAddr,
},
ethHttpClient,
ethWsClient,
txMgr,
logger,
eigenMetrics,
)
if err != nil {
return nil, utils.WrapError("Failed to create AVS Registry Reader and Writer", err)
return nil, utils.WrapError("Failed to create EL Reader and Writer", err)
}

return &Clients{
Expand All @@ -128,153 +135,6 @@ func BuildAll(

}

func (config *BuildAllConfig) buildAVSRegistryContractBindings(
ethHttpClient eth.Client,
logger logging.Logger,
) (*avsregistry.ContractBindings, error) {
avsRegistryContractBindings, err := avsregistry.NewAVSRegistryContractBindings(
gethcommon.HexToAddress(config.RegistryCoordinatorAddr),
gethcommon.HexToAddress(config.OperatorStateRetrieverAddr),
ethHttpClient,
logger,
)
if err != nil {
return nil, utils.WrapError("Failed to create AVSRegistryContractBindings", err)
}
return avsRegistryContractBindings, nil
}

func (config *BuildAllConfig) buildEigenLayerContractBindings(
delegationManagerAddr, avsDirectoryAddr gethcommon.Address,
ethHttpClient eth.Client,
logger logging.Logger,
) (*elcontracts.ContractBindings, error) {

elContractBindings, err := elcontracts.NewEigenlayerContractBindings(
delegationManagerAddr,
avsDirectoryAddr,
ethHttpClient,
logger,
)
if err != nil {
return nil, utils.WrapError("Failed to create ContractBindings", err)
}
return elContractBindings, nil
}

func (config *BuildAllConfig) BuildELClients(
ethHttpClient eth.Client,
txMgr txmgr.TxManager,
logger logging.Logger,
eigenMetrics *metrics.EigenMetrics,
) (*elcontracts.ELChainReader, *elcontracts.ELChainWriter, *elcontracts.ContractBindings, error) {
avsRegistryContractBindings, err := config.buildAVSRegistryContractBindings(ethHttpClient, logger)
if err != nil {
return nil, nil, nil, err
}

delegationManagerAddr, err := avsRegistryContractBindings.StakeRegistry.Delegation(&bind.CallOpts{})
if err != nil {
logger.Fatal("Failed to fetch DelegationManager contract", "err", err)
}
avsDirectoryAddr, err := avsRegistryContractBindings.ServiceManager.AvsDirectory(&bind.CallOpts{})
if err != nil {
logger.Fatal("Failed to fetch AVSDirectory contract", "err", err)
}

elContractBindings, err := config.buildEigenLayerContractBindings(
delegationManagerAddr,
avsDirectoryAddr,
ethHttpClient,
logger,
)
if err != nil {
return nil, nil, nil, utils.WrapError("Failed to create ContractBindings", err)
}

// get the Reader for the EL contracts
elChainReader := elcontracts.NewELChainReader(
elContractBindings.Slasher,
elContractBindings.DelegationManager,
elContractBindings.StrategyManager,
elContractBindings.AvsDirectory,
logger,
ethHttpClient,
)

elChainWriter := elcontracts.NewELChainWriter(
elContractBindings.Slasher,
elContractBindings.DelegationManager,
elContractBindings.StrategyManager,
nil, // this is nil because we don't have access to the rewards coordinator contract right now. we are going to refactor this method later
elContractBindings.StrategyManagerAddr,
elChainReader,
ethHttpClient,
logger,
eigenMetrics,
txMgr,
)

return elChainReader, elChainWriter, elContractBindings, nil
}

func (config *BuildAllConfig) BuildAVSRegistryClients(
elReader elcontracts.ELReader,
ethHttpClient eth.Client,
ethWsClient eth.Client,
txMgr txmgr.TxManager,
logger logging.Logger,
) (*avsregistry.ChainReader, *avsregistry.ChainSubscriber, *avsregistry.ChainWriter, *avsregistry.ContractBindings, error) {

avsRegistryContractBindings, err := avsregistry.NewAVSRegistryContractBindings(
gethcommon.HexToAddress(config.RegistryCoordinatorAddr),
gethcommon.HexToAddress(config.OperatorStateRetrieverAddr),
ethHttpClient,
logger,
)
if err != nil {
return nil, nil, nil, nil, utils.WrapError("Failed to create AVSRegistryContractBindings", err)
}

avsRegistryChainReader := avsregistry.NewChainReader(
avsRegistryContractBindings.RegistryCoordinatorAddr,
avsRegistryContractBindings.BlsApkRegistryAddr,
avsRegistryContractBindings.RegistryCoordinator,
avsRegistryContractBindings.OperatorStateRetriever,
avsRegistryContractBindings.StakeRegistry,
logger,
ethHttpClient,
)

avsRegistryChainWriter, err := avsregistry.NewChainWriter(
avsRegistryContractBindings.ServiceManagerAddr,
avsRegistryContractBindings.RegistryCoordinator,
avsRegistryContractBindings.OperatorStateRetriever,
avsRegistryContractBindings.StakeRegistry,
avsRegistryContractBindings.BlsApkRegistry,
elReader,
logger,
ethHttpClient,
txMgr,
)
if err != nil {
return nil, nil, nil, nil, utils.WrapError("Failed to create AVSRegistryChainWriter", err)
}

// get the Subscriber for Avs Registry contracts
// note that the subscriber needs a ws connection instead of http
avsRegistrySubscriber, err := avsregistry.BuildAvsRegistryChainSubscriber(
avsRegistryContractBindings.RegistryCoordinatorAddr,
ethWsClient,
logger,
)
if err != nil {
return nil, nil, nil, nil, utils.WrapError("Failed to create ELChainSubscriber", err)
}

return avsRegistryChainReader, avsRegistrySubscriber, avsRegistryChainWriter, avsRegistryContractBindings, nil
}

// Very basic validation that makes sure all fields are nonempty
// we might eventually want more sophisticated validation, based on regexp,
// or use something like https://json-schema.org/ (?)
Expand Down
Loading

0 comments on commit 5616c1d

Please sign in to comment.