Skip to content

Commit

Permalink
feat: complete JSON config (#15)
Browse files Browse the repository at this point in the history
* fix: proper error message when token in is too small

* go work sum

* feat: config path

* feat: full JSON config

* lint
  • Loading branch information
p0mvn authored Jan 5, 2024
1 parent 2b9cc12 commit 2e6f665
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 80 deletions.
19 changes: 9 additions & 10 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ func main() {
panic(err)
}

dbHost := viper.GetString(`database.host`)
dbPort := viper.GetString(`database.port`)
// Unmarshal the config into your Config struct
var config Config
if err := viper.Unmarshal(&config); err != nil {
fmt.Println("Error unmarshalling config:", err)
return
}

// Handle SIGINT and SIGTERM signals to initiate shutdown
exitChan := make(chan os.Signal, 1)
Expand All @@ -52,7 +56,7 @@ func main() {
}()

redisClient := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", dbHost, dbPort),
Addr: fmt.Sprintf("%s:%s", config.StorageHost, config.StoragePort),
Password: "", // no password set
DB: 0, // use default DB
})
Expand All @@ -63,10 +67,7 @@ func main() {
panic(err)
}

chainID := viper.GetString(`chain.id`)
chainNodeURI := viper.GetString(`chain.node_uri`)

chainClient, err := client.NewClient(chainID, chainNodeURI)
chainClient, err := client.NewClient(config.ChainID, config.ChainGRPCGatewayEndpoint)
if err != nil {
panic(err)
}
Expand All @@ -79,8 +80,6 @@ func main() {
panic(err)
}

config := DefaultConfig

encCfg := app.MakeEncodingConfig()

// logger
Expand All @@ -90,7 +89,7 @@ func main() {
}
logger.Info("Starting sidecar query server")

