CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-hardhat

Extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

network-management.mddocs/

Network Management

Hardhat's network management system provides abstraction for connecting to different Ethereum networks, managing providers, and handling network-specific configurations.

Capabilities

Network Manager

Central interface for network operations and provider access.

interface NetworkManager {
  /** Connect to a network and get a network connection */
  connect<ChainTypeT extends ChainType | string = DefaultChainType>(
    networkOrParams?: NetworkConnectionParams<ChainTypeT> | string
  ): Promise<NetworkConnection<ChainTypeT>>;
}

interface NetworkConnection<
  ChainTypeT extends ChainType | string = DefaultChainType
> {
  /** Connection ID */
  readonly id: number;
  
  /** Network name */
  readonly networkName: string;
  
  /** Network configuration */
  readonly networkConfig: Readonly<NetworkConfig>;
  
  /** Chain type (l1, op, generic) */
  readonly chainType: ChainTypeT;
  
  /** Ethereum provider for this connection */
  readonly provider: EthereumProvider;
  
  /** Close the network connection */
  close(): Promise<void>;
}

interface NetworkConnectionParams<
  ChainTypeT extends ChainType | string = DefaultChainType
> {
  /** Network name to connect to */
  network?: string;
  
  /** Chain type specification */
  chainType?: ChainTypeT;
  
  /** Configuration overrides */
  override?: NetworkConfigOverride;
}

type ChainType = GenericChainType | "l1" | "op";
type GenericChainType = "generic";
type DefaultChainType = GenericChainType;

Usage Examples:

import { network } from "hardhat";

// Connect to default network
const connection = await network.connect();
console.log(`Connected to: ${connection.networkName}`);
console.log(`Chain type: ${connection.chainType}`);

// Connect to specific network
const goerliConnection = await network.connect("goerli");
console.log(`Chain ID: ${goerliConnection.networkConfig.chainId}`);

// Connect with parameters and chain type
const l1Connection = await network.connect({
  network: "mainnet",
  chainType: "l1",
  override: { gasPrice: 20000000000 }
});

// Use the provider
const blockNumber = await connection.provider.request({
  method: "eth_blockNumber",
  params: []
});

// Close connection when done
await connection.close();

Network Configuration

Configuration objects for different types of networks.

interface NetworkConfig {
  chainId?: number;
  from?: string;
  gasPrice?: number | "auto";
  gasMultiplier?: number;
  gas?: number | "auto";
  blockGasLimit?: number;
  timeout?: number;
  httpHeaders?: { [name: string]: string };
  accounts?: NetworkAccounts;
}

interface HardhatNetworkConfig extends NetworkConfig {
  hardfork?: string;
  blockGasLimit?: number;
  gasPrice?: number;
  initialBaseFeePerGas?: number;
  coinbase?: string;
  mining?: {
    auto?: boolean;
    interval?: number | [number, number];
    mempool?: {
      order?: "priority" | "fifo";
    };
  };
  accounts?: HardhatNetworkAccountsConfig;
  allowUnlimitedContractSize?: boolean;
  throwOnTransactionFailures?: boolean;
  throwOnCallFailures?: boolean;
  loggingEnabled?: boolean;
  chains?: { [chainId: number]: ChainConfig };
}

interface HttpNetworkConfig extends NetworkConfig {
  url: string;
  timeout?: number;
  httpHeaders?: { [name: string]: string };
}

Provider Interface

Core provider interface for interacting with Ethereum networks.

interface RequestArguments {
  readonly method: string;
  readonly params?: readonly unknown[] | object;
}

interface ProviderRpcError extends Error {
  code: number;
  data?: unknown;
}

interface EIP1193Provider {
  /** Send JSON-RPC request */
  request(requestArguments: RequestArguments): Promise<any>;
  
  /** Event emitter functionality for provider events */
  on(event: string, listener: (...args: any[]) => void): this;
  removeListener(event: string, listener: (...args: any[]) => void): this;
}

interface EthereumProvider extends EIP1193Provider {  
  /** Close provider and release resources */
  close(): Promise<void>;
  
  /** Send JSON-RPC request (legacy method) */
  send(method: string, params?: unknown[]): Promise<any>;
  
  /** Send JSON-RPC request with callback (legacy) */
  sendAsync(
    payload: JsonRpcRequest,
    callback: (error: any, response: JsonRpcResponse) => void
  ): void;
}

interface HardhatNetworkProvider extends EthereumProvider {
  /** Get a snapshot of the current blockchain state */
  takeSnapshot(): Promise<string>;
  
  /** Revert to a previously taken snapshot */
  revertToSnapshot(snapshotId: string): Promise<boolean>;
  
  /** Mine a block */
  mine(): Promise<void>;
  
  /** Set the next block timestamp */
  setNextBlockTimestamp(timestamp: number): Promise<void>;
  
  /** Increase time by specified amount */
  increaseTime(amountInSeconds: number): Promise<void>;
  
  /** Impersonate an account */
  impersonateAccount(address: string): Promise<void>;
  
  /** Stop impersonating an account */
  stopImpersonatingAccount(address: string): Promise<void>;
  
  /** Set balance for an account */
  setBalance(address: string, balance: string): Promise<void>;
  
  /** Set storage at a specific slot */
  setStorageAt(address: string, slot: string, value: string): Promise<void>;
  
  /** Drop a transaction from the mempool */
  dropTransaction(hash: string): Promise<boolean>;
}

Usage Examples:

import { network } from "hardhat";

// Basic provider usage
const accounts = await network.provider.request({
  method: "eth_accounts",
  params: []
});

const blockNumber = await network.provider.send("eth_blockNumber");

// Hardhat Network specific features (only available on local network)
if (network.name === "hardhat") {
  // Take snapshot
  const snapshotId = await network.provider.request({
    method: "evm_snapshot",
    params: []
  });
  
  // Mine a block
  await network.provider.request({
    method: "evm_mine",
    params: []
  });
  
  // Impersonate account
  await network.provider.request({
    method: "hardhat_impersonateAccount",
    params: ["0x742d35Cc6734C0532925a3b8D5C50E652c97b5A1"]
  });
  
  // Revert to snapshot
  await network.provider.request({
    method: "evm_revert",
    params: [snapshotId]
  });
}

Account Configuration

Configuration for managing network accounts and signing.

type NetworkAccounts = 
  | "remote" 
  | string[] 
  | HardhatNetworkAccountsConfig 
  | HttpNetworkAccountsConfig;

interface HardhatNetworkAccountsConfig {
  mnemonic?: string;
  initialIndex?: number;
  count?: number;
  path?: string;
  accountsBalance?: string;
}

interface HttpNetworkAccountsConfig {
  mnemonic?: string;
  initialIndex?: number;
  count?: number;
  path?: string;
}

JSON-RPC Types

Type definitions for JSON-RPC request/response handling.

interface JsonRpcRequest {
  jsonrpc: "2.0";
  method: string;
  params?: any[];
  id?: string | number;
}

interface JsonRpcResponse {
  jsonrpc: "2.0";
  id?: string | number;
  result?: any;
  error?: {
    code: number;
    message: string;
    data?: any;
  };
}

Chain Configuration

Configuration for handling multiple chains and forking.

interface ChainConfig {
  chainId?: number;
  hardforkHistory?: { [hardforkName: string]: number };
}

docs

artifact-management.md

configuration.md

hook-system.md

index.md

network-management.md

plugin-system.md

solidity-build-system.md

task-management.md

utilities.md

tile.json