Eth.sendRawTransactionSync
Sends a signed, encoded transaction, then waits for it to be mined and returns its receipt (EIP-7966). Not every client serves it.
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 }); const receipt = yield* Eth.sendRawTransactionSync(transaction); return { status: receipt.status, block: receipt.blockNumber };});
const result = await program.pipe( Effect.provide(Transport.http("http://127.0.0.1:8545")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,);// { status: "success", block: 1n }One call sends the bytes and returns the Receipt: there is
nothing to poll.
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.
With a timeout
Section titled “With a timeout”The second param is how long the node may wait, in milliseconds. When the time runs out the
node answers with an error whose reason is TransactionNotMinedInTime, and the transaction
can still be mined later.
import { Effect } from "effect";import { type Hex } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
// A signed, encoded transaction.declare const transaction: Hex.Hex;
const outcome = Eth.sendRawTransactionSync(transaction, 2_000).pipe( Effect.map((receipt) => ({ mined: true as const, status: receipt.status })), Effect.catchTag("RpcError", (error) => // not mined in time: `error.data` is the hash of the transaction error.reason._tag === "TransactionNotMinedInTime" ? Effect.succeed({ mined: false as const, hash: error.data }) : Effect.fail(error), ),);Returns
Section titled “Returns”Effect<Receipt.Receipt, Method.Error, Transport>
The Receipt of the mined transaction.
Parameters
Section titled “Parameters”transaction
Section titled “transaction”- Type:
Hex
The signed transaction, encoded, as for
Eth.sendRawTransaction.
import { type Hex } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
declare const transaction: Hex.Hex; // "0x02f86c…"
const receipt = Eth.sendRawTransactionSync(transaction);timeout (optional)
Section titled “timeout (optional)”- Type:
number
The longest the node may wait for the transaction to be mined, in milliseconds. It is a JSON number on the wire, not a hex quantity.
import { type Hex } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
declare const transaction: Hex.Hex;
const receipt = Eth.sendRawTransactionSync(transaction, 5_000);Left out, the param is not sent and the node applies its own limit.
Behavior
Section titled “Behavior”- A revert is not a failure. The receipt of a reverted transaction is a success of the
call. Read
receipt.status. - A timeout is not a rejection. The transaction is in the pool and can still be mined. The error carries its hash: ask for the receipt later.
- The timeout has a reason. The code
4isTransactionNotMinedInTime, anderror.datais the hash of the transaction, typedunknown. - Not served by every client. anvil serves it.
Errors
Section titled “Errors”The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:
| Error | Reason | When |
|---|---|---|
RpcError |
TransactionNotMinedInTime |
the transaction is not mined within the timeout (code 4): error.data is its hash |
RpcError |
InvalidParams |
the bytes are not an encoded transaction |
RpcError |
TransactionRejected |
the nonce of the transaction was used already |
JSON-RPC method
Section titled “JSON-RPC method”eth_sendRawTransactionSync