The net_ methods (Net)
The net_ namespace: the node’s place in the peer-to-peer network. Its network id, whether it
listens for peers, and how many it has.
import { Effect } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Net } from "@ethect/core/rpc";
const networkId = await Net.version().pipe( Effect.provide(Transport.http("http://127.0.0.1:8545")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,);// 31337A method returns an
Effect<…, Method.Error, Transport> of the decoded
result. The name is the wire name without its prefix: net_version is Net.version.
Methods
Section titled “Methods”| Method | JSON-RPC | Returns | Description |
|---|---|---|---|
version |
net_version |
number |
The network id. |
listening |
net_listening |
boolean |
Whether the node accepts connections from peers. |
peerCount |
net_peerCount |
number |
How many peers the node is connected to. |
None of them takes a parameter.
version
Section titled “version”Returns the network id. It is usually, but not always, the chain id.
import { Effect } from "effect";import { Eth, Net } from "@ethect/core/rpc";
const ids = Effect.gen(function* () { const networkId = yield* Net.version(); // 31337 const chainId = yield* Eth.chainId(); // 31337 return { networkId, chainId };});- JSON-RPC:
net_version - Parameters: none.
- Returns:
number. The node sends the id as a decimal string,"31337", not as a hex quantity, and it is decoded to a number. - To know which chain a transaction is for, use
Eth.chainId: that is the id a signature commits to.
listening
Section titled “listening”Returns whether the node accepts connections from peers.
import { Effect } from "effect";import { Net } from "@ethect/core/rpc";
const listening = Net.listening().pipe( Effect.map((on) => (on ? "accepts peers" : "accepts no peer")),);- JSON-RPC:
net_listening - Parameters: none.
- Returns:
boolean. anvil answerstrue.
peerCount
Section titled “peerCount”Returns how many peers the node is connected to.
import { Effect } from "effect";import { Net } from "@ethect/core/rpc";
const peers = Net.peerCount().pipe( Effect.catchReason("RpcError", "MethodNotFound", () => Effect.succeed(0)),);- JSON-RPC:
net_peerCount - Parameters: none.
- Returns:
number. - anvil 1.9 does not serve it: it answers
Method not found, which fails the effect with anRpcErrorof reasonMethodNotFound. The decoding of the answer was not checked against a node.
Behavior
Section titled “Behavior”- On anvil. After
Anvil.setChainId,Net.versionreturns the new id too.