# Eth.getCode

Returns the code of a contract as hex, or `0x` for an account without code.

## Usage

```ts
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](https://ethect.dev/guides/transports/). The examples below leave the
transport out.

### Tell a contract from a plain account

An account without code answers `0x`.

```ts
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

`Hex.size` counts the bytes of a hex string.

```ts
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

At a block before the deployment the same address has no code.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

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

const before = Eth.getCode(ADDRESS, 0n); // "0x"
```

## Returns

[`Effect<Hex.Hex, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The runtime code as a [`Hex`](https://ethect.dev/reference/primitives/hex/) string: the code that runs when the
contract is called, not the code that created it.

## Parameters

### address

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/address/)

The account. A plain string is not accepted: build it with
[`Address.makeUnsafe`](https://ethect.dev/reference/primitives/address/#makeunsafe), or with `Address.make` when it
comes from outside.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

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

const code = Eth.getCode(ADDRESS, "latest");
```

### block (optional)

- **Type:** [`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)

The block to read at: a number, a tag (`"latest"`, `"earliest"`, `"pending"`, `"safe"`,
`"finalized"`), or an object that names the block by hash.

```ts
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

- **No default block.** Left out, the param is not sent and the node chooses the block:
  see [Naming a block](https://ethect.dev/rpc/guides/calling-methods/#naming-a-block).
- **`0x` means no code.** It is the answer for a plain account and for an address that was
  never used. It is never `null`.
- **A delegated account has code.** An account that delegates with EIP-7702 answers `0xef0100`
  followed by the address it delegates to.

## Errors

The three [errors](https://ethect.dev/reference/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

`eth_getCode`
