Skip to content

Commit

Permalink
more allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Sep 5, 2024
1 parent 42ce84f commit f8cd8dc
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 44 deletions.
10 changes: 10 additions & 0 deletions pkg/internal/common/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

Expand Down Expand Up @@ -45,3 +46,12 @@ func GetTxFeeDetails(tx *types.Transaction) *TxFeeDetails {
GasFeeCapGwei: gasFeeCapGwei,
}
}

func ConvertStringSliceToGethAddressSlice(addresses []string) []common.Address {
gethAddresses := make([]common.Address, 0, len(addresses))
for _, address := range addresses {
parsed := common.HexToAddress(address)
gethAddresses = append(gethAddresses, parsed)
}
return gethAddresses
}
2 changes: 1 addition & 1 deletion pkg/operator/allocations/initializedelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func initializeDelayAction(cCtx *cli.Context, p utils.Prompter) error {
noSendTxOpts.GasLimit = 150_000
}

unsignedTx, err := contractBindings.AvsDirectory.InitializeAllocationDelay(noSendTxOpts, config.allocationDelay)
unsignedTx, err := contractBindings.DelegationManager.InitializeAllocationDelay(noSendTxOpts, config.allocationDelay)
if err != nil {
return eigenSdkUtils.WrapError("failed to create unsigned tx", err)
}
Expand Down
103 changes: 99 additions & 4 deletions pkg/operator/allocations/show.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,115 @@
package allocations

import (
"sort"

"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"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"

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

func ShowCmd(p utils.Prompter) *cli.Command {
showCmd := &cli.Command{
Name: "show",
Usage: "Show allocations",
UsageText: "show",
After: telemetry.AfterRunAction(),
Name: "show",
Usage: "Show allocations",
After: telemetry.AfterRunAction(),
Description: `
Command to show allocations
`,
Flags: getShowFlags(),
Action: func(cCtx *cli.Context) error {
return showAction(cCtx, p)
},
}
return showCmd
}

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

config, err := readAndValidateShowConfig(cCtx, &logger)
if err != nil {
return 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)
}

avsDirectoryAddress, err := utils.GetAVSDirectoryAddress(config.chainID)
if err != nil {
return err
}

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

elReader, err := elcontracts.NewReaderFromConfig(
elcontracts.Config{
AvsDirectoryAddress: gethcommon.HexToAddress(avsDirectoryAddress),
},
ethClient,
logger,
)
if err != nil {
return eigenSdkUtils.WrapError("failed to create new reader from config", err)
}

// for each strategy address, get the allocatable magnitude
for _, strategyAddress := range config.strategyAddresses {
allocatableMagnitude, err := elReader.GetAllocatableMagnitude(&bind.CallOpts{Context: ctx}, strategyAddress, config.operatorAddress)
if err != nil {
return eigenSdkUtils.WrapError("failed to get allocatable magnitude", err)
}
logger.Debug("Allocatable magnitude for strategy", strategyAddress, ":", allocatableMagnitude)
}


return nil
}

func readAndValidateShowConfig(cCtx *cli.Context, logger *logging.Logger) (*showConfig, error) {
network := cCtx.String(flags.NetworkFlag.Name)
rpcUrl := cCtx.String(flags.ETHRpcUrlFlag.Name)
environment := cCtx.String(flags.EnvironmentFlag.Name)
operatorAddress := gethcommon.HexToAddress(cCtx.String(flags.OperatorAddressFlag.Name))
avsAddresses := common.ConvertStringSliceToGethAddressSlice(cCtx.StringSlice(flags.AVSAddressesFlag.Name))
strategyAddresses := common.ConvertStringSliceToGethAddressSlice(cCtx.StringSlice(flags.StrategyAddressesFlag.Name))

return &showConfig{
network: network,
rpcUrl: rpcUrl,
environment: environment,
operatorAddress: operatorAddress,
avsAddresses: avsAddresses,
strategyAddresses: strategyAddresses,
}, nil
}

func getShowFlags() []cli.Flag {
baseFlags := []cli.Flag{
&flags.OperatorAddressFlag,
&flags.AVSAddressesFlag,
&flags.StrategyAddressesFlag,
&flags.NetworkFlag,
&flags.EnvironmentFlag,
&flags.ETHRpcUrlFlag,
}

sort.Sort(cli.FlagsByName(baseFlags))
return baseFlags
}
12 changes: 12 additions & 0 deletions pkg/operator/allocations/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,15 @@ type allocationDelayConfig struct {
allocationDelay uint32
avsDirectoryAddress gethcommon.Address
}

type showConfig struct {
network string
rpcUrl string
environment string
chainID *big.Int
output string
outputType string
operatorAddress gethcommon.Address
avsAddresses []gethcommon.Address
strategyAddresses []gethcommon.Address
}
47 changes: 17 additions & 30 deletions pkg/operator/allocations/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
)

