Skip to content

Commit

Permalink
fix: rearrange stake api (ethersphere#4747)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Aug 1, 2024
1 parent c518552 commit 7e12c76
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
21 changes: 19 additions & 2 deletions openapi/Swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,23 @@ paths:
$ref: "SwarmCommon.yaml#/components/responses/500"
default:
description: Default response
delete:
summary: Withdraw the extra withdrawable staked amount.
description: This endpoint withdraws any amount that is possible to withdraw as surplus.
tags:
- Staking
parameters:
- $ref: "SwarmCommon.yaml#/components/parameters/GasPriceParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/GasLimitParameter"
responses:
"200":
$ref: "SwarmCommon.yaml#/components/schemas/StakeTransactionResponse"
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
"500":
$ref: "SwarmCommon.yaml#/components/responses/500"
default:
description: Default response

"/stake/{amount}":
post:
Expand Down Expand Up @@ -2103,8 +2120,8 @@ paths:
default:
description: Default response
delete:
summary: Withdraw the extra withdrawable staked amount.
description: This endpoint withdraws any amount that is possible to withdraw as surplus.
summary: Withdraws all past staked amount back to the wallet.
description: Be aware, this endpoint can only be called when the contract is paused and is in the process of being migrated to a new contract.
tags:
- Staking
parameters:
Expand Down
18 changes: 6 additions & 12 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,19 +559,12 @@ func (s *Service) mountBusinessDebug() {
web.FinalHandlerFunc(s.healthHandler),
))

handle("/stake/migrate", web.ChainHandlers(
s.stakingAccessHandler,
s.gasConfigMiddleware("migrate stake"),
web.FinalHandler(jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.migrateStakeHandler),
})),
)

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

Expand All @@ -585,12 +578,13 @@ func (s *Service) mountBusinessDebug() {

handle("/stake", web.ChainHandlers(
s.stakingAccessHandler,
s.gasConfigMiddleware("get or withdraw stake"),
s.gasConfigMiddleware("get or migrate stake"),
web.FinalHandler(jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.getPotentialStake),
"DELETE": http.HandlerFunc(s.withdrawStakeHandler),
"DELETE": http.HandlerFunc(s.migrateStakeHandler),
})),
)

handle("/redistributionstate", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.redistributionStatusHandler),
})
Expand Down
16 changes: 8 additions & 8 deletions pkg/api/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestWithdrawStake(t *testing.T) {
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake/withdrawable", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(
&api.StakeTransactionReponse{TxHash: txHash.String()}))
})

Expand All @@ -228,7 +228,7 @@ func TestWithdrawStake(t *testing.T) {
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusBadRequest,
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake/withdrawable", http.StatusBadRequest,
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusBadRequest, Message: "insufficient stake to withdraw"}))
})

Expand All @@ -241,7 +241,7 @@ func TestWithdrawStake(t *testing.T) {
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusInternalServerError)
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake/withdrawable", http.StatusInternalServerError)
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusInternalServerError, Message: "cannot withdraw stake"})
})

Expand All @@ -261,7 +261,7 @@ func TestWithdrawStake(t *testing.T) {
StakingContract: contract,
})

jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusOK,
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake/withdrawable", http.StatusOK,
jsonhttptest.WithRequestHeader(api.GasLimitHeader, "2000000"),
)
})
Expand All @@ -281,7 +281,7 @@ func TestMigrateStake(t *testing.T) {
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodPost, "/stake/migrate", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusOK, jsonhttptest.WithExpectedJSONResponse(
&api.StakeTransactionReponse{TxHash: txHash.String()}))
})

Expand All @@ -294,7 +294,7 @@ func TestMigrateStake(t *testing.T) {
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodPost, "/stake/migrate", http.StatusBadRequest,
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusBadRequest,
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusBadRequest, Message: "insufficient stake to migrate"}))
})

Expand All @@ -307,7 +307,7 @@ func TestMigrateStake(t *testing.T) {
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
jsonhttptest.Request(t, ts, http.MethodPost, "/stake/migrate", http.StatusInternalServerError)
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusInternalServerError)
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusInternalServerError, Message: "cannot withdraw stake"})
})

Expand All @@ -327,7 +327,7 @@ func TestMigrateStake(t *testing.T) {
StakingContract: contract,
})

jsonhttptest.Request(t, ts, http.MethodPost, "/stake/migrate", http.StatusOK,
jsonhttptest.Request(t, ts, http.MethodDelete, "/stake", http.StatusOK,
jsonhttptest.WithRequestHeader(api.GasLimitHeader, "2000000"),
)
})
Expand Down

0 comments on commit 7e12c76

Please sign in to comment.