Skip to content

Commit

Permalink
basic show command
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Sep 6, 2024
1 parent f8cd8dc commit 22e2a6a
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 42 deletions.
30 changes: 15 additions & 15 deletions pkg/operator/allocations/initializedelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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),

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an integer with architecture-dependent bit size from
strconv.Atoi
to a lower bit size type uint32 without an upper bound check.
}, nil
}
55 changes: 47 additions & 8 deletions pkg/operator/allocations/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}

Expand All @@ -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,
Expand All @@ -97,6 +131,8 @@ func readAndValidateShowConfig(cCtx *cli.Context, logger *logging.Logger) (*show
operatorAddress: operatorAddress,
avsAddresses: avsAddresses,
strategyAddresses: strategyAddresses,
output: outputFile,
outputType: outputType,
}, nil
}

Expand All @@ -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))
Expand Down
81 changes: 70 additions & 11 deletions pkg/operator/allocations/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package allocations

import (
"encoding/json"
"fmt"
"math/big"
"strings"

"github.com/Layr-Labs/eigenlayer-cli/pkg/types"

Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
8 changes: 6 additions & 2 deletions pkg/operator/allocations/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/operator/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/operator/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/types/operator_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 22e2a6a

Please sign in to comment.