Skip to content

Commit

Permalink
migrate governor/token_list
Browse files Browse the repository at this point in the history
  • Loading branch information
marianososto committed Oct 16, 2024
1 parent 56c96cf commit 0de3893
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
6 changes: 3 additions & 3 deletions api/handlers/governor/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ type availableNotionalByChainMongo struct {

// TokenList definition
type TokenList struct {
OriginChainID vaa.ChainID `bson:"originchainid" json:"originChainId"`
OriginAddress string `bson:"originaddress" json:"originAddress"`
Price float32 `bson:"price" json:"price"`
OriginChainID vaa.ChainID `db:"originchainid" bson:"originchainid" json:"originChainId"`
OriginAddress string `db:"originaddress" bson:"originaddress" json:"originAddress"`
Price float32 `db:"price" bson:"price" json:"price"`
}

// EnqueuedVaaItem definition
Expand Down
34 changes: 34 additions & 0 deletions api/handlers/governor/postgres_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,3 +917,37 @@ func (r *PostgresRepository) IsVaaEnqueued(ctx context.Context, chainID sdk.Chai

return len(result) > 0, nil
}

func (r *PostgresRepository) GetTokenList(ctx context.Context) ([]*TokenList, error) {
query := `
WITH governor_cfg_tokens AS (SELECT (gov_cfg_tokens ->> 'originchainid')::smallint as originchainid,
gov_cfg_tokens ->> 'originaddress' as originaddress,
(gov_cfg_tokens ->> 'price')::numeric as price
FROM wormholescan.wh_governor_config,
jsonb_array_elements(wormholescan.wh_governor_config.tokens) AS gov_cfg_tokens),
price_counts AS (SELECT originchainid,
originaddress,
price,
COUNT(*) as occurrences
FROM governor_cfg_tokens
GROUP BY originchainid, originaddress, price)
SELECT DISTINCT ON (originchainid, originaddress) originchainid,
originaddress,
price
FROM price_counts
ORDER BY originchainid,
originaddress,
occurrences DESC;
`

var result []*TokenList
err := r.db.Select(ctx, &result, query)
if err != nil {
r.logger.Error("failed to execute query to get token list",
zap.Error(err),
zap.String("query", query))
return nil, err
}

return result, nil
}
8 changes: 6 additions & 2 deletions api/handlers/governor/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,15 @@ func (s *Service) GetAvailNotionByChain(ctx context.Context, usePostgres bool) (

// Get governor token list.
// Guardian api migration.
func (s *Service) GetTokenList(ctx context.Context) ([]*TokenList, error) {
func (s *Service) GetTokenList(ctx context.Context, postgres bool) ([]*TokenList, error) {
key := tokenList
return cacheable.GetOrLoad(ctx, s.logger, s.cache, 1*time.Minute, key, s.metrics,
func() ([]*TokenList, error) {
return s.mongoRepo.GetTokenList(ctx)
if postgres {
return s.postgresRepo.GetTokenList(ctx)
} else {
return s.mongoRepo.GetTokenList(ctx)
}
})

}
Expand Down
2 changes: 1 addition & 1 deletion api/routes/guardian/governor/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (c *Controller) IsVaaEnqueued(ctx *fiber.Ctx) error {
// @Failure 500
// @Router /v1/governor/token_list [get]
func (c *Controller) GetTokenList(ctx *fiber.Ctx) error {
tokenList, err := c.srv.GetTokenList(ctx.Context())
tokenList, err := c.srv.GetTokenList(ctx.Context(), middleware.UsePostgres(ctx))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion api/rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (h *Handler) GovernorIsVAAEnqueued(ctx context.Context, request *publicrpcv

// GovernorGetTokenList get governor token list.
func (h *Handler) GovernorGetTokenList(ctx context.Context, _ *publicrpcv1.GovernorGetTokenListRequest) (*publicrpcv1.GovernorGetTokenListResponse, error) {
tokenList, err := h.govSrv.GetTokenList(ctx)
tokenList, err := h.govSrv.GetTokenList(ctx, h.usePostgres)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0de3893

Please sign in to comment.