Skip to content

Commit

Permalink
add more code
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Sep 18, 2024
1 parent f99f5b5 commit 700d876
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 15 deletions.
4 changes: 4 additions & 0 deletions pkg/internal/common/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ func ConvertStringSliceToGethAddressSlice(addresses []string) []common.Address {
}
return gethAddresses
}

func ShortEthAddress(address common.Address) string {
return fmt.Sprintf("%s...%s", address.Hex()[:6], address.Hex()[len(address.Hex())-4:])
}
10 changes: 10 additions & 0 deletions pkg/internal/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,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 GetTransactionLink(txHash string, chainId *big.Int) string {
chainIDInt := chainId.Int64()
chainMetadata, ok := ChainMetadataMap[chainIDInt]
Expand Down
8 changes: 4 additions & 4 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.delegationManagerAddress = gethcommon.HexToAddress("0xD9DFF502e91aE5887399C8ca11a0708dc1ee1cbf")
config.delegationManagerAddress = gethcommon.HexToAddress("0xec91e43612896E7D45736cE751bea6dbf1BBEdB5")

if config.broadcast {
confirm, err := p.Confirm(
Expand Down Expand Up @@ -80,7 +80,7 @@ func initializeDelayAction(cCtx *cli.Context, p utils.Prompter) error {
return eigenSdkUtils.WrapError("failed to get EL writer", err)
}

receipt, err := eLWriter.InitializeAllocationDelay(ctx, config.allocationDelay, true)
receipt, err := eLWriter.InitializeAllocationDelay(ctx, config.operatorAddress, config.allocationDelay, true)
if err != nil {
return err
}
Expand All @@ -102,7 +102,7 @@ func initializeDelayAction(cCtx *cli.Context, p utils.Prompter) error {
noSendTxOpts.GasLimit = 150_000
}

unsignedTx, err := contractBindings.DelegationManager.InitializeAllocationDelay(noSendTxOpts, config.allocationDelay)
unsignedTx, err := contractBindings.AllocationManager.SetAllocationDelay(noSendTxOpts, config.operatorAddress, config.allocationDelay)
if err != nil {
return eigenSdkUtils.WrapError("failed to create unsigned tx", err)
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func readAndValidateAllocationDelayConfig(c *cli.Context, logger logging.Logger)
logger.Debugf("Failed to get signer config: %s", err)
}

delegationManagerAddress, err := utils.GetDelegationManagerAddress(chainID)
delegationManagerAddress, err := common.GetDelegationManagerAddress(chainID)
if err != nil {
return nil, err
}
Expand Down
82 changes: 78 additions & 4 deletions pkg/operator/allocations/show.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package allocations

import (
"fmt"
"sort"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
Expand Down Expand Up @@ -51,7 +52,7 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
}

// Temp to test modify allocations
config.delegationManagerAddress = gethcommon.HexToAddress("0xD9DFF502e91aE5887399C8ca11a0708dc1ee1cbf")
config.delegationManagerAddress = gethcommon.HexToAddress("0xec91e43612896E7D45736cE751bea6dbf1BBEdB5")

elReader, err := elcontracts.NewReaderFromConfig(
elcontracts.Config{
Expand All @@ -77,7 +78,7 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
logger.Debugf("Allocatable magnitude for strategy %v: %d", strategyAddress, allocatableMagnitude)
}

opSet, slashableMagnitudes, err := elReader.GetCurrentSlashableMagnitudes(
opSets, slashableMagnitudes, err := elReader.GetCurrentSlashableMagnitudes(
&bind.CallOpts{Context: ctx},
config.operatorAddress,
config.strategyAddresses,
Expand All @@ -89,7 +90,7 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
slashableMagnitudeHolders := make(SlashableMagnitudeHolders, 0)
for i, strategyAddress := range config.strategyAddresses {
slashableMagnitude := slashableMagnitudes[i]
for j, opSet := range opSet {
for j, opSet := range opSets {
slashableMagnitudeHolders = append(slashableMagnitudeHolders, SlashableMagnitudesHolder{
StrategyAddress: strategyAddress,
AVSAddress: opSet.Avs,
Expand All @@ -99,6 +100,79 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
}
}

// Get Pending allocations
pendingAllocationsDetails := make(AllocationDetailsHolder, 0)
for _, strategyAddress := range config.strategyAddresses {
pendingAllocations, timestamps, err := elReader.GetPendingAllocations(
&bind.CallOpts{Context: ctx},
config.operatorAddress,
strategyAddress,
opSets,
)
if err != nil {
return eigenSdkUtils.WrapError("failed to get pending allocations", err)
}
for i, opSet := range opSets {
pendingAllocation := pendingAllocations[i]
timestamp := timestamps[i]
if pendingAllocation == 0 && timestamp == 0 {
continue
}
pendingAllocationsDetails = append(pendingAllocationsDetails, AllocationDetails{
StrategyAddress: strategyAddress,
AVSAddress: opSet.Avs,
OperatorSetId: opSet.OperatorSetId,
Allocation: pendingAllocation,
Timestamp: timestamp,
})
}
}

pendingDeallocationsDetails := make(AllocationDetailsHolder, 0)
for _, strategyAddress := range config.strategyAddresses {
pendingDeallocations, err := elReader.GetPendingDeallocations(
&bind.CallOpts{Context: ctx},
config.operatorAddress,
strategyAddress,
opSets,
)
if err != nil {
return eigenSdkUtils.WrapError("failed to get pending deallocations", err)
}
for i, opSet := range opSets {
pendingAllocation := pendingDeallocations[i]
if pendingAllocation.MagnitudeDiff == 0 && pendingAllocation.CompletableTimestamp == 0 {
continue
}
pendingDeallocationsDetails = append(pendingDeallocationsDetails, AllocationDetails{
StrategyAddress: strategyAddress,
AVSAddress: opSet.Avs,
OperatorSetId: opSet.OperatorSetId,
Allocation: pendingAllocation.MagnitudeDiff,
Timestamp: pendingAllocation.CompletableTimestamp,
})
}
}

fmt.Println()
fmt.Println("------------------Pending Allocations---------------------")
if config.outputType == string(common.OutputType_Json) {
pendingAllocationsDetails.PrintJSON()
} else {
pendingAllocationsDetails.PrintPretty()
}
fmt.Println()

fmt.Println()
fmt.Println("------------------Pending Deallocations---------------------")
if config.outputType == string(common.OutputType_Json) {
pendingDeallocationsDetails.PrintJSON()
} else {
pendingDeallocationsDetails.PrintPretty()
}
fmt.Println()

fmt.Println("------------------Current Slashable Magnitudes---------------------")
if config.outputType == string(common.OutputType_Json) {
slashableMagnitudeHolders.PrintJSON()
} else {
Expand All @@ -119,7 +193,7 @@ func readAndValidateShowConfig(cCtx *cli.Context, logger *logging.Logger) (*show
outputType := cCtx.String(flags.OutputTypeFlag.Name)

chainId := utils.NetworkNameToChainId(network)
delegationManagerAddress, err := utils.GetDelegationManagerAddress(chainId)
delegationManagerAddress, err := common.GetDelegationManagerAddress(chainId)
if err != nil {
return nil, err
}
Expand Down
79 changes: 74 additions & 5 deletions pkg/operator/allocations/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package allocations
import (
"encoding/json"
"fmt"
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
"math/big"
"strings"
"time"

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

Expand Down Expand Up @@ -102,7 +104,7 @@ type SlashableMagnitudesHolder struct {
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}
widths := []int{20, 20, 16, 20}

// print dashes
for _, width := range widths {
Expand All @@ -125,8 +127,8 @@ func (s SlashableMagnitudeHolders) PrintPretty() {
// 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[0], common.ShortEthAddress(holder.StrategyAddress),
widths[1], common.ShortEthAddress(holder.AVSAddress),
widths[2], holder.OperatorSetId,
widths[3], holder.SlashableMagnitude)
}
Expand All @@ -139,10 +141,77 @@ func (s SlashableMagnitudeHolders) PrintPretty() {
}

func (s SlashableMagnitudeHolders) PrintJSON() {
json, err := json.MarshalIndent(s, "", " ")
obj, err := json.MarshalIndent(s, "", " ")
if err != nil {
fmt.Println("Error marshalling to JSON:", err)
return
}
fmt.Println(string(json))
fmt.Println(string(obj))
}

type AllocationDetailsHolder []AllocationDetails

type AllocationDetails struct {
StrategyAddress gethcommon.Address
AVSAddress gethcommon.Address
OperatorSetId uint32
Allocation uint64
Timestamp uint32
}

func (a AllocationDetailsHolder) PrintPretty() {
// Define column headers and widths
headers := []string{"Strategy Address", "AVS Address", "Operator Set ID", "Allocation", "Application Timestamp"}
widths := []int{20, 20, 16, 20, 25}

// 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 a {
// Example timestamp (Unix timestamp in seconds)
timestamp := int64(holder.Timestamp)

// Convert timestamp to time.Time
t := time.Unix(timestamp, 0)

// Format the time as a string
formattedTime := t.Format("2006-01-02 15:04:05")
fmt.Printf("| %-*s| %-*s| %-*d| %-*d| %-*s|\n",
widths[0], common.ShortEthAddress(holder.StrategyAddress),
widths[1], common.ShortEthAddress(holder.AVSAddress),
widths[2], holder.OperatorSetId,
widths[3], holder.Allocation,
widths[4], formattedTime)
}

// print dashes
for _, width := range widths {
fmt.Print("+" + strings.Repeat("-", width+1))
}
fmt.Println("+")
}

func (a AllocationDetailsHolder) PrintJSON() {
obj, err := json.MarshalIndent(a, "", " ")
if err != nil {
fmt.Println("Error marshalling to JSON:", err)
return
}
fmt.Println(string(obj))
}
4 changes: 2 additions & 2 deletions pkg/operator/allocations/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func updateAllocations(cCtx *cli.Context, p utils.Prompter) error {
}

// Temp to test modify Allocations
config.delegationManagerAddress = gethcommon.HexToAddress("0xD9DFF502e91aE5887399C8ca11a0708dc1ee1cbf")
config.delegationManagerAddress = gethcommon.HexToAddress("0xec91e43612896E7D45736cE751bea6dbf1BBEdB5")

elReader, err := elcontracts.NewReaderFromConfig(
elcontracts.Config{
Expand Down Expand Up @@ -469,7 +469,7 @@ func readAndValidateUpdateFlags(cCtx *cli.Context, logger logging.Logger) (*upda
csvFilePath := cCtx.String(flags.CSVFileFlag.Name)
chainId := utils.NetworkNameToChainId(network)

delegationManagerAddress, err := utils.GetDelegationManagerAddress(chainId)
delegationManagerAddress, err := common.GetDelegationManagerAddress(chainId)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 700d876

Please sign in to comment.