CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-typechain

TypeScript bindings generator for Ethereum smart contracts that transforms ABI files into type-safe interfaces and classes

Pending
Overview
Eval results
Files

abi-parsing.mddocs/

ABI Parsing

Complete ABI parsing system that converts raw JSON ABI files into structured TypeScript representations with full type safety.

Capabilities

Main Parser Function

parse Function

Main ABI parser function that converts raw ABI to Contract object.

/**
 * Main ABI parser function that converts raw ABI to Contract object
 * @param abi - Array of raw ABI definitions from JSON
 * @param path - Contract file path for name resolution
 * @param documentation - Optional documentation from devdoc/userdoc
 * @returns Parsed contract object with functions, events, and metadata
 */
function parse(abi: RawAbiDefinition[], path: string, documentation?: DocumentationResult): Contract;

Usage Example:

import { parse, extractAbi } from "typechain";

const rawJson = `{
  "abi": [
    {
      "type": "function",
      "name": "transfer",
      "inputs": [
        {"name": "to", "type": "address"},
        {"name": "value", "type": "uint256"}
      ],
      "outputs": [{"name": "", "type": "bool"}],
      "stateMutability": "nonpayable"
    }
  ]
}`;

const abi = extractAbi(rawJson);
const contract = parse(abi, "./MyToken.sol/MyToken.json");
console.log(contract.functions.transfer[0].inputs[0].type); // AddressType

Extraction Functions

extractAbi Function

Extracts ABI array from various JSON formats.

/**
 * Extracts ABI array from various JSON formats (Truffle, Hardhat, raw ABI)
 * @param rawJson - Raw JSON string containing ABI data
 * @returns Array of raw ABI definitions
 * @throws MalformedAbiError if JSON is invalid or ABI not found
 */
function extractAbi(rawJson: string): RawAbiDefinition[];

extractBytecode Function

Extracts bytecode from various JSON formats.

/**
 * Extracts bytecode from various JSON formats
 * @param rawContents - Raw JSON string containing bytecode
 * @returns Bytecode with optional link references, or undefined if not found
 */
function extractBytecode(rawContents: string): BytecodeWithLinkReferences | undefined;

extractDocumentation Function

Extracts documentation from compiler output.

/**
 * Extracts documentation from compiler output (devdoc/userdoc)
 * @param rawContents - Raw JSON string containing documentation
 * @returns Combined documentation result, or undefined if not found
 */
function extractDocumentation(rawContents: string): DocumentationResult | undefined;

Contract Structure

Contract Interface

Represents a complete smart contract with all its components.

interface Contract {
  /** Contract name (normalized) */
  name: string;
  /** Original contract name from file */
  rawName: string;
  /** Contract path components for organization */
  path: string[];
  /** Optional fallback function */
  fallback?: FunctionWithoutInputDeclaration;
  /** Constructor functions (can be overloaded) */
  constructor: FunctionWithoutOutputDeclaration[];
  /** Contract functions grouped by name */
  functions: Dictionary<FunctionDeclaration[]>;
  /** Contract events grouped by name */
  events: Dictionary<EventDeclaration[]>;
  /** Contract structs grouped by name */
  structs: Dictionary<StructType[]>;
  /** Optional contract documentation */
  documentation?: object;
}

type Dictionary<T> = { [key: string]: T };

Function Types

FunctionDeclaration Interface

Represents a smart contract function with full signature information.

interface FunctionDeclaration {
  /** Function name */
  name: string;
  /** State mutability (pure, view, nonpayable, payable) */
  stateMutability: StateMutability;
  /** Input parameters */
  inputs: AbiParameter[];
  /** Output parameters */
  outputs: AbiOutputParameter[];
  /** Optional function documentation */
  documentation?: FunctionDocumentation;
}

type StateMutability = 'pure' | 'view' | 'nonpayable' | 'payable';

FunctionWithoutOutputDeclaration Interface

Function with no outputs (like constructors).

interface FunctionWithoutOutputDeclaration {
  name: string;
  stateMutability: StateMutability;
  inputs: AbiParameter[];
  outputs: [];
  documentation?: FunctionDocumentation;
}

FunctionWithoutInputDeclaration Interface

Function with no inputs (like fallback functions).

interface FunctionWithoutInputDeclaration {
  name: string;
  stateMutability: StateMutability;
  inputs: [];
  outputs: AbiOutputParameter[];
  documentation?: FunctionDocumentation;
}

Parameter Types

AbiParameter Interface

Represents an ABI function parameter.

interface AbiParameter {
  /** Parameter name */
  name: string;
  /** EVM type information */
  type: EvmType;
}

AbiOutputParameter Interface

Represents an ABI function output parameter.

interface AbiOutputParameter {
  /** Parameter name (may be empty) */
  name: string;
  /** EVM output type information */
  type: EvmOutputType;
}

Event Types

EventDeclaration Interface

Represents a smart contract event.

