Skip to content

Commit

Permalink
Add ChainID function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed May 26, 2022
1 parent 79cb6a7 commit a0bbba7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
35 changes: 35 additions & 0 deletions jsonrpc/chainid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © 2022 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jsonrpc

import (
"context"
"strconv"
"strings"
)

// ChainID returns the chain ID of the node.
func (s *Service) ChainID(ctx context.Context) (uint64, error) {
version := ""
if err := s.client.CallFor(&version, "eth_chainId"); err != nil {
return 0, err
}

res, err := strconv.ParseUint(strings.TrimPrefix(version, "0x"), 10, 64)
if err != nil {
return 0, err
}

return res, nil
}
41 changes: 41 additions & 0 deletions jsonrpc/chainid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2022 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jsonrpc_test

import (
"context"
"fmt"
"os"
"testing"

execclient "github.com/attestantio/go-execution-client"
"github.com/attestantio/go-execution-client/jsonrpc"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

func TestChainID(t *testing.T) {
ctx := context.Background()
s, err := jsonrpc.New(ctx,
jsonrpc.WithLogLevel(zerolog.Disabled),
jsonrpc.WithAddress(os.Getenv("JSONRPC_ADDRESS")),
jsonrpc.WithTimeout(timeout),
)
require.NoError(t, err)

id, err := s.(execclient.ChainIDProvider).ChainID(ctx)
require.NoError(t, err)
require.NotNil(t, id)
fmt.Printf("%d\n", id)
}
6 changes: 6 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ type ChainHeightProvider interface {
ChainHeight(ctx context.Context) (uint32, error)
}

// ChainIDProvider is the interface for providing the chain ID.
type ChainIDProvider interface {
// ChainID returns the chain ID.
ChainID(ctx context.Context) (uint64, error)
}

// EventsProvider is the interface for providing events.
type EventsProvider interface {
// Events returns the events matching the filter.
Expand Down

0 comments on commit a0bbba7

Please sign in to comment.