# The anvil_ methods (Anvil)

The `anvil_` namespace: what a local test node offers to stage a chain, by setting accounts and
state, mining, moving the clock and rewriting history.

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

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

const program = Effect.gen(function* () {
  // an account anvil holds no key for and does not fund
  yield* Anvil.setBalance(ADDRESS, 10n ** 18n);
  yield* Anvil.mine(3);
  const balance = yield* Eth.getBalance(ADDRESS, "latest");
  const block = yield* Eth.blockNumber();
  return { balance, block };
});

const staged = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// { balance: 1000000000000000000n, block: 3n }
```

The namespace is for a local test node: [anvil](https://getfoundry.sh) serves it, a production
node does not. A test gets a fresh node from
[`AnvilNode`](https://ethect.dev/reference/testing/anvil-node/): see [Testing](https://ethect.dev/guides/testing/).

A method takes its JSON-RPC params in order and returns an
[`Effect`](https://ethect.dev/rpc/method/#calling-a-method) of the decoded result. The name is the wire
name without its prefix: `anvil_setBalance` is `Anvil.setBalance`. Two names differ:
`anvil_mine_detailed` is `Anvil.mineDetailed`, and `Anvil.sendUnsignedTransaction` is
`eth_sendUnsignedTransaction` on the wire.

The names Ganache gave to some of these methods are in [`Evm`](https://ethect.dev/rpc/evm/).

## Accounts

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`impersonateAccount`](https://ethect.dev/rpc/anvil/accounts/#impersonateaccount) | `anvil_impersonateAccount` | `null` | Lets transactions be sent from an account the node has no key for. |
| [`stopImpersonatingAccount`](https://ethect.dev/rpc/anvil/accounts/#stopimpersonatingaccount) | `anvil_stopImpersonatingAccount` | `null` | Stops impersonating an account. |
| [`autoImpersonateAccount`](https://ethect.dev/rpc/anvil/accounts/#autoimpersonateaccount) | `anvil_autoImpersonateAccount` | `null` | Impersonates every account, or stops doing so. |
| [`impersonateSignature`](https://ethect.dev/rpc/anvil/accounts/#impersonatesignature) | `anvil_impersonateSignature` | `null` | Makes `ecrecover` answer an address for a signature. |
| [`sendUnsignedTransaction`](https://ethect.dev/rpc/anvil/accounts/#sendunsignedtransaction) | `eth_sendUnsignedTransaction` | `Hash` | Sends a transaction from any account, without a signature. |

## State

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

## Chain

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`setChainId`](https://ethect.dev/rpc/anvil/chain/#setchainid) | `anvil_setChainId` | `null` | Sets the chain id. |
| [`setCoinbase`](https://ethect.dev/rpc/anvil/chain/#setcoinbase) | `anvil_setCoinbase` | `null` | Sets the address that receives block rewards. |
| [`setBlockGasLimit`](https://ethect.dev/rpc/anvil/chain/#setblockgaslimit) | `anvil_setBlockGasLimit` | `boolean` | Sets the gas limit of the blocks to come. |
| [`setMinGasPrice`](https://ethect.dev/rpc/anvil/chain/#setmingasprice) | `anvil_setMinGasPrice` | `null` | Sets the lowest gas price the node accepts. Refused once EIP-1559 is active. |
| [`setNextBlockBaseFeePerGas`](https://ethect.dev/rpc/anvil/chain/#setnextblockbasefeepergas) | `anvil_setNextBlockBaseFeePerGas` | `null` | Sets the base fee of the next block. |
| [`setNextBlockPrevRandao`](https://ethect.dev/rpc/anvil/chain/#setnextblockprevrandao) | `anvil_setNextBlockPrevRandao` | `null` | Sets the `prevRandao` of the next block. |
| [`setNextBlockParentBeaconBlockRoot`](https://ethect.dev/rpc/anvil/chain/#setnextblockparentbeaconblockroot) | `anvil_setNextBlockParentBeaconBlockRoot` | `null` | Sets the parent beacon block root of the next block. |

## Mining

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getAutomine`](https://ethect.dev/rpc/anvil/mining/#getautomine) | `anvil_getAutomine` | `boolean` | Whether a block is mined as soon as a transaction arrives. |
| [`setAutomine`](https://ethect.dev/rpc/anvil/mining/#setautomine) | `anvil_setAutomine` | `null` | Turns mining a block per transaction on or off. |
| [`getIntervalMining`](https://ethect.dev/rpc/anvil/mining/#getintervalmining) | `anvil_getIntervalMining` | `number \| null` | The seconds between two blocks, when blocks are mined on a timer. |
| [`setIntervalMining`](https://ethect.dev/rpc/anvil/mining/#setintervalmining) | `anvil_setIntervalMining` | `null` | Mines a block every given number of seconds. |
| [`getMiningPaused`](https://ethect.dev/rpc/anvil/mining/#getminingpaused) | `anvil_getMiningPaused` | `boolean` | Whether mining is paused. |
| [`setMiningPaused`](https://ethect.dev/rpc/anvil/mining/#setminingpaused) | `anvil_setMiningPaused` | `null` | Pauses or resumes mining. |
| [`mine`](https://ethect.dev/rpc/anvil/mining/#mine) | `anvil_mine` | `null` | Mines blocks, one by default. |
| [`mineDetailed`](https://ethect.dev/rpc/anvil/mining/#minedetailed) | `anvil_mine_detailed` | `MinedBlock[]` | Mines blocks and returns them, with what each transaction did. |

## Pool

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`dropTransaction`](https://ethect.dev/rpc/anvil/pool/#droptransaction) | `anvil_dropTransaction` | `Hash \| null` | Removes a transaction from the pool. |
| [`dropAllTransactions`](https://ethect.dev/rpc/anvil/pool/#dropalltransactions) | `anvil_dropAllTransactions` | `null` | Removes every transaction from the pool. |
| [`removePoolTransactions`](https://ethect.dev/rpc/anvil/pool/#removepooltransactions) | `anvil_removePoolTransactions` | `null` | Removes the transactions of one sender from the pool. |

What waits in the pool is read with [`Txpool`](https://ethect.dev/rpc/txpool/).

## Time

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getGenesisTime`](https://ethect.dev/rpc/anvil/time/#getgenesistime) | `anvil_getGenesisTime` | `bigint` | The timestamp of the genesis block, in seconds. |
| [`getLastBlockWallTime`](https://ethect.dev/rpc/anvil/time/#getlastblockwalltime) | `anvil_getLastBlockWallTime` | `number` | The real time at which the latest block became the head, in milliseconds. |
| [`increaseTime`](https://ethect.dev/rpc/anvil/time/#increasetime) | `anvil_increaseTime` | `number` | Moves the clock forward by a number of seconds. |
| [`setTime`](https://ethect.dev/rpc/anvil/time/#settime) | `anvil_setTime` | `number` | Sets the clock to a timestamp. |
| [`setNextBlockTimestamp`](https://ethect.dev/rpc/anvil/time/#setnextblocktimestamp) | `anvil_setNextBlockTimestamp` | `null` | Sets the timestamp of the next block. |
| [`setBlockTimestampInterval`](https://ethect.dev/rpc/anvil/time/#setblocktimestampinterval) | `anvil_setBlockTimestampInterval` | `null` | Makes every block's timestamp a fixed number of seconds after the one before. |
| [`removeBlockTimestampInterval`](https://ethect.dev/rpc/anvil/time/#removeblocktimestampinterval) | `anvil_removeBlockTimestampInterval` | `boolean` | Removes that interval. |

## History

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`snapshot`](https://ethect.dev/rpc/anvil/history/#snapshot) | `anvil_snapshot` | `Hex` | Saves the state of the chain and returns an id. |
| [`revert`](https://ethect.dev/rpc/anvil/history/#revert) | `anvil_revert` | `boolean` | Brings the chain back to a snapshot. |
| [`dumpState`](https://ethect.dev/rpc/anvil/history/#dumpstate) | `anvil_dumpState` | `Hex` | The state of the chain as one blob. |
| [`loadState`](https://ethect.dev/rpc/anvil/history/#loadstate) | `anvil_loadState` | `boolean` | Merges a dumped state into the chain. |
| [`rollback`](https://ethect.dev/rpc/anvil/history/#rollback) | `anvil_rollback` | `null` | Removes the latest blocks, one by default. |
| [`reorg`](https://ethect.dev/rpc/anvil/reorg/) | `anvil_reorg` | `null` | Replaces the latest blocks with new ones. |

## Node

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`nodeInfo`](https://ethect.dev/rpc/anvil/node/#nodeinfo) | `anvil_nodeInfo` | `NodeInfo` | How the node is set up: its head, hard fork and fees, and the endpoint it forks. |
| [`metadata`](https://ethect.dev/rpc/anvil/node/#metadata) | `anvil_metadata` | `NodeMetadata` | What identifies the node: its version, chain and head, its fork, its snapshots. |
| [`setLoggingEnabled`](https://ethect.dev/rpc/anvil/node/#setloggingenabled) | `anvil_setLoggingEnabled` | `null` | Turns the node's logging of requests on or off. |

## Fork

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`setRpcUrl`](https://ethect.dev/rpc/anvil/fork/#setrpcurl) | `anvil_setRpcUrl` | `null` | Sets the endpoint the node forks from. |
| [`reset`](https://ethect.dev/rpc/anvil/fork/#reset) | `anvil_reset` | `null` | Starts the chain anew, as a fork or not. |

## Blobs

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getBlobByHash`](https://ethect.dev/rpc/anvil/blobs/#getblobbyhash) | `anvil_getBlobByHash` | `Hex \| null` | A blob the node has seen, by its versioned hash. |
| [`getBlobsByTransactionHash`](https://ethect.dev/rpc/anvil/blobs/#getblobsbytransactionhash) | `anvil_getBlobsByTransactionHash` | `Hex[] \| null` | The blobs a transaction carried. |

## Behavior

- **A setter mines nothing.** `setBalance`, `setCode`, `setNonce`, `setStorageAt` and the chain
  and clock setters change the node at once and add no block.
- **State set by hand belongs to no block.** [`rollback`](https://ethect.dev/rpc/anvil/history/#rollback)
  and [`reorg`](https://ethect.dev/rpc/anvil/reorg/) bring the state back to the last block they keep, and
  what a setter changed since that block is lost.
- **Most setters answer `null`.** The effect succeeds with `null` when the node accepted the
  change. `setStorageAt` and `setBlockGasLimit` answer `true` instead.
- **Block numbers and timestamps are `bigint`s**, here as everywhere, also where anvil sends
  them as JSON numbers: `getGenesisTime`, `currentBlockTimestamp` in
  [`NodeInfo`](https://ethect.dev/rpc/types/node-info/), `latestBlockNumber` in
  [`NodeMetadata`](https://ethect.dev/rpc/types/node-metadata/).
