-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaccept_pegout_quote.go
More file actions
58 lines (52 loc) · 2.21 KB
/
accept_pegout_quote.go
File metadata and controls
58 lines (52 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package handlers
import (
"errors"
"net/http"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/entrypoints/rest"
"github.com/rsksmart/liquidity-provider-server/internal/entities/quote"
"github.com/rsksmart/liquidity-provider-server/internal/usecases"
"github.com/rsksmart/liquidity-provider-server/internal/usecases/pegout"
"github.com/rsksmart/liquidity-provider-server/pkg"
)
// NewAcceptPegoutQuoteHandler
// @Title Accept Quote Pegout
// @Description Accepts Quote Pegout
// @Param QuoteHash body pkg.AcceptQuoteRequest true "Quote Hash"
// @Success 200 object pkg.AcceptPegoutResponse
// @Route /pegout/acceptQuote [post]
func NewAcceptPegoutQuoteHandler(useCase *pegout.AcceptQuoteUseCase) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
var err error
acceptRequest := pkg.AcceptQuoteRequest{}
if err = rest.DecodeRequest(w, req, &acceptRequest); err != nil {
return
} else if err = rest.ValidateRequest(w, &acceptRequest); err != nil {
return
}
if err = quote.ValidateQuoteHash(acceptRequest.QuoteHash); err != nil {
jsonErr := rest.NewErrorResponseWithDetails("invalid quote hash", rest.DetailsFromError(err), true)
rest.JsonErrorResponse(w, http.StatusBadRequest, jsonErr)
return
}
acceptedQuote, err := useCase.Run(req.Context(), acceptRequest.QuoteHash)
if errors.Is(err, usecases.QuoteNotFoundError) ||
errors.Is(err, usecases.ExpiredQuoteError) {
jsonErr := rest.NewErrorResponseWithDetails("invalid quote hash", rest.DetailsFromError(err), true)
rest.JsonErrorResponse(w, http.StatusNotFound, jsonErr)
return
} else if errors.Is(err, usecases.NoLiquidityError) {
jsonErr := rest.NewErrorResponseWithDetails("not enough liquidity", rest.DetailsFromError(err), true)
rest.JsonErrorResponse(w, http.StatusConflict, jsonErr)
return
} else if err != nil {
jsonErr := rest.NewErrorResponseWithDetails(UnknownErrorMessage, rest.DetailsFromError(err), false)
rest.JsonErrorResponse(w, http.StatusInternalServerError, jsonErr)
return
}
response := pkg.AcceptPegoutResponse{
Signature: acceptedQuote.Signature,
LbcAddress: acceptedQuote.DepositAddress,
}
rest.JsonResponseWithBody(w, http.StatusOK, &response)
}
}