Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
batphonghan committed Nov 30, 2023
1 parent d662db4 commit f967bee
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 27 deletions.
1 change: 1 addition & 0 deletions shared/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion shared/services/stader/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -625,13 +625,16 @@ func (c *Client) SetRewardAddress(operatorRewardAddress common.Address) (api.Set
if err != nil {
return api.SetRewardAddress{}, fmt.Errorf("could not get set-reward-address response: %w", err)
}

var response api.SetRewardAddress
if err := json.Unmarshal(responseBytes, &response); err != nil {
return api.SetRewardAddress{}, fmt.Errorf("could not decode set-reward-address response: %w", err)
}

if response.Error != "" {
return api.SetRewardAddress{}, fmt.Errorf("could not get set-reward-address response: %s", response.Error)
}

return response, nil
}

Expand All @@ -640,13 +643,16 @@ 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)
}

return response, nil
}

Expand All @@ -655,13 +661,16 @@ func (c *Client) CanNodeRepaySd(amountWei *big.Int) (api.CanRepaySDResponse, err
if err != nil {
return api.CanRepaySDResponse{}, fmt.Errorf("could not get CanNodeRepaySd SD: %w", err)
}

var response api.CanRepaySDResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return api.CanRepaySDResponse{}, fmt.Errorf("could not decode CanNodeRepaySd response: %w", err)
}

if response.Error != "" {
return api.CanRepaySDResponse{}, fmt.Errorf("could not can-repay SD: %s", response.Error)
}

return response, nil
}

Expand All @@ -670,13 +679,16 @@ func (c *Client) NodeUtilizeSd(amountWei *big.Int) (api.NodeUtilitySDResponse, e
if err != nil {
return api.NodeUtilitySDResponse{}, fmt.Errorf("could not utilize SD: %w", err)
}

var response api.NodeUtilitySDResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return api.NodeUtilitySDResponse{}, fmt.Errorf("could not decode utilize response: %w", err)
}

if response.Error != "" {
return api.NodeUtilitySDResponse{}, fmt.Errorf("could not utilize SD: %s", response.Error)
}

return response, nil
}

Expand All @@ -685,13 +697,16 @@ func (c *Client) CanNodeUtilizeSd(amountWei *big.Int) (api.CanUtilitySDResponse,
if err != nil {
return api.CanUtilitySDResponse{}, fmt.Errorf("could not utilize SD: %w", err)
}

var response api.CanUtilitySDResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return api.CanUtilitySDResponse{}, fmt.Errorf("could not decode node SD utilize response: %w", err)
}

if response.Error != "" {
return api.CanUtilitySDResponse{}, fmt.Errorf("could not utilize SD: %s", response.Error)
}

return response, nil
}

Expand All @@ -700,13 +715,16 @@ func (c *Client) NodeRepayExcessSd(amountWei *big.Int) (api.NodeRepaySDResponse,
if err != nil {
return api.NodeRepaySDResponse{}, fmt.Errorf("could not repay excess SD: %w", err)
}

var response api.NodeRepaySDResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return api.NodeRepaySDResponse{}, fmt.Errorf("could not decode repay excess response: %w", err)
}

if response.Error != "" {
return api.NodeRepaySDResponse{}, fmt.Errorf("could not repay excess SD: %s", response.Error)
}

return response, nil
}

Expand All @@ -715,10 +733,12 @@ func (c *Client) CanRepayExcessSD(amountWei *big.Int) (api.CanRepayExcessSDRespo
if err != nil {
return api.CanRepayExcessSDResponse{}, fmt.Errorf("could not repay excess SD: %w", err)
}

var response api.CanRepayExcessSDResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return api.CanRepayExcessSDResponse{}, fmt.Errorf("could not decode node repay SD excess response: %w", err)
}

if response.Error != "" {
return api.CanRepayExcessSDResponse{}, fmt.Errorf("could not repay excess SD: %s", response.Error)
}
Expand All @@ -730,12 +750,15 @@ func (c *Client) GetSDStatus() (api.GetSdStatusResponse, error) {
if err != nil {
return api.GetSdStatusResponse{}, fmt.Errorf("could not get-sd-status: %w", err)
}

var response api.GetSdStatusResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return api.GetSdStatusResponse{}, fmt.Errorf("could not decode node get SD status response: %w", err)
}

if response.Error != "" {
return api.GetSdStatusResponse{}, fmt.Errorf("could not get SD status: %s", response.Error)
}

return response, nil
}
6 changes: 1 addition & 5 deletions stader-cli/node/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion stader-cli/validator/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func nodeDeposit(c *cli.Context) error {
log.ColorBlue,
status.AccountAddress,
log.ColorReset,
math.RoundDown(eth.WeiToEth(status.AccountBalances.Sd), 18))
math.RoundDown(eth.WeiToEth(status.AccountBalances.Sd), Decimal))

canNodeDepositResponse, err := staderClient.CanNodeDeposit(baseAmount, big.NewInt(0), big.NewInt(int64(numValidators)), true)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions stader-lib/stader/stader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 0 additions & 14 deletions stader/api/node/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -191,15 +190,13 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) {
// Run
api.PrintResponse(utilitySd(c, amountWei))
return nil

},
},
{
Name: "can-utilize-sd",
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
Expand All @@ -208,19 +205,16 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) {
if err != nil {
return err
}

// Run
api.PrintResponse(canUtilitySd(c, amountWei))
return nil

},
},
{
Name: "repay-sd",
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
Expand All @@ -233,15 +227,13 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) {
// Run
api.PrintResponse(repaySD(c, amountWei))
return nil

},
},
{
Name: "can-repay-sd",
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
Expand All @@ -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
Expand Down Expand Up @@ -792,7 +781,6 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) {
// Run
api.PrintResponse(SetRewardAddress(c, operatorRewardAddress))
return nil

},
},

Expand All @@ -801,15 +789,13 @@ 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
}
// Run
api.PrintResponse(getSDStatus(c))
return nil

},
},
},
Expand Down
5 changes: 3 additions & 2 deletions stader/api/node/sd-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ 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
}

totalValidatorNonTerminalKeys, err := node.GetTotalNonTerminalValidatorKeys(prn, nodeAccount.Address, totalValidatorKeys, nil)
if err != nil {
return nil, err
Expand Down
3 changes: 0 additions & 3 deletions stader/api/node/utility-sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func utilitySd(c *cli.Context, amountWei *big.Int) (*api.NodeUtilitySDResponse, error) {

// Get services
if err := services.RequireNodeWallet(c); err != nil {
return nil, err
Expand Down Expand Up @@ -44,7 +43,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) {
Expand Down Expand Up @@ -79,5 +77,4 @@ func canUtilitySd(c *cli.Context, amountWei *big.Int) (*api.CanUtilitySDResponse
response.GasInfo = gasInfo

return &response, nil

}
1 change: 0 additions & 1 deletion stader/api/validator/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f967bee

Please sign in to comment.