71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
// Copyright 2021 Evmos Foundation
|
|
// This file is part of Evmos' Ethermint library.
|
|
//
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
|
package net
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
ethermint "github.com/cerc-io/laconicd/types"
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
|
)
|
|
|
|
// PublicAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
|
|
type PublicAPI struct {
|
|
networkVersion uint64
|
|
tmClient rpcclient.Client
|
|
}
|
|
|
|
// NewPublicAPI creates an instance of the public Net Web3 API.
|
|
func NewPublicAPI(clientCtx client.Context) *PublicAPI {
|
|
// parse the chainID from a integer string
|
|
chainIDEpoch, err := ethermint.ParseChainID(clientCtx.ChainID)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &PublicAPI{
|
|
networkVersion: chainIDEpoch.Uint64(),
|
|
tmClient: clientCtx.Client,
|
|
}
|
|
}
|
|
|
|
// Version returns the current ethereum protocol version.
|
|
func (s *PublicAPI) Version() string {
|
|
return fmt.Sprintf("%d", s.networkVersion)
|
|
}
|
|
|
|
// Listening returns if client is actively listening for network connections.
|
|
func (s *PublicAPI) Listening() bool {
|
|
ctx := context.Background()
|
|
netInfo, err := s.tmClient.NetInfo(ctx)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return netInfo.Listening
|
|
}
|
|
|
|
// PeerCount returns the number of peers currently connected to the client.
|
|
func (s *PublicAPI) PeerCount() int {
|
|
ctx := context.Background()
|
|
netInfo, err := s.tmClient.NetInfo(ctx)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return len(netInfo.Peers)
|
|
}
|