Skip to content

Commit

Permalink
refactor avs registry (#283)
Browse files Browse the repository at this point in the history
* refactor avs registry

* mocks

* fix mocks

* address comments
  • Loading branch information
shrimalmadhur authored Jun 28, 2024
1 parent cc3b8d2 commit 80be44e
Show file tree
Hide file tree
Showing 19 changed files with 488 additions and 221 deletions.
131 changes: 131 additions & 0 deletions chainio/clients/avsregistry/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type ContractBindings struct {
BlsApkRegistryAddr gethcommon.Address
OperatorStateRetrieverAddr gethcommon.Address
IndexRegistryAddr gethcommon.Address
DelegationManagerAddr gethcommon.Address
AvsDirectoryAddr gethcommon.Address
// contract bindings
ServiceManager *servicemanager.ContractServiceManagerBase
RegistryCoordinator *regcoordinator.ContractRegistryCoordinator
Expand All @@ -34,6 +36,8 @@ type ContractBindings struct {
OperatorStateRetriever *opstateretriever.ContractOperatorStateRetriever
}

// NewAVSRegistryContractBindings creates a new instance of ContractBindings
// Deprecated: Use NewBindingsFromConfig instead
func NewAVSRegistryContractBindings(
registryCoordinatorAddr gethcommon.Address,
operatorStateRetrieverAddr gethcommon.Address,
Expand Down Expand Up @@ -116,3 +120,130 @@ func NewAVSRegistryContractBindings(
OperatorStateRetriever: contractOperatorStateRetriever,
}, nil
}

// NewBindingsFromConfig creates a new instance of ContractBindings
func NewBindingsFromConfig(
cfg Config,
client eth.Client,
logger logging.Logger,
) (*ContractBindings, error) {
var (
err error

serviceManagerAddr gethcommon.Address
registryCoordinatorAddr gethcommon.Address
stakeRegistryAddr gethcommon.Address
blsApkRegistryAddr gethcommon.Address
indexRegistryAddr gethcommon.Address
operatorStateRetrieverAddr gethcommon.Address
delegationManagerAddr gethcommon.Address
avsDirectoryAddr 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.Warn("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)
}
}

if isZeroAddress(cfg.OperatorStateRetrieverAddress) {
logger.Warn("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: registryCoordinatorAddr,
StakeRegistryAddr: stakeRegistryAddr,
BlsApkRegistryAddr: blsApkRegistryAddr,
IndexRegistryAddr: indexRegistryAddr,
OperatorStateRetrieverAddr: operatorStateRetrieverAddr,
DelegationManagerAddr: delegationManagerAddr,
AvsDirectoryAddr: avsDirectoryAddr,
ServiceManager: contractServiceManager,
RegistryCoordinator: contractBlsRegistryCoordinator,
StakeRegistry: contractStakeRegistry,
BlsApkRegistry: contractBlsApkRegistry,
IndexRegistry: contractIndexRegistry,
OperatorStateRetriever: contractOperatorStateRetriever,
}, nil
}

func isZeroAddress(address gethcommon.Address) bool {
return address == gethcommon.Address{}
}
Loading

0 comments on commit 80be44e

Please sign in to comment.