2022-11-23 04:06:29 -08:00
|
|
|
// Package observations handle the request of observations data from governor endpoint defined in the api.
|
2022-11-17 07:37:29 -08:00
|
|
|
package observations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/api/middleware"
|
|
|
|
"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 {
|
|
|
|
srv *Service
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// NewController create a new controler.
|
2022-11-17 07:37:29 -08:00
|
|
|
func NewController(srv *Service, logger *zap.Logger) *Controller {
|
|
|
|
return &Controller{
|
|
|
|
srv: srv,
|
|
|
|
logger: logger.With(zap.String("module", "ObservationsController")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// FindAll handler for the endpoint /observations/.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindAll(ctx *fiber.Ctx) error {
|
2022-11-23 04:06:29 -08:00
|
|
|
p := middleware.GetPaginationFromContext(ctx)
|
2022-11-17 07:37:29 -08:00
|
|
|
obs, err := c.srv.FindAll(ctx.Context(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.JSON(obs)
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// FindAllByChain handler for the endpoint /observations/:chain.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindAllByChain(ctx *fiber.Ctx) error {
|
2022-11-23 04:06:29 -08:00
|
|
|
p := middleware.GetPaginationFromContext(ctx)
|
|
|
|
chainID, err := middleware.ExtractChainID(ctx, c.logger)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-17 07:37:29 -08:00
|
|
|
obs, err := c.srv.FindByChain(ctx.Context(), chainID, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.JSON(obs)
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// FindAllByEmitter handler for the endpoint /observations/:chain/:emitter.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindAllByEmitter(ctx *fiber.Ctx) error {
|
2022-11-23 04:06:29 -08:00
|
|
|
p := middleware.GetPaginationFromContext(ctx)
|
|
|
|
chainID, addr, err := middleware.ExtractVAAChainIDEmitter(ctx, c.logger)
|
|
|
|
if err != nil {
|
2022-11-17 07:37:29 -08:00
|
|
|
return err
|
|
|
|
}
|
2022-11-23 04:06:29 -08:00
|
|
|
|
2022-11-17 07:37:29 -08:00
|
|
|
obs, err := c.srv.FindByEmitter(ctx.Context(), chainID, addr, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.JSON(obs)
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// FindAllByVAA handler for the endpoint /observations/:chain/:emitter/:sequence
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindAllByVAA(ctx *fiber.Ctx) error {
|
2022-11-23 04:06:29 -08:00
|
|
|
p := middleware.GetPaginationFromContext(ctx)
|
|
|
|
chainID, addr, seq, err := middleware.ExtractVAAParams(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obs, err := c.srv.FindByVAA(ctx.Context(), chainID, addr, seq, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.JSON(obs)
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// FindOne handler for the endpoint /observations/:chain/:emitter/:sequence/:signer/:hash
|
2022-11-17 07:37:29 -08:00
|
|
|
func (c *Controller) FindOne(ctx *fiber.Ctx) error {
|
2022-11-23 04:06:29 -08:00
|
|
|
chainID, addr, seq, err := middleware.ExtractVAAParams(ctx, c.logger)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
signerAddr, err := middleware.ExtractObservationSigner(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-23 04:06:29 -08:00
|
|
|
hash, err := middleware.ExtractObservationHash(ctx, c.logger)
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-23 04:06:29 -08:00
|
|
|
obs, err := c.srv.FindOne(ctx.Context(), chainID, addr, seq, signerAddr, []byte(hash))
|
2022-11-17 07:37:29 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.JSON(obs)
|
|
|
|
}
|