Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support csv out for show command #275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions pkg/operator/allocations/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package allocations

import (
"context"
"errors"
"fmt"
"math/big"
"sort"
"strings"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common/flags"
Expand All @@ -22,11 +24,6 @@ import (
"github.com/urfave/cli/v2"
)

var (
// PrecisionFactor comes from the allocation manager contract
PrecisionFactor = big.NewInt(1e18)
)

func ShowCmd(p utils.Prompter) *cli.Command {
showCmd := &cli.Command{
Name: "show",
Expand Down Expand Up @@ -197,7 +194,18 @@ func showAction(cCtx *cli.Context, p utils.Prompter) error {
if config.outputType == string(common.OutputType_Json) {
slashableMagnitudeHolders.PrintJSON()
} else {
slashableMagnitudeHolders.PrintPretty()
if !common.IsEmptyString(config.output) {
if !strings.HasSuffix(config.output, ".csv") {
return errors.New("output file must be a .csv file")
}
err = slashableMagnitudeHolders.WriteToCSV(config.output)
if err != nil {
return err
}
logger.Infof("Allocation state written to file: %s", config.output)
} else {
slashableMagnitudeHolders.PrintPretty()
}
}

if len(dergisteredOpsets) > 0 {
Expand Down
80 changes: 70 additions & 10 deletions pkg/operator/allocations/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package allocations

import (
"encoding/csv"
"encoding/json"
"fmt"
"math/big"
"os"
"reflect"
"strings"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
Expand Down Expand Up @@ -139,16 +142,73 @@ type showConfig struct {
type SlashableMagnitudeHolders []SlashableMagnitudesHolder

type SlashableMagnitudesHolder struct {
StrategyAddress gethcommon.Address
AVSAddress gethcommon.Address
OperatorSetId uint32
SlashableMagnitude uint64
NewMagnitude uint64
NewAllocationShares *big.Int
UpdateBlock uint32
Shares *big.Int
SharesPercentage string
UpcomingSharesPercentage string
StrategyAddress gethcommon.Address `csv:"strategy_address"`
AVSAddress gethcommon.Address `csv:"avs_address"`
OperatorSetId uint32 `csv:"operator_set_id"`
SlashableMagnitude uint64 `csv:"-"`
NewMagnitude uint64 `csv:"-"`
Shares *big.Int `csv:"shares"`
SharesPercentage string `csv:"shares_percentage"`
NewAllocationShares *big.Int `csv:"new_allocation_shares"`
UpcomingSharesPercentage string `csv:"upcoming_shares_percentage"`
UpdateBlock uint32 `csv:"update_block"`
}

func (s SlashableMagnitudeHolders) WriteToCSV(filePath string) error {
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create file: %v", err)
}
defer file.Close()

writer := csv.NewWriter(file)
defer writer.Flush()

// Get fields and their CSV names, excluding skipped fields
var headers []string
var fieldIndices []int
val := reflect.ValueOf(SlashableMagnitudesHolder{})
typ := val.Type()

for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
csvTag := field.Tag.Get("csv")

// Skip if tag is "-"
if csvTag == "-" {
continue
}

// Use tag value if present, otherwise use field name
if csvTag != "" {
headers = append(headers, csvTag)
} else {
headers = append(headers, field.Name)
}
fieldIndices = append(fieldIndices, i)
}

// Write headers
if err := writer.Write(headers); err != nil {
return fmt.Errorf("failed to write headers: %v", err)
}

// Write data rows
for _, eachRow := range s {
val := reflect.ValueOf(eachRow)
row := make([]string, len(fieldIndices))
// Only include non-skipped fields
for i, fieldIndex := range fieldIndices {
field := val.Field(fieldIndex)
row[i] = fmt.Sprintf("%v", field.Interface())
}

if err := writer.Write(row); err != nil {
return fmt.Errorf("failed to write row: %v", err)
}
}

return nil
}

func (s SlashableMagnitudeHolders) PrintPretty() {
Expand Down
Loading