Skip to content

Commit

Permalink
refactor: minor changes and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
swarmHaseeb committed Dec 16, 2022
1 parent e7e54e5 commit 1b4b77b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
1 change: 1 addition & 0 deletions pkg/api/export_test.go
Expand Up @@ -100,6 +100,7 @@ type (
BucketData = bucketData
WalletResponse = walletResponse
GetStakeResponse = getStakeResponse
WithdrawAllStakeResponse = withdrawAllStakeResponse
)

var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/staking.go
Expand Up @@ -97,7 +97,7 @@ func (s *Service) getStakedAmountHandler(w http.ResponseWriter, r *http.Request)
}

func (s *Service) withdrawAllStakeHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("withdraw_all_stake").Build()
logger := s.logger.WithName("delete_withdraw_all_stake").Build()

txHash, err := s.stakingContract.WithdrawAllStake(r.Context())
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/staking_test.go
Expand Up @@ -186,7 +186,8 @@ func TestWithdrawAllStake(t *testing.T) {
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{DebugAPI: true, StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusOK)
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(
&api.WithdrawAllStakeResponse{TxHash: txHash.String()}))
})

t.Run("with invalid stake amount", func(t *testing.T) {
Expand Down
48 changes: 31 additions & 17 deletions pkg/storageincentives/staking/contract.go
Expand Up @@ -162,42 +162,46 @@ func (c *contract) getStake(ctx context.Context, overlay swarm.Address) (*big.In
if err != nil {
return nil, err
}

if len(results) == 0 {
return nil, errors.New("unexpected empty results")
}

return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}

func (c *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (txHash common.Hash, err error) {
func (c *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
prevStakedAmount, err := c.GetStake(ctx)
if err != nil {
return
return common.Hash{}, err
}

if len(prevStakedAmount.Bits()) == 0 {
if stakedAmount.Cmp(MinimumStakeAmount) == -1 {
err = ErrInsufficientStakeAmount
return
return common.Hash{}, ErrInsufficientStakeAmount
}
}

balance, err := c.getBalance(ctx)
if err != nil {
return
return common.Hash{}, err
}

if balance.Cmp(stakedAmount) < 0 {
err = ErrInsufficientFunds
return
return common.Hash{}, ErrInsufficientFunds
}

_, err = c.sendApproveTransaction(ctx, stakedAmount)
if err != nil {
return
return common.Hash{}, err
}

receipt, err := c.sendDepositStakeTransaction(ctx, c.owner, stakedAmount, c.overlayNonce)
if receipt != nil {
txHash = receipt.TxHash
if err != nil {
return common.Hash{}, err
}
return

return receipt.TxHash, nil
}

func (c *contract) GetStake(ctx context.Context) (*big.Int, error) {
Expand Down Expand Up @@ -226,6 +230,11 @@ func (c *contract) getBalance(ctx context.Context) (*big.Int, error) {
if err != nil {
return nil, err
}

if len(results) == 0 {
return nil, errors.New("unexpected empty results")
}

return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}

Expand All @@ -235,8 +244,7 @@ func (c *contract) WithdrawAllStake(ctx context.Context) (txHash common.Hash, er
return
}
if !isPaused {
err = ErrNotPaused
return
return common.Hash{}, ErrNotPaused
}

stakedAmount, err := c.getStake(ctx, c.overlay)
Expand All @@ -245,20 +253,22 @@ func (c *contract) WithdrawAllStake(ctx context.Context) (txHash common.Hash, er
}

if stakedAmount.Cmp(big.NewInt(0)) <= 0 {
err = ErrInsufficientStake
return
return common.Hash{}, ErrInsufficientStake
}

_, err = c.sendApproveTransaction(ctx, stakedAmount)
if err != nil {
return
return common.Hash{}, err
}

receipt, err := c.withdrawFromStake(ctx, stakedAmount)
if err != nil {
return common.Hash{}, err
}
if receipt != nil {
txHash = receipt.TxHash
}
return
return txHash, nil
}

func (c *contract) withdrawFromStake(ctx context.Context, stakedAmount *big.Int) (*types.Receipt, error) {
Expand Down Expand Up @@ -297,5 +307,9 @@ func (c *contract) paused(ctx context.Context) (bool, error) {
return false, err
}

if len(results) == 0 {
return false, errors.New("unexpected empty results")
}

return results[0].(bool), nil
}

0 comments on commit 1b4b77b

Please sign in to comment.