Skip to content

Commit

Permalink
fix handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
AR1011 committed Jan 19, 2024
1 parent 23fb08d commit 088f111
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 43 deletions.
22 changes: 6 additions & 16 deletions api/handlers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package api

import (
"encoding/json"
"fmt"
"log/slog"
"time"

"github.com/autoapev1/indexer/auth"
"github.com/autoapev1/indexer/types"
Expand Down Expand Up @@ -40,7 +39,7 @@ func (s *Server) handleJrpcRequest(r *JRPCRequest, authlvl auth.AuthLevel) Respo
case "idx_getBlockNumber":
return s.getBlockNumber(r)
case "idx_getChains":
return notImplemented(r)
return s.getChains(r)

// block timestamps
case "idx_getBlockTimestamps":
Expand Down Expand Up @@ -143,23 +142,12 @@ func (s *Server) getBlockNumber(r *JRPCRequest) *types.GetBlockNumberResponse {
return &types.GetBlockNumberResponse{
ID: r.ID,
Method: r.Method,
Result: nil,
Result: blockNumbers,
}
}

func (s *Server) getChains(r *JRPCRequest) *types.GetChainsResponse {
req := &types.GetChainsRequest{}

if err := json.Unmarshal(r.Params, req); err != nil {
return &types.GetChainsResponse{
ID: r.ID,
Method: r.Method,
Error: &types.JRPCError{
Code: -32602,
Message: "Invalid params",
},
}
}
chains := []types.Chain{}
for _, c := range s.config.Chains {
tc := types.Chain{
Expand All @@ -171,11 +159,13 @@ func (s *Server) getChains(r *JRPCRequest) *types.GetChainsResponse {
FactoryV2: c.FactoryV2Address,
RouterV3: c.RouterV3Address,
FactoryV3: c.FactoryV3Address,
BlockDuration: time.Duration(c.BlockDuration) * time.Second,
BlockDuration: int64(c.BlockDuration),
}
chains = append(chains, tc)
}

fmt.Println(len(s.config.Chains))

return &types.GetChainsResponse{
ID: r.ID,
Method: r.Method,
Expand Down
30 changes: 15 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ user = "postgres"
`

func getDefaultConfig() Config {
// parse default config
var config Config
err := toml.Unmarshal([]byte(defaultConfig), &config)
if err != nil {
panic(err)
}

// fmt.Printf("Chains: %+v\n", config.Chains)
// fmt.Printf("Sync: %+v\n", config.Sync)
// fmt.Printf("Storage: %+v\n", config.Storage)
// fmt.Printf("API: %+v\n", config.API)
return config
}
// func getDefaultConfig() Config {
// // parse default config
// var config Config
// err := toml.Unmarshal([]byte(defaultConfig), &config)
// if err != nil {
// panic(err)
// }

// fmt.Printf("Chains: %+v\n", config.Chains)
// // fmt.Printf("Sync: %+v\n", config.Sync)
// // fmt.Printf("Storage: %+v\n", config.Storage)
// // fmt.Printf("API: %+v\n", config.API)
// return config
// }

// Config has global config
var config Config = getDefaultConfig()
var config Config

type Config struct {
Chains []ChainConfig
Expand Down
2 changes: 1 addition & 1 deletion storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (p *PostgresStore) BulkGetBlockTimestamp(to int, from int) ([]*types.BlockT
func (p *PostgresStore) GetHight() (int64, error) {
var block int64
ctx := context.Background()
err := p.DB.NewSelect().ColumnExpr("MAX(block)").Scan(ctx, &block)
err := p.DB.NewSelect().Model(&types.BlockTimestamp{}).ColumnExpr("MAX(block)").Scan(ctx, &block)
if err != nil {
return block, err
}
Expand Down
21 changes: 10 additions & 11 deletions types/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"log"
"os"
"strings"
"time"
)

type Chain struct {
ChainID int `json:"chain_id"`
Name string `json:"name"`
ShortName string `json:"short_name"`
ExplorerURL string `json:"explorer_url"`
RouterV2 string `json:"router_v2"`
FactoryV2 string `json:"factory_v2"`
RouterV3 string `json:"router_v3"`
FactoryV3 string `json:"factory_v3"`
BlockDuration time.Duration `json:"block_duration"`
Http string `json:"-"`
ChainID int `json:"chain_id"`
Name string `json:"name"`
ShortName string `json:"short_name"`
ExplorerURL string `json:"explorer_url"`
RouterV2 string `json:"router_v2"`
FactoryV2 string `json:"factory_v2"`
RouterV3 string `json:"router_v3"`
FactoryV3 string `json:"factory_v3"`
BlockDuration int64 `json:"block_duration"`
Http string `json:"-"`
}

const (
Expand Down

0 comments on commit 088f111

Please sign in to comment.