# 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.

## Usage

```ts
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`](https://ethect.dev/reference/transport/eip1193/). The examples
below leave the transport out.

### As a fallback

Try [`Wallet.sendTransaction`](https://ethect.dev/rpc/eth/send-transaction/) first, and fall back to this
method when the wallet answers it with the reason `MethodNotFound`.

```ts
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),
    ),
  );
```

### When the user declines

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

```ts
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),
  ),
);
```

## Returns

[`Effect<Hash.Hash, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The [hash](https://ethect.dev/reference/primitives/hash/) of the transaction.

## Parameters

### request

- **Type:** [`TransactionRequest.TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/)

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

```ts
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",
});
```

## Behavior

- **Why the name.** `Wallet.sendTransaction` is the re-exported
  [`Eth.sendTransaction`](https://ethect.dev/rpc/eth/send-transaction/), 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](https://ethect.dev/rpc/guides/values/).

## Errors

The three [errors](https://ethect.dev/reference/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 |

## JSON-RPC method

`wallet_sendTransaction`
