Skip to content

Wallet.walletSendTransaction

Signs and sends a transaction under the wire name wallet_sendTransaction, which some wallets serve instead of eth_sendTransaction. It returns the transaction hash.

import { Effect } from "effect";
import { Address, type Eip1193, Transport } from "@ethect/core";
import { Wallet } 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 FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const program = Wallet.walletSendTransaction({ from: FROM, to: TO, value: 10n ** 18n });
const hash = await program.pipe(
Effect.provide(Transport.eip1193(provider)),
Effect.runPromise,
);

A wallet method runs over Transport.eip1193. The examples below leave the transport out.

Try Wallet.sendTransaction first, and fall back to this method when the wallet answers it with the reason MethodNotFound.

import { Effect } from "effect";
import { type TransactionRequest, Wallet } from "@ethect/core/rpc";
const send = (request: TransactionRequest.TransactionRequest) =>
Wallet.sendTransaction(request).pipe(
Effect.catchReason("RpcError", "MethodNotFound", () =>
Wallet.walletSendTransaction(request),
),
);

The user can refuse the transaction in the wallet. Catch the rejection to carry on without a hash.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Wallet } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const hash = Wallet.walletSendTransaction({ from: FROM, to: TO, value: 1n }).pipe(
Effect.catchReason("RpcError", "UserRejectedRequest", () =>
Effect.succeed(undefined),
),
);

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

The hash of the transaction.

The transaction to send. Every field is optional: the wallet fills in what is missing.

import { Address } from "@ethect/core";
import { Wallet } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const hash = Wallet.walletSendTransaction({
from: FROM,
to: TO,
value: 10n ** 18n,
// deposit() on the WETH contract
data: "0xd0e30db0",
});
  • Why the name. Wallet.sendTransaction is the re-exported Eth.sendTransaction, which calls eth_sendTransaction. This method had to take another name.
  • Same request, same result. Both methods take a TransactionRequest and decode a Hash. Only the wire name differs.
  • Quantities are encoded. value: 10n ** 18n is sent as "0xde0b6b3a7640000", and the addresses in their EIP-55 form. See values on the wire.

The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them and, for the wallet code, from EIP-1193:

Error Reason When
RpcError UserRejectedRequest the user rejected the request (code 4001)
RpcError MethodNotFound the call goes to a node: anvil serves eth_sendTransaction and not this name

wallet_sendTransaction