2022-11-23 04:06:29 -08:00
|
|
|
// Package governor handle the request of governor data from governor endpoint defined in the api.
|
2022-11-17 07:37:29 -08:00
|
|
|
package governor
|
|
|
|
|
|
|
|
import (
|
2023-02-24 05:47:20 -08:00
|
|
|
"fmt"
|
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-01-27 07:58:37 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/api/handlers/governor"
|
2022-11-17 07:37:29 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/api/middleware"
|
2023-02-24 05:47:20 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/api/response"
|
2023-01-25 11:19:14 -08:00
|
|
|
_ "github.com/wormhole-foundation/wormhole-explorer/api/response" // needed by swaggo docs
|
2022-11-17 07:37:29 -08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// Controller definition.
|
2022-11-17 07:37:29 -08:00
|
|
|
type Controller struct {
|
2023-01-27 07:58:37 -08:00
|
|
|
srv *governor.Service
|
2022-11-17 07:37:29 -08:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// NewController create a new controler.
|
2023-01-27 07:58:37 -08:00
|
|
|
func NewController(serv *governor.Service, logger *zap.Logger) *Controller {
|
2022-11-17 07:37:29 -08:00
|
|
|
return &Controller{srv: serv, logger: logger.With(zap.String("module", "GovernorController"))}
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// FindGovernorConfigurations godoc
|
|
|
|
// @Description Returns governor configuration for all guardians.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-config
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Success 200 {object} response.Response[GovConfig]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/config [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindGovernorConfigurations(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
governorConfigs, err := c.srv.FindGovernorConfig(ctx.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(governorConfigs)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// FindGovernorConfigurationByGuardianAddress godoc
|
|
|
|
// @Description Returns governor configuration for a given guardian.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-config-by-guardian-address
|
2023-02-24 05:47:20 -08:00
|
|
|
// @Success 200 {object} response.Response[GovConfig]
|
2023-01-25 11:19:14 -08:00
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/config/:guardian_address [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindGovernorConfigurationByGuardianAddress(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2023-02-24 05:47:20 -08:00
|
|
|
// extract query params
|
2022-11-23 04:06:29 -08:00
|
|
|
guardianAddress, err := middleware.ExtractGuardianAddress(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2023-02-24 05:47:20 -08:00
|
|
|
// query the database
|
2023-03-16 11:42:34 -07:00
|
|
|
govConfigs, err := c.srv.FindGovernorConfigByGuardianAddress(ctx.Context(), guardianAddress.Hex())
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-02-24 05:47:20 -08:00
|
|
|
} else if len(govConfigs) == 0 {
|
|
|
|
return response.NewNotFoundError(ctx)
|
|
|
|
} else if len(govConfigs) > 1 {
|
|
|
|
err = fmt.Errorf("expected at most 1 governor config, but found %d", len(govConfigs))
|
|
|
|
return response.NewInternalError(ctx, err)
|
2022-11-17 07:37:29 -08:00
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2023-02-24 05:47:20 -08:00
|
|
|
// populate the response struct and return
|
|
|
|
res := response.Response[*governor.GovConfig]{Data: govConfigs[0]}
|
|
|
|
return ctx.JSON(res)
|
2022-11-17 07:37:29 -08:00
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// FindGovernorStatus godoc
|
|
|
|
// @Description Returns the governor status for all guardians.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-status
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Success 200 {object} response.Response[[]GovStatus]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/status [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindGovernorStatus(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
governorStatus, err := c.srv.FindGovernorStatus(ctx.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(governorStatus)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// FindGovernorStatusByGuardianAddress godoc
|
|
|
|
// @Description Returns the governor status for a given guardian.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-status-by-guardian-address
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Success 200 {object} response.Response[GovStatus]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/status/:guardian_address [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindGovernorStatusByGuardianAddress(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
guardianAddress, err := middleware.ExtractGuardianAddress(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2023-03-16 11:42:34 -07:00
|
|
|
govStatus, err := c.srv.FindGovernorStatusByGuardianAddress(ctx.Context(), guardianAddress.Hex(), p)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(govStatus)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// GetGovernorLimit godoc
|
|
|
|
// @Description Returns the governor limit for all blockchains.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-notional-limit
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Success 200 {object} response.Response[[]GovernorLimit]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/limit [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) GetGovernorLimit(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
governorLimit, err := c.srv.GetGovernorLimit(ctx.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(governorLimit)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// FindNotionalLimit godoc
|
|
|
|
// @Description Returns the detailed notional limit for all blockchains.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-notional-limit-detail
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Success 200 {object} response.Response[[]NotionalLimitDetail]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/notional/limit [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindNotionalLimit(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
notionalLimit, err := c.srv.FindNotionalLimit(ctx.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(notionalLimit)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// GetNotionalLimitByChainID godoc
|
|
|
|
// @Description Returns the detailed notional limit available for a given blockchain.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-notional-limit-detail-by-chain
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Success 200 {object} response.Response[[]NotionalLimitDetail]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/notional/limit/:chain [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) GetNotionalLimitByChainID(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
chainID, err := middleware.ExtractChainID(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
notionalLimit, err := c.srv.GetNotionalLimitByChainID(ctx.Context(), p, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(notionalLimit)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// GetAvailableNotional godoc
|
|
|
|
// @Description Returns the amount of notional value available for each blockchain.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-notional-available
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Param sortOrder query string false "Sort results in ascending or descending order." Enums(ASC, DESC)
|
|
|
|
// @Success 200 {object} response.Response[[]NotionalAvailable]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/notional/available [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) GetAvailableNotional(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
notionalAvaialabilies, err := c.srv.GetAvailableNotional(ctx.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(notionalAvaialabilies)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// GetAvailableNotionalByChainID godoc
|
|
|
|
// @Description Returns the amount of notional value available for a given blockchain.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-notional-available-by-chain
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Success 200 {object} response.Response[[]NotionalAvailableDetail]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/notional/available/:chain [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) GetAvailableNotionalByChainID(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
chainID, err := middleware.ExtractChainID(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
response, err := c.srv.GetAvailableNotionalByChainID(ctx.Context(), p, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(response)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// GetMaxNotionalAvailableByChainID godoc
|
|
|
|
// @Description Returns the maximum amount of notional value available for a given blockchain.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-max-notional-available-by-chain
|
|
|
|
// @Success 200 {object} response.Response[MaxNotionalAvailableRecord]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
2023-02-23 11:54:40 -08:00
|
|
|
// @Router /api/v1/governor/notional/max_available/:chain [get]
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) GetMaxNotionalAvailableByChainID(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
chainID, err := middleware.ExtractChainID(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2023-02-24 05:47:20 -08:00
|
|
|
response, err := c.srv.GetMaxNotionalAvailableByChainID(ctx.Context(), chainID)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(response)
|
|
|
|
}
|
|
|
|
|
2023-02-10 09:31:33 -08:00
|
|
|
// GetEnqueuedVaas godoc
|
2023-01-25 11:19:14 -08:00
|
|
|
// @Description Returns enqueued VAAs for each blockchain.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID governor-enqueued-vaas
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Param sortOrder query string false "Sort results in ascending or descending order." Enums(ASC, DESC)
|
|
|
|
// @Success 200 {object} response.Response[[]EnqueuedVaas]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/enqueued_vaas/ [get]
|
2023-02-10 09:31:33 -08:00
|
|
|
func (c *Controller) GetEnqueuedVaas(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
enqueuedVaas, err := c.srv.GetEnqueueVass(ctx.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(enqueuedVaas)
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:19:14 -08:00
|
|
|
// GetEnqueuedVaasByChainID godoc
|
|
|
|
// @Description Returns all enqueued VAAs for a given blockchain.
|
|
|
|
// @Tags Wormscan
|
|
|
|
// @ID guardians-enqueued-vaas-by-chain
|
|
|
|
// @Param page query integer false "Page number."
|
|
|
|
// @Param pageSize query integer false "Number of elements per page."
|
|
|
|
// @Param sortOrder query string false "Sort results in ascending or descending order." Enums(ASC, DESC)
|
|
|
|
// @Success 200 {object} response.Response[[]EnqueuedVaaDetail]
|
|
|
|
// @Failure 400
|
|
|
|
// @Failure 500
|
|
|
|
// @Router /api/v1/governor/enqueued_vaas/:chain [get]
|
|
|
|
func (c *Controller) GetEnqueuedVaasByChainID(ctx *fiber.Ctx) error {
|
2023-02-09 09:28:39 -08:00
|
|
|
|
|
|
|
p, err := middleware.ExtractPagination(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
chainID, err := middleware.ExtractChainID(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
enqueuedVaas, err := c.srv.GetEnqueueVassByChainID(ctx.Context(), p, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-09 09:28:39 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
return ctx.JSON(enqueuedVaas)
|
|
|
|
}
|