|
1 | 1 | package allocations
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/csv" |
4 | 5 | "encoding/json"
|
5 | 6 | "fmt"
|
6 | 7 | "math/big"
|
| 8 | + "os" |
| 9 | + "reflect" |
7 | 10 | "strings"
|
8 | 11 |
|
9 | 12 | "github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common"
|
@@ -139,16 +142,73 @@ type showConfig struct {
|
139 | 142 | type SlashableMagnitudeHolders []SlashableMagnitudesHolder
|
140 | 143 |
|
141 | 144 | type SlashableMagnitudesHolder struct {
|
142 |
| - StrategyAddress gethcommon.Address |
143 |
| - AVSAddress gethcommon.Address |
144 |
| - OperatorSetId uint32 |
145 |
| - SlashableMagnitude uint64 |
146 |
| - NewMagnitude uint64 |
147 |
| - NewAllocationShares *big.Int |
148 |
| - UpdateBlock uint32 |
149 |
| - Shares *big.Int |
150 |
| - SharesPercentage string |
151 |
| - UpcomingSharesPercentage string |
| 145 | + StrategyAddress gethcommon.Address `csv:"strategy_address"` |
| 146 | + AVSAddress gethcommon.Address `csv:"avs_address"` |
| 147 | + OperatorSetId uint32 `csv:"operator_set_id"` |
| 148 | + SlashableMagnitude uint64 `csv:"-"` |
| 149 | + NewMagnitude uint64 `csv:"-"` |
| 150 | + Shares *big.Int `csv:"shares"` |
| 151 | + SharesPercentage string `csv:"shares_percentage"` |
| 152 | + NewAllocationShares *big.Int `csv:"new_allocation_shares"` |
| 153 | + UpcomingSharesPercentage string `csv:"upcoming_shares_percentage"` |
| 154 | + UpdateBlock uint32 `csv:"update_block"` |
| 155 | +} |
| 156 | + |
| 157 | +func (s SlashableMagnitudeHolders) WriteToCSV(filePath string) error { |
| 158 | + file, err := os.Create(filePath) |
| 159 | + if err != nil { |
| 160 | + return fmt.Errorf("failed to create file: %v", err) |
| 161 | + } |
| 162 | + defer file.Close() |
| 163 | + |
| 164 | + writer := csv.NewWriter(file) |
| 165 | + defer writer.Flush() |
| 166 | + |
| 167 | + // Get fields and their CSV names, excluding skipped fields |
| 168 | + var headers []string |
| 169 | + var fieldIndices []int |
| 170 | + val := reflect.ValueOf(SlashableMagnitudesHolder{}) |
| 171 | + typ := val.Type() |
| 172 | + |
| 173 | + for i := 0; i < typ.NumField(); i++ { |
| 174 | + field := typ.Field(i) |
| 175 | + csvTag := field.Tag.Get("csv") |
| 176 | + |
| 177 | + // Skip if tag is "-" |
| 178 | + if csvTag == "-" { |
| 179 | + continue |
| 180 | + } |
| 181 | + |
| 182 | + // Use tag value if present, otherwise use field name |
| 183 | + if csvTag != "" { |
| 184 | + headers = append(headers, csvTag) |
| 185 | + } else { |
| 186 | + headers = append(headers, field.Name) |
| 187 | + } |
| 188 | + fieldIndices = append(fieldIndices, i) |
| 189 | + } |
| 190 | + |
| 191 | + // Write headers |
| 192 | + if err := writer.Write(headers); err != nil { |
| 193 | + return fmt.Errorf("failed to write headers: %v", err) |
| 194 | + } |
| 195 | + |
| 196 | + // Write data rows |
| 197 | + for _, eachRow := range s { |
| 198 | + val := reflect.ValueOf(eachRow) |
| 199 | + row := make([]string, len(fieldIndices)) |
| 200 | + // Only include non-skipped fields |
| 201 | + for i, fieldIndex := range fieldIndices { |
| 202 | + field := val.Field(fieldIndex) |
| 203 | + row[i] = fmt.Sprintf("%v", field.Interface()) |
| 204 | + } |
| 205 | + |
| 206 | + if err := writer.Write(row); err != nil { |
| 207 | + return fmt.Errorf("failed to write row: %v", err) |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return nil |
152 | 212 | }
|
153 | 213 |
|
154 | 214 | func (s SlashableMagnitudeHolders) PrintPretty() {
|
|
0 commit comments