Skip to content

Tx.send

Sends calls and waits for them to be in a block. One call gives its receipt, several give their receipts in order.

deposit.ts
import { Config, Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Erc20, Signer, Transport, Tx } from "@ethect/core";
import { Vault } from "./vault";
// A key and a node, both read from the environment as secrets.
const Script = Layer.unwrap(
Effect.gen(function* () {
const url = yield* Config.Redacted("RPC_URL");
const key = yield* Config.Redacted("PRIVATE_KEY");
return Layer.merge(Signer.privateKey(key), Transport.http(url));
}),
).pipe(Layer.provide(FetchHttpClient.layer));
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
const VAULT = Address.makeUnsafe("0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB");
const program = Effect.gen(function* () {
const me = yield* Signer.address;
const amount = 100_000_000n;
// Signed, sent, and waited for. With a key: one receipt per call.
const receipts = yield* Tx.send([
Erc20.at(USDC).approve(VAULT, amount),
Vault.at(VAULT).deposit(amount, me),
]);
return receipts.map((receipt) => receipt.blockNumber);
});
const blocks = await program.pipe(Effect.provide(Script), Effect.runPromise);
vault.ts
import { Abi, Contract, Erc4626, Fn } from "@ethect/core";
// ERC-4626 is built in, and declares no error. This adds the one the vault reverts with.
export const Vault = Erc4626.extend(
Abi.parse(["error ERC4626ExceededMaxDeposit(address receiver, uint256 assets, uint256 max)"]),
);
// Not a standard: WETH takes ether with `deposit()`.
export const Weth = Contract.make({ deposit: Fn.payable()("deposit", { inputs: [] }) });

Tx.send needs a Signer to sign and a Transport to follow what was sent. The same line runs with a key in a script and with a wallet in a browser. The examples below leave both out.

One call gives one receipt.

import { type Address, Erc20, Tx } from "@ethect/core";
const pay = (usdc: Address.Address, to: Address.Address) =>
Tx.send(Erc20.at(usdc).transfer(to, 1_000_000n));
// Effect<Receipt, Tx.Failure<never>, Signer | Transport>

Several calls give their receipts in order. How many signatures they take depends on the signer, and the code does not change.

import { type Address, Erc20, Signer, Tx } from "@ethect/core";
import { Vault } from "./vault";
const deposit = (usdc: Address.Address, vault: Address.Address, amount: bigint, me: Address.Address) =>
// one signature or two: the same line
Tx.send([Erc20.at(usdc).approve(vault, amount), Vault.at(vault).deposit(amount, me)]);
The signer The calls go You get
a local key together, as transactions with nonces in a row one receipt per call
a wallet that batches (EIP-5792 atomic) as one batch, all or nothing: one prompt one receipt for them all
a wallet that does not one prompt per call, each mined before the next is asked one receipt per call

Tx.plan is what decides, and says so before anything is signed.

batch says how several calls may share a signature.

import { type Call, Tx } from "@ethect/core";
const three = (calls: ReadonlyArray<Call.Call<unknown, unknown>>) => [
// the default: one signature when the signer can, one per call otherwise
Tx.send(calls, { batch: "auto" }),
// each call is mined before the next one is signed
Tx.send(calls, { batch: "never" }),
// all or nothing, or `TxUnsupported` before any prompt
Tx.send(calls, { batch: "atomic" }),
];

A payable function takes a value as its last argument. A plain transfer is a raw call.

import { type Address, Call, Tx } from "@ethect/core";
import { Weth } from "./vault";
const wrap = (weth: Address.Address) =>
Tx.send(Weth.at(weth).deposit({ value: 10n ** 18n }));
const tip = (to: Address.Address) =>
Tx.send(Call.make({ to, value: 10n ** 17n }));
import { type Address, Erc20, Tx } from "@ethect/core";
const fromTreasury = (usdc: Address.Address, treasury: Address.Address, to: Address.Address) =>
Tx.send(Erc20.at(usdc).transfer(to, 1n), { from: treasury, confirmations: "finalized" });

Each send is a signature of its own. A local signer hands out the nonces: see Signer.Nonces.

import { Effect } from "effect";
import { type Address, Erc20, Tx } from "@ethect/core";
const payAll = (usdc: Address.Address, recipients: ReadonlyArray<Address.Address>) =>
Effect.forEach(recipients, (to) => Tx.send(Erc20.at(usdc).transfer(to, 1n)), {
concurrency: 20,
});

EIP-5792 capabilities go to the wallet with the calls. One marked optional is left out for a signer that does not have it. Without optional, such a signer refuses.

import { type Address, Erc20, Tx } from "@ethect/core";
const sponsored = (usdc: Address.Address, to: Address.Address, url: string) =>
Tx.send(Erc20.at(usdc).transfer(to, 1n), {
capabilities: { paymasterService: { url, optional: true } },
});

Every failure is a value with a message. A revert carries the error of the contract, typed by what the calls declare. Effect.catchReason handles one reason of a TxReverted, and leaves the other reverts in the type.

import { Effect } from "effect";
import { type Address, Tx } from "@ethect/core";
import { Vault } from "./vault";
const deposit = (vault: Address.Address, amount: bigint, me: Address.Address) =>
Tx.send(Vault.at(vault).deposit(amount, me)).pipe(
Effect.catchTag("Rejected", () => Effect.succeed("You refused the transaction" as const)),
// the vault's own error: its fields are typed
Effect.catchReason("TxReverted", "ERC4626ExceededMaxDeposit", (reason) =>
Effect.succeed(`The vault takes ${reason.max} more` as const),
),
);

