Skip to content

Commit

Permalink
Merge pull request #30 from flare-foundation/include-api-key
Browse files Browse the repository at this point in the history
Include API key in RPC URL
  • Loading branch information
mboben authored Nov 15, 2023
2 parents e9395a2 + a48b6fc commit ac187bf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
31 changes: 31 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package config
import (
"fmt"
"log"
"net/url"
"os"
"strings"

"github.com/BurntSushi/toml"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/kelseyhightower/envconfig"
)

Expand Down Expand Up @@ -67,6 +69,35 @@ func (cfg ChainConfig) GetPrivateKey() (string, error) {
}
}

// 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.EthRPCURL)
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
}

type EpochConfig struct {
First int64 `toml:"first" envconfig:"EPOCH_FIRST"`
}
Expand Down
2 changes: 1 addition & 1 deletion indexer/cronjob/mirror_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func initMirrorJobContracts(cfg *config.Config) (mirrorContracts, error) {
return nil, errors.New("voting contract address not set")
}

eth, err := ethclient.Dial(cfg.Chain.EthRPCURL)
eth, err := cfg.Chain.DialETH()
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions indexer/cronjob/uptime_voting.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ava-labs/avalanchego/ids"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/pkg/errors"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -59,7 +58,7 @@ func NewUptimeVotingCronjob(ctx context.IndexerContext) (*uptimeVotingCronjob, e
return &uptimeVotingCronjob{}, nil
}

eth, err := ethclient.Dial(cfg.Chain.EthRPCURL)
eth, err := cfg.Chain.DialETH()
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions indexer/cronjob/voting_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -40,8 +39,7 @@ type votingContractCChain struct {
}

func newVotingContractCChain(cfg *config.Config) (votingContract, error) {

eth, err := ethclient.Dial(cfg.Chain.EthRPCURL)
eth, err := cfg.Chain.DialETH()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func BuildContext() (ServicesContext, error) {
return nil, err
}

ethRPCClient, err := ethclient.Dial(cfg.Chain.EthRPCURL)
ethRPCClient, err := cfg.Chain.DialETH()
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions services/routes/mirroring.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/ethclient"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -66,7 +65,7 @@ func newMirroringRouteHandlers(ctx context.ServicesContext) (*mirroringRouteHand
}

func getEpochStartAndPeriod(cfg *config.Config) (time.Time, time.Duration, error) {
eth, err := ethclient.Dial(cfg.Chain.EthRPCURL)
eth, err := cfg.Chain.DialETH()
if err != nil {
return time.Time{}, 0, err
}
Expand Down

0 comments on commit ac187bf

Please sign in to comment.