From 088f111cdc441539fce88f6cd19e9fdfd5d2b1f8 Mon Sep 17 00:00:00 2001 From: Ashton Date: Fri, 19 Jan 2024 16:55:32 +0000 Subject: [PATCH] fix handlers --- api/handlers.go | 22 ++++++---------------- config/config.go | 30 +++++++++++++++--------------- storage/sql.go | 2 +- types/eth.go | 21 ++++++++++----------- 4 files changed, 32 insertions(+), 43 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index caa3f32..2a0dc33 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -1,9 +1,8 @@ package api import ( - "encoding/json" + "fmt" "log/slog" - "time" "github.com/autoapev1/indexer/auth" "github.com/autoapev1/indexer/types" @@ -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": @@ -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{ @@ -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, diff --git a/config/config.go b/config/config.go index cf7ec6d..1b11f8e 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/storage/sql.go b/storage/sql.go index c23e3ef..cf3025e 100644 --- a/storage/sql.go +++ b/storage/sql.go @@ -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 } diff --git a/types/eth.go b/types/eth.go index 2eeda99..8d1885a 100644 --- a/types/eth.go +++ b/types/eth.go @@ -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 (