Eth.getCode
Returns the code of a contract as hex, or 0x for an account without code.
import { Effect } from "effect";import { FetchHttpClient } from "effect/http";import { Address, Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const program = Effect.gen(function* () { // a contract deployed on the local node return yield* Eth.getCode(ADDRESS, "latest");});
const code = await program.pipe( Effect.provide(Transport.http("http://127.0.0.1:8545")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,);// "0x3660105763deadbeef5f526004601cfd5b5f35805f555f527fabab…abab60205fa160205ff3"The call works over every transport. The examples below leave the transport out.
Tell a contract from a plain account
Section titled “Tell a contract from a plain account”An account without code answers 0x.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const isContract = Effect.gen(function* () { const code = yield* Eth.getCode(ADDRESS, "latest"); return code !== "0x"; // false});The size of the code
Section titled “The size of the code”Hex.size counts the bytes of a hex string.
import { Effect } from "effect";import { Address, Hex } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const size = Effect.gen(function* () { const code = yield* Eth.getCode(ADDRESS, "latest"); return Hex.size(code); // 65});Before the contract existed
Section titled “Before the contract existed”At a block before the deployment the same address has no code.
import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const before = Eth.getCode(ADDRESS, 0n); // "0x"Returns
Section titled “Returns”Effect<Hex.Hex, Method.Error, Transport>
The runtime code as a Hex string: the code that runs when the
contract is called, not the code that created it.
Parameters
Section titled “Parameters”address
Section titled “address”- Type:
Address.Address
The account. A plain string is not accepted: build it with
Address.makeUnsafe, or with Address.make when it
comes from outside.
import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const code = Eth.getCode(ADDRESS, "latest");block (optional)
Section titled “block (optional)”- Type:
BlockId.BlockId
The block to read at: a number, a tag ("latest", "earliest", "pending", "safe",
"finalized"), or an object that names the block by hash.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const codes = Effect.all([ Eth.getCode(ADDRESS, "finalized"), Eth.getCode(ADDRESS, 19_000_000n), Eth.getCode(ADDRESS, { blockHash: `0x${"ab".repeat(32)}` }),]);Behavior
Section titled “Behavior”- No default block. Left out, the param is not sent and the node chooses the block: see Naming a block.
0xmeans no code. It is the answer for a plain account and for an address that was never used. It is nevernull.- A delegated account has code. An account that delegates with EIP-7702 answers
0xef0100followed by the address it delegates to.
Errors
Section titled “Errors”The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:
| Error | Reason | When |
|---|---|---|
RpcError |
InvalidParams |
the block number is above the chain’s head |
RpcError |
ResourceNotFound |
no block has the given blockHash |
CodecError |
on: "params" |
the block number is negative: nothing is sent |
JSON-RPC method
Section titled “JSON-RPC method”eth_getCode