# State

The eight methods that write the state of an account directly: its balance, its ERC-20 balances
and allowances, its code, its nonce and its storage.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`setBalance`](#setbalance) | `anvil_setBalance` | `null` | Sets the balance of an account, in wei. |
| [`addBalance`](#addbalance) | `anvil_addBalance` | `null` | Adds to the balance of an account, in wei. |
| [`dealERC20`](#dealerc20) | `anvil_dealERC20` | `null` | Sets an account's balance of an ERC-20 token. |
| [`setERC20Balance`](#seterc20balance) | `anvil_setERC20Balance` | `null` | The same, under its other name. |
| [`setERC20Allowance`](#seterc20allowance) | `anvil_setERC20Allowance` | `null` | Sets an ERC-20 allowance. |
| [`setCode`](#setcode) | `anvil_setCode` | `null` | Sets the code of an account. |
| [`setNonce`](#setnonce) | `anvil_setNonce` | `null` | Sets the nonce of an account. |
| [`setStorageAt`](#setstorageat) | `anvil_setStorageAt` | `boolean` | Writes one storage slot of an account. |

Each takes the account first, as an [`Address.Address`](https://ethect.dev/reference/primitives/address/), and
returns an [`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). The
change is made at once: no transaction is sent and no block is mined.

## setBalance

Sets the balance of an account, in wei.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Anvil, Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const funded = Effect.gen(function* () {
  yield* Anvil.setBalance(ADDRESS, 5n * 10n ** 18n);
  return yield* Eth.getBalance(ADDRESS, "latest"); // 5000000000000000000n
});
```

- **JSON-RPC:** `anvil_setBalance`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)), then
  `balance` (`bigint`, in wei).
- **Returns:** `null`.

## addBalance

Adds an amount to the balance of an account, in wei.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Anvil, Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const topped = Effect.gen(function* () {
  yield* Anvil.setBalance(ADDRESS, 5n * 10n ** 18n);
  yield* Anvil.addBalance(ADDRESS, 1n);
  return yield* Eth.getBalance(ADDRESS, "latest"); // 5000000000000000001n
});
```

- **JSON-RPC:** `anvil_addBalance`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)), then
  `amount` (`bigint`, in wei).
- **Returns:** `null`.

## dealERC20

Sets an account's balance of an ERC-20 token, by writing the storage of the token.

```ts
import { Effect } from "effect";
import { Address, Erc20 } from "@ethect/core";
import { Anvil } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TOKEN = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");

const dealt = Effect.gen(function* () {
  // an ERC-20 token deployed on the node
  yield* Anvil.dealERC20(ADDRESS, TOKEN, 5n);
  return yield* Erc20.at(TOKEN).balanceOf(ADDRESS); // 5n
});
```

| Parameter | Type | Description |
| --- | --- | --- |
| `address` | [`Address.Address`](https://ethect.dev/reference/primitives/address/) | the account that holds the tokens |
| `token` | [`Address.Address`](https://ethect.dev/reference/primitives/address/) | the token contract |
| `balance` | `bigint` | the new balance, in the token's smallest unit |

- **JSON-RPC:** `anvil_dealERC20`
- **Returns:** `null`.
- anvil has to find the storage slot that holds the balance. When it finds none, for instance
  at an address without code, the call fails with an `RpcError` whose message is
  `Unable to set ERC20 balance, no slot found`.

## setERC20Balance

The same method as [`dealERC20`](#dealerc20), under its other name.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Anvil } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TOKEN = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");

const dealt = Effect.gen(function* () {
  yield* Anvil.setERC20Balance(ADDRESS, TOKEN, 6n);
});
```

- **JSON-RPC:** `anvil_setERC20Balance`
- **Parameters:** `address`, `token` and `balance`, as for [`dealERC20`](#dealerc20).
- **Returns:** `null`.

## setERC20Allowance

Sets how much of an owner's tokens a spender may move.

```ts
import { Effect } from "effect";
import { Address, Erc20 } from "@ethect/core";
import { Anvil } from "@ethect/core/rpc";

const OWNER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const SPENDER = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const TOKEN = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");

const allowed = Effect.gen(function* () {
  yield* Anvil.setERC20Allowance(OWNER, SPENDER, TOKEN, 7n);
  return yield* Erc20.at(TOKEN).allowance(OWNER, SPENDER); // 7n
});
```

| Parameter | Type | Description |
| --- | --- | --- |
| `owner` | [`Address.Address`](https://ethect.dev/reference/primitives/address/) | the account that holds the tokens |
| `spender` | [`Address.Address`](https://ethect.dev/reference/primitives/address/) | the account allowed to move them |
| `token` | [`Address.Address`](https://ethect.dev/reference/primitives/address/) | the token contract |
| `amount` | `bigint` | the allowance, in the token's smallest unit |

- **JSON-RPC:** `anvil_setERC20Allowance`
- **Returns:** `null`.
- It fails like [`dealERC20`](#dealerc20) when anvil finds no slot, with the message
  `Unable to set ERC20 allowance, no slot found`.

## setCode

Sets the code of an account.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Anvil, Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const coded = Effect.gen(function* () {
  // runtime code: PUSH1 1
  yield* Anvil.setCode(ADDRESS, "0x6001");
  return yield* Eth.getCode(ADDRESS, "latest"); // "0x6001"
});
```

- **JSON-RPC:** `anvil_setCode`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)), then `code`
  ([`Hex.Hex`](https://ethect.dev/reference/primitives/hex/)): the runtime code, not the code that deploys it.
- **Returns:** `null`.
- `"0x"` removes the code.

## setNonce

Sets the nonce of an account.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Anvil, Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const next = Effect.gen(function* () {
  yield* Anvil.setNonce(ADDRESS, 7);
  return yield* Eth.getTransactionCount(ADDRESS, "latest"); // 7
});
```

- **JSON-RPC:** `anvil_setNonce`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)), then `nonce`
  (`number`).
- **Returns:** `null`.
- The nonce can be set lower than it is.

## setStorageAt

Writes one storage slot of an account.

```ts
import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Anvil, Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const written = Effect.gen(function* () {
  yield* Anvil.setStorageAt(ADDRESS, "0x1", "0x7");
  return yield* Eth.getStorageAt(ADDRESS, "0x1", "latest");
  // "0x0000000000000000000000000000000000000000000000000000000000000007"
});
```

| Parameter | Type | Description |
| --- | --- | --- |
| `address` | [`Address.Address`](https://ethect.dev/reference/primitives/address/) | the account |
| `slot` | [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/) | the slot, short (`"0x1"`) or as a 32-byte word |
| `value` | [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/) | the value, short (`"0x7"`) or as a 32-byte word |

- **JSON-RPC:** `anvil_setStorageAt`
- **Returns:** `boolean`. anvil answers `true`.
- The slot and the value are sent as 32 bytes each, however short they were written. One
  longer than 32 bytes fails with a [`CodecError`](https://ethect.dev/reference/errors/codec-error/) on the
  params, and nothing is sent.

## Behavior

- **No block.** None of these methods mines a block. The new state is what `"latest"` reads at
  once.
- **Lost with the blocks after it.** A change made by hand is undone by
  [`Anvil.rollback`](https://ethect.dev/rpc/anvil/history/#rollback) and
  [`Anvil.reorg`](https://ethect.dev/rpc/anvil/reorg/) when they remove the latest block, because both
  restore the state of the last block they keep.
- **Any account.** The account does not have to exist or to be one the node holds a key for.
