From 482ea0e1a8708aa0f4a3711b20139ce3b1aab46e Mon Sep 17 00:00:00 2001 From: Madhur Shrimal Date: Mon, 26 Aug 2024 22:23:23 -0700 Subject: [PATCH] chore: clean up and tests --- pkg/rewards/claim.go | 27 ++++++---------------- pkg/rewards/claim_test.go | 36 +++++++++++++++++++++++++++++ pkg/rewards/setclaimer.go | 15 ------------ pkg/rewards/show.go | 10 -------- pkg/rewards/types.go | 48 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 90 insertions(+), 46 deletions(-) diff --git a/pkg/rewards/claim.go b/pkg/rewards/claim.go index 8b929b5..6aa9de3 100644 --- a/pkg/rewards/claim.go +++ b/pkg/rewards/claim.go @@ -14,7 +14,6 @@ import ( "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/types" "github.com/Layr-Labs/eigenlayer-cli/pkg/utils" contractrewardscoordinator "github.com/Layr-Labs/eigenlayer-contracts/pkg/bindings/IRewardsCoordinator" @@ -36,22 +35,11 @@ import ( "github.com/urfave/cli/v2" ) -type ClaimConfig struct { - Network string - RPCUrl string - EarnerAddress gethcommon.Address - RecipientAddress gethcommon.Address - ClaimerAddress gethcommon.Address - Output string - OutputType string - Broadcast bool - TokenAddresses []gethcommon.Address - RewardsCoordinatorAddress gethcommon.Address - ClaimTimestamp string - ChainID *big.Int - ProofStoreBaseURL string - Environment string - SignerConfig *types.SignerConfig +type elChainReader interface { + GetDistributionRootsLength(opts *bind.CallOpts) (*big.Int, error) + GetRootIndexFromHash(opts *bind.CallOpts, hash [32]byte) (uint32, error) + GetCurrentClaimableDistributionRoot(opts *bind.CallOpts) (rewardscoordinator.IRewardsCoordinatorDistributionRoot, error) + CurrRewardsCalculationEndTimestamp(opts *bind.CallOpts) (uint32, error) } func ClaimCmd(p utils.Prompter) *cli.Command { @@ -123,7 +111,7 @@ func Claim(cCtx *cli.Context, p utils.Prompter) error { http.DefaultClient, ) - claimDate, rootIndex, err := getClaimDistributionRoot(ctx, config.ClaimTimestamp, df, elReader, logger) + claimDate, rootIndex, err := getClaimDistributionRoot(ctx, config.ClaimTimestamp, elReader, logger) if err != nil { return eigenSdkUtils.WrapError("failed to get claim distribution root", err) } @@ -275,8 +263,7 @@ func Claim(cCtx *cli.Context, p utils.Prompter) error { func getClaimDistributionRoot( ctx context.Context, claimTimestamp string, - df *httpProofDataFetcher.HttpProofDataFetcher, - elReader *elcontracts.ChainReader, + elReader elChainReader, logger logging.Logger, ) (string, uint32, error) { if claimTimestamp == "latest" { diff --git a/pkg/rewards/claim_test.go b/pkg/rewards/claim_test.go index ba8b143..3eec707 100644 --- a/pkg/rewards/claim_test.go +++ b/pkg/rewards/claim_test.go @@ -52,3 +52,39 @@ func TestReadAndValidateConfig_RecipientProvided(t *testing.T) { assert.NoError(t, err) assert.Equal(t, common.HexToAddress(recipientAddress), config.RecipientAddress) } + +func TestReadAndValidateConfig_NoClaimerProvided(t *testing.T) { + earnerAddress := testutils.GenerateRandomEthereumAddressString() + fs := flag.NewFlagSet("test", flag.ContinueOnError) + fs.String(flags.ETHRpcUrlFlag.Name, "rpc", "") + fs.String(EarnerAddressFlag.Name, earnerAddress, "") + fs.String(RewardsCoordinatorAddressFlag.Name, "0x1234", "") + fs.String(ClaimTimestampFlag.Name, "latest", "") + fs.String(ProofStoreBaseURLFlag.Name, "dummy-url", "") + cliCtx := cli.NewContext(nil, fs, nil) + + logger := logging.NewJsonSLogger(os.Stdout, &logging.SLoggerOptions{}) + + config, err := readAndValidateClaimConfig(cliCtx, logger) + + assert.NoError(t, err) + assert.Equal(t, common.HexToAddress(earnerAddress), config.ClaimerAddress) +} + +func TestReadAndValidateConfig_ClaimerProvided(t *testing.T) { + claimerAddress := testutils.GenerateRandomEthereumAddressString() + fs := flag.NewFlagSet("test", flag.ContinueOnError) + fs.String(flags.ETHRpcUrlFlag.Name, "rpc", "") + fs.String(ClaimerAddressFlag.Name, claimerAddress, "") + fs.String(RewardsCoordinatorAddressFlag.Name, "0x1234", "") + fs.String(ClaimTimestampFlag.Name, "latest", "") + fs.String(ProofStoreBaseURLFlag.Name, "dummy-url", "") + cliCtx := cli.NewContext(nil, fs, nil) + + logger := logging.NewJsonSLogger(os.Stdout, &logging.SLoggerOptions{}) + + config, err := readAndValidateClaimConfig(cliCtx, logger) + + assert.NoError(t, err) + assert.Equal(t, common.HexToAddress(claimerAddress), config.ClaimerAddress) +} diff --git a/pkg/rewards/setclaimer.go b/pkg/rewards/setclaimer.go index 629e42c..829c5c2 100644 --- a/pkg/rewards/setclaimer.go +++ b/pkg/rewards/setclaimer.go @@ -3,13 +3,11 @@ package rewards import ( "context" "fmt" - "math/big" "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/types" "github.com/Layr-Labs/eigenlayer-cli/pkg/utils" "github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts" @@ -23,19 +21,6 @@ import ( "github.com/urfave/cli/v2" ) -type SetClaimerConfig struct { - ClaimerAddress gethcommon.Address - Network string - RPCUrl string - Broadcast bool - RewardsCoordinatorAddress gethcommon.Address - ChainID *big.Int - SignerConfig *types.SignerConfig - EarnerAddress gethcommon.Address - Output string - OutputType string -} - func SetClaimerCmd(p utils.Prompter) *cli.Command { setClaimerCmd := &cli.Command{ Name: "set-claimer", diff --git a/pkg/rewards/show.go b/pkg/rewards/show.go index 7102023..9b04f54 100644 --- a/pkg/rewards/show.go +++ b/pkg/rewards/show.go @@ -40,16 +40,6 @@ const ( GetEarnedTokensForStrategyEndpoint = "grpc/eigenlayer.RewardsService/GetEarnedTokensForStrategy" ) -type ShowConfig struct { - EarnerAddress gethcommon.Address - NumberOfDays int64 - Network string - Environment string - ClaimType ClaimType - ChainID *big.Int - Output string -} - func ShowCmd(p utils.Prompter) *cli.Command { showCmd := &cli.Command{ Name: "show", diff --git a/pkg/rewards/types.go b/pkg/rewards/types.go index d72f8b9..6b0aa02 100644 --- a/pkg/rewards/types.go +++ b/pkg/rewards/types.go @@ -1,6 +1,11 @@ package rewards -import "math/big" +import ( + "math/big" + + "github.com/Layr-Labs/eigenlayer-cli/pkg/types" + gethcommon "github.com/ethereum/go-ethereum/common" +) type RewardResponse struct { Rewards []Reward `json:"rewards"` @@ -37,3 +42,44 @@ type NormalizedUnclaimedReward struct { TokenAddress string `csv:"tokenAddress"` WeiAmount *big.Int `csv:"weiAmount"` } + +type ClaimConfig struct { + Network string + RPCUrl string + EarnerAddress gethcommon.Address + RecipientAddress gethcommon.Address + ClaimerAddress gethcommon.Address + Output string + OutputType string + Broadcast bool + TokenAddresses []gethcommon.Address + RewardsCoordinatorAddress gethcommon.Address + ClaimTimestamp string + ChainID *big.Int + ProofStoreBaseURL string + Environment string + SignerConfig *types.SignerConfig +} + +type SetClaimerConfig struct { + ClaimerAddress gethcommon.Address + Network string + RPCUrl string + Broadcast bool + RewardsCoordinatorAddress gethcommon.Address + ChainID *big.Int + SignerConfig *types.SignerConfig + EarnerAddress gethcommon.Address + Output string + OutputType string +} + +type ShowConfig struct { + EarnerAddress gethcommon.Address + NumberOfDays int64 + Network string + Environment string + ClaimType ClaimType + ChainID *big.Int + Output string +}