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 5740f6a commit d662db4
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion shared/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func GetSdUtilityAddress(c *cli.Context) (common.Address, error) {
return common.Address{}, err
}

return stader_config.GetSDUtilityPool(sdcfg, nil)
return stader_config.GetSDUtilityPoolAddress(sdcfg, nil)
}

func GetPermissionlessNodeRegistry(c *cli.Context) (*stader.PermissionlessNodeRegistryContractManager, error) {
Expand Down
11 changes: 5 additions & 6 deletions shared/services/stader/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func (c *Client) GetNodeDepositSdAllowance() (api.NodeDepositSdAllowanceResponse
}

// Check whether the node can make a deposit
func (c *Client) CanNodeDeposit(amountWei *big.Int, numValidators *big.Int, reloadKeys bool) (api.CanNodeDepositResponse, error) {
responseBytes, err := c.callAPI(fmt.Sprintf("validator can-deposit %s %s %t", amountWei.String(), numValidators, reloadKeys))
func (c *Client) CanNodeDeposit(amountWei, amountUtilityWei *big.Int, numValidators *big.Int, reloadKeys bool) (api.CanNodeDepositResponse, error) {

Check failure on line 210 in shared/services/stader/node.go

View workflow job for this annotation

GitHub Actions / build

paramTypeCombine: func(amountWei, amountUtilityWei *big.Int, numValidators *big.Int, reloadKeys bool) (api.CanNodeDepositResponse, error) could be replaced with func(amountWei, amountUtilityWei, numValidators *big.Int, reloadKeys bool) (api.CanNodeDepositResponse, error) (gocritic)
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 @@ -650,18 +650,17 @@ func (c *Client) NodeRepaySd(amountWei *big.Int) (api.NodeRepaySDResponse, error
return response, nil
}

// Approve SD for depositing as collateral
func (c *Client) CanNodeRepaySd(amountWei *big.Int) (api.CanRepaySDResponse, error) {
responseBytes, err := c.callAPI(fmt.Sprintf("node can-repay-sd %s", amountWei.String()))
if err != nil {
return api.CanRepaySDResponse{}, fmt.Errorf("could not repay SD: %w", err)
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 repay SD response: %w", err)
return api.CanRepaySDResponse{}, fmt.Errorf("could not decode CanNodeRepaySd response: %w", err)
}
if response.Error != "" {
return api.CanRepaySDResponse{}, fmt.Errorf("could not repay SD: %s", response.Error)
return api.CanRepaySDResponse{}, fmt.Errorf("could not can-repay SD: %s", response.Error)
}
return response, nil
}
Expand Down
1 change: 0 additions & 1 deletion stader-cli/node/approve-sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func nodeApproveSd(c *cli.Context) error {
cliutils.PrintMultiTransactionNonceWarning()
}

// Get stake mount
amountInString := c.String("amount")

amount, err := strconv.ParseFloat(amountInString, 64)
Expand Down
3 changes: 2 additions & 1 deletion stader-cli/node/deposit-sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func nodeDepositSd(c *cli.Context) error {
cliutils.PrintMultiTransactionNonceWarning()
}

// Get stake mount
amountInString := c.String("amount")

amount, err := strconv.ParseFloat(amountInString, 64)
if err != nil {
return err
}

autoConfirm := c.Bool("yes")

amountWei := eth.EthToWei(amount)
Expand Down
11 changes: 2 additions & 9 deletions stader-cli/node/repay-exess-sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func repayExcessSD(c *cli.Context) error {
return err
}

// Get stake mount
amountInString := c.String("amount")

amount, err := strconv.ParseFloat(amountInString, 64)
Expand All @@ -49,19 +48,13 @@ func repayExcessSD(c *cli.Context) error {

sdStatus := canRepayExcessSdResponse.SdStatusResponse

// Less than 200 %
if sdStatus.SdCollateralCurrentAmount.Cmp(sdStatus.SdMaxCollateralAmount) < 0 {
fmt.Printf("Not enough SD collateral to repay utilized SD %s \n", sdStatus.PoolAvailableSDBalance.String())
return nil
}

// Do not had position
if sdStatus.SdUtilizerLatestBalance.Cmp(big.NewInt(0)) <= 0 {
fmt.Printf("You don't have an existing utilization position. To withdraw excess SD to your wallet execute the following command: stader-cli node withdraw-sd --amount <SD amount>\n")
return nil
}

amountExcess := new(big.Int).Sub(sdStatus.SdMaxCollateralAmount, sdStatus.SdCollateralCurrentAmount)
amountExcess := new(big.Int).Sub(sdStatus.SdCollateralCurrentAmount, sdStatus.SdMaxCollateralAmount)

if amountExcess.Cmp(big.NewInt(0)) <= 0 {
fmt.Printf("You don't have excess SD collateral\n")
Expand All @@ -85,7 +78,7 @@ func repayExcessSD(c *cli.Context) error {
return err
}

res, err := staderClient.NodeRepaySd(amountExcess)
res, err := staderClient.NodeRepaySd(amountWei)
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion stader-cli/node/repay-sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func repaySD(c *cli.Context) error {
return err
}

// Get stake mount
amountInString := c.String("amount")

amount, err := strconv.ParseFloat(amountInString, 64)
Expand Down
7 changes: 6 additions & 1 deletion stader-cli/validator/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func nodeDeposit(c *cli.Context) error {
log.ColorReset,
math.RoundDown(eth.WeiToEth(status.AccountBalances.Sd), 18))

canNodeDepositResponse, err := staderClient.CanNodeDeposit(baseAmount, big.NewInt(int64(numValidators)), true)
canNodeDepositResponse, err := staderClient.CanNodeDeposit(baseAmount, big.NewInt(0), big.NewInt(int64(numValidators)), true)
if err != nil {
return err
}
Expand Down Expand Up @@ -162,6 +162,11 @@ func nodeDeposit(c *cli.Context) error {
}
}

canNodeDepositResponse, err = staderClient.CanNodeDeposit(baseAmount, utilityAmount, big.NewInt(int64(numValidators)), true)
if err != nil {
return err
}

if canNodeDepositResponse.MaxValidatorLimitReached {
fmt.Printf("Max validator limit reached")
return nil
Expand Down
7 changes: 2 additions & 5 deletions stader-lib/sd_utility/sd_utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stader-labs/stader-node/stader-lib/stader"
"github.com/stader-labs/stader-node/stader-lib/utils/eth"
)

func GetUtilizerLatestBalance(sp *stader.SDUtilityPoolContractManager, address common.Address, opts *bind.CallOpts) (*big.Int, error) {
return big.NewInt(0), nil
return sp.SDUtilityPool.GetUtilizerLatestBalance(opts, address)
}

func GetPoolAvailableSDBalance(sp *stader.SDUtilityPoolContractManager, address common.Address, opts *bind.CallOpts) (*big.Int, error) {
return eth.EthToWei(1000), nil
// return sp.SDUtilityPool.GetPoolAvailableSDBalance(opts)
func GetPoolAvailableSDBalance(sp *stader.SDUtilityPoolContractManager, opts *bind.CallOpts) (*big.Int, error) {
return sp.SDUtilityPool.GetPoolAvailableSDBalance(opts)
}

// Estimate the gas of Utilize
Expand Down
2 changes: 1 addition & 1 deletion stader-lib/stader-config/stader-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ func GetOperatorRewardsCollectorAddress(sdConfig *stader.StaderConfigContractMan
return sdConfig.StaderConfig.GetOperatorRewardsCollector(opts)
}

func GetSDUtilityPool(sdConfig *stader.StaderConfigContractManager, opts *bind.CallOpts) (common.Address, error) {
func GetSDUtilityPoolAddress(sdConfig *stader.StaderConfigContractManager, opts *bind.CallOpts) (common.Address, error) {
return sdConfig.StaderConfig.GetSDUtilityPool(opts)
}
8 changes: 4 additions & 4 deletions stader/api/validator/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) {
Name: "deposit",
Aliases: []string{"d"},
Usage: "Make a deposit and create a validator",
UsageText: "stader-cli api validator deposit amount utility num-validators reload-keys",
UsageText: "stader-cli api validator deposit-amount utility-amount num-validators reload-keys",
Action: func(c *cli.Context) error {

// Validate args
if err := cliutils.ValidateArgCount(c, 4); err != nil {
return err
}
amountWei, err := cliutils.ValidateWeiAmount("deposit amount", c.Args().Get(0))
baseAmountWei, err := cliutils.ValidateWeiAmount("deposit amount", c.Args().Get(0))
if err != nil {
return err
}

utilityAmount, err := cliutils.ValidateWeiAmount("utility amount", c.Args().Get(1))
utilityAmountWei, err := cliutils.ValidateWeiAmount("utility amount", c.Args().Get(1))
if err != nil {
return err
}
Expand All @@ -98,7 +98,7 @@ func RegisterSubcommands(command *cli.Command, name string, aliases []string) {
}

// Run
response, err := nodeDeposit(c, amountWei, utilityAmount, numValidators, reloadKeys)
response, err := nodeDeposit(c, baseAmountWei, utilityAmountWei, numValidators, reloadKeys)
api.PrintResponse(response, err)

return nil
Expand Down
8 changes: 4 additions & 4 deletions stader/api/validator/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetSDStatus(
return nil, err
}

poolAvailableSDBalance, err := sd_utility.GetPoolAvailableSDBalance(sdu, operatorAddress, nil)
poolAvailableSDBalance, err := sd_utility.GetPoolAvailableSDBalance(sdu, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func canNodeDeposit(c *cli.Context, amountWei *big.Int, numValidators *big.Int,
return &canNodeDepositResponse, nil
}

func nodeDeposit(c *cli.Context, amountWei *big.Int, amountUtility *big.Int, numValidators *big.Int, reloadKeys bool) (*api.NodeDepositResponse, error) {
func nodeDeposit(c *cli.Context, baseAmountWei, utilityAmountWei, numValidators *big.Int, reloadKeys bool) (*api.NodeDepositResponse, error) {
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
Expand Down Expand Up @@ -340,7 +340,7 @@ func nodeDeposit(c *cli.Context, amountWei *big.Int, amountUtility *big.Int, num
preDepositSignatures := make([][]byte, numValidators.Int64())
depositSignatures := make([][]byte, numValidators.Int64())

amountToSend := amountWei.Mul(amountWei, numValidators)
amountToSend := baseAmountWei.Mul(baseAmountWei, numValidators)
opts.Value = amountToSend

validatorKeyCount, err := node.GetTotalValidatorKeys(prn, operatorId, nil)
Expand Down Expand Up @@ -426,7 +426,7 @@ func nodeDeposit(c *cli.Context, amountWei *big.Int, amountUtility *big.Int, num
pubKeys,
preDepositSignatures,
depositSignatures,
amountUtility,
utilityAmountWei,
opts)
if err != nil {
return nil, err
Expand Down

0 comments on commit d662db4

Please sign in to comment.