type elChainReader interface {
GetLatestTotalMagnitude(
GetTotalMagnitudes(
opts *bind.CallOpts,
operator gethcommon.Address,
strategy gethcommon.Address,
) (uint64, error)
operatorAddress gethcommon.Address,
strategyAddresses []gethcommon.Address,
) ([]uint64, error)
GetAllocatableMagnitude(
opts *bind.CallOpts,
operator gethcommon.Address,
Expand Down Expand Up @@ -218,10 +218,10 @@ func generateAllocationsParams(

var err error
if len(config.csvFilePath) == 0 {
magnitude, err := elReader.GetLatestTotalMagnitude(
magnitude, err := elReader.GetTotalMagnitudes(
&bind.CallOpts{Context: ctx},
config.operatorAddress,
config.strategyAddress,
[]gethcommon.Address{config.strategyAddress},
)
if err != nil {
return nil, eigenSdkUtils.WrapError("failed to get latest total magnitude", err)
Expand All @@ -237,11 +237,11 @@ func generateAllocationsParams(
logger.Debugf("Total Magnitude: %d", magnitude)
logger.Debugf("Allocatable Magnitude: %d", allocatableMagnitude)
logger.Debugf("Bips to allocate: %d", config.bipsToAllocate)
magnitudeToUpdate := calculateMagnitudeToUpdate(magnitude, config.bipsToAllocate)
magnitudeToUpdate := calculateMagnitudeToUpdate(magnitude[0], config.bipsToAllocate)
logger.Debugf("Magnitude to update: %d", magnitudeToUpdate)
malloc := contractIAVSDirectory.IAVSDirectoryMagnitudeAllocation{
Strategy: config.strategyAddress,
ExpectedTotalMagnitude: magnitude,
ExpectedTotalMagnitude: magnitude[0],
OperatorSets: []contractIAVSDirectory.IAVSDirectoryOperatorSet{
{
Avs: config.avsAddress,
Expand Down Expand Up @@ -280,7 +280,7 @@ func computeAllocations(
}

strategies := getUniqueStrategies(allocations)
strategyTotalMagnitudes, err := parallelGetMagnitudes(strategies, operatorAddress, elReader)
strategyTotalMagnitudes, err := getMagnitudes(strategies, operatorAddress, elReader)
if err != nil {
return nil, nil, eigenSdkUtils.WrapError("failed to get total magnitudes", err)
}
Expand Down Expand Up @@ -346,33 +346,20 @@ func parallelGetAllocatableMagnitudes(
return strategyAllocatableMagnitudes, nil
}

func parallelGetMagnitudes(
func getMagnitudes(
strategies []gethcommon.Address,
operatorAddress gethcommon.Address,
reader elChainReader,
) (map[gethcommon.Address]uint64, error) {
strategyTotalMagnitudes := make(map[gethcommon.Address]uint64, len(strategies))
var wg sync.WaitGroup
errChan := make(chan error, len(strategies))

for _, s := range strategies {
wg.Add(1)
go func(strategy gethcommon.Address) {
defer wg.Done()
magnitude, err := reader.GetLatestTotalMagnitude(&bind.CallOpts{}, operatorAddress, strategy)
if err != nil {
errChan <- err
return
}
strategyTotalMagnitudes[strategy] = magnitude
}(s)
totalMagnitudes, err := reader.GetTotalMagnitudes(&bind.CallOpts{Context: context.Background()}, operatorAddress, strategies)
if err != nil {
return nil, err
}

wg.Wait()
close(errChan)

if len(errChan) > 0 {
return nil, <-errChan // Return the first error encountered
i := 0
for _, strategy := range strategies {
strategyTotalMagnitudes[strategy] = totalMagnitudes[i]
i++
}

return strategyTotalMagnitudes, nil
Expand Down
24 changes: 15 additions & 9 deletions pkg/operator/allocations/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package allocations

import (
"context"
"errors"
"math"
"os"
"testing"
Expand All @@ -13,7 +14,7 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"

"github.com/stretchr/testify/assert"
)

Expand All @@ -37,21 +38,26 @@ func newFakeElChainReader(
}
}

func (f *fakeElChainReader) GetLatestTotalMagnitude(
func (f *fakeElChainReader) GetTotalMagnitudes(
opts *bind.CallOpts,
operator gethcommon.Address,
strategy gethcommon.Address,
) (uint64, error) {
strategyAddresses []gethcommon.Address,
) ([]uint64, error) {
stratMap, ok := f.totalMagnitudeMap[operator]
if !ok {
return initialMagnitude, nil
return []uint64{}, errors.New("operator not found")
}

magnitude, ok := stratMap[strategy]
if !ok {
return initialMagnitude, nil
// iterate over strategyAddresses and return the corresponding magnitudes
magnitudes := make([]uint64, 0, len(strategyAddresses))
for _, strategy := range strategyAddresses {
magnitude, ok := stratMap[strategy]
if !ok {
magnitude = 0
}
magnitudes = append(magnitudes, magnitude)
}
return magnitude, nil
return magnitudes, nil
}

func (f *fakeElChainReader) GetAllocatableMagnitude(
Expand Down
26 changes: 26 additions & 0 deletions pkg/operator/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/big"
"os"
"strconv"

"github.com/Layr-Labs/eigenlayer-cli/pkg/telemetry"
"github.com/Layr-Labs/eigenlayer-cli/pkg/types"
Expand Down Expand Up @@ -157,6 +158,31 @@ 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", "",
func(i int64) error {
if i < 0 {
return errors.New("allocation delay should be non-negative")
}
return nil
},
)
if err != nil {
return types.OperatorConfig{}, err
}

// 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.")
if err != nil {
return types.OperatorConfig{}, err
}

if confirm {
config.AllocationDelay = uint32(allocationDelay)
} else {
return types.OperatorConfig{}, errors.New("operator cancelled")
}

// Prompt for network & set chainId
chainId, err := p.Select("Select your network:", []string{"mainnet", "holesky", "local"})
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/operator_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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 @@ -64,6 +65,7 @@ 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 @@ -73,6 +75,7 @@ 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

0 comments on commit f8cd8dc

Please sign in to comment.