Skip to content

Commit

Permalink
feat: add working cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Sep 5, 2024
1 parent 75042e6 commit fdc62ac
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 52 deletions.
11 changes: 11 additions & 0 deletions pkg/internal/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,14 @@ func GetNoSendTxOpts(from common.Address) *bind.TransactOpts {
func Trim0x(s string) string {
return strings.TrimPrefix(s, "0x")
}

func GetEnvFromNetwork(network string) string {
switch network {
case utils.HoleskyNetworkName:
return "testnet"
case utils.MainnetNetworkName:
return "mainnet"
default:
return "local"
}
}
1 change: 1 addition & 0 deletions pkg/operator/allocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func AllocationsCmd(p utils.Prompter) *cli.Command {
Subcommands: []*cli.Command{
allocations.ShowCmd(p),
allocations.UpdateCmd(p),
allocations.InitializeDelayCmd(p),
},
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/operator/allocations/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ var (
EnvVars: []string{"BIPS_TO_ALLOCATE"},
}

AllocationDelayFlag = cli.Uint64Flag{
Name: "delay",
Usage: "This determines how long it takes for allocations to take in the future. Can only be set one time for each operator. Specified in seconds",
Aliases: []string{"allocation-delay", "ad"},
EnvVars: []string{"ALLOCATION_DELAY"},
EnvironmentFlag = cli.StringFlag{
Name: "environment",
Aliases: []string{"env"},
Usage: "environment to use. Currently supports 'preprod' ,'testnet' and 'prod'. If not provided, it will be inferred based on network",
EnvVars: []string{"ENVIRONMENT"},
}
)
120 changes: 114 additions & 6 deletions pkg/operator/allocations/initializedelay.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package allocations

import (
"fmt"
"sort"
"strconv"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common/flags"
"github.com/Layr-Labs/eigenlayer-cli/pkg/telemetry"
"github.com/Layr-Labs/eigenlayer-cli/pkg/utils"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts"
"github.com/Layr-Labs/eigensdk-go/logging"
eigenSdkUtils "github.com/Layr-Labs/eigensdk-go/utils"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"

"github.com/urfave/cli/v2"
"sort"
)

func InitializeDelayCmd(p utils.Prompter) {
initialializeAllocationDelayCmd := &cli.Command{
Name: "initialize-allocation-delay",
func InitializeDelayCmd(p utils.Prompter) *cli.Command {
initializeDelayCmd := &cli.Command{
Name: "initialize-delay",
UsageText: "initialize-delay [flags] <delay>",
Usage: "Initialize the allocation delay for operator",
Description: "Initializes the allocation delay for operator. This is a one time command. You can not change the allocation delay once",
Flags: getInitializeAllocationDelayFlags(),
Expand All @@ -20,10 +32,55 @@ func InitializeDelayCmd(p utils.Prompter) {
return initializeDelayAction(c, p)
},
}

return initializeDelayCmd
}

func initializeDelayAction(c *cli.Context, p utils.Prompter) error {
func initializeDelayAction(cCtx *cli.Context, p utils.Prompter) error {
ctx := cCtx.Context
logger := common.GetLogger(cCtx)

config, err := readAndValidateAllocationDelayConfig(cCtx, logger)
if err != nil {
return eigenSdkUtils.WrapError("failed to read and validate claim config", err)
}
cCtx.App.Metadata["network"] = config.chainID.String()
ethClient, err := ethclient.Dial(config.rpcUrl)
if err != nil {
return eigenSdkUtils.WrapError("failed to create new eth client", err)
}

// Temp to test modify allocations
config.avsDirectoryAddress = gethcommon.HexToAddress("0x8BffE5a668DB26bc5Ce8dC9C0096fB634747b62A")

if config.broadcast {
eLWriter, err := common.GetELWriter(
config.operatorAddress,
config.signerConfig,
ethClient,
elcontracts.Config{
AvsDirectoryAddress: config.avsDirectoryAddress,
},
p,
config.chainID,
logger,
)

if err != nil {
return eigenSdkUtils.WrapError("failed to get EL writer", err)
}

receipt, err := eLWriter.InitializeAllocationDelay(ctx, config.allocationDelay, true)
if err != nil {
return err
}
common.PrintTransactionInfo(receipt.TxHash.String(), config.chainID)
} else {
logger.Infof("Operator Address: %s", config.operatorAddress.String())
logger.Infof("Allocation Delay: %d", config.allocationDelay)
}

return nil
}

func getInitializeAllocationDelayFlags() []cli.Flag {
Expand All @@ -36,13 +93,64 @@ func getInitializeAllocationDelayFlags() []cli.Flag {
&flags.BroadcastFlag,
&flags.VerboseFlag,
&flags.OperatorAddressFlag,
&AllocationDelayFlag,
}
allFlags := append(baseFlags, flags.GetSignerFlags()...)
sort.Sort(cli.FlagsByName(allFlags))
return allFlags
}

func readAndValidateAllocationDelayConfig(c *cli.Context, logger logging.Logger) (*allocationDelayConfig, error) {
args := c.Args()
if args.Len() != 1 {
return nil, fmt.Errorf("accepts 1 arg, received %d", args.Len())
}

allocationDelayString := c.Args().First()
allocationDelayInt, err := strconv.Atoi(allocationDelayString)
if err != nil {
return nil, eigenSdkUtils.WrapError("failed to convert allocation delay to int", err)
}

network := c.String(flags.NetworkFlag.Name)
environment := c.String(EnvironmentFlag.Name)
rpcUrl := c.String(flags.ETHRpcUrlFlag.Name)
output := c.String(flags.OutputFileFlag.Name)
outputType := c.String(flags.OutputTypeFlag.Name)
broadcast := c.Bool(flags.BroadcastFlag.Name)
operatorAddress := c.String(flags.OperatorAddressFlag.Name)

chainID := utils.NetworkNameToChainId(network)
logger.Debugf("Using chain ID: %s", chainID.String())

if common.IsEmptyString(environment) {
environment = common.GetEnvFromNetwork(network)
}
logger.Debugf("Using network %s and environment: %s", network, environment)

// Get signerConfig
signerConfig, err := common.GetSignerConfig(c, logger)
if err != nil {
// We don't want to throw error since people can still use it to generate the claim
// without broadcasting it
logger.Debugf("Failed to get signer config: %s", err)
}

avsDirectoryAddress, err := utils.GetAVSDirectoryAddress(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),
}, nil
}
35 changes: 23 additions & 12 deletions pkg/operator/allocations/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ type bulkModifyAllocations struct {

func (b *bulkModifyAllocations) Print() {
for _, a := range b.allocations {
fmt.Printf("Strategy: %s, Expected Total Magnitude: %d, Allocatable Magnitude %d\n", a.Strategy.Hex(), a.ExpectedTotalMagnitude, b.allocatableMagnitudes[a.Strategy])
fmt.Printf(
"Strategy: %s, Expected Total Magnitude: %d, Allocatable Magnitude %d\n",
a.Strategy.Hex(),
a.ExpectedTotalMagnitude,
b.allocatableMagnitudes[a.Strategy],
)
for i, opSet := range a.OperatorSets {
fmt.Printf("Operator Set: %d, AVS: %s, Magnitude: %d\n", opSet.OperatorSetId, opSet.Avs.Hex(), a.Magnitudes[i])
fmt.Printf(
"Operator Set: %d, AVS: %s, Magnitude: %d\n",
opSet.OperatorSetId,
opSet.Avs.Hex(),
a.Magnitudes[i],
)
}
fmt.Println()
}
Expand Down Expand Up @@ -52,14 +62,15 @@ 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 uint64
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
}
73 changes: 44 additions & 29 deletions pkg/operator/allocations/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package allocations
import (
"context"
"errors"
"github.com/Layr-Labs/eigenlayer-cli/pkg/telemetry"
"math/big"
"os"
"sort"
Expand All @@ -15,22 +14,29 @@ import (

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common/flags"
"github.com/Layr-Labs/eigenlayer-cli/pkg/telemetry"
"github.com/Layr-Labs/eigenlayer-cli/pkg/utils"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts"
"github.com/Layr-Labs/eigensdk-go/chainio/txmgr"
contractIAVSDirectory "github.com/Layr-Labs/eigensdk-go/contracts/bindings/IAVSDirectory"
"github.com/Layr-Labs/eigensdk-go/logging"
eigenMetrics "github.com/Layr-Labs/eigensdk-go/metrics"
eigenSdkUtils "github.com/Layr-Labs/eigensdk-go/utils"

"github.com/gocarina/gocsv"
"github.com/urfave/cli/v2"
)

type elChainReader interface {
GetLatestTotalMagnitude(opts *bind.CallOpts, operator gethcommon.Address, strategy gethcommon.Address) (uint64, error)
GetAllocatableMagnitude(opts *bind.CallOpts, operator gethcommon.Address, strategy gethcommon.Address) (uint64, error)
GetLatestTotalMagnitude(
opts *bind.CallOpts,
operator gethcommon.Address,
strategy gethcommon.Address,
) (uint64, error)
GetAllocatableMagnitude(
opts *bind.CallOpts,
operator gethcommon.Address,
strategy gethcommon.Address,
) (uint64, error)
}

func UpdateCmd(p utils.Prompter) *cli.Command {
Expand Down Expand Up @@ -72,7 +78,7 @@ func updateAllocations(cCtx *cli.Context, p utils.Prompter) error {
}

// Temp to test modify allocations
avsDirectoryAddress = "0xc2c0bc13571aC5115709C332dc7AE666606b08E8"
avsDirectoryAddress = "0x8BffE5a668DB26bc5Ce8dC9C0096fB634747b62A"

elReader, err := elcontracts.NewReaderFromConfig(
elcontracts.Config{
Expand All @@ -95,35 +101,30 @@ func updateAllocations(cCtx *cli.Context, p utils.Prompter) error {
return errors.New("signer is required for broadcasting")
}
logger.Info("Broadcasting magnitude allocation update...")
keyWallet, sender, err := common.GetWallet(
*config.signerConfig,
config.operatorAddress.String(),
eLWriter, err := common.GetELWriter(
config.operatorAddress,
config.signerConfig,
ethClient,
elcontracts.Config{
AvsDirectoryAddress: gethcommon.HexToAddress(avsDirectoryAddress),
},
p,
*config.chainID,
config.chainID,
logger,
)
if err != nil {
return eigenSdkUtils.WrapError("failed to get wallet", err)
return eigenSdkUtils.WrapError("failed to get EL writer", err)
}

txMgr := txmgr.NewSimpleTxManager(keyWallet, ethClient, logger, sender)
noopMetrics := eigenMetrics.NewNoopMetrics()
eLWriter, err := elcontracts.NewWriterFromConfig(
elcontracts.Config{
AvsDirectoryAddress: gethcommon.HexToAddress(avsDirectoryAddress),
receipt, err := eLWriter.ModifyAllocations(
ctx,
config.operatorAddress,
allocationsToUpdate.allocations,
contractIAVSDirectory.ISignatureUtilsSignatureWithSaltAndExpiry{
Expiry: big.NewInt(0),
},
ethClient,
logger,
noopMetrics,
txMgr,
true,
)
if err != nil {
return eigenSdkUtils.WrapError("failed to create new writer from config", err)
}
receipt, err := eLWriter.ModifyAllocations(ctx, config.operatorAddress, allocationsToUpdate.allocations, contractIAVSDirectory.ISignatureUtilsSignatureWithSaltAndExpiry{
Expiry: big.NewInt(0),
}, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -167,11 +168,19 @@ func generateAllocationsParams(

var err error
if len(config.csvFilePath) == 0 {
magnitude, err := elReader.GetLatestTotalMagnitude(&bind.CallOpts{Context: ctx}, config.operatorAddress, config.strategyAddress)
magnitude, err := elReader.GetLatestTotalMagnitude(
&bind.CallOpts{Context: ctx},
config.operatorAddress,
config.strategyAddress,
)
if err != nil {
return nil, eigenSdkUtils.WrapError("failed to get latest total magnitude", err)
}
allocatableMagnitude, err := elReader.GetAllocatableMagnitude(&bind.CallOpts{Context: ctx}, config.operatorAddress, config.strategyAddress)
allocatableMagnitude, err := elReader.GetAllocatableMagnitude(
&bind.CallOpts{Context: ctx},
config.operatorAddress,
config.strategyAddress,
)
if err != nil {
return nil, eigenSdkUtils.WrapError("failed to get allocatable magnitude", err)
}
Expand Down Expand Up @@ -377,7 +386,13 @@ func readAndValidateUpdateFlags(cCtx *cli.Context, logger logging.Logger) (*upda
strategyAddress := gethcommon.HexToAddress(cCtx.String(flags.StrategyAddressFlag.Name))
operatorSetId := uint32(cCtx.Uint64(flags.OperatorSetIdFlag.Name))
bipsToAllocate := cCtx.Uint64(BipsToAllocateFlag.Name)
logger.Debugf("Operator address: %s, AVS address: %s, Strategy address: %s, Bips to allocate: %d", operatorAddress.Hex(), avsAddress.Hex(), strategyAddress.Hex(), bipsToAllocate)
logger.Debugf(
"Operator address: %s, AVS address: %s, Strategy address: %s, Bips to allocate: %d",
operatorAddress.Hex(),
avsAddress.Hex(),
strategyAddress.Hex(),
bipsToAllocate,
)

// Get signerConfig
signerConfig, err := common.GetSignerConfig(cCtx, logger)
Expand Down

0 comments on commit fdc62ac

Please sign in to comment.