From 22e2a6a548b419e5c7097bb07fe4df9bddb07f3d Mon Sep 17 00:00:00 2001 From: Madhur Shrimal Date: Fri, 6 Sep 2024 13:56:02 -0700 Subject: [PATCH] basic show command --- pkg/operator/allocations/initializedelay.go | 30 ++++---- pkg/operator/allocations/show.go | 55 ++++++++++++-- pkg/operator/allocations/types.go | 81 ++++++++++++++++++--- pkg/operator/allocations/update.go | 8 +- pkg/operator/config/create.go | 14 +++- pkg/operator/register.go | 1 + pkg/types/operator_config.go | 3 - pkg/utils/utils.go | 10 +++ 8 files changed, 160 insertions(+), 42 deletions(-) diff --git a/pkg/operator/allocations/initializedelay.go b/pkg/operator/allocations/initializedelay.go index 5b962ebe..37176a99 100644 --- a/pkg/operator/allocations/initializedelay.go +++ b/pkg/operator/allocations/initializedelay.go @@ -51,7 +51,7 @@ func initializeDelayAction(cCtx *cli.Context, p utils.Prompter) error { } // Temp to test modify Allocations - config.avsDirectoryAddress = gethcommon.HexToAddress("0x8BffE5a668DB26bc5Ce8dC9C0096fB634747b62A") + config.delegationManagerAddress = gethcommon.HexToAddress("0x05e7c29D006a44D9eB1b9ad74FD6ac3bafbfeB04") if config.broadcast { confirm, err := p.Confirm( @@ -69,7 +69,7 @@ func initializeDelayAction(cCtx *cli.Context, p utils.Prompter) error { config.signerConfig, ethClient, elcontracts.Config{ - AvsDirectoryAddress: config.avsDirectoryAddress, + DelegationManagerAddress: config.delegationManagerAddress, }, p, config.chainID, @@ -88,7 +88,7 @@ func initializeDelayAction(cCtx *cli.Context, p utils.Prompter) error { } else { noSendTxOpts := common.GetNoSendTxOpts(config.operatorAddress) _, _, contractBindings, err := elcontracts.BuildClients(elcontracts.Config{ - AvsDirectoryAddress: config.avsDirectoryAddress, + DelegationManagerAddress: config.delegationManagerAddress, }, ethClient, nil, logger, nil) if err != nil { return err @@ -186,22 +186,22 @@ func readAndValidateAllocationDelayConfig(c *cli.Context, logger logging.Logger) logger.Debugf("Failed to get signer config: %s", err) } - avsDirectoryAddress, err := utils.GetAVSDirectoryAddress(chainID) + delegationManagerAddress, err := utils.GetDelegationManagerAddress(chainID) if err != nil { return nil, err } return &allocationDelayConfig{ - network: network, - rpcUrl: rpcUrl, - environment: environment, - chainID: chainID, - output: output, - outputType: outputType, - broadcast: broadcast, - operatorAddress: gethcommon.HexToAddress(operatorAddress), - signerConfig: signerConfig, - avsDirectoryAddress: gethcommon.HexToAddress(avsDirectoryAddress), - allocationDelay: uint32(allocationDelayInt), + network: network, + rpcUrl: rpcUrl, + environment: environment, + chainID: chainID, + output: output, + outputType: outputType, + broadcast: broadcast, + operatorAddress: gethcommon.HexToAddress(operatorAddress), + signerConfig: signerConfig, + delegationManagerAddress: gethcommon.HexToAddress(delegationManagerAddress), + allocationDelay: uint32(allocationDelayInt), }, nil } diff --git a/pkg/operator/allocations/show.go b/pkg/operator/allocations/show.go index 37eee435..4b631d5d 100644 --- a/pkg/operator/allocations/show.go +++ b/pkg/operator/allocations/show.go @@ -50,13 +50,14 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error { return eigenSdkUtils.WrapError("failed to create new eth client", err) } - avsDirectoryAddress, err := utils.GetAVSDirectoryAddress(config.chainID) - if err != nil { - return err - } + // TODO(shrimalmadhur): uncomment it before we merge the PR + // avsDirectoryAddress, err := utils.GetAVSDirectoryAddress(config.chainID) + // if err != nil { + // return err + // } // Temp to test modify allocations - avsDirectoryAddress = "0x8BffE5a668DB26bc5Ce8dC9C0096fB634747b62A" + avsDirectoryAddress := "0x2a9854D331B8142d024E4007fB4A34311c5176cE" elReader, err := elcontracts.NewReaderFromConfig( elcontracts.Config{ @@ -71,14 +72,45 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error { // for each strategy address, get the allocatable magnitude for _, strategyAddress := range config.strategyAddresses { - allocatableMagnitude, err := elReader.GetAllocatableMagnitude(&bind.CallOpts{Context: ctx}, strategyAddress, config.operatorAddress) + allocatableMagnitude, err := elReader.GetAllocatableMagnitude( + &bind.CallOpts{Context: ctx}, + config.operatorAddress, + strategyAddress, + ) if err != nil { return eigenSdkUtils.WrapError("failed to get allocatable magnitude", err) } - logger.Debug("Allocatable magnitude for strategy", strategyAddress, ":", allocatableMagnitude) + logger.Debugf("Allocatable magnitude for strategy %v: %d", strategyAddress, allocatableMagnitude) + } + + opSet, slashableMagnitudes, err := elReader.GetCurrentSlashableMagnitudes( + &bind.CallOpts{Context: ctx}, + config.operatorAddress, + config.strategyAddresses, + ) + if err != nil { + return eigenSdkUtils.WrapError("failed to get slashable magnitude", err) + } + + slashableMagnitudeHolders := make(SlashableMagnitudeHolders, 0) + for i, strategyAddress := range config.strategyAddresses { + slashableMagnitude := slashableMagnitudes[i] + for j, opSet := range opSet { + slashableMagnitudeHolders = append(slashableMagnitudeHolders, SlashableMagnitudesHolder{ + StrategyAddress: strategyAddress, + AVSAddress: opSet.Avs, + OperatorSetId: opSet.OperatorSetId, + SlashableMagnitude: slashableMagnitude[j], + }) + } + } + + if config.outputType == string(common.OutputType_Json) { + slashableMagnitudeHolders.PrintJSON() + } else { + slashableMagnitudeHolders.PrintPretty() } - return nil } @@ -89,6 +121,8 @@ func readAndValidateShowConfig(cCtx *cli.Context, logger *logging.Logger) (*show operatorAddress := gethcommon.HexToAddress(cCtx.String(flags.OperatorAddressFlag.Name)) avsAddresses := common.ConvertStringSliceToGethAddressSlice(cCtx.StringSlice(flags.AVSAddressesFlag.Name)) strategyAddresses := common.ConvertStringSliceToGethAddressSlice(cCtx.StringSlice(flags.StrategyAddressesFlag.Name)) + outputFile := cCtx.String(flags.OutputFileFlag.Name) + outputType := cCtx.String(flags.OutputTypeFlag.Name) return &showConfig{ network: network, @@ -97,6 +131,8 @@ func readAndValidateShowConfig(cCtx *cli.Context, logger *logging.Logger) (*show operatorAddress: operatorAddress, avsAddresses: avsAddresses, strategyAddresses: strategyAddresses, + output: outputFile, + outputType: outputType, }, nil } @@ -108,6 +144,9 @@ func getShowFlags() []cli.Flag { &flags.NetworkFlag, &flags.EnvironmentFlag, &flags.ETHRpcUrlFlag, + &flags.VerboseFlag, + &flags.OutputFileFlag, + &flags.OutputTypeFlag, } sort.Sort(cli.FlagsByName(baseFlags)) diff --git a/pkg/operator/allocations/types.go b/pkg/operator/allocations/types.go index 8f7fba08..e2980185 100644 --- a/pkg/operator/allocations/types.go +++ b/pkg/operator/allocations/types.go @@ -1,8 +1,10 @@ package allocations import ( + "encoding/json" "fmt" "math/big" + "strings" "github.com/Layr-Labs/eigenlayer-cli/pkg/types" @@ -61,17 +63,17 @@ type allocation struct { } type allocationDelayConfig struct { - network string - rpcUrl string - environment string - chainID *big.Int - output string - outputType string - broadcast bool - operatorAddress gethcommon.Address - signerConfig *types.SignerConfig - allocationDelay uint32 - avsDirectoryAddress gethcommon.Address + network string + rpcUrl string + environment string + chainID *big.Int + output string + outputType string + broadcast bool + operatorAddress gethcommon.Address + signerConfig *types.SignerConfig + allocationDelay uint32 + delegationManagerAddress gethcommon.Address } type showConfig struct { @@ -85,3 +87,60 @@ type showConfig struct { avsAddresses []gethcommon.Address strategyAddresses []gethcommon.Address } + +type SlashableMagnitudeHolders []SlashableMagnitudesHolder + +type SlashableMagnitudesHolder struct { + StrategyAddress gethcommon.Address + AVSAddress gethcommon.Address + OperatorSetId uint32 + SlashableMagnitude uint64 +} + +func (s SlashableMagnitudeHolders) PrintPretty() { + // Define column headers and widths + headers := []string{"Strategy Address", "AVS Address", "Operator Set ID", "Slashable Magnitude"} + widths := []int{43, 43, 16, 20} + + // print dashes + for _, width := range widths { + fmt.Print("+" + strings.Repeat("-", width+1)) + } + fmt.Println("+") + + // Print header + for i, header := range headers { + fmt.Printf("| %-*s", widths[i], header) + } + fmt.Println("|") + + // Print separator + for _, width := range widths { + fmt.Print("|", strings.Repeat("-", width+1)) + } + fmt.Println("|") + + // Print data rows + for _, holder := range s { + fmt.Printf("| %-*s| %-*s| %-*d| %-*d|\n", + widths[0], holder.StrategyAddress.Hex(), + widths[1], holder.AVSAddress.Hex(), + widths[2], holder.OperatorSetId, + widths[3], holder.SlashableMagnitude) + } + + // print dashes + for _, width := range widths { + fmt.Print("+" + strings.Repeat("-", width+1)) + } + fmt.Println("+") +} + +func (s SlashableMagnitudeHolders) PrintJSON() { + json, err := json.MarshalIndent(s, "", " ") + if err != nil { + fmt.Println("Error marshalling to JSON:", err) + return + } + fmt.Println(string(json)) +} diff --git a/pkg/operator/allocations/update.go b/pkg/operator/allocations/update.go index c01b3c3f..3160b5dd 100644 --- a/pkg/operator/allocations/update.go +++ b/pkg/operator/allocations/update.go @@ -79,7 +79,7 @@ func updateAllocations(cCtx *cli.Context, p utils.Prompter) error { } // Temp to test modify allocations - avsDirectoryAddress = "0x8BffE5a668DB26bc5Ce8dC9C0096fB634747b62A" + avsDirectoryAddress = "0x2a9854D331B8142d024E4007fB4A34311c5176cE" elReader, err := elcontracts.NewReaderFromConfig( elcontracts.Config{ @@ -352,7 +352,11 @@ func getMagnitudes( reader elChainReader, ) (map[gethcommon.Address]uint64, error) { strategyTotalMagnitudes := make(map[gethcommon.Address]uint64, len(strategies)) - totalMagnitudes, err := reader.GetTotalMagnitudes(&bind.CallOpts{Context: context.Background()}, operatorAddress, strategies) + totalMagnitudes, err := reader.GetTotalMagnitudes( + &bind.CallOpts{Context: context.Background()}, + operatorAddress, + strategies, + ) if err != nil { return nil, err } diff --git a/pkg/operator/config/create.go b/pkg/operator/config/create.go index 37e44051..64aa7c49 100644 --- a/pkg/operator/config/create.go +++ b/pkg/operator/config/create.go @@ -159,7 +159,10 @@ func promptOperatorInfo(config *types.OperatorConfig, p utils.Prompter) (types.O config.EthRPCUrl = rpcUrl // Prompt for allocation delay - allocationDelay, err := p.InputInteger("Enter your allocation delay (in seconds, default is 17.5 days):", "1512000", "", + allocationDelay, err := p.InputInteger( + "Enter your allocation delay (in seconds, default is 17.5 days):", + "1512000", + "", func(i int64) error { if i < 0 { return errors.New("allocation delay should be non-negative") @@ -172,13 +175,18 @@ func promptOperatorInfo(config *types.OperatorConfig, p utils.Prompter) (types.O } // confirm again - confirm, err := p.Confirm("Are you sure you want to set the allocation delay to " + strconv.FormatInt(allocationDelay, 10) + " seconds? This cannot be changed once set.") + confirm, err := p.Confirm( + "Are you sure you want to set the allocation delay to " + strconv.FormatInt( + allocationDelay, + 10, + ) + " seconds? This cannot be changed once set.", + ) if err != nil { return types.OperatorConfig{}, err } if confirm { - config.AllocationDelay = uint32(allocationDelay) + config.Operator.AllocationDelay = uint32(allocationDelay) } else { return types.OperatorConfig{}, errors.New("operator cancelled") } diff --git a/pkg/operator/register.go b/pkg/operator/register.go index 964649af..d02e3d21 100644 --- a/pkg/operator/register.go +++ b/pkg/operator/register.go @@ -45,6 +45,7 @@ func RegisterCmd(p utils.Prompter) *cli.Command { configurationFilePath := args.Get(0) operatorCfg, err := common.ValidateAndReturnConfig(configurationFilePath, logger) + logger.Debugf("operatorCfg: %v", operatorCfg) if err != nil { return err } diff --git a/pkg/types/operator_config.go b/pkg/types/operator_config.go index 6dc212f5..04c9be56 100644 --- a/pkg/types/operator_config.go +++ b/pkg/types/operator_config.go @@ -28,7 +28,6 @@ type OperatorConfig struct { EthRPCUrl string `yaml:"eth_rpc_url"` ChainId big.Int `yaml:"chain_id"` SignerConfig SignerConfig - AllocationDelay uint32 `yaml:"allocation_delay"` } func (o *OperatorConfig) MarshalYAML() (interface{}, error) { @@ -65,7 +64,6 @@ func (o *OperatorConfig) UnmarshalYAML(unmarshal func(interface{}) error) error SignerType SignerType `yaml:"signer_type"` Fireblocks FireblocksConfig `yaml:"fireblocks"` Web3 Web3SignerConfig `yaml:"web3"` - AllocationDelay uint32 `yaml:"allocation_delay"` } if err := unmarshal(&aux); err != nil { return err @@ -75,7 +73,6 @@ func (o *OperatorConfig) UnmarshalYAML(unmarshal func(interface{}) error) error o.ELAVSDirectoryAddress = aux.ELAVSDirectoryAddress o.ELRewardsCoordinatorAddress = aux.ELRewardsCoordinatorAddress o.EthRPCUrl = aux.EthRPCUrl - o.AllocationDelay = aux.AllocationDelay chainId := new(big.Int) chainId.SetInt64(aux.ChainId) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 8e23edc6..98f92e33 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -51,6 +51,16 @@ func GetAVSDirectoryAddress(chainID *big.Int) (string, error) { } } +func GetDelegationManagerAddress(chainID *big.Int) (string, error) { + chainIDInt := chainID.Int64() + chainMetadata, ok := ChainMetadataMap[chainIDInt] + if !ok { + return "", fmt.Errorf("chain ID %d not supported", chainIDInt) + } else { + return chainMetadata.ELDelegationManagerAddress, nil + } +} + func GetRewardCoordinatorAddress(chainID *big.Int) (string, error) { chainIDInt := chainID.Int64() chainMetadata, ok := ChainMetadataMap[chainIDInt]