Skip to content

Commit

Permalink
api fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AR1011 committed Jan 24, 2024
1 parent ca95976 commit b5ef9b9
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 59 deletions.
176 changes: 160 additions & 16 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (s *Server) handleJrpcRequest(r *JRPCRequest, authlvl auth.AuthLevel) Respo
return s.getBlockNumber(r)
case "idx_getChains":
return s.getChains(r)
case "idx_getHeights":
return s.getHeights(r)

// block timestamps
case "idx_getBlockTimestamps":
Expand Down Expand Up @@ -164,11 +166,98 @@ func (s *Server) getChains(r *JRPCRequest) *types.GetChainsResponse {
}
}

func (s *Server) getHeights(r *JRPCRequest) *types.GetHeightsResponse {
req := &types.GetHeightsRequest{}

if r.Params == nil {
return &types.GetHeightsResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errMissingParams.Error(),
},
}
}

err := json.Unmarshal(r.Params, req)
if err != nil {
return &types.GetHeightsResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errUnmarshalParams.Error(),
},
}
}

err = req.Validate()
if err != nil {
return &types.GetHeightsResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: err.Error(),
},
}
}

store := s.stores.GetStore(*req.ChainID)
if store == nil {
return &types.GetHeightsResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: "invalid chain_id",
},
}
}

hs, err := store.GetHeights()
if err != nil {
if s.debug {
slog.Error("failed to get heights", "err", err)
}
return &types.GetHeightsResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errInternalServer.Error(),
},
}
}

return &types.GetHeightsResponse{
ID: r.ID,
Method: r.Method,
Result: hs,
}

}

func (s *Server) getBlockTimestamps(r *JRPCRequest) *types.GetBlockTimestampsResponse {
var req *types.GetBlockTimestampsRequest
req := &types.GetBlockTimestampsRequest{}

if r.Params == nil {
return &types.GetBlockTimestampsResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errMissingParams.Error(),
},
}
}

err := json.Unmarshal(r.Params, req)
if err != nil {
if s.debug {
slog.Error("failed to unmarshal params", "err", err)
}
return &types.GetBlockTimestampsResponse{
ID: r.ID,
Method: r.Method,
Expand All @@ -191,7 +280,7 @@ func (s *Server) getBlockTimestamps(r *JRPCRequest) *types.GetBlockTimestampsRes
}
}

store := s.stores.GetStore(req.ChainID)
store := s.stores.GetStore(*req.ChainID)
if store == nil {
return &types.GetBlockTimestampsResponse{
ID: r.ID,
Expand All @@ -203,7 +292,7 @@ func (s *Server) getBlockTimestamps(r *JRPCRequest) *types.GetBlockTimestampsRes
}
}

blockTimestamps, err := store.GetBlockTimestamps(req.ToBlock, req.FromBlock)
blockTimestamps, err := store.GetBlockTimestamps(*req.ToBlock, *req.FromBlock)
if err != nil {
if s.debug {
slog.Error("failed to get block timestamps", "err", err)
Expand All @@ -226,7 +315,18 @@ func (s *Server) getBlockTimestamps(r *JRPCRequest) *types.GetBlockTimestampsRes
}

func (s *Server) getBlockAtTimestamp(r *JRPCRequest) *types.GetBlockAtTimestampResponse {
var req *types.GetBlockAtTimestampRequest
req := &types.GetBlockAtTimestampRequest{}

if r.Params == nil {
return &types.GetBlockAtTimestampResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errMissingParams.Error(),
},
}
}

err := json.Unmarshal(r.Params, req)
if err != nil {
Expand All @@ -252,7 +352,7 @@ func (s *Server) getBlockAtTimestamp(r *JRPCRequest) *types.GetBlockAtTimestampR
}
}

store := s.stores.GetStore(req.ChainID)
store := s.stores.GetStore(*req.ChainID)
if store == nil {
return &types.GetBlockAtTimestampResponse{
ID: r.ID,
Expand All @@ -264,7 +364,7 @@ func (s *Server) getBlockAtTimestamp(r *JRPCRequest) *types.GetBlockAtTimestampR
}
}

