Eth.sendTransaction
Asks the node or wallet to sign a transaction with an account it holds the key of, and to send it. Returns the transaction 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 = Eth.sendTransaction({ from: FROM, to: TO, value: 10n ** 18n });
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 bytesThe example sends one ether between two of anvil’s development accounts, whose keys anvil holds.
This method does not sign. The request goes to the node or the wallet, which holds the key,
and from must be an account it can sign for. To sign with a key your program holds, and to
wait for the receipt, use Tx.send with a
Signer.
The examples below leave the transport out.
Waiting for the receipt
Section titled “Waiting for the receipt”The hash comes back when the node accepts the transaction, which can be before it is mined. Ask for the receipt until there is one.
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 hash = yield* Eth.sendTransaction({ from: FROM, to: TO, value: 1n }); return yield* Eth.getTransactionReceipt(hash).pipe( Effect.repeat({ until: (receipt) => receipt !== null, schedule: Schedule.spaced("1 second"), }), );});Where the node serves it, Eth.sendTransactionSync
does both in one call.
Calling a contract
Section titled “Calling a contract”Put the contract in to and the calldata in data, as hex.
Call.encode builds both from a call of a contract.
import { Effect } from "effect";import { Address, Call, Erc20 } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");const RECIPIENT = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const hash = Effect.gen(function* () { // transfer(address,uint256): the selector, then one 32-byte word per argument const { data } = yield* Call.encode(Erc20.at(TOKEN).transfer(RECIPIENT, 10n ** 18n)); return yield* Eth.sendTransaction({ from: FROM, to: TOKEN, data });});Deploying a contract
Section titled “Deploying a contract”Leave to out and put the creation code in data.
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 hash = Eth.sendTransaction({ from: FROM, data: bytecode });The address of the new contract is the contractAddress of the receipt.
From a wallet
Section titled “From a wallet”Over the EIP-1193 transport, the wallet asks its user to
approve. A refusal is an RpcError whose reason is UserRejectedRequest.
import { Effect } from "effect";import { Address, type Eip1193, Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const sent = await Effect.gen(function* () { const [from] = yield* Eth.accounts(); return yield* Eth.sendTransaction({ from, to: TO, value: 10n ** 16n }).pipe( Effect.map((hash) => ({ sent: true as const, hash })), Effect.catchReason("RpcError", "UserRejectedRequest", () => Effect.succeed({ sent: false as const }), ), );}).pipe(Effect.provide(Transport.eip1193(provider)), Effect.runPromise);Returns
Section titled “Returns”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.
Parameters
Section titled “Parameters”request
Section titled “request”The transaction to send. Every field is optional: the node or wallet fills in what is missing.
| Field | Type | Description |
|---|---|---|
from |
Address |
the account that signs and pays |
to |
Address |
the recipient or the contract, left out to create a contract |
value |
bigint |
wei to send |
data |
Hex |
the calldata, or the creation code of a contract |
gas |
bigint |
the gas limit |
nonce |
number |
the nonce, to replace a pending transaction or to order several |
maxFeePerGas, maxPriorityFeePerGas |
bigint |
the fees of an EIP-1559 transaction, in wei |
gasPrice |
bigint |
the gas price of a legacy transaction, in wei |
The other fields (an access list, blobs, authorizations) are on the
TransactionRequest page.
import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const hash = Eth.sendTransaction({ from: FROM, to: TO, value: 1n, maxFeePerGas: 30n * 10n ** 9n, maxPriorityFeePerGas: 10n ** 9n,});Behavior
Section titled “Behavior”- No signing here. The request goes to the node or wallet unsigned. A node that holds no
key for
fromrefuses it: sign with a key of your own throughTx.send, or sign elsewhere and useEth.sendRawTransaction. - Sent is not mined. With automatic mining off on anvil, the hash comes back while the
receipt is still
null. - A revert still has a hash. anvil mines a transaction that reverts: only its receipt, with
the status
"reverted", says so. - The fees choose the type. anvil sends an EIP-1559 transaction, and a legacy one when the
request has a
gasPrice. - Write the calldata as
data, the name wallets know.input, the name a node returns it by, is sent as well when a request has 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 |
InvalidParams |
the node holds no key for from |
RpcError |
TransactionRejected |
from cannot pay the value and the gas |
RpcError |
TransactionRejected |
the nonce was used already |
RpcError |
InvalidInput |
gas is below the intrinsic gas of the transaction |
A wallet adds UserRejectedRequest when its user refuses, as defined by EIP-1193.
JSON-RPC method
Section titled “JSON-RPC method”eth_sendTransaction