Skip to content

Commit

Permalink
fix: commited stake
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Jul 22, 2024
1 parent de841bd commit bb0e6f4
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 197 deletions.
10 changes: 9 additions & 1 deletion pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,14 @@ func (s *Service) mountBusinessDebug() {
})),
)

handle("/stake/withdrawable", web.ChainHandlers(
s.stakingAccessHandler,
s.gasConfigMiddleware("get withdrawable stake"),
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getWithdrawableStakeHandler),
})),
)

handle("/stake/{amount}", web.ChainHandlers(
s.stakingAccessHandler,
s.gasConfigMiddleware("deposit stake"),
Expand All @@ -579,7 +587,7 @@ func (s *Service) mountBusinessDebug() {
s.stakingAccessHandler,
s.gasConfigMiddleware("get or withdraw stake"),
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getStakedAmountHandler),
"GET": http.HandlerFunc(s.getCommittedStakeHandler),
"DELETE": http.HandlerFunc(s.withdrawStakeHandler),
})),
)
Expand Down
18 changes: 16 additions & 2 deletions pkg/api/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,24 @@ func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request)
})
}

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

stakedAmount, err := s.stakingContract.GetStake(r.Context())
stakedAmount, err := s.stakingContract.GetCommittedStake(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{StakedAmount: bigint.Wrap(stakedAmount)})
}

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

stakedAmount, 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")
Expand Down
32 changes: 31 additions & 1 deletion pkg/api/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestDepositStake(t *testing.T) {
})
}

func TestGetStake(t *testing.T) {
func TestGetStakeCommitted(t *testing.T) {
t.Parallel()

t.Run("ok", func(t *testing.T) {
Expand Down Expand Up @@ -135,6 +135,36 @@ func TestGetStake(t *testing.T) {
})
}

func TestGetStakeWithdrawable(t *testing.T) {
t.Parallel()

t.Run("ok", func(t *testing.T) {
t.Parallel()

contract := stakingContractMock.New(
stakingContractMock.WithGetStake(func(ctx context.Context) (*big.Int, error) {
return big.NewInt(1), nil
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodGet, "/stake/withdrawable", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&api.GetStakeResponse{StakedAmount: bigint.Wrap(big.NewInt(1))}))
})

t.Run("with error", func(t *testing.T) {
t.Parallel()

contractWithError := stakingContractMock.New(
stakingContractMock.WithGetStake(func(ctx context.Context) (*big.Int, error) {
return big.NewInt(0), fmt.Errorf("get stake failed")
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contractWithError})
jsonhttptest.Request(t, ts, http.MethodGet, "/stake/withdrawable", http.StatusInternalServerError,
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusInternalServerError, Message: "get staked amount failed"}))
})
}

func Test_stakingDepositHandler_invalidInputs(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ func NewBee(
stakingContract := staking.New(overlayEthAddress, stakingContractAddress, abiutil.MustParseABI(chainCfg.StakingABI), bzzTokenAddress, transactionService, common.BytesToHash(nonce), o.TrxDebugMode)

if chainEnabled && changedOverlay {
stake, err := stakingContract.GetStake(ctx)
stake, err := stakingContract.GetCommittedStake(ctx)
if err != nil {
return nil, errors.New("getting stake balance")
}
Expand Down
Loading

0 comments on commit bb0e6f4

Please sign in to comment.