diff --git a/docs/docs.go b/docs/docs.go index 59c98743..0350680d 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -399,39 +399,30 @@ const docTemplate = `{ } } }, - "/v2/finality-providers": { + "/v2/delegation": { "get": { - "description": "Fetches finality providers with optional filtering and pagination", + "description": "Retrieves a delegation by a given transaction hash", "produces": [ "application/json" ], "tags": [ "v2" ], - "summary": "List Finality Providers", + "summary": "Get a delegation", "parameters": [ { "type": "string", - "description": "Pagination key to fetch the next page", - "name": "pagination_key", - "in": "query" - }, - { - "enum": [ - "active", - "standby" - ], - "type": "string", - "description": "Filter by state", - "name": "state", - "in": "query" + "description": "Staking transaction hash in hex format", + "name": "staking_tx_hash_hex", + "in": "query", + "required": true } ], "responses": { "200": { - "description": "List of finality providers and pagination token", + "description": "Staker delegation", "schema": { - "$ref": "#/definitions/handler.PublicResponse-array_v2service_FinalityProviderPublic" + "$ref": "#/definitions/handler.PublicResponse-v2service_StakerDelegationPublic" } }, "400": { @@ -443,56 +434,80 @@ const docTemplate = `{ } } }, - "/v2/global-params": { + "/v2/delegations": { "get": { - "description": "Fetches global parameters for babylon chain and BTC chain", + "description": "Fetches delegations for babylon staking including tvl, total delegations, active tvl, active delegations and total stakers.", "produces": [ "application/json" ], "tags": [ "v2" ], - "summary": "Get Global Parameters", + "summary": "Get Delegations", + "parameters": [ + { + "type": "string", + "description": "Staker public key in hex format", + "name": "staker_pk_hex", + "in": "query", + "required": true + }, + { + "type": "string", + "description": "Pagination key to fetch the next page of delegations", + "name": "pagination_key", + "in": "query" + } + ], "responses": { "200": { - "description": "Global parameters", + "description": "List of staker delegations and pagination token", "schema": { - "$ref": "#/definitions/handler.PublicResponse-v2service_GlobalParamsPublic" + "$ref": "#/definitions/handler.PublicResponse-array_v2service_StakerDelegationPublic" + } + }, + "400": { + "description": "Error: Bad Request", + "schema": { + "$ref": "#/definitions/github_com_babylonlabs-io_staking-api-service_internal_shared_types.Error" } } } } }, - "/v2/staker/delegations": { + "/v2/finality-providers": { "get": { - "description": "Fetches staker delegations for babylon staking including tvl, total delegations, active tvl, active delegations and total stakers.", + "description": "Fetches finality providers with optional filtering and pagination", "produces": [ "application/json" ], "tags": [ "v2" ], - "summary": "Get Staker Delegations", + "summary": "List Finality Providers", "parameters": [ { "type": "string", - "description": "Staker public key in hex format", - "name": "staker_pk_hex", - "in": "query", - "required": true + "description": "Pagination key to fetch the next page", + "name": "pagination_key", + "in": "query" }, { + "enum": [ + "active", + "standby" + ], "type": "string", - "description": "Pagination key to fetch the next page of delegations", - "name": "pagination_key", + "description": "Filter by state", + "name": "state", "in": "query" } ], "responses": { "200": { - "description": "List of staker delegations and pagination token", + "description": "List of finality providers and pagination token", "schema": { - "$ref": "#/definitions/handler.PublicResponse-array_v2service_StakerDelegationPublic" + "$ref": "#/definitions/handler.PublicResponse-array_v2service_FinalityProviderPublic" } }, "400": { @@ -504,6 +519,26 @@ const docTemplate = `{ } } }, + "/v2/global-params": { + "get": { + "description": "Fetches global parameters for babylon chain and BTC chain", + "produces": [ + "application/json" + ], + "tags": [ + "v2" + ], + "summary": "Get Global Parameters", + "responses": { + "200": { + "description": "Global parameters", + "schema": { + "$ref": "#/definitions/handler.PublicResponse-v2service_GlobalParamsPublic" + } + } + } + } + }, "/v2/staker/stats": { "get": { "description": "Fetches staker stats for babylon staking including active tvl, total tvl, active delegations and total delegations.", @@ -688,6 +723,17 @@ const docTemplate = `{ } } }, + "handler.PublicResponse-v2service_StakerDelegationPublic": { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/v2service.StakerDelegationPublic" + }, + "pagination": { + "$ref": "#/definitions/handler.paginationResponse" + } + } + }, "handler.PublicResponse-v2service_StakerStatsPublic": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 5fe2b971..f9478215 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -391,39 +391,30 @@ } } }, - "/v2/finality-providers": { + "/v2/delegation": { "get": { - "description": "Fetches finality providers with optional filtering and pagination", + "description": "Retrieves a delegation by a given transaction hash", "produces": [ "application/json" ], "tags": [ "v2" ], - "summary": "List Finality Providers", + "summary": "Get a delegation", "parameters": [ { "type": "string", - "description": "Pagination key to fetch the next page", - "name": "pagination_key", - "in": "query" - }, - { - "enum": [ - "active", - "standby" - ], - "type": "string", - "description": "Filter by state", - "name": "state", - "in": "query" + "description": "Staking transaction hash in hex format", + "name": "staking_tx_hash_hex", + "in": "query", + "required": true } ], "responses": { "200": { - "description": "List of finality providers and pagination token", + "description": "Staker delegation", "schema": { - "$ref": "#/definitions/handler.PublicResponse-array_v2service_FinalityProviderPublic" + "$ref": "#/definitions/handler.PublicResponse-v2service_StakerDelegationPublic" } }, "400": { @@ -435,56 +426,80 @@ } } }, - "/v2/global-params": { + "/v2/delegations": { "get": { - "description": "Fetches global parameters for babylon chain and BTC chain", + "description": "Fetches delegations for babylon staking including tvl, total delegations, active tvl, active delegations and total stakers.", "produces": [ "application/json" ], "tags": [ "v2" ], - "summary": "Get Global Parameters", + "summary": "Get Delegations", + "parameters": [ + { + "type": "string", + "description": "Staker public key in hex format", + "name": "staker_pk_hex", + "in": "query", + "required": true + }, + { + "type": "string", + "description": "Pagination key to fetch the next page of delegations", + "name": "pagination_key", + "in": "query" + } + ], "responses": { "200": { - "description": "Global parameters", + "description": "List of staker delegations and pagination token", "schema": { - "$ref": "#/definitions/handler.PublicResponse-v2service_GlobalParamsPublic" + "$ref": "#/definitions/handler.PublicResponse-array_v2service_StakerDelegationPublic" + } + }, + "400": { + "description": "Error: Bad Request", + "schema": { + "$ref": "#/definitions/github_com_babylonlabs-io_staking-api-service_internal_shared_types.Error" } } } } }, - "/v2/staker/delegations": { + "/v2/finality-providers": { "get": { - "description": "Fetches staker delegations for babylon staking including tvl, total delegations, active tvl, active delegations and total stakers.", + "description": "Fetches finality providers with optional filtering and pagination", "produces": [ "application/json" ], "tags": [ "v2" ], - "summary": "Get Staker Delegations", + "summary": "List Finality Providers", "parameters": [ { "type": "string", - "description": "Staker public key in hex format", - "name": "staker_pk_hex", - "in": "query", - "required": true + "description": "Pagination key to fetch the next page", + "name": "pagination_key", + "in": "query" }, { + "enum": [ + "active", + "standby" + ], "type": "string", - "description": "Pagination key to fetch the next page of delegations", - "name": "pagination_key", + "description": "Filter by state", + "name": "state", "in": "query" } ], "responses": { "200": { - "description": "List of staker delegations and pagination token", + "description": "List of finality providers and pagination token", "schema": { - "$ref": "#/definitions/handler.PublicResponse-array_v2service_StakerDelegationPublic" + "$ref": "#/definitions/handler.PublicResponse-array_v2service_FinalityProviderPublic" } }, "400": { @@ -496,6 +511,26 @@ } } }, + "/v2/global-params": { + "get": { + "description": "Fetches global parameters for babylon chain and BTC chain", + "produces": [ + "application/json" + ], + "tags": [ + "v2" + ], + "summary": "Get Global Parameters", + "responses": { + "200": { + "description": "Global parameters", + "schema": { + "$ref": "#/definitions/handler.PublicResponse-v2service_GlobalParamsPublic" + } + } + } + } + }, "/v2/staker/stats": { "get": { "description": "Fetches staker stats for babylon staking including active tvl, total tvl, active delegations and total delegations.", @@ -680,6 +715,17 @@ } } }, + "handler.PublicResponse-v2service_StakerDelegationPublic": { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/v2service.StakerDelegationPublic" + }, + "pagination": { + "$ref": "#/definitions/handler.paginationResponse" + } + } + }, "handler.PublicResponse-v2service_StakerStatsPublic": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 8b6ee916..d786ba85 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -87,6 +87,13 @@ definitions: pagination: $ref: '#/definitions/handler.paginationResponse' type: object + handler.PublicResponse-v2service_StakerDelegationPublic: + properties: + data: + $ref: '#/definitions/v2service.StakerDelegationPublic' + pagination: + $ref: '#/definitions/handler.paginationResponse' + type: object handler.PublicResponse-v2service_StakerStatsPublic: properties: data: @@ -727,6 +734,57 @@ paths: summary: Check unbonding eligibility tags: - v1 + /v2/delegation: + get: + description: Retrieves a delegation by a given transaction hash + parameters: + - description: Staking transaction hash in hex format + in: query + name: staking_tx_hash_hex + required: true + type: string + produces: + - application/json + responses: + "200": + description: Staker delegation + schema: + $ref: '#/definitions/handler.PublicResponse-v2service_StakerDelegationPublic' + "400": + description: 'Error: Bad Request' + schema: + $ref: '#/definitions/github_com_babylonlabs-io_staking-api-service_internal_shared_types.Error' + summary: Get a delegation + tags: + - v2 + /v2/delegations: + get: + description: Fetches delegations for babylon staking including tvl, total delegations, + active tvl, active delegations and total stakers. + parameters: + - description: Staker public key in hex format + in: query + name: staker_pk_hex + required: true + type: string + - description: Pagination key to fetch the next page of delegations + in: query + name: pagination_key + type: string + produces: + - application/json + responses: + "200": + description: List of staker delegations and pagination token + schema: + $ref: '#/definitions/handler.PublicResponse-array_v2service_StakerDelegationPublic' + "400": + description: 'Error: Bad Request' + schema: + $ref: '#/definitions/github_com_babylonlabs-io_staking-api-service_internal_shared_types.Error' + summary: Get Delegations + tags: + - v2 /v2/finality-providers: get: description: Fetches finality providers with optional filtering and pagination @@ -769,34 +827,6 @@ paths: summary: Get Global Parameters tags: - v2 - /v2/staker/delegations: - get: - description: Fetches staker delegations for babylon staking including tvl, total - delegations, active tvl, active delegations and total stakers. - parameters: - - description: Staker public key in hex format - in: query - name: staker_pk_hex - required: true - type: string - - description: Pagination key to fetch the next page of delegations - in: query - name: pagination_key - type: string - produces: - - application/json - responses: - "200": - description: List of staker delegations and pagination token - schema: - $ref: '#/definitions/handler.PublicResponse-array_v2service_StakerDelegationPublic' - "400": - description: 'Error: Bad Request' - schema: - $ref: '#/definitions/github_com_babylonlabs-io_staking-api-service_internal_shared_types.Error' - summary: Get Staker Delegations - tags: - - v2 /v2/staker/stats: get: description: Fetches staker stats for babylon staking including active tvl, diff --git a/internal/indexer/db/client/delegation.go b/internal/indexer/db/client/delegation.go index 781f2ed0..92950bcc 100644 --- a/internal/indexer/db/client/delegation.go +++ b/internal/indexer/db/client/delegation.go @@ -2,17 +2,36 @@ package indexerdbclient import ( "context" + "errors" indexerdbmodel "github.com/babylonlabs-io/staking-api-service/internal/indexer/db/model" "github.com/babylonlabs-io/staking-api-service/internal/shared/db" dbmodel "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) -func (indexerdbclient *IndexerDatabase) GetStakerDelegations( +func (indexerdbclient *IndexerDatabase) GetDelegation(ctx context.Context, stakingTxHashHex string) (*indexerdbmodel.IndexerDelegationDetails, error) { + client := indexerdbclient.Client.Database(indexerdbclient.DbName).Collection(indexerdbmodel.BTCDelegationDetailsCollection) + filter := bson.M{"_id": stakingTxHashHex} + var delegation indexerdbmodel.IndexerDelegationDetails + err := client.FindOne(ctx, filter).Decode(&delegation) + if err != nil { + if errors.Is(err, mongo.ErrNoDocuments) { + return nil, &db.NotFoundError{ + Key: stakingTxHashHex, + Message: "Delegation not found", + } + } + return nil, err + } + return &delegation, nil +} + +func (indexerdbclient *IndexerDatabase) GetDelegations( ctx context.Context, stakerPKHex string, paginationToken string, -) (*db.DbResultMap[indexerdbmodel.IndexerStakerDelegationDetails], error) { +) (*db.DbResultMap[indexerdbmodel.IndexerDelegationDetails], error) { client := indexerdbclient.Client.Database(indexerdbclient.DbName).Collection(indexerdbmodel.BTCDelegationDetailsCollection) // Base filter with stakingTxHashHex @@ -25,7 +44,7 @@ func (indexerdbclient *IndexerDatabase) GetStakerDelegations( // Decode the pagination token if it exists if paginationToken != "" { - decodedToken, err := dbmodel.DecodePaginationToken[indexerdbmodel.IndexerStakerDelegationPagination](paginationToken) + decodedToken, err := dbmodel.DecodePaginationToken[indexerdbmodel.IndexerDelegationPagination](paginationToken) if err != nil { return nil, &db.InvalidPaginationTokenError{ Message: "Invalid pagination token", @@ -41,6 +60,6 @@ func (indexerdbclient *IndexerDatabase) GetStakerDelegations( return db.FindWithPagination( ctx, client, filter, options, indexerdbclient.Cfg.MaxPaginationLimit, - indexerdbmodel.BuildStakerDelegationPaginationToken, + indexerdbmodel.BuildDelegationPaginationToken, ) } diff --git a/internal/indexer/db/client/interface.go b/internal/indexer/db/client/interface.go index 12156666..e2c74b0c 100644 --- a/internal/indexer/db/client/interface.go +++ b/internal/indexer/db/client/interface.go @@ -19,5 +19,6 @@ type IndexerDBClient interface { SearchFinalityProviders(ctx context.Context, searchQuery string, paginationToken string) (*db.DbResultMap[indexerdbmodel.IndexerFinalityProviderDetails], error) GetFinalityProviderByPk(ctx context.Context, fpPk string) (*indexerdbmodel.IndexerFinalityProviderDetails, error) // Staker Delegations - GetStakerDelegations(ctx context.Context, stakerPKHex string, paginationToken string) (*db.DbResultMap[indexerdbmodel.IndexerStakerDelegationDetails], error) + GetDelegation(ctx context.Context, stakingTxHashHex string) (*indexerdbmodel.IndexerDelegationDetails, error) + GetDelegations(ctx context.Context, stakerPKHex string, paginationToken string) (*db.DbResultMap[indexerdbmodel.IndexerDelegationDetails], error) } diff --git a/internal/indexer/db/model/delegation.go b/internal/indexer/db/model/delegation.go index 4cad24d2..0605ac76 100644 --- a/internal/indexer/db/model/delegation.go +++ b/internal/indexer/db/model/delegation.go @@ -5,12 +5,12 @@ import ( dbmodel "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model" ) -type IndexerStakerDelegationPagination struct { +type IndexerDelegationPagination struct { StakingTxHashHex string `json:"staking_tx_hash_hex"` StartHeight uint32 `json:"start_height"` } -type IndexerStakerDelegationDetails struct { +type IndexerDelegationDetails struct { StakingTxHashHex string `bson:"_id"` // Primary key ParamsVersion string `bson:"params_version"` FinalityProviderBtcPksHex []string `bson:"finality_provider_btc_pks_hex"` @@ -24,8 +24,8 @@ type IndexerStakerDelegationDetails struct { EndHeight uint32 `bson:"end_height"` } -func BuildStakerDelegationPaginationToken(d IndexerStakerDelegationDetails) (string, error) { - page := &IndexerStakerDelegationPagination{ +func BuildDelegationPaginationToken(d IndexerDelegationDetails) (string, error) { + page := &IndexerDelegationPagination{ StakingTxHashHex: d.StakingTxHashHex, StartHeight: d.StartHeight, } diff --git a/internal/shared/api/routes.go b/internal/shared/api/routes.go index 521c3378..2de1aee9 100644 --- a/internal/shared/api/routes.go +++ b/internal/shared/api/routes.go @@ -37,6 +37,7 @@ func (a *Server) SetupRoutes(r *chi.Mux) { r.Get("/v2/stats", registerHandler(handlers.V2Handler.GetStats)) r.Get("/v2/finality-providers", registerHandler(handlers.V2Handler.GetFinalityProviders)) r.Get("/v2/global-params", registerHandler(handlers.V2Handler.GetGlobalParams)) - r.Get("/v2/staker/delegations", registerHandler(handlers.V2Handler.GetStakerDelegations)) + r.Get("/v2/delegation", registerHandler(handlers.V2Handler.GetDelegation)) + r.Get("/v2/delegations", registerHandler(handlers.V2Handler.GetDelegations)) r.Get("/v2/staker/stats", registerHandler(handlers.V2Handler.GetStakerStats)) } diff --git a/internal/v2/api/handlers/delegation.go b/internal/v2/api/handlers/delegation.go new file mode 100644 index 00000000..c2869c89 --- /dev/null +++ b/internal/v2/api/handlers/delegation.go @@ -0,0 +1,57 @@ +package v2handlers + +import ( + "net/http" + + "github.com/babylonlabs-io/staking-api-service/internal/shared/api/handlers/handler" + "github.com/babylonlabs-io/staking-api-service/internal/shared/types" +) + +// GetDelegation @Summary Get a delegation +// @Summary Get a delegation +// @Description Retrieves a delegation by a given transaction hash +// @Produce json +// @Tags v2 +// @Param staking_tx_hash_hex query string true "Staking transaction hash in hex format" +// @Success 200 {object} handler.PublicResponse[v2service.StakerDelegationPublic] "Staker delegation" +// @Failure 400 {object} types.Error "Error: Bad Request" +// @Router /v2/delegation [get] +func (h *V2Handler) GetDelegation(request *http.Request) (*handler.Result, *types.Error) { + stakingTxHash, err := handler.ParseTxHashQuery(request, "staking_tx_hash_hex") + if err != nil { + return nil, err + } + delegation, err := h.Service.GetDelegation(request.Context(), stakingTxHash) + if err != nil { + return nil, err + } + + return handler.NewResult(delegation), nil +} + +// GetDelegations gets delegations for babylon staking +// @Summary Get Delegations +// @Description Fetches delegations for babylon staking including tvl, total delegations, active tvl, active delegations and total stakers. +// @Produce json +// @Tags v2 +// @Param staker_pk_hex query string true "Staker public key in hex format" +// @Param pagination_key query string false "Pagination key to fetch the next page of delegations" +// @Success 200 {object} handler.PublicResponse[[]v2service.StakerDelegationPublic]{array} "List of staker delegations and pagination token" +// @Failure 400 {object} types.Error "Error: Bad Request" +// @Router /v2/delegations [get] +func (h *V2Handler) GetDelegations(request *http.Request) (*handler.Result, *types.Error) { + const stakerPKHexKey string = "staker_pk_hex" + stakerPKHex, err := handler.ParsePublicKeyQuery(request, stakerPKHexKey, false) + if err != nil { + return nil, err + } + paginationKey, err := handler.ParsePaginationQuery(request) + if err != nil { + return nil, err + } + delegations, paginationToken, err := h.Service.GetDelegations(request.Context(), stakerPKHex, paginationKey) + if err != nil { + return nil, err + } + return handler.NewResultWithPagination(delegations, paginationToken), nil +} diff --git a/internal/v2/api/handlers/staker.go b/internal/v2/api/handlers/staker.go index dde867a8..0a16a948 100644 --- a/internal/v2/api/handlers/staker.go +++ b/internal/v2/api/handlers/staker.go @@ -7,33 +7,6 @@ import ( "github.com/babylonlabs-io/staking-api-service/internal/shared/types" ) -// GetStakerDelegations gets staker delegations for babylon staking -// @Summary Get Staker Delegations -// @Description Fetches staker delegations for babylon staking including tvl, total delegations, active tvl, active delegations and total stakers. -// @Produce json -// @Tags v2 -// @Param staker_pk_hex query string true "Staker public key in hex format" -// @Param pagination_key query string false "Pagination key to fetch the next page of delegations" -// @Success 200 {object} handler.PublicResponse[[]v2service.StakerDelegationPublic]{array} "List of staker delegations and pagination token" -// @Failure 400 {object} types.Error "Error: Bad Request" -// @Router /v2/staker/delegations [get] -func (h *V2Handler) GetStakerDelegations(request *http.Request) (*handler.Result, *types.Error) { - const stakerPKHexKey string = "staker_pk_hex" - stakerPKHex, err := handler.ParsePublicKeyQuery(request, stakerPKHexKey, false) - if err != nil { - return nil, err - } - paginationKey, err := handler.ParsePaginationQuery(request) - if err != nil { - return nil, err - } - delegations, paginationToken, err := h.Service.GetStakerDelegations(request.Context(), stakerPKHex, paginationKey) - if err != nil { - return nil, err - } - return handler.NewResultWithPagination(delegations, paginationToken), nil -} - // GetStakerStats gets staker stats for babylon staking // @Summary Get Staker Stats // @Description Fetches staker stats for babylon staking including active tvl, total tvl, active delegations and total delegations. diff --git a/internal/v2/service/delegation.go b/internal/v2/service/delegation.go new file mode 100644 index 00000000..1b46a7f4 --- /dev/null +++ b/internal/v2/service/delegation.go @@ -0,0 +1,91 @@ +package v2service + +import ( + "context" + "net/http" + + indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types" + "github.com/babylonlabs-io/staking-api-service/internal/shared/types" +) + +type DelegationStaking struct { + StakingTxHashHex string `json:"staking_tx_hash_hex"` + StakingTime string `json:"staking_time"` + StakingAmount string `json:"staking_amount"` + StartHeight uint32 `json:"start_height,omitempty"` + EndHeight uint32 `json:"end_height,omitempty"` +} + +type DelegationUnbonding struct { + UnbondingTime string `json:"unbonding_time"` + UnbondingTx string `json:"unbonding_tx"` +} + +type StakerDelegationPublic struct { + ParamsVersion string `json:"params_version"` + StakerBtcPkHex string `json:"staker_btc_pk_hex"` + FinalityProviderBtcPksHex []string `json:"finality_provider_btc_pks_hex"` + DelegationStaking DelegationStaking `json:"delegation_staking"` + DelegationUnbonding DelegationUnbonding `json:"delegation_unbonding"` + State indexertypes.DelegationState `json:"state"` +} + +func (s *V2Service) GetDelegation(ctx context.Context, stakingTxHashHex string) (*StakerDelegationPublic, *types.Error) { + delegation, err := s.DbClients.IndexerDBClient.GetDelegation(ctx, stakingTxHashHex) + if err != nil { + return nil, types.NewErrorWithMsg(http.StatusInternalServerError, types.InternalServiceError, "failed to get staker delegation") + } + + delegationPublic := &StakerDelegationPublic{ + ParamsVersion: delegation.ParamsVersion, + FinalityProviderBtcPksHex: delegation.FinalityProviderBtcPksHex, + StakerBtcPkHex: delegation.StakerBtcPkHex, + DelegationStaking: DelegationStaking{ + StakingTxHashHex: delegation.StakingTxHashHex, + StakingTime: delegation.StakingTime, + StakingAmount: delegation.StakingAmount, + StartHeight: delegation.StartHeight, + EndHeight: delegation.EndHeight, + }, + DelegationUnbonding: DelegationUnbonding{ + UnbondingTime: delegation.UnbondingTime, + UnbondingTx: delegation.UnbondingTx, + }, + State: delegation.State, + } + return delegationPublic, nil +} + +func (s *V2Service) GetDelegations(ctx context.Context, stakingTxHashHex string, paginationKey string) ([]*StakerDelegationPublic, string, *types.Error) { + resultMap, err := s.DbClients.IndexerDBClient.GetDelegations(ctx, stakingTxHashHex, paginationKey) + if err != nil { + return nil, "", types.NewErrorWithMsg(http.StatusInternalServerError, types.InternalServiceError, "failed to get staker delegations") + } + + // Initialize result structure + delegationsPublic := make([]*StakerDelegationPublic, 0, len(resultMap.Data)) + + // Group delegations by state + for _, delegation := range resultMap.Data { + delegationPublic := &StakerDelegationPublic{ + ParamsVersion: delegation.ParamsVersion, + FinalityProviderBtcPksHex: delegation.FinalityProviderBtcPksHex, + StakerBtcPkHex: delegation.StakerBtcPkHex, + DelegationStaking: DelegationStaking{ + StakingTxHashHex: delegation.StakingTxHashHex, + StakingTime: delegation.StakingTime, + StakingAmount: delegation.StakingAmount, + StartHeight: delegation.StartHeight, + EndHeight: delegation.EndHeight, + }, + DelegationUnbonding: DelegationUnbonding{ + UnbondingTime: delegation.UnbondingTime, + UnbondingTx: delegation.UnbondingTx, + }, + State: delegation.State, + } + delegationsPublic = append(delegationsPublic, delegationPublic) + } + + return delegationsPublic, resultMap.PaginationToken, nil +} diff --git a/internal/v2/service/interface.go b/internal/v2/service/interface.go index b21b0fda..feafcb11 100644 --- a/internal/v2/service/interface.go +++ b/internal/v2/service/interface.go @@ -13,6 +13,7 @@ type V2ServiceProvider interface { SearchFinalityProviders(ctx context.Context, searchQuery string, paginationKey string) ([]*FinalityProviderPublic, string, *types.Error) GetGlobalParams(ctx context.Context) (*GlobalParamsPublic, *types.Error) GetOverallStats(ctx context.Context) (OverallStatsPublic, *types.Error) - GetStakerDelegations(ctx context.Context, stakerPKHex string, paginationKey string) ([]*StakerDelegationPublic, string, *types.Error) + GetDelegation(ctx context.Context, stakingTxHashHex string) (*StakerDelegationPublic, *types.Error) + GetDelegations(ctx context.Context, stakerPKHex string, paginationKey string) ([]*StakerDelegationPublic, string, *types.Error) GetStakerStats(ctx context.Context, stakerPKHex string) (StakerStatsPublic, *types.Error) } diff --git a/internal/v2/service/staker.go b/internal/v2/service/staker.go index 2e5062f2..2b71a7dd 100644 --- a/internal/v2/service/staker.go +++ b/internal/v2/service/staker.go @@ -3,36 +3,12 @@ package v2service import ( "context" "math/rand" - "net/http" "time" - indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types" "github.com/babylonlabs-io/staking-api-service/internal/shared/types" "github.com/babylonlabs-io/staking-api-service/internal/shared/utils/datagen" ) -type DelegationStaking struct { - StakingTxHashHex string `json:"staking_tx_hash_hex"` - StakingTime string `json:"staking_time"` - StakingAmount string `json:"staking_amount"` - StartHeight uint32 `json:"start_height,omitempty"` - EndHeight uint32 `json:"end_height,omitempty"` -} - -type DelegationUnbonding struct { - UnbondingTime string `json:"unbonding_time"` - UnbondingTx string `json:"unbonding_tx"` -} - -type StakerDelegationPublic struct { - ParamsVersion string `json:"params_version"` - StakerBtcPkHex string `json:"staker_btc_pk_hex"` - FinalityProviderBtcPksHex []string `json:"finality_provider_btc_pks_hex"` - DelegationStaking DelegationStaking `json:"delegation_staking"` - DelegationUnbonding DelegationUnbonding `json:"delegation_unbonding"` - State indexertypes.DelegationState `json:"state"` -} - type StakerStatsPublic struct { StakerPKHex string `json:"staker_pk_hex"` ActiveTVL int64 `json:"active_tvl"` @@ -41,40 +17,6 @@ type StakerStatsPublic struct { TotalDelegations int64 `json:"total_delegations"` } -func (s *V2Service) GetStakerDelegations(ctx context.Context, stakingTxHashHex string, paginationKey string) ([]*StakerDelegationPublic, string, *types.Error) { - resultMap, err := s.DbClients.IndexerDBClient.GetStakerDelegations(ctx, stakingTxHashHex, paginationKey) - if err != nil { - return nil, "", types.NewErrorWithMsg(http.StatusInternalServerError, types.InternalServiceError, "failed to get staker delegations") - } - - // Initialize result structure - delegationsPublic := make([]*StakerDelegationPublic, 0, len(resultMap.Data)) - - // Group delegations by state - for _, delegation := range resultMap.Data { - delegationPublic := &StakerDelegationPublic{ - ParamsVersion: delegation.ParamsVersion, - FinalityProviderBtcPksHex: delegation.FinalityProviderBtcPksHex, - StakerBtcPkHex: delegation.StakerBtcPkHex, - DelegationStaking: DelegationStaking{ - StakingTxHashHex: delegation.StakingTxHashHex, - StakingTime: delegation.StakingTime, - StakingAmount: delegation.StakingAmount, - StartHeight: delegation.StartHeight, - EndHeight: delegation.EndHeight, - }, - DelegationUnbonding: DelegationUnbonding{ - UnbondingTime: delegation.UnbondingTime, - UnbondingTx: delegation.UnbondingTx, - }, - State: delegation.State, - } - delegationsPublic = append(delegationsPublic, delegationPublic) - } - - return delegationsPublic, resultMap.PaginationToken, nil -} - func (s *V2Service) GetStakerStats(ctx context.Context, stakerPKHex string) (StakerStatsPublic, *types.Error) { r := rand.New(rand.NewSource(time.Now().UnixNano())) stakerStats := StakerStatsPublic{