# 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.

```ts
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,
);
// 31337
```

A method returns an
[`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method) of the decoded
result. The name is the wire name without its prefix: `net_version` is `Net.version`.

## Methods

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`version`](#version) | `net_version` | `number` | The network id. |
| [`listening`](#listening) | `net_listening` | `boolean` | Whether the node accepts connections from peers. |
| [`peerCount`](#peercount) | `net_peerCount` | `number` | How many peers the node is connected to. |

None of them takes a parameter.

## version

Returns the network id. It is usually, but not always, the chain id.

```ts
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`](https://ethect.dev/rpc/eth/chain-id/): that is the id a signature commits to.

## listening

Returns whether the node accepts connections from peers.

```ts
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 answers `true`.

## peerCount

Returns how many peers the node is connected to.

```ts
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 an
  `RpcError` of reason `MethodNotFound`. The decoding of the answer was not checked against a
  node.

## Behavior

- **On anvil.** After
  [`Anvil.setChainId`](https://ethect.dev/rpc/anvil/chain/#setchainid), `Net.version` returns the new id
  too.
