Skip to content

Eth.sendRawTransaction

Sends a transaction that is already signed and encoded. Returns its hash.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const program = Effect.gen(function* () {
// signed by the node, which holds the key of this account
const transaction = yield* Eth.signTransaction({ from: FROM, to: TO, value: 10n ** 18n });
return yield* Eth.sendRawTransaction(transaction);
});
const hash = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// "0x…": the hash of the transaction, 32 bytes

The node only forwards the bytes: it needs no key, so this works on a public node.

The bytes are a signed transaction. Signer.signTransaction gives them for a key your program holds, and Eth.signTransaction for a key the node holds. Tx.send signs and sends in one step.

The examples below leave the transport out.

The param is the signed transaction as hex, whatever produced it.

import { type Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
// A signed, encoded transaction: from a hardware wallet, a signing service,
// or another library.
declare const transaction: Hex.Hex;
const hash = Eth.sendRawTransaction(transaction);

The hash of a transaction is the Keccak-256 of its signed bytes, so you can know it without the node.

import { Effect } from "effect";
import { Hash, Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const send = (transaction: Hex.Hex) =>
Effect.gen(function* () {
const expected = Hash.keccak256(yield* Hex.toBytes(transaction));
const hash = yield* Eth.sendRawTransaction(transaction);
return hash === expected; // true
});

Effect<Hash, Method.Error, Transport>

The hash of the transaction. It says the node accepted the transaction, not that it is mined or that it succeeded.

The signed transaction, encoded: the type byte followed by the RLP of its fields, or the RLP alone for a legacy transaction. An EIP-1559 transaction starts with 0x02.

import { type Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
declare const transaction: Hex.Hex; // "0x02f86c…"
const hash = Eth.sendRawTransaction(transaction);
  • Sent is not mined. The hash comes back when the node accepts the transaction. Ask for the receipt with Eth.getTransactionReceipt until there is one, or use Eth.sendRawTransactionSync.
  • Nothing is filled in. The nonce, the gas and the fees are inside the signed bytes. The node cannot change them.
  • Once only. Sending the same bytes again after the transaction is mined fails: its nonce is used.

The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:

Error Reason When
RpcError InvalidParams the bytes are not an encoded transaction
RpcError TransactionRejected the nonce of the transaction was used already

eth_sendRawTransaction