Tx.Broadcast
How the bytes a local signer signed reach the network. The default is eth_sendRawTransaction
on the transport, and one function replaces it.
import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { type Hex, Signer, Transport, Tx } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
/** `eth_sendRawTransaction` on the transport in context: the method of the JSON-RPC layer. */export const send = (raw: Hex.Hex) => Eth.sendRawTransaction(raw);
const Relay = Transport.http("https://relay.example").pipe(Layer.provide(FetchHttpClient.layer));
/** Signed bytes go to the relay, which is built once. */export const ViaRelay = Layer.effect(Tx.Broadcast)( Effect.map(Layer.build(Relay), (relay) => ({ send: (raw: Hex.Hex) => Effect.provide(send(raw), relay), })),);
// Whatever sends inside this goes out through the relay.const signature = Signer.signMessage("hello").pipe(Effect.provide(ViaRelay));Tx.Broadcast is a reference: it has a default, so a program needs to provide nothing. Only
signers that sign here use it: Signer.privateKey,
Signer.fromSign, Signer.make. A
wallet sends what it signs itself.
For one part of a program
Section titled “For one part of a program”A reference is replaced for what is inside, like any service.
import { Effect } from "effect";import { type Signer, type Transport, Tx } from "@ethect/core";
declare const relay: Tx.Broadcast["send"];
/** `program` sends through the relay, and the rest of the application as before. */const privately = <A, E>(program: Effect.Effect<A, E, Signer.Signer | Transport.Transport>) => program.pipe(Effect.provideService(Tx.Broadcast, { send: relay }));Two nodes at once
Section titled “Two nodes at once”The first node that takes the transaction gives its hash. A node that refuses or does not answer loses nothing, as long as the other one takes it.
import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport, Tx } from "@ethect/core";import { send } from "./relay";
const Other = Transport.http("https://other.example").pipe(Layer.provide(FetchHttpClient.layer));
export const ToBoth = Layer.effect(Tx.Broadcast)( Effect.map(Layer.build(Other), (other) => ({ // the node of the program, and the other one send: (raw: `0x${string}`) => Effect.raceAll([send(raw), Effect.provide(send(raw), other)]), })),);The reference
Section titled “The reference”- Type:
Context.Reference<Tx.Broadcast>
| Member | Type | Does |
|---|---|---|
send |
(raw: Hex) => Effect<Hash, Transport.Error | CodecError, Transport.Transport> |
hands signed bytes to the network, and gives the hash of the transaction |
send runs with the Transport of whoever sends, and may use it or not.
Behavior
Section titled “Behavior”What send answers is what the signer acts on. The whole table is on
Signer.Nonces.
- A hash means accepted. The nonce is used.
- An
RpcErrormeans refused. The nonce is given back, unless the message says “already known”, which is accepted, or “nonce too low”. - A
TransportErrormeans no answer.sendis called again with the same bytes: 200 ms later, then twice as long each time up to 5 s, for 2 minutes. Makesendsafe to call twice with the same bytes: a node answers “already known”, which is fine. - A
CodecErroris read as aTransportError. An answer that is no hash is no answer: the same bytes are sent again. Bytes that could not be written never left, and are not. - The account’s count and its transactions are still read from the node of the program. A relay only gets the bytes. After “nonce too low”, the signer asks its own node whether it has the transaction.
- It is not used by
Signer.signTransaction, which sends nothing.
Errors
Section titled “Errors”send fails with what a method of the JSON-RPC layer fails with: an RpcError when the
transaction is refused, a TransportError when nobody answered, a
CodecError when the answer is no hash. So
Eth.sendRawTransaction is a send as it is. What a relay
says in another way has to be turned into one of the three.
The signer that sends fails with a Transport.Error alone: a
CodecError reaches you as a TransportError, with the CodecError as its cause.
Under the hood
Section titled “Under the hood”The default, on the Transport in context:
- your node
eth_sendRawTransaction(raw)gives the hash of the transaction