Skip to content

Commit

Permalink
Add RPC api key support, minor config tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
adg-flare authored and tilenflare committed May 9, 2024
1 parent e9e2fb4 commit a6dfd12
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions go-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ console = true

[chain]
node_url = "https://coston2-api.flare.network/ext/C/rpc"
# optional rpc api key, can also be set via API_KEY env variable
api_key = ""
chain_id = 114
```

Expand Down
2 changes: 1 addition & 1 deletion go-client/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func CreateFastUpdatesClient(cfg *config.Config, valuesProvider provider.ValuesP
fastUpdatesClient.valuesProvider = valuesProvider

var err error
fastUpdatesClient.chainClient, err = ethclient.Dial(cfg.Chain.NodeURL)
fastUpdatesClient.chainClient, err = cfg.Chain.DialETH()
if err != nil {
return nil, fmt.Errorf("CreateFastUpdatesClient: Dial: %w", err)
}
Expand Down
48 changes: 46 additions & 2 deletions go-client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

"github.com/BurntSushi/toml"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -40,6 +41,7 @@ type LoggerConfig struct {
type ChainConfig struct {
NodeURL string `toml:"node_url"`
ChainId int `toml:"chain_id"`
ApiKey string `toml:"api_key" envconfig:"API_KEY"`
}

type FastUpdateClientConfig struct {
Expand Down Expand Up @@ -82,9 +84,9 @@ func BuildConfig() (*Config, error) {
return nil, err
}

_, err = url.ParseRequestURI(cfg.Client.ValueProviderUrl)
err = validateConfig(cfg)
if err != nil {
return nil, errors.Wrap(err, "invalid URL specified for ValueProviderUrl: %w")
return nil, errors.Wrap(err, "config validation failed")
}

return cfg, nil
Expand Down Expand Up @@ -115,3 +117,45 @@ func ReadEnv(cfg *Config) error {
func (c Config) LoggerConfig() LoggerConfig {
return c.Logger
}

func validateConfig(cfg *Config) error {
_, err := url.ParseRequestURI(cfg.Client.ValueProviderUrl)
if err != nil {
return errors.Wrap(err, "invalid URL specified for ValueProviderUrl: %w")
}

if len(cfg.Transactions.Accounts) == 0 {
return errors.New("no submission accounts provided")
}

return nil
}

// Dial the chain node and return an ethclient.Client.
func (chain *ChainConfig) DialETH() (*ethclient.Client, error) {
rpcURL, err := chain.getRPCURL()
if err != nil {
return nil, err
}

return ethclient.Dial(rpcURL)
}

// Get the full RPC URL which may be passed to ethclient.Dial. Includes API key
// as query param if it is configured.
func (chain *ChainConfig) getRPCURL() (string, error) {
u, err := url.Parse(chain.NodeURL)
if err != nil {
return "", err
}

if chain.ApiKey == "" {
return u.String(), nil
}

q := u.Query()
q.Set("x-apikey", chain.ApiKey)
u.RawQuery = q.Encode()

return u.String(), nil
}
6 changes: 4 additions & 2 deletions go-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (
"fast-updates-client/logger"
"fast-updates-client/provider"
"flag"
"fmt"
"os"
"time"
)

func main() {
flag.Parse()
cfg, err := config.BuildConfig()
if err != nil {
logger.Fatal("Config error: %s", err)
return
fmt.Printf("Config error: %v\n", err)
os.Exit(1)
}
config.GlobalConfigCallback.Call(cfg)

Expand Down
5 changes: 2 additions & 3 deletions go-client/tests/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ services:

value-provider:
container_name: value-provider
image: ghcr.io/flare-foundation/ftso-scaling:latest
image: ghcr.io/flare-foundation/ftso-v2-example-value-provider:latest
ports:
- 3101:3101
environment:
- VALUE_PROVIDER_IMPL=${VALUE_PROVIDER_IMPL}
- VALUE_PROVIDER_CLIENT_PORT=3101
command: /bin/bash -c 'node dist/apps/example_provider/apps/example_provider/src/main.js'
- VALUE_PROVIDER_CLIENT_PORT=3101

0 comments on commit a6dfd12

Please sign in to comment.