|
| 1 | +package operator |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common" |
| 8 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common/flags" |
| 9 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/operator/split" |
| 10 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/rewards" |
| 11 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/telemetry" |
| 12 | + "github.com/Layr-Labs/eigenlayer-cli/pkg/utils" |
| 13 | + "github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts" |
| 14 | + "github.com/Layr-Labs/eigensdk-go/logging" |
| 15 | + eigenSdkUtils "github.com/Layr-Labs/eigensdk-go/utils" |
| 16 | + gethcommon "github.com/ethereum/go-ethereum/common" |
| 17 | + "github.com/ethereum/go-ethereum/ethclient" |
| 18 | + "github.com/urfave/cli/v2" |
| 19 | +) |
| 20 | + |
| 21 | +func SetOperatorSplitCmd(p utils.Prompter) *cli.Command { |
| 22 | + var operatorSplitCmd = &cli.Command{ |
| 23 | + Name: "set-rewards-split", |
| 24 | + Usage: "Set operator rewards split", |
| 25 | + Action: func(cCtx *cli.Context) error { |
| 26 | + return SetOperatorSplit(cCtx, p) |
| 27 | + }, |
| 28 | + After: telemetry.AfterRunAction(), |
| 29 | + Flags: getSetOperatorSplitFlags(), |
| 30 | + } |
| 31 | + |
| 32 | + return operatorSplitCmd |
| 33 | +} |
| 34 | + |
| 35 | +func SetOperatorSplit(cCtx *cli.Context, p utils.Prompter) error { |
| 36 | + ctx := cCtx.Context |
| 37 | + logger := common.GetLogger(cCtx) |
| 38 | + |
| 39 | + config, err := readAndValidateSetOperatorSplitConfig(cCtx, logger) |
| 40 | + if err != nil { |
| 41 | + return eigenSdkUtils.WrapError("failed to read and validate operator split config", err) |
| 42 | + } |
| 43 | + |
| 44 | + cCtx.App.Metadata["network"] = config.ChainID.String() |
| 45 | + |
| 46 | + ethClient, err := ethclient.Dial(config.RPCUrl) |
| 47 | + if err != nil { |
| 48 | + return eigenSdkUtils.WrapError("failed to create new eth client", err) |
| 49 | + } |
| 50 | + |
| 51 | + if config.Broadcast { |
| 52 | + |
| 53 | + eLWriter, err := common.GetELWriter( |
| 54 | + config.OperatorAddress, |
| 55 | + config.SignerConfig, |
| 56 | + ethClient, |
| 57 | + elcontracts.Config{ |
| 58 | + RewardsCoordinatorAddress: config.RewardsCoordinatorAddress, |
| 59 | + }, |
| 60 | + p, |
| 61 | + config.ChainID, |
| 62 | + logger, |
| 63 | + ) |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + return eigenSdkUtils.WrapError("failed to get EL writer", err) |
| 67 | + } |
| 68 | + |
| 69 | + logger.Infof("Broadcasting set operator transaction...") |
| 70 | + |
| 71 | + receipt, err := eLWriter.SetOperatorAVSSplit(ctx, config.OperatorAddress, config.AVSAddress, config.Split, true) |
| 72 | + |
| 73 | + if err != nil { |
| 74 | + return eigenSdkUtils.WrapError("failed to process claim", err) |
| 75 | + } |
| 76 | + |
| 77 | + logger.Infof("Set operator transaction submitted successfully") |
| 78 | + common.PrintTransactionInfo(receipt.TxHash.String(), config.ChainID) |
| 79 | + } else { |
| 80 | + noSendTxOpts := common.GetNoSendTxOpts(config.OperatorAddress) |
| 81 | + _, _, contractBindings, err := elcontracts.BuildClients(elcontracts.Config{ |
| 82 | + RewardsCoordinatorAddress: config.RewardsCoordinatorAddress, |
| 83 | + }, ethClient, nil, logger, nil) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + code, err := ethClient.CodeAt(ctx, config.OperatorAddress, nil) |
| 89 | + if err != nil { |
| 90 | + return eigenSdkUtils.WrapError("failed to get code at address", err) |
| 91 | + } |
| 92 | + if len(code) > 0 { |
| 93 | + // Operator is a smart contract |
| 94 | + noSendTxOpts.GasLimit = 150_000 |
| 95 | + } |
| 96 | + |
| 97 | + unsignedTx, err := contractBindings.RewardsCoordinator.SetOperatorAVSSplit(noSendTxOpts, config.OperatorAddress, config.AVSAddress, config.Split) |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + return eigenSdkUtils.WrapError("failed to create unsigned tx", err) |
| 101 | + } |
| 102 | + if config.OutputType == string(common.OutputType_Calldata) { |
| 103 | + calldataHex := gethcommon.Bytes2Hex(unsignedTx.Data()) |
| 104 | + |
| 105 | + if !common.IsEmptyString(config.OutputFile) { |
| 106 | + err = common.WriteToFile([]byte(calldataHex), config.OutputFile) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + logger.Infof("Call data written to file: %s", config.OutputFile) |
| 111 | + } else { |
| 112 | + fmt.Println(calldataHex) |
| 113 | + } |
| 114 | + } else { |
| 115 | + logger.Infof("This transaction would set the operator split to %d", config.Split) |
| 116 | + } |
| 117 | + |
| 118 | + if !config.IsSilent { |
| 119 | + txFeeDetails := common.GetTxFeeDetails(unsignedTx) |
| 120 | + fmt.Println() |
| 121 | + txFeeDetails.Print() |
| 122 | + |
| 123 | + fmt.Println("To broadcast the operator set split, use the --broadcast flag") |
| 124 | + } |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func getSetOperatorSplitFlags() []cli.Flag { |
| 130 | + baseFlags := []cli.Flag{ |
| 131 | + &flags.NetworkFlag, |
| 132 | + &flags.ETHRpcUrlFlag, |
| 133 | + &flags.OperatorAddressFlag, |
| 134 | + &split.OperatorSplitFlag, |
| 135 | + &rewards.RewardsCoordinatorAddressFlag, |
| 136 | + &split.AVSAddressFlag, |
| 137 | + &flags.BroadcastFlag, |
| 138 | + &flags.OutputTypeFlag, |
| 139 | + &flags.OutputFileFlag, |
| 140 | + &flags.SilentFlag, |
| 141 | + } |
| 142 | + |
| 143 | + allFlags := append(baseFlags, flags.GetSignerFlags()...) |
| 144 | + sort.Sort(cli.FlagsByName(allFlags)) |
| 145 | + return allFlags |
| 146 | +} |
| 147 | + |
| 148 | +func readAndValidateSetOperatorSplitConfig( |
| 149 | + cCtx *cli.Context, |
| 150 | + logger logging.Logger, |
| 151 | +) (*split.SetOperatorAVSSplitConfig, error) { |
| 152 | + network := cCtx.String(flags.NetworkFlag.Name) |
| 153 | + rpcUrl := cCtx.String(flags.ETHRpcUrlFlag.Name) |
| 154 | + opSplit := cCtx.Int(split.OperatorSplitFlag.Name) |
| 155 | + broadcast := cCtx.Bool(flags.BroadcastFlag.Name) |
| 156 | + outputType := cCtx.String(flags.OutputTypeFlag.Name) |
| 157 | + outputFile := cCtx.String(flags.OutputFileFlag.Name) |
| 158 | + isSilent := cCtx.Bool(flags.SilentFlag.Name) |
| 159 | + |
| 160 | + rewardsCoordinatorAddress := cCtx.String(rewards.RewardsCoordinatorAddressFlag.Name) |
| 161 | + |
| 162 | + var err error |
| 163 | + if common.IsEmptyString(rewardsCoordinatorAddress) { |
| 164 | + rewardsCoordinatorAddress, err = common.GetRewardCoordinatorAddress(utils.NetworkNameToChainId(network)) |
| 165 | + if err != nil { |
| 166 | + return nil, err |
| 167 | + } |
| 168 | + } |
| 169 | + logger.Debugf("Using Rewards Coordinator address: %s", rewardsCoordinatorAddress) |
| 170 | + |
| 171 | + operatorAddress := gethcommon.HexToAddress(cCtx.String(flags.OperatorAddressFlag.Name)) |
| 172 | + logger.Infof("Using operator address: %s", operatorAddress.String()) |
| 173 | + |
| 174 | + avsAddress := gethcommon.HexToAddress(cCtx.String(split.AVSAddressFlag.Name)) |
| 175 | + logger.Infof("Using AVS address: %s", avsAddress.String()) |
| 176 | + |
| 177 | + chainID := utils.NetworkNameToChainId(network) |
| 178 | + logger.Debugf("Using chain ID: %s", chainID.String()) |
| 179 | + |
| 180 | + // Get SignerConfig |
| 181 | + signerConfig, err := common.GetSignerConfig(cCtx, logger) |
| 182 | + if err != nil { |
| 183 | + // We don't want to throw error since people can still use it to generate the claim |
| 184 | + // without broadcasting it |
| 185 | + logger.Debugf("Failed to get signer config: %s", err) |
| 186 | + } |
| 187 | + |
| 188 | + return &split.SetOperatorAVSSplitConfig{ |
| 189 | + Network: network, |
| 190 | + RPCUrl: rpcUrl, |
| 191 | + RewardsCoordinatorAddress: gethcommon.HexToAddress(rewardsCoordinatorAddress), |
| 192 | + ChainID: chainID, |
| 193 | + SignerConfig: signerConfig, |
| 194 | + OperatorAddress: operatorAddress, |
| 195 | + AVSAddress: avsAddress, |
| 196 | + Split: uint16(opSplit), |
| 197 | + Broadcast: broadcast, |
| 198 | + OutputType: outputType, |
| 199 | + OutputFile: outputFile, |
| 200 | + IsSilent: isSilent, |
| 201 | + }, nil |
| 202 | +} |
0 commit comments