From 8a994ea218cd3db5c47c656c708dce8a453d8962 Mon Sep 17 00:00:00 2001 From: batphonghan Date: Thu, 30 Nov 2023 16:40:47 +0700 Subject: [PATCH] Refactor --- shared/services/services.go | 1 + shared/services/stader/node.go | 4 +++- stader-cli/node/commands.go | 6 +----- stader-lib/stader/stader.go | 1 + stader/api/node/commands.go | 14 -------------- stader/api/node/sd-status.go | 4 ++-- stader/api/node/utility-sd.go | 1 - stader/api/validator/deposit.go | 1 - 8 files changed, 8 insertions(+), 24 deletions(-) diff --git a/shared/services/services.go b/shared/services/services.go index da6e13eac..3c4294d6f 100644 --- a/shared/services/services.go +++ b/shared/services/services.go @@ -436,6 +436,7 @@ func GetSdUtilityContract(c *cli.Context) (*stader.SDUtilityPoolContractManager, if err != nil { return nil, err } + ec, err := getEthClient(c, cfg) if err != nil { return nil, err diff --git a/shared/services/stader/node.go b/shared/services/stader/node.go index 1029cba90..1c526edde 100644 --- a/shared/services/stader/node.go +++ b/shared/services/stader/node.go @@ -207,7 +207,7 @@ func (c *Client) GetNodeDepositSdAllowance() (api.NodeDepositSdAllowanceResponse } // Check whether the node can make a deposit -func (c *Client) CanNodeDeposit(amountWei, amountUtilityWei *big.Int, numValidators *big.Int, reloadKeys bool) (api.CanNodeDepositResponse, error) { +func (c *Client) CanNodeDeposit(amountWei, amountUtilityWei, numValidators *big.Int, reloadKeys bool) (api.CanNodeDepositResponse, error) { responseBytes, err := c.callAPI(fmt.Sprintf("validator can-deposit %s %s %s %t", amountWei.String(), amountUtilityWei.String(), numValidators, reloadKeys)) if err != nil { return api.CanNodeDepositResponse{}, fmt.Errorf("could not get can validator deposit status: %w", err) @@ -640,10 +640,12 @@ func (c *Client) NodeRepaySd(amountWei *big.Int) (api.NodeRepaySDResponse, error if err != nil { return api.NodeRepaySDResponse{}, fmt.Errorf("could not repay SD: %w", err) } + var response api.NodeRepaySDResponse if err := json.Unmarshal(responseBytes, &response); err != nil { return api.NodeRepaySDResponse{}, fmt.Errorf("could not decode repay node SD response: %w", err) } + if response.Error != "" { return api.NodeRepaySDResponse{}, fmt.Errorf("could not repay SD: %s", response.Error) } diff --git a/stader-cli/node/commands.go b/stader-cli/node/commands.go index 77b90443b..7b221242a 100644 --- a/stader-cli/node/commands.go +++ b/stader-cli/node/commands.go @@ -379,9 +379,7 @@ func RegisterCommands(app *cli.App, name string, aliases []string) { Usage: "Automatically confirm SD utilize", }, }, - Action: func(c *cli.Context) error { - return utilizeSD(c) - }, + Action: utilizeSD, }, { Name: "repay-sd", @@ -399,7 +397,6 @@ func RegisterCommands(app *cli.App, name string, aliases []string) { }, }, Action: func(c *cli.Context) error { - if _, err := cliutils.ValidatePositiveEthAmount("sd repay amount", c.String("amount")); err != nil { return err } @@ -424,7 +421,6 @@ func RegisterCommands(app *cli.App, name string, aliases []string) { }, }, Action: func(c *cli.Context) error { - if _, err := cliutils.ValidatePositiveEthAmount("sd repay amount", c.String("amount")); err != nil { return err } diff --git a/stader-lib/stader/stader.go b/stader-lib/stader/stader.go index f165b3a5d..a0b80a1bc 100644 --- a/stader-lib/stader/stader.go +++ b/stader-lib/stader/stader.go @@ -468,6 +468,7 @@ func NewSDUtilityPool(client ExecutionClient, sdUtilizeAddress common.Address) ( if err != nil { return nil, err } + sdUtilizeContract := &Contract{ Contract: bind.NewBoundContract(sdUtilizeAddress, sdUtilizeAbi, client, client, client), Address: &sdUtilizeAddress, diff --git a/stader/api/node/commands.go b/stader/api/node/commands.go index becb2fe84..a6b2ebbe5 100644 --- a/stader/api/node/commands.go +++ b/stader/api/node/commands.go @@ -178,7 +178,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { Usage: "Utilize SD", UsageText: "stader-cli api node utilize-sd amount", Action: func(c *cli.Context) error { - // Validate args if err := cliutils.ValidateArgCount(c, 1); err != nil { return err @@ -191,7 +190,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { // Run api.PrintResponse(utilitySd(c, amountWei)) return nil - }, }, { @@ -199,7 +197,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { Usage: "Utilize SD", UsageText: "stader-cli api node can-utilize-sd amount", Action: func(c *cli.Context) error { - // Validate args if err := cliutils.ValidateArgCount(c, 1); err != nil { return err @@ -208,11 +205,9 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { if err != nil { return err } - // Run api.PrintResponse(canUtilitySd(c, amountWei)) return nil - }, }, { @@ -220,7 +215,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { Usage: "Utilize SD", UsageText: "stader-cli api node repay-sd amount", Action: func(c *cli.Context) error { - // Validate args if err := cliutils.ValidateArgCount(c, 1); err != nil { return err @@ -233,7 +227,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { // Run api.PrintResponse(repaySD(c, amountWei)) return nil - }, }, { @@ -241,7 +234,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { Usage: "Can repay SD", UsageText: "stader-cli api node can-repay-sd amount", Action: func(c *cli.Context) error { - // Validate args if err := cliutils.ValidateArgCount(c, 1); err != nil { return err @@ -254,17 +246,14 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { // Run api.PrintResponse(canRepaySD(c, amountWei)) return nil - }, }, - { Name: "wait-and-deposit-sd", Aliases: []string{"k2"}, Usage: "Deposit SD against the node, waiting for approval tx-hash to be included in a block first", UsageText: "stader-cli api node wait-and-deposit-sd amount tx-hash", Action: func(c *cli.Context) error { - // Validate args if err := cliutils.ValidateArgCount(c, 2); err != nil { return err @@ -792,7 +781,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { // Run api.PrintResponse(SetRewardAddress(c, operatorRewardAddress)) return nil - }, }, @@ -801,7 +789,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { Usage: "Get SD Status", UsageText: "stader-cli api node get-sd-status", Action: func(c *cli.Context) error { - // Validate args if err := cliutils.ValidateArgCount(c, 0); err != nil { return err @@ -809,7 +796,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) { // Run api.PrintResponse(getSDStatus(c)) return nil - }, }, }, diff --git a/stader/api/node/sd-status.go b/stader/api/node/sd-status.go index 951138630..8c1e18624 100644 --- a/stader/api/node/sd-status.go +++ b/stader/api/node/sd-status.go @@ -44,12 +44,12 @@ func getSDStatus(c *cli.Context) (*api.GetSdStatusResponse, error) { return nil, err } - operatorId, err := node.GetOperatorId(prn, nodeAccount.Address, nil) + operatorID, err := node.GetOperatorId(prn, nodeAccount.Address, nil) if err != nil { return nil, err } - totalValidatorKeys, err := node.GetTotalValidatorKeys(prn, operatorId, nil) + totalValidatorKeys, err := node.GetTotalValidatorKeys(prn, operatorID, nil) if err != nil { return nil, err } diff --git a/stader/api/node/utility-sd.go b/stader/api/node/utility-sd.go index b47290e1f..b265b4a42 100644 --- a/stader/api/node/utility-sd.go +++ b/stader/api/node/utility-sd.go @@ -44,7 +44,6 @@ func utilitySd(c *cli.Context, amountWei *big.Int) (*api.NodeUtilitySDResponse, response.TxHash = tx.Hash() return &response, nil - } func canUtilitySd(c *cli.Context, amountWei *big.Int) (*api.CanUtilitySDResponse, error) { diff --git a/stader/api/validator/deposit.go b/stader/api/validator/deposit.go index 81da98c94..87851e487 100644 --- a/stader/api/validator/deposit.go +++ b/stader/api/validator/deposit.go @@ -29,7 +29,6 @@ func GetSDStatus( operatorAddress common.Address, totalValidatorsPostAddition *big.Int, ) (*api.SdStatusResponse, error) { - sdUtilityBalance, err := sd_utility.GetUtilizerLatestBalance(sdu, operatorAddress, nil) if err != nil { return nil, err