TypeScript bindings generator for Ethereum smart contracts that transforms ABI files into type-safe interfaces and classes
—
Comprehensive file system utilities for path manipulation, globbing, and cross-platform compatibility in TypeChain operations.
File globbing utility that resolves patterns to file paths.
/**
* File globbing utility that resolves patterns to file paths
* @param cwd - Current working directory for pattern resolution
* @param patternsOrFiles - Array of glob patterns or specific file paths
* @param ignoreNodeModules - Whether to ignore node_modules directory (default: true)
* @returns Array of resolved file paths matching the patterns
*/
function glob(cwd: string, patternsOrFiles: string[], ignoreNodeModules?: boolean): string[];Usage Example:
import { glob } from "typechain";
// Find all JSON files in artifacts directory
const files = glob(process.cwd(), ["./artifacts/**/*.json"]);
// Result: ["/path/to/artifacts/Contract1.json", "/path/to/artifacts/Contract2.json", ...]
// Multiple patterns
const abiFiles = glob(process.cwd(), [
"./artifacts/**/*.json",
"./contracts/**/*.abi",
"./build/**/*.json"
]);
// Include node_modules (normally ignored)
const allFiles = glob(process.cwd(), ["**/*.json"], false);Detects the root directory for input files by finding the common path.
/**
* Detects the root directory for input files by finding the common path
* @param allFiles - Array of all input file paths
* @returns Common root directory path for all input files
*/
function detectInputsRoot(allFiles: string[]): string;Ensures path is absolute, converting relative paths using current working directory.
/**
* Ensures path is absolute, converting relative paths using current working directory
* @param path - File or directory path that may be relative or absolute
* @returns Absolute path
*/
function ensureAbsPath(path: string): string;Finds lowest common path among multiple paths.
/**
* Finds lowest common path among multiple paths
* @param paths - Array of file or directory paths
* @returns Lowest common directory path shared by all inputs
*/
function lowestCommonPath(paths: string[]): string;Usage Example:
import { detectInputsRoot, lowestCommonPath, ensureAbsPath } from "typechain";
const files = [
"./artifacts/contracts/MyToken.sol/MyToken.json",
"./artifacts/contracts/MyNFT.sol/MyNFT.json",
"./artifacts/contracts/governance/Governor.sol/Governor.json"
];
const inputRoot = detectInputsRoot(files);
// Result: "./artifacts/contracts"
const commonPath = lowestCommonPath(files);
// Result: "./artifacts/contracts"
const absolutePath = ensureAbsPath("./artifacts");
// Result: "/full/path/to/project/artifacts"Gets file extension from path.
/**
* Gets file extension from path
* @param path - File path
* @returns File extension including the dot (e.g., ".json", ".abi")
*/
function getFileExtension(path: string): string;Gets filename without extension from path.
/**
* Gets filename without extension from path
* @param path - File path
* @returns Filename without directory path and extension
*/
function getFilename(path: string): string;Converts valid file names to valid JavaScript symbols.
/**
* Converts valid file names to valid JavaScript symbols
* Handles cases like "ds-token.test" becoming "DsTokenTest"
* @param rawName - Original file name or contract name
* @returns Valid JavaScript identifier in PascalCase
*/
function normalizeName(rawName: string): string;Usage Example:
import { getFileExtension, getFilename, normalizeName } from "typechain";
const path = "./contracts/my-token.test.sol/MyToken.json";
const extension = getFileExtension(path);
// Result: ".json"
const filename = getFilename(path);
// Result: "MyToken"
const normalized = normalizeName("ds-token.test");
// Result: "DsTokenTest"
const normalizedContract = normalizeName("0x-exchange");
// Result: "_0xExchange"Normalizes path slashes for cross-platform compatibility.
/**
* Normalizes path slashes for cross-platform compatibility
* Converts backslashes to forward slashes and handles double slashes
* @param path - File or directory path with potentially mixed slashes
* @returns Normalized path with consistent forward slashes
*/
function normalizeSlashes(path: string): string;Shortens full JSON file paths by removing redundant contract name directories.
/**
* Shortens full JSON file paths by removing redundant contract name directories
* Handles Hardhat's nested directory structure like "Contract.sol/Contract.json"
* @param path - Full file path that may contain redundant directories
* @param allPaths - All file paths for context in shortening decisions
* @returns Shortened path with redundant directories removed
*/
function shortenFullJsonFilePath(path: string, allPaths: string[]): string;Usage Example:
import { normalizeSlashes, shortenFullJsonFilePath } from "typechain";
// Cross-platform path normalization
const windowsPath = "contracts\\MyToken.sol\\MyToken.json";
const normalized = normalizeSlashes(windowsPath);
// Result: "contracts/MyToken.sol/MyToken.json"
// Shorten Hardhat-style paths
const fullPaths = [
"./artifacts/contracts/MyToken.sol/MyToken.json",
"./artifacts/contracts/MyNFT.sol/MyNFT.json"
];
const shortened = shortenFullJsonFilePath(
"./artifacts/contracts/MyToken.sol/MyToken.json",
fullPaths
);
// Result: "./artifacts/contracts/MyToken.json" (removes redundant .sol directory)Functions for generating contract function and event signatures:
/**
* Get the signature for a function declaration
* @param fn - Function declaration with name and inputs
* @returns Function signature string like "transfer(address,uint256)"
*/
function getSignatureForFn(fn: FunctionDeclaration): string;
/**
* Get the full signature as symbol for event declaration
* @param event - Event declaration with name and inputs
* @returns Event signature symbol for use as identifier
*/
function getFullSignatureAsSymbolForEvent(event: EventDeclaration): string;
/**
* Get the full signature for event declaration
* @param event - Event declaration with name and inputs
* @returns Event signature string like "Transfer(address,address,uint256)"
*/
function getFullSignatureForEvent(event: EventDeclaration): string;
/**
* Get the indexed signature for event declaration
* @param event - Event declaration with name and inputs
* @returns Indexed event signature with only indexed parameters
*/
function getIndexedSignatureForEvent(event: EventDeclaration): string;
/**
* Get argument representation for signature generation
* @param argument - Event argument or ABI parameter
* @returns String representation of the argument type for signatures
*/
function getArgumentForSignature(argument: EventArgDeclaration | AbiParameter): string;Usage Example:
import {
getSignatureForFn,
getFullSignatureForEvent,
getIndexedSignatureForEvent,
getFullSignatureAsSymbolForEvent
} from "typechain";
// Function signature generation
const transferFn = {
name: "transfer",
inputs: [
{ name: "to", type: { originalType: "address" } },
{ name: "amount", type: { originalType: "uint256" } }
]
};
const fnSignature = getSignatureForFn(transferFn);
// Result: "transfer(address,uint256)"
// Event signature generation
const transferEvent = {
name: "Transfer",
inputs: [
{ name: "from", type: { originalType: "address" }, isIndexed: true },
{ name: "to", type: { originalType: "address" }, isIndexed: true },
{ name: "value", type: { originalType: "uint256" }, isIndexed: false }
]
};
const eventSignature = getFullSignatureForEvent(transferEvent);
// Result: "Transfer(address,address,uint256)"
const indexedSignature = getIndexedSignatureForEvent(transferEvent);
// Result: "Transfer(address,address)"
const symbolSignature = getFullSignatureAsSymbolForEvent(transferEvent);
// Result: "Transfer_address_address_uint256"Install with Tessl CLI
npx tessl i tessl/npm-typechain