Skip to content

Eth.sendTransactionSync

Asks the node to sign and send a transaction like Eth.sendTransaction, then waits for it to be mined and returns its receipt. 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* () {
const receipt = yield* Eth.sendTransactionSync({ from: FROM, to: TO, value: 10n ** 18n });
return { status: receipt.status, block: receipt.blockNumber, gas: receipt.gasUsed };
});
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, gas: 21000n }

One call sends the transaction and returns its Receipt: there is nothing to poll. The node signs, so from must be an account it holds the key of.

The examples below leave the transport out.

A transaction that reverts is mined too. The call succeeds and the receipt says "reverted".

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const outcome = Effect.gen(function* () {
// a contract that reverts when it gets no calldata
const receipt = yield* Eth.sendTransactionSync({ from: FROM, to: TO, gas: 100_000n });
return receipt.status === "success" ? "done" : "reverted";
});
// "reverted"

Leave to out and put the creation code in data. The receipt has the address of the new contract.

import { Effect } from "effect";
import { Address, type Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
// The creation code of the contract, as your compiler outputs it.
declare const bytecode: Hex.Hex;
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const deployed = Effect.gen(function* () {
const receipt = yield* Eth.sendTransactionSync({ from: FROM, data: bytecode });
return receipt.contractAddress;
});

A node that does not know the method answers with the reason MethodNotFound. Fall back to sending and polling.

import { Effect, Schedule } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const receipt = Effect.gen(function* () {
const request = { from: FROM, to: TO, value: 1n };
return yield* Eth.sendTransactionSync(request).pipe(
Effect.catchReason("RpcError", "MethodNotFound", () =>
Effect.flatMap(Eth.sendTransaction(request), (hash) =>
Eth.getTransactionReceipt(hash).pipe(
Effect.repeat({
until: (receipt) => receipt !== null,
schedule: Schedule.spaced("1 second"),
}),
),
),
),
);
});

The transaction is sent once, and only the request for its receipt is repeated.

Effect<Receipt.Receipt, Method.Error, Transport>

The Receipt of the mined transaction: its status, the block it is in, the gas it used and its logs.

The transaction to send. The fields are those of Eth.sendTransaction.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const receipt = Eth.sendTransactionSync({ from: FROM, to: TO, value: 1n, gas: 21_000n });
  • The call waits for a block. With automatic mining off on anvil, it does not return until a block is mined. Bound the wait with Effect.timeout.
  • A revert is not a failure. The receipt of a reverted transaction is a success of the call. Read receipt.status.
  • No local signing. As with Eth.sendTransaction, the key is the node’s.
  • Not served by every client. anvil serves it.

The three errors every method can fail with. The cases specific to this method are those of Eth.sendTransaction, and:

Error Reason When
RpcError MethodNotFound the node does not serve the method

eth_sendTransactionSync