Hardhat's integrated Solidity compilation system provides comprehensive build management with dependency resolution, version management, incremental compilation, and extensive configuration options.
Core interface for Solidity compilation and build management.
interface SolidityBuildSystem {
/** Compile all Solidity files */
compile(): Promise<void>;
/** Get the current compiler version being used */
getCompilerVersion(): string;
/** Get all source file names in the project */
getSourceNames(): Promise<string[]>;
/** Get the dependency graph of contracts */
getDependencyGraph(): Promise<DependencyGraph>;
/** Check if compilation is needed */
needsCompilation(): Promise<boolean>;
/** Clean build artifacts */
clean(): Promise<void>;
}Usage Examples:
import { solidity } from "hardhat";
// Compile all contracts
await solidity.compile();
// Check compiler version
console.log(`Using Solidity ${solidity.getCompilerVersion()}`);
// Get all source files
const sources = await solidity.getSourceNames();
console.log(`Found ${sources.length} Solidity files`);
// Clean build artifacts
await solidity.clean();Configuration structure for Solidity compiler settings and version management.
interface SolcConfig {
/** Solidity compiler version */
version: string;
/** Compiler settings */
settings?: CompilerSettings;
/** Additional compiler paths */
paths?: string[];
/** Override settings for specific contracts */
overrides?: {
[fileName: string]: {
version?: string;
settings?: CompilerSettings;
};
};
}
interface SolcUserConfig {
/** Solidity compiler version or configuration */
version?: string | SolcConfig;
/** Default compiler settings */
settings?: CompilerSettings;
/** Multiple compiler configurations */
compilers?: SolcConfig[];
/** Override settings for specific files */
overrides?: {
[fileName: string]: SolcUserConfig;
};
}Detailed compiler configuration options for optimization, output, and behavior.
interface CompilerSettings {
/** Optimizer configuration */
optimizer?: OptimizerConfig;
/** EVM version target */
evmVersion?: string;
/** Metadata settings */
metadata?: {
/** Use literal content in metadata */
useLiteralContent?: boolean;
/** Bytecode hash method */
bytecodeHash?: "none" | "ipfs" | "bzzr1";
};
/** Output selection for compilation artifacts */
outputSelection?: {
[file: string]: {
[contract: string]: string[];
};
};
/** Via-IR compilation pipeline */
viaIR?: boolean;
/** Debug information generation */
debug?: {
revertStrings?: "default" | "strip" | "debug" | "verboseDebug";
};
/** Library linking addresses */
libraries?: {
[sourceName: string]: {
[libraryName: string]: string;
};
};
/** Import remappings */
remappings?: string[];
/** Model checker settings */
modelChecker?: {
contracts?: { [sourceName: string]: string[] };
divModNoSlacks?: boolean;
engine?: "chc" | "bmc" | "all";
extCalls?: "trusted" | "untrusted";
invariants?: string[];
showUnproved?: boolean;
solvers?: string[];
targets?: string[];
timeout?: number;
};
}
interface OptimizerConfig {
/** Enable optimizer */
enabled: boolean;
/** Number of optimization runs */
runs?: number;
/** Detailed optimizer settings */
details?: {
peephole?: boolean;
inliner?: boolean;
jumpdestRemover?: boolean;
orderLiterals?: boolean;
deduplicate?: boolean;
cse?: boolean;
constantOptimizer?: boolean;
yul?: boolean;
yulDetails?: {
stackAllocation?: boolean;
optimizerSteps?: string;
};
};
}Usage Examples:
import { HardhatUserConfig } from "hardhat/config";
const config: HardhatUserConfig = {
solidity: {
version: "0.8.19",
settings: {
optimizer: {
enabled: true,
runs: 200
},
evmVersion: "paris",
viaIR: true,
metadata: {
bytecodeHash: "ipfs"
}
}
}
};
// Multiple compiler configurations
const multiConfig: HardhatUserConfig = {
solidity: {
compilers: [
{
version: "0.8.19",
settings: {
optimizer: { enabled: true, runs: 200 }
}
},
{
version: "0.7.6",
settings: {
optimizer: { enabled: false }
}
}
],
overrides: {
"contracts/legacy/OldContract.sol": {
version: "0.6.12",
settings: {
optimizer: { enabled: false }
}
}
}
}
};Structure representing contract dependencies and compilation order.
interface DependencyGraph {
/** Map of file to its dependencies */
dependencies: Map<string, Set<string>>;
/** Get files in compilation order */
getCompilationOrder(): string[];
/** Get dependencies for a specific file */
getDependencies(file: string): string[];
/** Check if a file depends on another */
dependsOn(file: string, dependency: string): boolean;
}Functions for managing and resolving Solidity source files.
/**
* Factory function for reading source files with caching
* @returns Function for reading source file content
*/
function readSourceFileFactory(): (sourcePath: string) => Promise<string>;
/**
* Dependency resolver implementation
*/
class ResolverImplementation implements Resolver {
/** Resolve import path to absolute path */
resolve(importPath: string, sourcePath: string): Promise<string>;
/** Check if path exists */
exists(path: string): Promise<boolean>;
/** Read file content */
readFile(path: string): Promise<string>;
}
interface Resolver {
resolve(importPath: string, sourcePath: string): Promise<string>;
exists(path: string): Promise<boolean>;
readFile(path: string): Promise<string>;
}Utilities for formatting compilation and resolution errors.
/**
* Format project root resolution error
* @param error - The resolution error
* @returns Formatted error message
*/
function formatProjectRootResolutionError(error: Error): string;
/**
* Format npm root resolution error
* @param error - The resolution error
* @returns Formatted error message
*/
function formatNpmRootResolutionError(error: Error): string;
/**
* Format import resolution error
* @param error - The resolution error
* @param importPath - The import path that failed
* @returns Formatted error message
*/
function formatImportResolutionError(error: Error, importPath: string): string;Event-driven compilation system for monitoring build progress.
interface CompilationEvent {
type: "compilation:started" | "compilation:finished" | "compilation:error";
data?: any;
}
interface CompilationResult {
success: boolean;
contracts: CompiledContract[];
errors: CompilationError[];
warnings: CompilationError[];
}
interface CompiledContract {
sourceName: string;
contractName: string;
artifact: Artifact;
buildInfo: BuildInfo;
}
interface CompilationError {
severity: "error" | "warning";
message: string;
sourceLocation?: {
file: string;
start: number;
end: number;
};
type: string;
component: string;
}