Skip to content

Commit b974e20

Browse files
committed
fix: clean up 2
1 parent 606ffc8 commit b974e20

5 files changed

Lines changed: 35 additions & 26 deletions

File tree

openapi/Swarm.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,7 +2558,11 @@ paths:
25582558
schema:
25592559
$ref: "SwarmCommon.yaml#/components/schemas/StakeTransactionResponse"
25602560
"400":
2561-
$ref: "SwarmCommon.yaml#/components/responses/400"
2561+
description: Deposit amount is below the required minimum.
2562+
content:
2563+
application/json:
2564+
schema:
2565+
$ref: "SwarmCommon.yaml#/components/schemas/StakeDepositErrorResponse"
25622566
"500":
25632567
$ref: "SwarmCommon.yaml#/components/responses/500"
25642568
default:

openapi/SwarmCommon.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,16 @@ components:
684684
minimumDeposit:
685685
$ref: "#/components/schemas/BigInt"
686686

687+
StakeDepositErrorResponse:
688+
type: object
689+
properties:
690+
code:
691+
type: integer
692+
message:
693+
type: string
694+
minimumDeposit:
695+
$ref: "#/components/schemas/BigInt"
696+
687697
GetWithdrawableResponse:
688698
type: object
689699
properties:

pkg/api/export_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type (
9494
WalletResponse = walletResponse
9595
WalletTxResponse = walletTxResponse
9696
GetStakeResponse = getStakeResponse
97+
StakeDepositErrorResponse = stakeDepositErrorResponse
9798
GetWithdrawableResponse = getWithdrawableResponse
9899
StakeTransactionReponse = stakeTransactionReponse
99100
StatusSnapshotResponse = statusSnapshotResponse

pkg/api/staking.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package api
66

77
import (
88
"errors"
9-
"fmt"
109
"math/big"
1110
"net/http"
1211

@@ -43,6 +42,12 @@ type stakeTransactionReponse struct {
4342
TxHash string `json:"txHash"`
4443
}
4544

45+
type stakeDepositErrorResponse struct {
46+
Code int `json:"code"`
47+
Message string `json:"message"`
48+
MinimumDeposit *bigint.BigInt `json:"minimumDeposit"`
49+
}
50+
4651
func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request) {
4752
logger := s.logger.WithName("post_stake_deposit").Build()
4853

@@ -56,16 +61,15 @@ func (s *Service) stakingDepositHandler(w http.ResponseWriter, r *http.Request)
5661

5762
txHash, err := s.stakingContract.DepositStake(r.Context(), paths.Amount)
5863
if err != nil {
59-
if errors.Is(err, staking.ErrInsufficientStakeAmount) {
60-
minDeposit := staking.MinimumStakeAmount
61-
var minErr *staking.MinDepositError
62-
if errors.As(err, &minErr) && minErr.Minimum != nil {
63-
minDeposit = minErr.Minimum
64-
}
65-
msg := fmt.Sprintf("insufficient stake amount, minimum is %s", minDeposit)
66-
logger.Debug("insufficient stake amount", "minimum_stake", minDeposit, "error", err)
64+
var minErr *staking.MinDepositError
65+
if errors.As(err, &minErr) {
66+
logger.Debug("insufficient stake amount", "minimum_deposit", minErr.Minimum, "error", err)
6767
logger.Error(nil, "insufficient stake amount")
68-
jsonhttp.BadRequest(w, msg)
68+
jsonhttp.BadRequest(w, stakeDepositErrorResponse{
69+
Code: http.StatusBadRequest,
70+
Message: "insufficient stake amount",
71+
MinimumDeposit: bigint.Wrap(minErr.Minimum),
72+
})
6973
return
7074
}
7175
if errors.Is(err, staking.ErrNotImplemented) {

pkg/api/staking_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,6 @@ func TestDepositStake(t *testing.T) {
4343
jsonhttptest.Request(t, ts, http.MethodPost, depositStake(minStake), http.StatusOK)
4444
})
4545

46-
t.Run("with invalid stake amount", func(t *testing.T) {
47-
t.Parallel()
48-
49-
invalidMinStake := big.NewInt(0).String()
50-
contract := stakingContractMock.New(
51-
stakingContractMock.WithDepositStake(func(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
52-
return common.Hash{}, staking.ErrInsufficientStakeAmount
53-
}),
54-
)
55-
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
56-
jsonhttptest.Request(t, ts, http.MethodPost, depositStake(invalidMinStake), http.StatusBadRequest,
57-
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusBadRequest, Message: "insufficient stake amount, minimum is 100000000000000000"}))
58-
})
59-
6046
t.Run("with insufficient amount reports minimum", func(t *testing.T) {
6147
t.Parallel()
6248

@@ -68,7 +54,11 @@ func TestDepositStake(t *testing.T) {
6854
)
6955
ts, _, _, _ := newTestServer(t, testServerOptions{StakingContract: contract})
7056
jsonhttptest.Request(t, ts, http.MethodPost, depositStake("1"), http.StatusBadRequest,
71-
jsonhttptest.WithExpectedJSONResponse(&jsonhttp.StatusResponse{Code: http.StatusBadRequest, Message: "insufficient stake amount, minimum is 123"}))
57+
jsonhttptest.WithExpectedJSONResponse(&api.StakeDepositErrorResponse{
58+
Code: http.StatusBadRequest,
59+
Message: "insufficient stake amount",
60+
MinimumDeposit: bigint.Wrap(minDeposit),
61+
}))
7262
})
7363

7464
t.Run("out of funds", func(t *testing.T) {

0 commit comments

Comments
 (0)