Skip to content

Commit

Permalink
feat: update existing delegation check endpoint to add code in respon…
Browse files Browse the repository at this point in the history
…se (#63)
  • Loading branch information
jrwbabylonlab authored Sep 30, 2024
1 parent 59c4e85 commit a598e22
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
15 changes: 13 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ const docTemplate = `{
],
"responses": {
"200": {
"description": "Result",
"description": "Delegation check result",
"schema": {
"$ref": "#/definitions/handlers.Result"
"$ref": "#/definitions/handlers.DelegationCheckPublicResponse"
}
},
"400": {
Expand Down Expand Up @@ -379,6 +379,17 @@ const docTemplate = `{
}
}
},
"handlers.DelegationCheckPublicResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"data": {
"type": "boolean"
}
}
},
"handlers.PublicResponse-array_services_DelegationPublic": {
"type": "object",
"properties": {
Expand Down
15 changes: 13 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@
],
"responses": {
"200": {
"description": "Result",
"description": "Delegation check result",
"schema": {
"$ref": "#/definitions/handlers.Result"
"$ref": "#/definitions/handlers.DelegationCheckPublicResponse"
}
},
"400": {
Expand Down Expand Up @@ -371,6 +371,17 @@
}
}
},
"handlers.DelegationCheckPublicResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"data": {
"type": "boolean"
}
}
},
"handlers.PublicResponse-array_services_DelegationPublic": {
"type": "object",
"properties": {
Expand Down
11 changes: 9 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ definitions:
statusCode:
type: integer
type: object
handlers.DelegationCheckPublicResponse:
properties:
code:
type: integer
data:
type: boolean
type: object
handlers.PublicResponse-array_services_DelegationPublic:
properties:
data:
Expand Down Expand Up @@ -326,9 +333,9 @@ paths:
- application/json
responses:
"200":
description: Result
description: Delegation check result
schema:
$ref: '#/definitions/handlers.Result'
$ref: '#/definitions/handlers.DelegationCheckPublicResponse'
"400":
description: 'Error: Bad Request'
schema:
Expand Down
4 changes: 4 additions & 0 deletions internal/api/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type Handler struct {
services *services.Services
}

type ResultOptions struct {
Code int
}

type paginationResponse struct {
NextKey string `json:"next_key"`
}
Expand Down
20 changes: 17 additions & 3 deletions internal/api/handlers/staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/utils"
)

type DelegationCheckPublicResponse struct {
Data bool `json:"data"`
Code int `json:"code"`
}

// GetStakerDelegations @Summary Get staker delegations
// @Description Retrieves delegations for a given staker
// @Produce json
Expand Down Expand Up @@ -46,7 +51,7 @@ func (h *Handler) GetStakerDelegations(request *http.Request) (*Result, *types.E
// @Produce json
// @Param address query string true "Staker BTC address in Taproot/Native Segwit format"
// @Param timeframe query string false "Check if the delegation is active within the provided timeframe" Enums(today)
// @Success 200 {object} Result "Result"
// @Success 200 {object} DelegationCheckPublicResponse "Delegation check result"
// @Failure 400 {object} types.Error "Error: Bad Request"
// @Router /v1/staker/delegation/check [get]
func (h *Handler) CheckStakerDelegationExist(request *http.Request) (*Result, *types.Error) {
Expand All @@ -65,7 +70,7 @@ func (h *Handler) CheckStakerDelegationExist(request *http.Request) (*Result, *t
return nil, err
}
if _, exist := addressToPkMapping[address]; !exist {
return NewResult(false), nil
return buildDelegationCheckResponse(false), nil
}

exist, err := h.services.CheckStakerHasActiveDelegationByPk(
Expand All @@ -75,7 +80,16 @@ func (h *Handler) CheckStakerDelegationExist(request *http.Request) (*Result, *t
return nil, err
}

return NewResult(exist), nil
return buildDelegationCheckResponse(exist), nil
}

func buildDelegationCheckResponse(exist bool) *Result {
return &Result{
Data: &DelegationCheckPublicResponse{
Data: exist, Code: 0,
},
Status: http.StatusOK,
}
}

func parseTimeframeToAfterTimestamp(timeframe string) (int64, *types.Error) {
Expand Down
11 changes: 6 additions & 5 deletions internal/api/middlewares/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
const (
maxAge = 300
stakerDelegationCheckPath = "/v1/staker/delegation/check"
galxeOrigin = "https://app.galxe.com"
dashboardGalxeOrigin = "https://dashboard.galxe.com"
)

func CorsMiddleware(cfg *config.Config) func(http.Handler) http.Handler {
Expand All @@ -21,7 +21,7 @@ func CorsMiddleware(cfg *config.Config) func(http.Handler) http.Handler {
if r.URL.Path == stakerDelegationCheckPath {
// Return CORS options specific to this route
return cors.Options{
AllowedOrigins: []string{galxeOrigin},
AllowedOrigins: []string{dashboardGalxeOrigin},
AllowedMethods: []string{"GET", "OPTIONS", "POST"},
MaxAge: maxAge,
// Below is a workaround to allow the custom CORS header to be set.
Expand All @@ -44,9 +44,10 @@ func CorsMiddleware(cfg *config.Config) func(http.Handler) http.Handler {
cors := cors.New(options)
corsHandler := cors.Handler(next)

// Set the custom cors header for the special route for GET requests
if r.URL.Path == stakerDelegationCheckPath {
w.Header().Set("Access-Control-Allow-Origin", galxeOrigin)
origin := r.Header.Get("Origin")
// Set the custom cors header for the special route for GET requests from Galxe
if r.URL.Path == stakerDelegationCheckPath && origin == dashboardGalxeOrigin {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST")
if r.Method == http.MethodOptions {
// This is a preflight request, respond with 204 immediately
Expand Down
17 changes: 13 additions & 4 deletions tests/integration_test/staker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestActiveStakingFetchedByStakerPkWithInvalidPaginationKey(t *testing.T) {
assert.Equal(t, "invalid pagination key format", response.Message)
}

func TestCheckStakerDelegationAllowOptionRequest(t *testing.T) {
func TestCheckStakerDelegationAllowOptionRequestForGalxe(t *testing.T) {
testServer := setupTestServer(t, nil)
defer testServer.Close()

Expand All @@ -144,7 +144,7 @@ func TestCheckStakerDelegationAllowOptionRequest(t *testing.T) {
req, err := http.NewRequest("OPTIONS", url, nil)
assert.NoError(t, err)
req.Header.Add("Access-Control-Request-Method", "GET")
req.Header.Add("Origin", "https://app.galxe.com")
req.Header.Add("Origin", "https://dashboard.galxe.com")
req.Header.Add("Access-Control-Request-Headers", "Content-Type")

// Send the request
Expand All @@ -154,8 +154,15 @@ func TestCheckStakerDelegationAllowOptionRequest(t *testing.T) {

// Check that the status code is HTTP 204
assert.Equal(t, http.StatusNoContent, resp.StatusCode, "expected HTTP 204")
assert.Equal(t, "https://app.galxe.com", resp.Header.Get("Access-Control-Allow-Origin"), "expected Access-Control-Allow-Origin to be https://app.galxe.com")
assert.Equal(t, "https://dashboard.galxe.com", resp.Header.Get("Access-Control-Allow-Origin"), "expected Access-Control-Allow-Origin to be https://dashboard.galxe.com")
assert.Equal(t, "GET, OPTIONS, POST", resp.Header.Get("Access-Control-Allow-Methods"), "expected Access-Control-Allow-Methods to be GET and OPTIONS")

// Try with a different origin
req.Header.Add("Origin", "https://dashboard.galxe.com")
resp, err = client.Do(req)
assert.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusNoContent, resp.StatusCode, "expected HTTP 204")
}

func FuzzCheckStakerActiveDelegations(f *testing.F) {
Expand Down Expand Up @@ -384,10 +391,12 @@ func fetchCheckStakerActiveDelegations(
bodyBytes, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "reading response body should not fail")

var response handlers.PublicResponse[bool]
var response handlers.DelegationCheckPublicResponse
err = json.Unmarshal(bodyBytes, &response)
assert.NoError(t, err, "unmarshalling response body should not fail")

assert.Equal(t, response.Code, 0, "expected response code to be 0")

return response.Data
}

Expand Down

0 comments on commit a598e22

Please sign in to comment.