CLI
mud abi-ts

mud abi-ts

This command converts JSON files (opens in a new tab) that contain the ABI (opens in a new tab) to TypeScript (opens in a new tab) with strong types so they can be used to infer types for tools like viem (opens in a new tab) or wagmi (opens in a new tab).

Using the command

  1. Build your Solidity files (e.g. with forge (opens in a new tab)) to obtain an ABI JSON file. For example, you can use this file:

    greeter.abi.json
    [
      {
        "inputs": [
          {
            "internalType": "string",
            "name": "_greeting",
            "type": "string"
          }
        ],
        "stateMutability": "nonpayable",
        "type": "constructor"
      },
      {
        "anonymous": false,
        "inputs": [
          {
            "indexed": false,
            "internalType": "address",
            "name": "sender",
            "type": "address"
          },
          {
            "indexed": false,
            "internalType": "string",
            "name": "greeting",
            "type": "string"
          }
        ],
        "name": "SetGreeting",
        "type": "event"
      },
      {
        "inputs": [],
        "name": "greet",
        "outputs": [
          {
            "internalType": "string",
            "name": "",
            "type": "string"
          }
        ],
        "stateMutability": "view",
        "type": "function"
      },
      {
        "inputs": [
          {
            "internalType": "string",
            "name": "_greeting",
            "type": "string"
          }
        ],
        "name": "setGreeting",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
      }
    ]
  2. Run the command.

    pnpm mud abi-ts --input greeting.abi.json

    Note that if you don't include the --input parameter the command runs on every file with the extension .abi.json in the directory where it is run and every directory below it.

  3. See the new file.

    less greeting.abi.json.d.ts

Debugging

Set the DEBUG environment variable to abi-ts to get debugging outputs.

export DEBUG=abi-ts