Skip to content

Commit

Permalink
Allow implicit connection for block info.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed May 26, 2022
1 parent 6f47e73 commit 81d93b8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
6 changes: 4 additions & 2 deletions cmd/blockinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/attestantio/go-execution-client/types"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/wealdtech/ethereal/v2/cli"
string2eth "github.com/wealdtech/go-string2eth"
)
Expand All @@ -52,9 +51,12 @@ In quiet mode this will return 0 if the block exists, otherwise 1.`,
ctx := context.Background()
cli.Assert(blockStr != "", quiet, "--block is required")

connectionAddress, err := connectionAddress(ctx)
cli.ErrCheck(err, quiet, "Failed to obtain connection address")

execClient, err := jsonrpc.New(context.Background(),
jsonrpc.WithLogLevel(zerolog.Disabled),
jsonrpc.WithAddress(viper.GetString("connection")),
jsonrpc.WithAddress(connectionAddress),
)
cli.ErrCheck(err, quiet, "Failed to access client")
block, err := execClient.(execclient.BlocksProvider).Block(ctx, blockStr)
Expand Down
62 changes: 33 additions & 29 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,10 @@ func connect(ctx context.Context) error {
// Handle offline connection.
c, err = conn.New(ctx, "offline")
} else {
if viper.GetString("connection") != "" {
outputIf(debug, fmt.Sprintf("Connecting to %s", viper.GetString("connection")))
c, err = conn.New(ctx, viper.GetString("connection"))
} else {
switch strings.ToLower(viper.GetString("network")) {
case "mainnet":
outputIf(debug, "Connecting to mainnet")
c, err = conn.New(ctx, "https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
case "ropsten":
outputIf(debug, "Connecting to ropsten")
c, err = conn.New(ctx, "https://ropsten.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
case "rinkeby":
outputIf(debug, "Connecting to rinkeby")
c, err = conn.New(ctx, "https://rinkeby.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
case "goerli", "gorli", "görli":
outputIf(debug, "Connecting to goerli")
c, err = conn.New(ctx, "https://goerli.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
case "sepolia":
outputIf(debug, "Connecting to sepolia")
c, err = conn.New(ctx, "https://sepolia.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
default:
cli.Err(quiet, fmt.Sprintf("Unknown network %s", viper.GetString("network")))
}
var address string
address, err = connectionAddress(ctx)
if err == nil {
c, err = conn.New(ctx, address)
}
}
if err != nil {
Expand All @@ -201,6 +182,28 @@ func connect(ctx context.Context) error {
return nil
}

// connectionAddress provides the address of an execution client.
func connectionAddress(ctx context.Context) (string, error) {
if viper.GetString("connection") != "" {
return viper.GetString("connection"), nil
}

switch strings.ToLower(viper.GetString("network")) {
case "mainnet":
return "https://mainnet.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6", nil
case "ropsten":
return "https://ropsten.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6", nil
case "rinkeby":
return "https://rinkeby.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6", nil
case "goerli", "gorli", "görli":
return "https://goerli.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6", nil
case "sepolia":
return "https://sepolia.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6", nil
default:
return "", fmt.Errorf("unknown network %s", viper.GetString("network"))
}
}

// cmdPath recurses up the command information to create a path for this command through commands and subcommands
func cmdPath(cmd *cobra.Command) string {
if cmd.Parent() == nil || cmd.Parent().Name() == "ethereal" {
Expand Down Expand Up @@ -266,12 +269,13 @@ func logTransaction(tx *types.Transaction, fields log.Fields) {
setupLogging()

txFields := log.Fields{
"networkid": c.ChainID(),
"transactionid": tx.Hash().Hex(),
"gas": tx.Gas(),
"gasprice": tx.GasPrice().String(),
"value": tx.Value().String(),
"data": hex.EncodeToString(tx.Data()),
"networkid": c.ChainID(),
"transactionid": tx.Hash().Hex(),
"gas": tx.Gas(),
"fee-per-gas": tx.GasFeeCap().String(),
"priority-fee-per-gas": tx.GasTipCap().String(),
"value": tx.Value().String(),
"data": hex.EncodeToString(tx.Data()),
}
fromAddress, err := types.Sender(signer, tx)
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var versionCmd = &cobra.Command{
ethereal version.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("2.8.0")
fmt.Println("2.8.1")
if viper.GetBool("verbose") {
buildInfo, ok := dbg.ReadBuildInfo()
if ok {
Expand Down

0 comments on commit 81d93b8

Please sign in to comment.