Skip to content

Commit

Permalink
refactor avs registry
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Jun 28, 2024
1 parent 2c8b1d5 commit d74f527
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 96 deletions.
128 changes: 128 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,127 @@ 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
var serviceManagerAddr gethcommon.Address
var registryCoordinatorAddr gethcommon.Address
var stakeRegistryAddr gethcommon.Address
var blsApkRegistryAddr gethcommon.Address
var indexRegistryAddr gethcommon.Address
var operatorStateRetrieverAddr gethcommon.Address
var delegationManagerAddr gethcommon.Address
var avsDirectoryAddr gethcommon.Address

var contractBlsRegistryCoordinator *regcoordinator.ContractRegistryCoordinator
var contractServiceManager *servicemanager.ContractServiceManagerBase
var contractStakeRegistry *stakeregistry.ContractStakeRegistry
var contractBlsApkRegistry *blsapkregistry.ContractBLSApkRegistry
var contractIndexRegistry *indexregistry.ContractIndexRegistry
var 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 fetch 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 fetch 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 fetch 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 fetch 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 d74f527

Please sign in to comment.