Skip to content

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.

relay.ts
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.

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 }));

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)]),
})),
);
  • 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.

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 RpcError means refused. The nonce is given back, unless the message says “already known”, which is accepted, or “nonce too low”.
  • A TransportError means no answer. send is called again with the same bytes: 200 ms later, then twice as long each time up to 5 s, for 2 minutes. Make send safe to call twice with the same bytes: a node answers “already known”, which is fine.
  • A CodecError is read as a TransportError. 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.

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.

The default, on the Transport in context:

  1. your nodeeth_sendRawTransaction(raw)gives the hash of the transaction