Skip to content

Commit 555849c

Browse files
committed
Refactor
1 parent caef917 commit 555849c

33 files changed

+103
-56
lines changed

.golangci.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ linters-settings:
1313
- style
1414
govet:
1515
check-shadowing: true
16-
enable:
17-
- fieldalignment
1816
nolintlint:
1917
require-explanation: true
2018
require-specific: true
@@ -33,7 +31,7 @@ linters:
3331
- gocritic
3432
- gofmt
3533
- goimports
36-
- gocyclo
34+
# - gocyclo
3735
- gosec
3836
- gosimple
3937
- govet

shared/services/bc-manager.go

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func (m *BeaconClientManager) GetNodeVersion() (beacon.NodeVersion, error) {
141141
if err != nil {
142142
return beacon.NodeVersion{}, err
143143
}
144+
144145
return result.(beacon.NodeVersion), nil
145146
}
146147

shared/services/beacon/client/std-http-client.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,12 @@ func (c *StandardHttpClient) GetValidatorIndex(pubkey types.ValidatorPubkey) (ui
423423

424424
// Get domain data for a domain type at a given epoch
425425
func (c *StandardHttpClient) GetExitDomainData(domainType []byte, network config.Network) ([]byte, error) {
426-
427426
var genesis GenesisResponse
428427

429428
genesis, err := c.getGenesis()
429+
if err != nil {
430+
return []byte{}, fmt.Errorf("GetGenesis %w", err)
431+
}
430432

431433
// Get fork version
432434
var capellaForkVersion string
@@ -445,8 +447,8 @@ func (c *StandardHttpClient) GetExitDomainData(domainType []byte, network config
445447
// Compute & return domain
446448
var dt [4]byte
447449
copy(dt[:], domainType[:])
448-
return eth2types.Domain(dt, decodedForkVersion, genesis.Data.GenesisValidatorsRoot), nil
449450

451+
return eth2types.Domain(dt, decodedForkVersion, genesis.Data.GenesisValidatorsRoot), nil
450452
}
451453

452454
// Perform a voluntary exit on a validator
@@ -589,13 +591,16 @@ func (c *StandardHttpClient) getNodeVersion() (NodeVersionResponse, error) {
589591
if err != nil {
590592
return NodeVersionResponse{}, fmt.Errorf("Could not get node sync status: %w", err)
591593
}
594+
592595
if status != http.StatusOK {
593596
return NodeVersionResponse{}, fmt.Errorf("Could not get node sync status: HTTP status %d; response body: '%s'", status, string(responseBody))
594597
}
598+
595599
var nodeVersion NodeVersionResponse
596600
if err := json.Unmarshal(responseBody, &nodeVersion); err != nil {
597601
return NodeVersionResponse{}, fmt.Errorf("Could not decode node sync status: %w", err)
598602
}
603+
599604
return nodeVersion, nil
600605
}
601606

shared/services/config/stader-config.go

+1
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ func (cfg *StaderConfig) GenerateEnvironmentVariables() map[string]string {
986986
envVars["PROMETHEUS_ADDITIONAL_FLAGS"] = fmt.Sprintf(", \"%s\"", cfg.Prometheus.AdditionalFlags.Value.(string))
987987
}
988988
}
989+
989990
if cfg.ExposeGuardianPort.Value == true {
990991
envVars["GUARDIAN_OPEN_PORTS"] = fmt.Sprintf("%d:%d/tcp", cfg.NodeMetricsPort.Value, cfg.NodeMetricsPort.Value)
991992
}

shared/services/ec-manager.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,12 @@ func (p *ExecutionClientManager) Version() (string, error) {
541541
Jsonrpc string `json:"jsonrpc"`
542542
Method string `json:"method"`
543543
Params []string `json:"params"`
544-
Id int64 `json:"id"`
544+
ID int64 `json:"id"`
545545
}{
546546
Jsonrpc: "2.0",
547547
Method: "web3_clientVersion",
548548
Params: []string{},
549-
Id: 1,
549+
ID: 1,
550550
}
551551

552552
res, err := net.MakePostRequest(url, payload)

shared/services/stader/node.go

+8
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ func (c *Client) NodeSdApprovalGas(amountWei *big.Int, address common.Address) (
118118
if err != nil {
119119
return api.SdApproveGasResponse{}, fmt.Errorf("could not get new SD approval gas: %w", err)
120120
}
121+
121122
var response api.SdApproveGasResponse
122123
if err := json.Unmarshal(responseBytes, &response); err != nil {
123124
return api.SdApproveGasResponse{}, fmt.Errorf("could not decode node deposit S approve gas response: %w", err)
124125
}
126+
125127
if response.Error != "" {
126128
return api.SdApproveGasResponse{}, fmt.Errorf("could not get new SD approval gas: %s", response.Error)
127129
}
@@ -134,13 +136,16 @@ func (c *Client) NodeSdApprove(amountWei *big.Int, address common.Address) (api.
134136
if err != nil {
135137
return api.SdApproveResponse{}, fmt.Errorf("could not approve SD for staking: %w", err)
136138
}
139+
137140
var response api.SdApproveResponse
138141
if err := json.Unmarshal(responseBytes, &response); err != nil {
139142
return api.SdApproveResponse{}, fmt.Errorf("could not decode deposit node SD approve response: %w", err)
140143
}
144+
141145
if response.Error != "" {
142146
return api.SdApproveResponse{}, fmt.Errorf("could not approve SD for staking: %s", response.Error)
143147
}
148+
144149
return response, nil
145150
}
146151

@@ -196,13 +201,16 @@ func (c *Client) GetNodeSdAllowance(contractAddress common.Address) (api.SdAllow
196201
if err != nil {
197202
return api.SdAllowanceResponse{}, fmt.Errorf("could not get node deposit SD allowance: %w", err)
198203
}
204+
199205
var response api.SdAllowanceResponse
200206
if err := json.Unmarshal(responseBytes, &response); err != nil {
201207
return api.SdAllowanceResponse{}, fmt.Errorf("could not decode node deposit SD allowance response: %w", err)
202208
}
209+
203210
if response.Error != "" {
204211
return api.SdAllowanceResponse{}, fmt.Errorf("could not get node deposit SD allowance: %s", response.Error)
205212
}
213+
206214
return response, nil
207215
}
208216

shared/services/wallet/node.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ func (w *Wallet) getNodeDerivedKey(index uint) (*hdkeychain.ExtendedKey, string,
190190

191191
// Get the node hex encoding public key
192192
func (w *Wallet) GetNodePubkey() (string, error) {
193-
194193
// Check wallet is initialized
195194
if !w.IsInitialized() {
196195
return "", errors.New("Wallet is not initialized")
@@ -204,6 +203,7 @@ func (w *Wallet) GetNodePubkey() (string, error) {
204203

205204
// Get public key
206205
publicKey := privateKey.Public()
206+
207207
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
208208
if !ok {
209209
return "", errors.New("Could not get node public key")

shared/utils/stader/node-diversity.go

-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,4 @@ func SendNodeDiversityResponseType(
3333
}
3434

3535
return &resp, nil
36-
3736
}

stader-cli/node/claim-rewards.go

+2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ func ClaimRewards(c *cli.Context) error {
7979
if err != nil {
8080
return err
8181
}
82+
8283
fmt.Printf("Withdrawing %s Rewards to Operator Reward Address: %s\n\n", eth.DisplayAmountInUnits(res.RewardsClaimed, "eth"), res.OperatorRewardAddress)
8384
cliutils.PrintTransactionHash(staderClient, res.TxHash)
85+
8486
if _, err = staderClient.WaitForTransaction(res.TxHash); err != nil {
8587
return err
8688
}

stader-cli/node/claim-sp-rewards.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@ func ClaimSpRewards(c *cli.Context) error {
8282
if !ok {
8383
return fmt.Errorf("Unable to parse eth rewards: %s", cycleInfo.MerkleProofInfo.Eth)
8484
}
85+
8586
ethRewardsConverted := math.RoundDown(eth.WeiToEth(ethRewards), 5)
87+
8688
sdRewards, ok := big.NewInt(0).SetString(cycleInfo.MerkleProofInfo.Sd, 10)
8789
if !ok {
8890
return fmt.Errorf("Unable to parse sd rewards: %s", cycleInfo.MerkleProofInfo.Sd)
8991
}
92+
9093
sdRewardsConverted := math.RoundDown(eth.WeiToEth(sdRewards), 5)
9194

9295
if ethRewards.Cmp(big.NewInt(0)) == 0 && sdRewards.Cmp(big.NewInt(0)) == 0 {
@@ -142,21 +145,27 @@ func ClaimSpRewards(c *cli.Context) error {
142145

143146
totalClaimableEth := big.NewInt(0)
144147
totalClaimableSd := big.NewInt(0)
148+
145149
for _, cycle := range cyclesToClaimArray {
146150
cycleInfo := indexedDetailedCyclesInfo[cycle.Int64()]
151+
147152
ethRewards, ok := big.NewInt(0).SetString(cycleInfo.Eth, 10)
148153
if !ok {
149154
return fmt.Errorf("Unable to parse eth rewards: %s", cycleInfo.Eth)
150155
}
156+
151157
totalClaimableEth = totalClaimableEth.Add(totalClaimableEth, ethRewards)
158+
152159
sdRewards, ok := big.NewInt(0).SetString(cycleInfo.Sd, 10)
153160
if !ok {
154161
return fmt.Errorf("Unable to parse sd rewards: %s", cycleInfo.Sd)
155162
}
163+
156164
totalClaimableSd = totalClaimableSd.Add(totalClaimableSd, sdRewards)
157165
}
158166

159167
depositSd := false
168+
160169
if totalClaimableSd.Cmp(big.NewInt(0)) > 0 {
161170
fmt.Printf("You will claim %s and %s with the following selection - cycles %v\n\n", eth.DisplayAmountInUnits(totalClaimableSd, "sd"), eth.DisplayAmountInUnits(totalClaimableEth, "eth"), cyclesToClaimArray)
162171
fmt.Printf("Your ETH rewards will be sent to your Reward Address\n")
@@ -179,18 +188,18 @@ func ClaimSpRewards(c *cli.Context) error {
179188
fmt.Println("Claim Cancelled.")
180189
return nil
181190
}
191+
182192
depositSd = true
183193
}
184-
} else {
185-
if !cliutils.Confirm(fmt.Sprintf(
186-
"Are you sure you want to claim %s ETH for cycles %v to your reward address?", totalClaimableEth.String(), cyclesToClaimArray)) {
187-
fmt.Println("Cancelled.")
188-
return nil
189-
}
194+
} else if !cliutils.Confirm(fmt.Sprintf(
195+
"Are you sure you want to claim %s ETH for cycles %v to your reward address?", totalClaimableEth.String(), cyclesToClaimArray)) {
196+
fmt.Println("Cancelled.")
197+
return nil
190198
}
191199

192200
// estimate gas
193201
fmt.Println("Estimating gas...")
202+
194203
estimateGasResponse, err := staderClient.EstimateClaimSpRewardsGas(cyclesToClaimArray, depositSd)
195204
if err != nil {
196205
return err
@@ -215,6 +224,7 @@ func ClaimSpRewards(c *cli.Context) error {
215224
}
216225

217226
fmt.Printf("Transaction Successful\n")
227+
218228
if depositSd {
219229
fmt.Printf("%s rewards have been sent to your Reward Address and %s rewards have been re-deposited as SD collateral\n", eth.DisplayAmountInUnits(totalClaimableEth, "eth"), eth.DisplayAmountInUnits(totalClaimableSd, "sd"))
220230
} else {

stader-cli/node/commands.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ func RegisterCommands(app *cli.App, name string, aliases []string) {
367367
Usage: "Automatically confirm SD repay",
368368
},
369369
},
370-
Action: func(c *cli.Context) error {
371-
// Run
372-
return repaySD(c)
373-
},
370+
Action: repaySD,
374371
},
375372
{
376373
Name: "approve-deposit-sd",

stader-cli/node/deposit-sd.go

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func DepositSdWithAmount(staderClient *stader.Client, amountWei *big.Int, autoCo
6363
maxApproval := maxUint256()
6464

6565
fmt.Println("Before depositing SD, you must first give the collateral contract approval to interact with your SD.")
66+
6667
err = nodeApproveSdWithAmountAndAddress(staderClient, maxApproval, contracts.SdCollateralContract, autoConfirm, nonce)
6768
if err != nil {
6869
return err

stader-cli/node/repay-sd.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func repaySD(c *cli.Context) error {
6161
i, _ := cliutils.Select("Please choose one of the following options for repayment. Enter 1 or 2:", ops)
6262

6363
fullRepay := false
64+
6465
var amountWei *big.Int
6566

6667
switch i {
@@ -86,6 +87,7 @@ func repaySD(c *cli.Context) error {
8687

8788
if allowance.Allowance.Cmp(sdStatus.SdUtilizerLatestBalance) < 0 {
8889
fmt.Println("Before repaying the SD, you must first give the utility contract approval to interact with your SD.")
90+
8991
maxApproval := maxUint256()
9092

9193
err = nodeApproveUtilitySd(c, maxApproval.String())
@@ -131,11 +133,11 @@ func repaySD(c *cli.Context) error {
131133
remainUtilize := new(big.Int).Sub(sdStatus.SdUtilizerLatestBalance, amountWei)
132134
fmt.Printf("Repayment of %s successful. Current Utilization Position: %s.\n", eth.DisplayAmountInUnits(amountWei, "sd"), eth.DisplayAmountInUnits(remainUtilize, "sd"))
133135
}
136+
134137
return nil
135138
}
136139

137140
func PromptChooseRepayAmount(sdStatus *api.SdStatusResponse) (*big.Int, error) {
138-
139141
msg := fmt.Sprintf(`%sPlease enter the amount of SD you wish to repay. Your current Utilization Position is %s%s`, log.ColorYellow, eth.DisplayAmountInUnits(sdStatus.SdUtilizerLatestBalance, "sd"), log.ColorReset)
140142

141143
errMsg := fmt.Sprintf("%sInvalid input, please specify a valid amount of SD you wish to repay. Your current Utilization Position is %s SD%s", log.ColorRed, eth.DisplayAmountInUnits(sdStatus.SdUtilizerLatestBalance, "sd"), log.ColorReset)

stader-cli/node/send-el-rewards.go

+2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ func SendElRewards(c *cli.Context) error {
5252
if err != nil {
5353
return err
5454
}
55+
5556
fmt.Printf("Sending %s EL Rewards to Claim Vault\n\n", eth.DisplayAmountInUnits(res.ElRewardsAmount, "eth"))
5657
cliutils.PrintTransactionHash(staderClient, res.TxHash)
58+
5759
if _, err = staderClient.WaitForTransaction(res.TxHash); err != nil {
5860
return err
5961
}

stader-cli/node/status.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func getNodeStatus(c *cli.Context) error {
154154
if err != nil {
155155
return err
156156
}
157+
157158
collateralPct := 0.0
158159
sdStatus := sdStatusResp.SDStatus
159160
totalCollateral := new(big.Int).Add(sdStatus.SdCollateralCurrentAmount, sdStatus.SdUtilizedBalance)
@@ -183,7 +184,6 @@ func getNodeStatus(c *cli.Context) error {
183184
totalRegisteredValidators,
184185
eth.DisplayAmountInUnits(sdStatus.SdCollateralRequireAmount, "sd"),
185186
"10%", "10%", "10%")
186-
187187
} else {
188188
fmt.Println("")
189189
}
@@ -194,6 +194,7 @@ func getNodeStatus(c *cli.Context) error {
194194

195195
fmt.Printf("The Operator has a current Utilization Position of %s. (including the utilization fee)\n",
196196
eth.DisplayAmountInUnits(sdStatus.SdUtilizerLatestBalance, "sd"))
197+
197198
if sdStatus.SdUtilizerLatestBalance.Cmp(big.NewInt(0)) == 0 {
198199
fmt.Println("")
199200
} else {
@@ -204,6 +205,7 @@ func getNodeStatus(c *cli.Context) error {
204205
if maxUtilizable.Cmp(sdStatus.PoolAvailableSDBalance) > 0 {
205206
maxUtilizable = sdStatus.PoolAvailableSDBalance
206207
}
208+
207209
if maxUtilizable.Sign() < 0 {
208210
maxUtilizable = big.NewInt(0)
209211
}

stader-cli/node/utilize-sd.go

-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ Maximum utilization amount: %s
159159
}
160160

161161
func PromptChooseSelfBondAmount(sdStatus *api.SdStatusResponse) (*big.Int, error) {
162-
163162
totalCollateral := new(big.Int).Add(sdStatus.SdCollateralCurrentAmount, sdStatus.SdUtilizedBalance)
164163

165164
amountToCollateralRemain := new(big.Int).Sub(sdStatus.SdCollateralRequireAmount, totalCollateral)

stader-cli/validator/deposit.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ func nodeDeposit(c *cli.Context) error {
116116
return nil
117117
}
118118
case 1:
119-
selfBondAmount, err := node.PromptChooseSelfBondAmount(sdStatus)
120-
if err != nil {
121-
return err
119+
selfBondAmount, errSelfBond := node.PromptChooseSelfBondAmount(sdStatus)
120+
if errSelfBond != nil {
121+
return errSelfBond
122122
}
123123

124124
if status.AccountBalances.Sd.Cmp(selfBondAmount) < 0 {
@@ -132,10 +132,10 @@ func nodeDeposit(c *cli.Context) error {
132132
}
133133

134134
nounce := c.GlobalUint64("nonce")
135-
err = node.DepositSdWithAmount(staderClient, selfBondAmount, true, nounce)
135+
errSelfBond = node.DepositSdWithAmount(staderClient, selfBondAmount, true, nounce)
136136

137-
if err != nil {
138-
return err
137+
if errSelfBond != nil {
138+
return errSelfBond
139139
}
140140

141141
default:

stader-cli/wallet/export.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func exportWallet(c *cli.Context) error {
5151
// Check if stdout is interactive
5252
stat, err := os.Stdout.Stat()
5353
if err != nil {
54-
fmt.Fprintf(os.Stderr, "An error occured while determining whether or not the output is a tty: %s\n"+
54+
fmt.Fprintf(os.Stderr, "An error occurred while determining whether or not the output is a tty: %s\n"+
5555
"Use \"stader-cli --secure-session wallet export\" to bypass.\n", err.Error())
5656
os.Exit(1)
5757
}

0 commit comments

Comments
 (0)