2022-12-16 09:48:38 +00:00
|
|
|
// 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
|
2021-06-23 06:38:05 +00:00
|
|
|
package net
|
2019-10-15 01:20:35 +00:00
|
|
|
|
|
|
|
import (
|
2021-07-12 12:26:33 +00:00
|
|
|
"context"
|
2019-10-15 01:20:35 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2022-06-19 09:43:41 +00:00
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
2021-07-12 12:26:33 +00:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
2019-10-15 01:20:35 +00:00
|
|
|
)
|
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
// PublicAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec.
|
|
|
|
type PublicAPI struct {
|
2019-10-15 01:20:35 +00:00
|
|
|
networkVersion uint64
|
2021-07-12 12:26:33 +00:00
|
|
|
tmClient rpcclient.Client
|
2019-10-15 01:20:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
// NewPublicAPI creates an instance of the public Net Web3 API.
|
|
|
|
func NewPublicAPI(clientCtx client.Context) *PublicAPI {
|
2019-10-15 01:20:35 +00:00
|
|
|
// parse the chainID from a integer string
|
2021-04-19 07:30:55 +00:00
|
|
|
chainIDEpoch, err := ethermint.ParseChainID(clientCtx.ChainID)
|
2019-10-15 01:20:35 +00:00
|
|
|
if err != nil {
|
2020-09-24 17:50:47 +00:00
|
|
|
panic(err)
|
2019-10-15 01:20:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
return &PublicAPI{
|
2020-09-24 17:50:47 +00:00
|
|
|
networkVersion: chainIDEpoch.Uint64(),
|
2021-07-12 12:26:33 +00:00
|
|
|
tmClient: clientCtx.Client,
|
2019-10-15 01:20:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the current ethereum protocol version.
|
2021-06-23 06:38:05 +00:00
|
|
|
func (s *PublicAPI) Version() string {
|
2021-04-18 16:39:15 +00:00
|
|
|
return fmt.Sprintf("%d", s.networkVersion)
|
2019-10-15 01:20:35 +00:00
|
|
|
}
|
2021-07-12 12:26:33 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|