Eth.syncing
Returns false when the node is in sync, or how far it has come while it is still syncing.
import { Effect } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const status = await Eth.syncing().pipe( Effect.provide(Transport.http("http://127.0.0.1:8545")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,);// falseThe call works over every transport. The examples below leave the transport out.
Read the progress
Section titled “Read the progress”Compare the result with false first. What is left is the progress, with three block
numbers.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const progress = Eth.syncing().pipe( Effect.map((status) => status === false ? "in sync" : `at block ${status.currentBlock} of ${status.highestBlock}`, ),);Refuse to read from a node that is behind
Section titled “Refuse to read from a node that is behind”A node that is syncing answers from old state. Fail before reading from it.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const balance = Effect.gen(function* () { const status = yield* Eth.syncing(); if (status !== false) return yield* Effect.fail("the node is still syncing"); return yield* Eth.getBalance(ADDRESS, "latest");});Returns
Section titled “Returns”Effect<SyncStatus.SyncStatus, Method.Error, Transport>
false, or a SyncStatus.Progress with currentBlock,
highestBlock and, where the client reports it, startingBlock.
Parameters
Section titled “Parameters”None.
Behavior
Section titled “Behavior”falseon a development node. anvil has nothing to sync and answersfalse.- Three fields are kept. A node that reports more than the three block numbers still decodes. The extra fields are not in the result.
- To be told when it changes, use
Eth.subscribe.syncinginstead of asking again.
JSON-RPC method
Section titled “JSON-RPC method”eth_syncing