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 |
anvil_setBalance |
null |
Sets the balance of an account, in wei. |
addBalance |
anvil_addBalance |
null |
Adds to the balance of an account, in wei. |
dealERC20 |
anvil_dealERC20 |
null |
Sets an account’s balance of an ERC-20 token. |
setERC20Balance |
anvil_setERC20Balance |
null |
The same, under its other name. |
setERC20Allowance |
anvil_setERC20Allowance |
null |
Sets an ERC-20 allowance. |
setCode |
anvil_setCode |
null |
Sets the code of an account. |
setNonce |
anvil_setNonce |
null |
Sets the nonce of an account. |
setStorageAt |
anvil_setStorageAt |
boolean |
Writes one storage slot of an account. |
Each takes the account first, as an Address.Address, and
returns an Effect<…, Method.Error, Transport>. The
change is made at once: no transaction is sent and no block is mined.
setBalance
Section titled “setBalance”Sets the balance of an account, in wei.
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), thenbalance(bigint, in wei). - Returns:
null.
addBalance
Section titled “addBalance”Adds an amount to the balance of an account, in wei.
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), thenamount(bigint, in wei). - Returns:
null.
dealERC20
Section titled “dealERC20”Sets an account’s balance of an ERC-20 token, by writing the storage of the token.
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 |
the account that holds the tokens |
token |
Address.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
RpcErrorwhose message isUnable to set ERC20 balance, no slot found.
setERC20Balance
Section titled “setERC20Balance”The same method as dealERC20, under its other name.
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,tokenandbalance, as fordealERC20. - Returns:
null.
setERC20Allowance
Section titled “setERC20Allowance”Sets how much of an owner’s tokens a spender may move.
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 |
the account that holds the tokens |
spender |
Address.Address |
the account allowed to move them |
token |
Address.Address |
the token contract |
amount |
bigint |
the allowance, in the token’s smallest unit |
- JSON-RPC:
anvil_setERC20Allowance - Returns:
null. - It fails like
dealERC20when anvil finds no slot, with the messageUnable to set ERC20 allowance, no slot found.
setCode
Section titled “setCode”Sets the code of an account.
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), thencode(Hex.Hex): the runtime code, not the code that deploys it. - Returns:
null. "0x"removes the code.
setNonce
Section titled “setNonce”Sets the nonce of an account.
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), thennonce(number). - Returns:
null. - The nonce can be set lower than it is.
setStorageAt
Section titled “setStorageAt”Writes one storage slot of an account.
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 |
the account |
slot |
Hex.Hex |
the slot, short ("0x1") or as a 32-byte word |
value |
Hex.Hex |
the value, short ("0x7") or as a 32-byte word |
- JSON-RPC:
anvil_setStorageAt - Returns:
boolean. anvil answerstrue. - The slot and the value are sent as 32 bytes each, however short they were written. One
longer than 32 bytes fails with a
CodecErroron the params, and nothing is sent.
Behavior
Section titled “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.rollbackandAnvil.reorgwhen 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.