Accounts
The five methods that act as an account the node holds no key for: impersonation, a signature that recovers to any address, and a transaction sent without a signature.
| Method | JSON-RPC | Returns | Description |
|---|---|---|---|
impersonateAccount |
anvil_impersonateAccount |
null |
Lets transactions be sent from an account the node has no key for. |
stopImpersonatingAccount |
anvil_stopImpersonatingAccount |
null |
Stops impersonating an account. |
autoImpersonateAccount |
anvil_autoImpersonateAccount |
null |
Impersonates every account, or stops doing so. |
impersonateSignature |
anvil_impersonateSignature |
null |
Makes ecrecover answer an address for a signature. |
sendUnsignedTransaction |
eth_sendUnsignedTransaction |
Hash |
Sends a transaction from any account, without a signature. |
Each returns an Effect<…, Method.Error, Transport>. An
account is an Address.Address: build it with
Address.makeUnsafe, or with Address.make when
it comes from outside.
impersonateAccount
Section titled “impersonateAccount”Lets Eth.sendTransaction send from an account the node
has no key for.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Anvil, Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const sent = Effect.gen(function* () { yield* Anvil.setBalance(FROM, 10n ** 18n); yield* Anvil.impersonateAccount(FROM); const receipt = yield* Eth.sendTransactionSync({ from: FROM, to: TO, value: 1n }); return receipt.from; // "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"});- JSON-RPC:
anvil_impersonateAccount - Parameters:
address(Address.Address). - Returns:
null. - While it is impersonated, the account is in the list
Eth.accountsreturns.
stopImpersonatingAccount
Section titled “stopImpersonatingAccount”Stops impersonating an account: the node refuses its transactions again.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Anvil } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const scoped = Effect.gen(function* () { yield* Anvil.impersonateAccount(ADDRESS); yield* Anvil.stopImpersonatingAccount(ADDRESS);});- JSON-RPC:
anvil_stopImpersonatingAccount - Parameters:
address(Address.Address). - Returns:
null, also for an account that was not impersonated.
autoImpersonateAccount
Section titled “autoImpersonateAccount”Impersonates every account with true, and goes back to the accounts asked for one by one
with false.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Anvil, Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const sent = Effect.gen(function* () { yield* Anvil.setBalance(FROM, 10n ** 18n); yield* Anvil.autoImpersonateAccount(true); return yield* Eth.sendTransaction({ from: FROM, to: TO, value: 1n });});- JSON-RPC:
anvil_autoImpersonateAccount - Parameters:
enabled(boolean). - Returns:
null.
impersonateSignature
Section titled “impersonateSignature”Makes the ecrecover precompile answer an address for a signature, whatever was signed.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Anvil, Eth } from "@ethect/core/rpc";import type { Hex } from "@ethect/core";
const SIGNER = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");const ECRECOVER = Address.makeUnsafe("0x0000000000000000000000000000000000000001");
const recovered = Effect.gen(function* () { // a made-up signature of 65 bytes: r, s, then v const signature: Hex.Hex = `0x${"11".repeat(65)}`; yield* Anvil.impersonateSignature([signature, SIGNER]); // what the precompile reads: a hash, then v, r and s as 32-byte words const hash = "22".repeat(32); const data: Hex.Hex = `0x${hash}${"11".padStart(64, "0")}${"11".repeat(64)}`; return yield* Eth.call({ to: ECRECOVER, data }, "latest"); // "0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045"});- JSON-RPC:
anvil_impersonateSignature - Parameters: one pair,
[signature, address]: the signature (Hex.Hex), then the address it recovers to (Address.Address). - Returns:
null. - anvil takes the two values as one param. Pass them as one array, not as two arguments.
sendUnsignedTransaction
Section titled “sendUnsignedTransaction”Sends a transaction from any account, without a signature and without impersonating the account first.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Anvil, Eth } from "@ethect/core/rpc";
const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
const sent = Effect.gen(function* () { yield* Anvil.setBalance(FROM, 10n ** 18n); const hash = yield* Anvil.sendUnsignedTransaction({ from: FROM, to: TO, value: 1n }); const transaction = yield* Eth.getTransactionByHash(hash); return transaction?.from; // "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"});- JSON-RPC:
eth_sendUnsignedTransaction. The wire name is in theeth_namespace although the method is inAnvil. - Parameters:
request(TransactionRequest).fromis the account to send from. - Returns:
Hash, the hash of the transaction.
Behavior
Section titled “Behavior”- The account pays. An impersonated account needs a balance for the value and the gas.
Give it one with
Anvil.setBalance. - Impersonation lasts. An account stays impersonated until
stopImpersonatingAccount, and every account does untilautoImpersonateAccount(false). - Any hash recovers. After
impersonateSignature, the signature recovers to the address for every hashecrecoveris given.
The refusals, as anvil 1.9 reports them:
| Error | Reason | When |
|---|---|---|
RpcError |
InvalidParams |
from is neither held by the node nor impersonated, or sendUnsignedTransaction has no from. The message is No Signer available. |
RpcError |
TransactionRejected |
the account cannot pay for the value and the gas |