interface EventDeclaration {
  /** Event name */
  name: string;
  /** Whether event is anonymous */
  isAnonymous: boolean;
  /** Event arguments */
  inputs: EventArgDeclaration[];
}

EventArgDeclaration Interface

Represents an event argument.

interface EventArgDeclaration {
  /** Whether argument is indexed for filtering */
  isIndexed: boolean;
  /** Optional argument name */
  name?: string;
  /** EVM type */
  type: EvmType;
}

Documentation Types

FunctionDocumentation Interface

Documentation metadata for functions from NatSpec comments.

interface FunctionDocumentation {
  /** Function author */
  author?: string;
  /** Detailed description */
  details?: string;
  /** User notice */
  notice?: string;
  /** Parameter descriptions by name */
  params?: { [paramName: string]: string };
  /** Return value description */
  return?: string;
}

DocumentationResult Interface

Combined devdoc and userdoc documentation.

interface DocumentationResult {
  /** Author information */
  author?: string;
  /** Detailed description */
  details?: string;
  /** User notice */
  notice?: string;
  /** Documentation title */
  title?: string;
  /** Method documentation by signature */
  methods?: { [methodName: string]: FunctionDocumentation };
}

Raw ABI Types

RawAbiDefinition Interface

Raw ABI definition from JSON (before parsing).

interface RawAbiDefinition {
  /** Function/event name */
  name: string;
  /** Legacy constant flag */
  constant: boolean;
  /** Legacy payable flag */
  payable: boolean;
  /** State mutability (preferred over constant/payable) */
  stateMutability?: StateMutability;
  /** Input parameters */
  inputs: RawAbiParameter[];
  /** Output parameters */
  outputs: RawAbiParameter[];
  /** ABI type (function, event, constructor, fallback, receive) */
  type: string;
}

RawAbiParameter Interface

Raw ABI parameter definition from JSON.

interface RawAbiParameter {
  /** Parameter name */
  name: string;
  /** Solidity type string */
  type: string;
  /** Optional internal type for structs */
  internalType?: string;
  /** Optional tuple components for complex types */
  components?: RawAbiParameter[];
}

Bytecode Types

BytecodeWithLinkReferences Interface

Bytecode with library link references.

interface BytecodeWithLinkReferences {
  /** Contract bytecode as hex string */
  bytecode: string;
  /** Optional library link references */
  linkReferences?: BytecodeLinkReference[];
}

interface BytecodeLinkReference {
  /** Reference placeholder in bytecode */
  reference: string;
  /** Optional library name */
  name?: string;
}

Utility Functions

parseContractPath Function

Parses contract file path into name components.

/**
 * Parses contract file path into name components
 * @param path - Contract file path (e.g., "./contracts/MyToken.sol/MyToken.json")
 * @returns Object with normalized name, raw name, and path components
 */
function parseContractPath(path: string): { name: string; rawName: string; path: string[] };

parseEvent Function

Parses event ABI definition.

/**
 * Parses event ABI definition
 * @param abiPiece - Raw event ABI definition
 * @param registerStruct - Callback to register discovered struct types
 * @returns Parsed event declaration
 */
function parseEvent(abiPiece: RawEventAbiDefinition, registerStruct: (struct: StructType) => void): EventDeclaration;

interface RawEventAbiDefinition extends RawAbiDefinition {
  type: 'event';
  anonymous?: boolean;
}

getFunctionDocumentation Function

Extracts documentation for a specific function.

/**
 * Extracts documentation for a specific function from contract documentation
 * @param abiPiece - Raw ABI definition for the function
 * @param documentation - Contract documentation result
 * @returns Function documentation if found, undefined otherwise
 */
function getFunctionDocumentation(abiPiece: RawAbiDefinition, documentation?: DocumentationResult): FunctionDocumentation | undefined;

ensure0xPrefix Function

Ensures hex string has 0x prefix.

/**
 * Ensures hex string has 0x prefix
 * @param hexString - Hex string that may or may not have 0x prefix
 * @returns Hex string with 0x prefix
 */
function ensure0xPrefix(hexString: string): string;

Function Classification

isConstant Function

Checks if function is a constant (view/pure with no inputs and single output).

/**
 * Checks if function is a simple constant (getter)
 * @param fn - Function declaration to check
 * @returns True if function is a constant getter
 */
function isConstant(fn: FunctionDeclaration): boolean;

isConstantFn Function

Checks if function is constant (view/pure) but not a simple constant.

/**
 * Checks if function is constant (view/pure) but not a simple getter
 * @param fn - Function declaration to check
 * @returns True if function is constant but has inputs or multiple outputs
 */
function isConstantFn(fn: FunctionDeclaration): boolean;

Install with Tessl CLI

npx tessl i tessl/npm-typechain

docs

abi-parsing.md

cli-interface.md

code-generation.md

core-api.md

file-utilities.md

index.md

types-interfaces.md

tile.json