sidecarQueryServer, err := NewSideCarQueryServer(encCfg.Marshaler, *config.Router, dbHost, dbPort, config.ServerAddress, config.ChainGRPCGatewayEndpoint, config.ServerTimeoutDurationSecs, logger)
sidecarQueryServer, err := NewSideCarQueryServer(encCfg.Marshaler, *config.Router, config.StorageHost, config.StoragePort, config.ServerAddress, config.ChainGRPCGatewayEndpoint, config.ServerTimeoutDurationSecs, logger)
if err != nil {
panic(err)
}
Expand Down
27 changes: 10 additions & 17 deletions app/sqs_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (

// Config defines the config for the sidecar query server.
type Config struct {
// IsEnabled defines if the sidecar query server is enabled.
IsEnabled bool `mapstructure:"enabled"`

// Storage defines the storage host and port.
StorageHost string `mapstructure:"db-host"`
StoragePort string `mapstructure:"db-port"`
Expand All @@ -23,18 +20,14 @@ type Config struct {
LoggerLevel string `mapstructure:"logger-level"`

ChainGRPCGatewayEndpoint string `mapstructure:"grpc-gateway-endpoint"`
ChainID string `mapstructure:"chain-id"`

// Router encapsulates the router config.
Router *domain.RouterConfig `mapstructure:"router"`
}

const groupOptName = "osmosis-sqs"

// DefaultConfig defines the default config for the sidecar query server.
var DefaultConfig = Config{

IsEnabled: false,

StorageHost: "localhost",
StoragePort: "6379",

Expand All @@ -46,16 +39,16 @@ var DefaultConfig = Config{
LoggerLevel: "info",

ChainGRPCGatewayEndpoint: "http://localhost:26657",
ChainID: "osmosis-1",

Router: &domain.RouterConfig{
PreferredPoolIDs: []uint64{},
MaxPoolsPerRoute: 4,
MaxRoutes: 5,
MaxSplitRoutes: 3,
MaxSplitIterations: 10,
MinOSMOLiquidity: 10000, // 10_000 OSMO
RouteUpdateHeightInterval: 0,
RouteCacheEnabled: false,
RouteCacheExpirySeconds: 600, // 10 minutes
PreferredPoolIDs: []uint64{},
MaxPoolsPerRoute: 4,
MaxRoutes: 5,
MaxSplitRoutes: 3,
MaxSplitIterations: 10,
MinOSMOLiquidity: 10000, // 10_000 OSMO
RouteCacheEnabled: false,
RouteCacheExpirySeconds: 600, // 10 minutes
},
}
27 changes: 0 additions & 27 deletions chaininfo/client/chain_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import (

rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
"google.golang.org/grpc/encoding/proto"

clpoolmodel "github.com/osmosis-labs/osmosis/v21/x/concentrated-liquidity/model"
cwpoolmodel "github.com/osmosis-labs/osmosis/v21/x/cosmwasmpool/model"
Expand All @@ -24,7 +20,6 @@ type Client interface {
}

type chainClient struct {
context client.Context
rpcClient *rpchttp.HTTP
}

Expand All @@ -40,30 +35,8 @@ func NewClient(chainID string, nodeURI string) (Client, error) {
clpoolmodel.RegisterInterfaces(interfaceRegistry)
cwpoolmodel.RegisterInterfaces(interfaceRegistry)

clientCtx := client.Context{}.
WithCodec(codec.NewProtoCodec(interfaceRegistry)).
WithChainID(chainID)

// If grpc is enabled, configure grpc client for grpc gateway.
grpcClient, err := grpc.Dial(
viper.GetString(`chain.node_grpc`), // TODO: get from config
// nolint: staticcheck
grpc.WithInsecure(),
grpc.WithDefaultCallOptions(
grpc.ForceCodec(encoding.GetCodec(proto.Name)),
grpc.MaxCallRecvMsgSize(10485760),
grpc.MaxCallSendMsgSize(2147483647),
),
)
if err != nil {
return nil, err
}

clientCtx = clientCtx.WithGRPCClient(grpcClient)

return &chainClient{
rpcClient: rpcClient,
context: clientCtx,
}, nil
}

Expand Down
35 changes: 18 additions & 17 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"debug": true,
"server": {
"address": ":9092"
},
"context":{
"timeout":2
},
"database": {
"host": "0.0.0.0",
"port": "6379",
"user": "user",
"pass": "password",
"name": "article"
},
"chain": {
"id": "osmosis-1",
"node_uri": "tcp://0.0.0.0:26657",
"node_grpc": "0.0.0.0:9090"
"db-host": "localhost",
"db-port": "6379",
"server-address": ":9092",
"timeout-duration-secs": 2,
"logger-filename": "sqs.log",
"logger-is-production": true,
"logger-level": "info",
"grpc-gateway-endpoint": "http://localhost:26657",
"chain-id": "osmosis-1",
"router": {
"preferred-pool-ids": [],
"max-pools-per-route": 4,
"max-routes": 20,
"max-split-routes": 3,
"max-split-iterations": 10,
"min-osmo-liquidity": 10000,
"route-cache-enabled": true,
"route-cache-expiry-seconds": 600
}
}

18 changes: 9 additions & 9 deletions domain/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ type Quote interface {
}

type RouterConfig struct {
PreferredPoolIDs []uint64 `mapstructure:"preferred_pool_ids"`
MaxPoolsPerRoute int `mapstructure:"max_pools_per_route"`
MaxRoutes int `mapstructure:"max_routes"`
MaxSplitRoutes int `mapstructure:"max_split_routes"`
MaxSplitIterations int `mapstructure:"max_split_iterations"`
PreferredPoolIDs []uint64 `mapstructure:"preferred-pool-ids"`
MaxPoolsPerRoute int `mapstructure:"max-pools-per-route"`
MaxRoutes int `mapstructure:"max-routes"`
MaxSplitRoutes int `mapstructure:"max-split-routes"`
MaxSplitIterations int `mapstructure:"max-split-iterations"`
// Denominated in OSMO (not uosmo)
MinOSMOLiquidity int `mapstructure:"min_osmo_liquidity"`
RouteUpdateHeightInterval int `mapstructure:"route_update_height_interval"`
RouteCacheEnabled bool `mapstructure:"route_cache_enabled"`
MinOSMOLiquidity int `mapstructure:"min-osmo-liquidity"`
RouteUpdateHeightInterval int `mapstructure:"route-update-height-interval"`
RouteCacheEnabled bool `mapstructure:"route-cache-enabled"`
// The number of seconds to cache routes for before expiry.
RouteCacheExpirySeconds uint64 `mapstructure:"route_cache_expiry_seconds"`
RouteCacheExpirySeconds uint64 `mapstructure:"route-cache-expiry-seconds"`
}

0 comments on commit 2e6f665

Please sign in to comment.