Skip to content

Commit

Permalink
fix: rename only withdrawable stake response (#4801)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinconic authored Sep 5, 2024
1 parent f1ed79e commit 3ebd780
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 57 deletions.
2 changes: 1 addition & 1 deletion openapi/Swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,7 @@ paths:
- Staking
responses:
"200":
$ref: "SwarmCommon.yaml#/components/schemas/GetStakeResponse"
$ref: "SwarmCommon.yaml#/components/schemas/GetWithdrawableResponse"
"500":
$ref: "SwarmCommon.yaml#/components/responses/500"
default:
Expand Down
8 changes: 7 additions & 1 deletion openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,13 @@ components:
GetStakeResponse:
type: object
properties:
withdrawableStake:
stakedAmount:
$ref: "#/components/schemas/BigInt"

GetWithdrawableResponse:
type: object
properties:
withdrawableAmount:
$ref: "#/components/schemas/BigInt"

StakeTransactionResponse:
Expand Down
1 change: 1 addition & 0 deletions pkg/api/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type (
WalletResponse = walletResponse
WalletTxResponse = walletTxResponse
GetStakeResponse = getStakeResponse
GetWithdrawableResponse = getWithdrawableResponse
StakeTransactionReponse = stakeTransactionReponse
StatusSnapshotResponse = statusSnapshotResponse
StatusResponse = statusResponse
Expand Down
14 changes: 9 additions & 5 deletions pkg/api/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func (s *Service) stakingAccessHandler(h http.Handler) http.Handler {
}

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

type getWithdrawableResponse struct {
WithdrawableAmount *bigint.BigInt `json:"withdrawableAmount"`
}
type stakeTransactionReponse struct {
TxHash string `json:"txHash"`
Expand Down Expand Up @@ -81,29 +85,29 @@ func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request)
func (s *Service) getPotentialStake(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("get_stake").Build()

withdrawableStake, err := s.stakingContract.GetPotentialStake(r.Context())
stakedAmount, err := s.stakingContract.GetPotentialStake(r.Context())
if err != nil {
logger.Debug("get staked amount failed", "overlayAddr", s.overlay, "error", err)
logger.Error(nil, "get staked amount failed")
jsonhttp.InternalServerError(w, "get staked amount failed")
return
}

jsonhttp.OK(w, getStakeResponse{WithdrawableStake: bigint.Wrap(withdrawableStake)})
jsonhttp.OK(w, getStakeResponse{StakedAmount: bigint.Wrap(stakedAmount)})
}

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

withdrawableStake, err := s.stakingContract.GetWithdrawableStake(r.Context())
withdrawableAmount, err := s.stakingContract.GetWithdrawableStake(r.Context())
if err != nil {
logger.Debug("get staked amount failed", "overlayAddr", s.overlay, "error", err)
logger.Error(nil, "get staked amount failed")
jsonhttp.InternalServerError(w, "get staked amount failed")
return
}

jsonhttp.OK(w, getStakeResponse{WithdrawableStake: bigint.Wrap(withdrawableStake)})
jsonhttp.OK(w, getWithdrawableResponse{WithdrawableAmount: bigint.Wrap(withdrawableAmount)})
}

func (s *Service) withdrawStakeHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
14 changes: 7 additions & 7 deletions pkg/api/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestDepositStake(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, withdrawableStake *big.Int) (common.Hash, error) {
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return txHash, nil
}),
)
Expand All @@ -48,7 +48,7 @@ func TestDepositStake(t *testing.T) {

invalidMinStake := big.NewInt(0).String()
contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, withdrawableStake *big.Int) (common.Hash, error) {
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return common.Hash{}, staking.ErrInsufficientStakeAmount
}),
)
Expand All @@ -61,7 +61,7 @@ func TestDepositStake(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, withdrawableStake *big.Int) (common.Hash, error) {
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return common.Hash{}, staking.ErrInsufficientFunds
}),
)
Expand All @@ -74,7 +74,7 @@ func TestDepositStake(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, withdrawableStake *big.Int) (common.Hash, error) {
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return common.Hash{}, fmt.Errorf("some error")
}),
)
Expand All @@ -87,7 +87,7 @@ func TestDepositStake(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, withdrawableStake *big.Int) (common.Hash, error) {
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
gasLimit := sctx.GetGasLimit(ctx)
if gasLimit != 2000000 {
t.Fatalf("want 2000000, got %d", gasLimit)
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestGetStakeCommitted(t *testing.T) {
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodGet, "/stake", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&api.GetStakeResponse{WithdrawableStake: bigint.Wrap(big.NewInt(1))}))
jsonhttptest.WithExpectedJSONResponse(&api.GetStakeResponse{StakedAmount: bigint.Wrap(big.NewInt(1))}))
})

