> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Generating utility modules

> Generate typed clients and utility modules from EVM contract ABIs.

The `squid-evm-typegen(1)` tool generates TypeScript facades for EVM transactions, logs and `eth_call` queries.

The generated facades are assumed to be used by squids indexing EVM data.

The tool takes a JSON ABIs as an input. Those can be specified in three ways:

1. as a plain JSON file(s):

   ```bash theme={"system"}
   npx squid-evm-typegen src/abi abi/erc20.json
   ```

   To include all files in `./abi` and add an interface for the Multicall contract, run

   ```bash theme={"system"}
   npx squid-evm-typegen ./src/abi ./abi/*.json --multicall
   ```

   You can get JSON ABIs for explorer (Etherscan, Bscscan etc) verified contract by visiting the contract page, going to the "Contract" tab and scrolling down to the "Contract ABI" section. *Do not use the "Export ABI" function!* Copy the contents to the clipboard and paste them to a new JSON file.

2. as a contract address. One can pass multiple addresses at once. Without an Etherscan key, typegen uses the SQD ABI proxy. Pass a key only when you want to call Etherscan directly.

   ```bash theme={"system"}
   npx squid-evm-typegen --etherscan-api-key <your_key> src/abi 0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413
   ```

<Info>
  Please check if your contract is a proxy when using this method. If it is,
  consult [this page](../../guides/advanced/proxy-contracts) for guidance.
</Info>

3. as an arbitrary URL:

   ```bash theme={"system"}
   npx squid-evm-typegen src/abi https://example.com/erc721.json
   ```

In all cases typegen will use `basename` of the ABI as the root name for the generated files. You can change the basename of generated files using the fragment `(#)` suffix.

```bash theme={"system"}
squid-evm-typegen src/abi 0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413#my-contract-name
```

**Arguments:**

|              |                                                                                         |
| ------------ | --------------------------------------------------------------------------------------- |
| `output-dir` | output directory for generated definitions                                              |
| `abi`        | A contract address, an URL or a local path to the ABI file. Accepts multiple contracts. |

**Options:**

|                             |                                                                                                                                                                                               |
| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--multicall`               | generate a facade for the MakerDAO multicall contract. May significantly improve the performance of contract state calls by [batching RPC requests](./direct-rpc-queries#batch-state-queries) |
| `--etherscan-api <url>`     | API used to fetch a contract ABI. Defaults to the SQD proxy without a key and Etherscan v2 with a key                                                                                         |
| `--etherscan-api-key <key>` | Etherscan API key                                                                                                                                                                             |
| `--chain-id <id>`           | numeric or named chain ID used for address lookups; default `1`                                                                                                                               |
| `--clean`                   | delete output directory before the run                                                                                                                                                        |
| `-h, --help`                | display help for command                                                                                                                                                                      |

<Warning>
  Generated modules import both `@subsquid/evm-abi` and
  `@subsquid/evm-codec`. Add both as direct dependencies:

  ```bash theme={"system"}
  npm i @subsquid/evm-abi @subsquid/evm-codec
  ```
</Warning>

Typegen v5 writes one directory per ABI, for example `src/abi/erc20/{events,functions,contract,index}.ts`, plus `src/abi/abi.support.ts`. It removes a stale `src/abi/erc20.ts` file from the older layout. Imports such as `import * as erc20 from './abi/erc20'` continue to resolve through `index.ts`.

Local and URL inputs can be a raw ABI array or a compiler artifact with an `abi` array. NatSpec in artifact `userdoc` and `devdoc` fields is carried into generated documentation.

## Usage

The generated utility modules have three intended uses:

1. Constants: EVM log topic0 values and sighashes for transactions. Example:

   ```typescript theme={"system"}
   // generated by evm-typegen
   import * as weth from './abi/weth'

   const CONTRACT_ADDRESS = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'.toLowerCase()

   const dataSource = new DataSourceBuilder()
     .setPortal('https://portal.sqd.dev/datasets/ethereum-mainnet')
     .addLog({
       where: {
         address: [CONTRACT_ADDRESS],
         topic0: [
           weth.events.Deposit.topic,
           weth.events.Withdrawal.topic
         ]
       }
     })
     .build()
   ```

2. [Decoding of EVM logs and transactions](./data-decoding)

3. [Direct chain state queries](./direct-rpc-queries), including [queries to multicall](./direct-rpc-queries#batch-state-queries).