Effect<Receipt, Tx.Failure<E>, Signer | Transport> for one call.

Effect<ReadonlyArray<Receipt>, Tx.Failure<E>, Signer | Transport> for a list of calls.

A Receipt is always a success: a transaction that reverted is a failure. E is the errors the calls declare. A list of no call gives no receipt, and asks nothing.

  • Type: Call.Call<A, E> | ReadonlyArray<Call.Call<any, any>>

One call, or several: what fn.at(address)(...args) and Call.make build.

  • Type: "auto" | "atomic" | "never"
  • Default: "auto"

How several calls share a signature. The table is on Tx.plan.

The account that sends.

  • Type: number | "safe" | "finalized"
  • Default: 1

How deep each transaction must be before its receipt is given. See Tx.wait.

EIP-5792 capabilities to ask of the wallet, by name. See Tx.submit.

Transaction fields to set instead of leaving them to the wallet or to an estimate. One set for every transaction, or a list with one set per call, in the order of the calls.

Tx.send is a loop over the three other functions, and one rule.

send.ts
import { Effect } from "effect";
import { type Call, Err, Tx } from "@ethect/core";
import type { Receipt } from "@ethect/core/rpc";
export const send = (calls: ReadonlyArray<Call.Call<unknown, unknown>>, options?: Tx.SendOptions) =>
Effect.gen(function* () {
const steps = yield* Tx.plan(calls, options); // one step per signature
const receipts: Array<Receipt.Receipt> = [];
for (const step of steps) {
const pending = yield* Tx.submit(step, options); // one signature
const errors = Err.of(step.calls); // what a revert may be
receipts.push(...(yield* Tx.wait(pending, { ...options, errors }))); // in a block
}
return receipts;
});
  • The one rule. Under batch: "auto", a step of several calls that fails with TxUnsupported is planned again with batch: "never", and the loop goes on with one step per call. Nothing else is tried again.
  • What the rule covers. A user who declines the upgrade a batch needs. A node that cannot measure the gas of calls sent together. A batch the wallet finds too large. Transaction fields set on a wallet batch, which has no place for them.
  • Each signature waits for the one before it. A step is submitted once the step before it is in a block, as deep as confirmations asks.
  • A send does not fail with UnknownChain. Tx.wait refuses what was submitted on another chain than the transport’s. A send waits on the transport it has just submitted on, so it does not compare them again.
  • Failures speak of the calls you gave. The call of a TxReverted counts from your first call, whichever signature it was in. Its receipts, and those of a TxDropped, start with the receipts of the signatures before.
  • A list of fields follows the calls. With transaction as a list, each signature gets the sets of its own calls.
  • TxPartial is passed on as it is. With a local key, calls sent together can stop half way: sent holds what went out, and can be given to Tx.wait.
  • The state of a flow is yours. Tx.send keeps nothing. To show each signature as a step of its own, call Tx.plan, Tx.submit and Tx.wait yourself.
Error From When
Signer.Rejected submit the user refused to sign. Nothing was sent for that signature
Tx.Unsupported plan, submit the signer cannot do what was asked: reason says what
Tx.Reverted submit, wait a call reverted, in its block or while it was estimated. reason is typed
Tx.Dropped wait a transaction left the mempool, or another one took its nonce
Tx.Partial submit some calls went out before one was refused
AbiError submit an argument is not a value its type takes: nothing was signed
Signer.Error plan, submit, wait the wallet is unreachable, on another chain, or does not hold the account
RpcError, TransportError all the node refused, or could not be reached

Two services are asked: the Signer signs, the Transport says where things stand. The requests are those of Tx.plan, Tx.submit and Tx.wait, in that order.

One call, with a local key. Seven requests when it is mined at once, as on a development node, and four when every field is set:

  1. your nodeeth_chainIdonce per transport
  2. your nodeeth_getBlockByNumber("latest")the base fee
  3. your nodeeth_maxPriorityFeePerGasthe tip
  4. your nodeeth_estimateGasthe gas. A revert here fails the send: nothing is sent
  5. your nodeeth_getTransactionCount(from, "pending")once per account: then the signer counts
  6. your nodeeth_sendRawTransactionthrough Tx.Broadcast
  7. your nodeeth_getTransactionReceiptasked first: in a block already, the send is over

On a chain with a block time, the receipt is not there yet. The wait goes on at the heads:

  1. your nodeeth_getBlockByNumber("latest")the head: pushed over a WebSocket, shared by every wait
  2. your nodeeth_getTransactionCount(from, "latest")was its nonce taken?at each head with no receipt
  3. your nodeeth_getTransactionReceiptat each head after the first, until it is in a block

One call, with a wallet. Three requests to the wallet and two to your node, then the heads as above while it is in no block:

  1. the walleteth_accountsthe sender, unless from is given
  2. your nodeeth_chainIdonce per transport
  3. the walleteth_chainIdis the wallet on that chain?
  4. the walleteth_sendTransactionthe prompt
  5. your nodeeth_getTransactionReceiptasked firstthen at each head until it is in a block

Several calls:

  1. a local key

    1. your nodeeth_simulateV1in place of eth_estimateGas
    2. your nodeeth_sendRawTransactionthenone per call
    3. your nodeeth_getTransactionReceiptone per call
  2. a wallet that batches

    1. the walletwallet_getCapabilities
    2. the walletwallet_sendCalls
    3. the walletwallet_getCallsStatusthen, oncethen at each head while the batch is pending
    4. your nodeeth_getTransactionReceiptone, for the receipt
  3. a wallet that does not

    1. the walletwallet_getCapabilities
    2. the wallet and your nodethen the requests of one call, once per call