t.Run("with error", func(t *testing.T) {
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestGetStakeWithdrawable(t *testing.T) {
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodGet, "/stake/withdrawable", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&api.GetStakeResponse{WithdrawableStake: bigint.Wrap(big.NewInt(1))}))
jsonhttptest.WithExpectedJSONResponse(&api.GetWithdrawableResponse{WithdrawableAmount: bigint.Wrap(big.NewInt(1))}))
})

t.Run("with error", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/node/devnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func NewDevBee(logger log.Logger, o *DevOptions) (b *DevBee, err error) {
mockSteward := new(mockSteward.Steward)

mockStaking := stakingContractMock.New(
stakingContractMock.WithDepositStake(func(ctx context.Context, withdrawableStake *big.Int) (common.Hash, error) {
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
return common.Hash{}, staking.ErrNotImplemented
}),
stakingContractMock.WithGetStake(func(ctx context.Context) (*big.Int, error) {
Expand Down
22 changes: 11 additions & 11 deletions pkg/storageincentives/staking/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
)

type Contract interface {
DepositStake(ctx context.Context, withdrawableStake *big.Int) (common.Hash, error)
DepositStake(ctx context.Context, stakedAmount *big.Int) (common.Hash, error)
ChangeStakeOverlay(ctx context.Context, nonce common.Hash) (common.Hash, error)
GetPotentialStake(ctx context.Context) (*big.Int, error)
GetWithdrawableStake(ctx context.Context) (*big.Int, error)
Expand Down Expand Up @@ -87,14 +87,14 @@ func New(
}
}

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

if len(prevStakedAmount.Bits()) == 0 {
if withdrawableStake.Cmp(MinimumStakeAmount) == -1 {
if stakedAmount.Cmp(MinimumStakeAmount) == -1 {
return common.Hash{}, ErrInsufficientStakeAmount
}
}
Expand All @@ -104,16 +104,16 @@ func (c *contract) DepositStake(ctx context.Context, withdrawableStake *big.Int)
return common.Hash{}, err
}

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

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

receipt, err := c.sendDepositStakeTransaction(ctx, withdrawableStake, c.overlayNonce)
receipt, err := c.sendDepositStakeTransaction(ctx, stakedAmount, c.overlayNonce)
if err != nil {
return common.Hash{}, err
}
Expand All @@ -133,11 +133,11 @@ func (c *contract) ChangeStakeOverlay(ctx context.Context, nonce common.Hash) (c
}

func (c *contract) GetPotentialStake(ctx context.Context) (*big.Int, error) {
withdrawableStake, err := c.getPotentialStake(ctx)
stakedAmount, err := c.getPotentialStake(ctx)
if err != nil {
return nil, fmt.Errorf("staking contract: failed to get stake: %w", err)
}
return withdrawableStake, nil
return stakedAmount, nil
}

func (c *contract) GetWithdrawableStake(ctx context.Context) (*big.Int, error) {
Expand Down Expand Up @@ -292,15 +292,15 @@ func (c *contract) sendTransaction(ctx context.Context, callData []byte, desc st
return receipt, nil
}

func (c *contract) sendDepositStakeTransaction(ctx context.Context, withdrawableStake *big.Int, nonce common.Hash) (*types.Receipt, error) {
callData, err := c.stakingContractABI.Pack("manageStake", nonce, withdrawableStake)
func (c *contract) sendDepositStakeTransaction(ctx context.Context, stakedAmount *big.Int, nonce common.Hash) (*types.Receipt, error) {
callData, err := c.stakingContractABI.Pack("manageStake", nonce, stakedAmount)
if err != nil {
return nil, err
}

receipt, err := c.sendTransaction(ctx, callData, depositStakeDescription)
if err != nil {
return nil, fmt.Errorf("deposit stake: withdrawableStake %d: %w", withdrawableStake, err)
return nil, fmt.Errorf("deposit stake: stakedAmount %d: %w", stakedAmount, err)
}

return receipt, nil
Expand Down
Loading

0 comments on commit 3ebd780

Please sign in to comment.