block, err := store.GetBlockAtTimestamp(req.Timestamp)
block, err := store.GetBlockAtTimestamp(*req.Timestamp)
if err != nil {
if s.debug {
slog.Error("failed to get block at timestamp", "err", err)
Expand All @@ -288,6 +388,17 @@ func (s *Server) getBlockAtTimestamp(r *JRPCRequest) *types.GetBlockAtTimestampR
func (s *Server) findTokens(r *JRPCRequest) *types.FindTokensResponse {
var req *types.FindTokensRequest

if r.Params == nil {
return &types.FindTokensResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errMissingParams.Error(),
},
}
}

err := json.Unmarshal(r.Params, &req)
if err != nil {
return &types.FindTokensResponse{
Expand All @@ -312,7 +423,7 @@ func (s *Server) findTokens(r *JRPCRequest) *types.FindTokensResponse {
}
}

store := s.stores.GetStore(req.ChainID)
store := s.stores.GetStore(*req.ChainID)
if store == nil {
return &types.FindTokensResponse{
ID: r.ID,
Expand Down Expand Up @@ -348,9 +459,20 @@ func (s *Server) findTokens(r *JRPCRequest) *types.FindTokensResponse {
}

func (s *Server) getTokenCount(r *JRPCRequest) *types.GetTokenCountResponse {
var req *types.GetTokenCountRequest
req := &types.GetTokenCountRequest{}

err := json.Unmarshal(r.Params, &req)
if r.Params == nil {
return &types.GetTokenCountResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errMissingParams.Error(),
},
}
}

err := json.Unmarshal(r.Params, req)
if err != nil {
return &types.GetTokenCountResponse{
ID: r.ID,
Expand All @@ -374,7 +496,7 @@ func (s *Server) getTokenCount(r *JRPCRequest) *types.GetTokenCountResponse {
}
}

store := s.stores.GetStore(req.ChainID)
store := s.stores.GetStore(*req.ChainID)
if store == nil {
return &types.GetTokenCountResponse{
ID: r.ID,
Expand Down Expand Up @@ -409,9 +531,20 @@ func (s *Server) getTokenCount(r *JRPCRequest) *types.GetTokenCountResponse {
}

func (s *Server) findPairs(r *JRPCRequest) *types.FindPairsResponse {
var req *types.FindPairsRequest
req := &types.FindPairsRequest{}

err := json.Unmarshal(r.Params, &req)
if r.Params == nil {
return &types.FindPairsResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errMissingParams.Error(),
},
}
}

err := json.Unmarshal(r.Params, req)
if err != nil {
return &types.FindPairsResponse{
ID: r.ID,
Expand All @@ -435,7 +568,7 @@ func (s *Server) findPairs(r *JRPCRequest) *types.FindPairsResponse {
}
}

store := s.stores.GetStore(req.ChainID)
store := s.stores.GetStore(*req.ChainID)
if store == nil {
return &types.FindPairsResponse{
ID: r.ID,
Expand Down Expand Up @@ -471,9 +604,20 @@ func (s *Server) findPairs(r *JRPCRequest) *types.FindPairsResponse {
}

func (s *Server) getPairCount(r *JRPCRequest) *types.GetPairCountResponse {
var req types.GetPairCountRequest
req := &types.GetPairCountRequest{}

err := json.Unmarshal(r.Params, &req)
if r.Params == nil {
return &types.GetPairCountResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: errMissingParams.Error(),
},
}
}

err := json.Unmarshal(r.Params, req)
if err != nil {
return &types.GetPairCountResponse{
ID: r.ID,
Expand All @@ -497,7 +641,7 @@ func (s *Server) getPairCount(r *JRPCRequest) *types.GetPairCountResponse {
}
}

store := s.stores.GetStore(req.ChainID)
store := s.stores.GetStore(*req.ChainID)
if store == nil {
return &types.GetPairCountResponse{
ID: r.ID,
Expand Down
1 change: 1 addition & 0 deletions api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
errMissingBody = errors.New("missing request body")
errMissingAuth = errors.New("missing Authentication header")
errUnmarshalParams = errors.New("failed to unmarshal params")
errMissingParams = errors.New("missing params")
)

type apiHandler func(w http.ResponseWriter, r *http.Request) error
Expand Down
15 changes: 15 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"log"
"log/slog"
"os"
Expand All @@ -10,9 +11,11 @@ import (
"github.com/autoapev1/indexer/config"
"github.com/autoapev1/indexer/storage"
"github.com/autoapev1/indexer/utils"
"github.com/autoapev1/indexer/version"
)

func main() {
fmt.Println(banner())
var (
configFile string
)
Expand Down Expand Up @@ -51,3 +54,15 @@ func main() {

log.Fatal(server.Listen(utils.ToAddress(conf.API.Host, conf.API.Port)))
}

func banner() string {
return fmt.Sprintf(`
██╗███╗ ██╗██████╗ ███████╗██╗ ██╗███████╗██████╗
██║████╗ ██║██╔══██╗██╔════╝╚██╗██╔╝██╔════╝██╔══██╗
██║██╔██╗ ██║██║ ██║█████╗ ╚███╔╝ █████╗ ██████╔╝
██║██║╚██╗██║██║ ██║██╔══╝ ██╔██╗ ██╔══╝ ██╔══██╗
██║██║ ╚████║██████╔╝███████╗██╔╝ ██╗███████╗██║ ██║
╚═╝╚═╝ ╚═══╝╚═════╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ v%s
`, version.Version)
}
2 changes: 1 addition & 1 deletion storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (p *PostgresStore) GetBlockTimestamps(to int64, from int64) ([]*types.Block
ctx := context.Background()

err := p.DB.NewSelect().
Table("block_timestamps").
Model(&blockTimestamps).
Where("block >= ?", from).
Where("block <= ?", to).
Scan(ctx)
Expand Down
10 changes: 7 additions & 3 deletions types/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "strings"

type SortOrder string

const (
Expand All @@ -15,12 +17,14 @@ const (
TokenSortByName TokenSortBy = "name"
TokenSortBySymbol TokenSortBy = "symbol"
TokenSortByDecimals TokenSortBy = "decimals"
TokenSortByCreatedAt TokenSortBy = "created_at_block"
TokenSortByCreatedAt TokenSortBy = "created_at"
TokenSortByHash TokenSortBy = "creation_hash"
)

func ValidateSortOrder(order SortOrder) bool {
switch order {
case SortASC, SortDESC:
o := strings.ToLower(string(order))
switch o {
case string(SortASC), string(SortDESC):
return true
default:
return false
Expand Down
Loading

0 comments on commit b5ef9b9

Please sign in to comment.