Skip to content

Commit

Permalink
fix: rename only withdrawable stake response (#414)
Browse files Browse the repository at this point in the history
* fix: rename only withdrawable stake response

* fix: use and add the right path to withdrawable stake

* fix: use of  withdrawable stake

* fix: expected withdrawable stake error
  • Loading branch information
martinconic authored Sep 4, 2024
1 parent fc9212f commit 271e15a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
22 changes: 18 additions & 4 deletions pkg/bee/api/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
type StakingService service

type getStakeResponse struct {
WithdrawableStake *bigint.BigInt `json:"withdrawableStake"`
StakedAmount *bigint.BigInt `json:"stakedAmount"`
}

type getWithdrawableResponse struct {
WithdrawableAmount *bigint.BigInt `json:"withdrawableAmount"`
}
type stakeDepositResponse struct {
TxHash string `json:"txhash"`
Expand All @@ -32,14 +36,24 @@ func (s *StakingService) DepositStake(ctx context.Context, amount *big.Int) (txH
return r.TxHash, nil
}

// GetWithdrawableStake gets stake
func (s *StakingService) GetWithdrawableStake(ctx context.Context) (withdrawableStake *big.Int, err error) {
// GetStakedAmount gets stake
func (s *StakingService) GetStakedAmount(ctx context.Context) (stakedAmount *big.Int, err error) {
r := new(getStakeResponse)
err = s.client.requestJSON(ctx, http.MethodGet, "/stake", nil, r)
if err != nil {
return nil, err
}
return r.WithdrawableStake.Int, nil
return r.StakedAmount.Int, nil
}

// GetWithdrawableStake gets stake
func (s *StakingService) GetWithdrawableStake(ctx context.Context) (withdrawableStake *big.Int, err error) {
r := new(getWithdrawableResponse)
err = s.client.requestJSON(ctx, http.MethodGet, "/stake/withdrawable", nil, r)
if err != nil {
return nil, err
}
return r.WithdrawableAmount.Int, nil
}

// MigrateStake withdraws stake
Expand Down
5 changes: 5 additions & 0 deletions pkg/bee/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,11 @@ func (c *Client) DepositStake(ctx context.Context, amount *big.Int) (string, err

// GetStake returns stake amount
func (c *Client) GetStake(ctx context.Context) (*big.Int, error) {
return c.api.Stake.GetStakedAmount(ctx)
}

// GetWithdrawableStake returns withdrawable amount
func (c *Client) GetWithdrawableStake(ctx context.Context) (*big.Int, error) {
return c.api.Stake.GetWithdrawableStake(ctx)
}

Expand Down
36 changes: 33 additions & 3 deletions pkg/check/stake/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,23 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int
return err
}

if err := expectWithdrawableStake(ctx, client, o.Amount); err != nil {
return err
}

// should allow increasing the stake amount
withdrawableStake := new(big.Int).Add(o.Amount, big.NewInt(1))
stakedAmount := new(big.Int).Add(o.Amount, big.NewInt(1))

_, err = client.DepositStake(ctx, big.NewInt(1))
if err != nil {
return fmt.Errorf("increase stake amount: %w", err)
}

if err := expectStakeAmountIs(ctx, client, withdrawableStake); err != nil {
if err := expectStakeAmountIs(ctx, client, stakedAmount); err != nil {
return err
}

if err := expectWithdrawableStake(ctx, client, stakedAmount); err != nil {
return err
}

Expand All @@ -127,7 +135,7 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int
return errors.New("withdraw from running contract should fail")
}

if err := expectStakeAmountIs(ctx, client, withdrawableStake); err != nil {
if err := expectStakeAmountIs(ctx, client, stakedAmount); err != nil {
return err
}

Expand Down Expand Up @@ -157,6 +165,10 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int
return err
}

if err := expectWithdrawableStake(ctx, client, zero); err != nil {
return err
}

return nil
}

Expand All @@ -172,3 +184,21 @@ func expectStakeAmountIs(ctx context.Context, client *bee.Client, expected *big.

return nil
}

func expectWithdrawableStake(ctx context.Context, client *bee.Client, expected *big.Int) error {
withdrawable, err := client.GetWithdrawableStake(ctx)
if err != nil {
return fmt.Errorf("get stake amount: %w", err)
}

if (expected.Cmp(zero) == 0 && withdrawable.Cmp(expected) != 0) ||
(expected.Cmp(zero) != 0 && withdrawable.Cmp(zero) == 0) {
if expected.Cmp(zero) == 0 {
return fmt.Errorf("expected withdrawable stake to be %d, got: %d", expected, withdrawable)
} else {
return fmt.Errorf("expected withdrawable stake should not be equal to 0")
}
}

return nil
}

0 comments on commit 271e15a

Please sign in to comment.