> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Hardhat Methods

> Hardhat Network RPC methods for testing and development

## Overview

Hardhat Network-specific RPC methods for controlling test environments. These methods provide similar functionality to Anvil but with Hardhat-specific naming conventions.

## Network Control

### hardhat\_reset

Resets the fork to a fresh state or a specific block.

```typescript theme={null}
import { hardhat } from '@tevm/voltaire/jsonrpc';

// Reset to fresh state
const request = hardhat.hardhat_reset();

// Reset with forking config
const request = hardhat.hardhat_reset({
  forking: {
    jsonRpcUrl: 'https://eth-mainnet.alchemyapi.io/v2/your-api-key',
    blockNumber: 14390000
  }
});
```

**Example: Fork reset**

```typescript theme={null}
// Reset fork to different block
await provider.request(hardhat.hardhat_reset({
  forking: {
    jsonRpcUrl: process.env.MAINNET_RPC,
    blockNumber: 19000000
  }
}));
```

## State Manipulation

### hardhat\_setBalance

Sets the balance of an account.

```typescript theme={null}
const request = hardhat.hardhat_setBalance(
  '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  '0xde0b6b3a7640000' // 1 ETH
);
```

### hardhat\_setCode

Sets the bytecode at an address.

```typescript theme={null}
const request = hardhat.hardhat_setCode(
  '0x5FbDB2315678afecb367f032d93F642f64180aa3',
  '0x6080604052...' // bytecode
);
```

### hardhat\_setNonce

Sets the nonce of an account.

```typescript theme={null}
const request = hardhat.hardhat_setNonce(
  '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  '0x10'
);
```

### hardhat\_setStorageAt

Sets the storage value at a specific slot.

```typescript theme={null}
const request = hardhat.hardhat_setStorageAt(
  '0x5FbDB2315678afecb367f032d93F642f64180aa3',
  '0x0', // slot
  '0x0000000000000000000000000000000000000000000000000000000000000001' // value
);
```

**Example: Test account setup**

```typescript theme={null}
// Fund account
await provider.request(hardhat.hardhat_setBalance(
  testAccount,
  '0x56bc75e2d63100000' // 100 ETH
));

// Set initial nonce
await provider.request(hardhat.hardhat_setNonce(testAccount, '0x0'));
```

## Account Impersonation

### hardhat\_impersonateAccount

Start impersonating an account.

```typescript theme={null}
const request = hardhat.hardhat_impersonateAccount(
  '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
);
```

### hardhat\_stopImpersonatingAccount

Stop impersonating an account.

```typescript theme={null}
const request = hardhat.hardhat_stopImpersonatingAccount(
  '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
);
```

**Example: Impersonate mainnet account**

```typescript theme={null}
const daiWhale = '0x...; // Large DAI holder

// Impersonate
await provider.request(hardhat.hardhat_impersonateAccount(daiWhale));

// Transfer DAI as whale
await dai.connect(daiWhale).transfer(recipient, parseEther('1000'));

// Stop
await provider.request(hardhat.hardhat_stopImpersonatingAccount(daiWhale));
```

## Mining

### hardhat\_mine

Mines one or more blocks.

```typescript theme={null}
// Mine single block
const request = hardhat.hardhat_mine();

// Mine multiple blocks (hex-encoded)
const request = hardhat.hardhat_mine('0xa'); // 10 blocks

// Mine with interval (hex-encoded seconds)
const request = hardhat.hardhat_mine('0xa', '0x5'); // 10 blocks, 5s interval
```

**Example: Mine to trigger time-based logic**

```typescript theme={null}
// Mine 100 blocks to pass time lock
await provider.request(hardhat.hardhat_mine('0x64')); // 100 blocks

// Check time-locked contract
const unlocked = await contract.isUnlocked();
expect(unlocked).toBe(true);
```

## Common Patterns

### Forking Mainnet

```typescript theme={null}
// Fork mainnet at specific block
await provider.request(hardhat.hardhat_reset({
  forking: {
    jsonRpcUrl: process.env.MAINNET_RPC,
    blockNumber: 18000000
  }
}));

// Impersonate mainnet address
await provider.request(hardhat.hardhat_impersonateAccount(mainnetAddress));

// Interact with mainnet state
await mainnetContract.connect(mainnetAddress).someMethod();
```

### Test Setup

```typescript theme={null}
async function setupTestEnvironment() {
  // Fund test accounts
  for (const account of testAccounts) {
    await provider.request(hardhat.hardhat_setBalance(
      account,
      '0x56bc75e2d63100000' // 100 ETH
    ));
  }

  // Deploy mock contracts
  await provider.request(hardhat.hardhat_setCode(
    mockAddress,
    mockBytecode
  ));

  // Set storage values
  await provider.request(hardhat.hardhat_setStorageAt(
    mockAddress,
    '0x0',
    encodedValue
  ));
}
```

### State Manipulation for Testing

```typescript theme={null}
// Set ERC20 balance directly
const balanceSlot = computeStorageSlot(userAddress, 0); // slot 0 for mapping

await provider.request(hardhat.hardhat_setStorageAt(
  tokenAddress,
  balanceSlot,
  '0x' + parseEther('1000').toString(16).padStart(64, '0')
));

// Verify balance
const balance = await token.balanceOf(userAddress);
expect(balance).toBe(parseEther('1000'));
```

## Hardhat vs Anvil

Both Hardhat and Anvil provide similar testing functionality with different naming:

| Feature | Hardhat | Anvil |
| - | - | - |
| Set Balance | `hardhat_setBalance` | `anvil_setBalance` |
| Set Code | `hardhat_setCode` | `anvil_setCode` |
| Set Nonce | `hardhat_setNonce` | `anvil_setNonce` |
| Set Storage | `hardhat_setStorageAt` | `anvil_setStorageAt` |
| Impersonate | `hardhat_impersonateAccount` | `anvil_impersonateAccount` |
| Stop Impersonate | `hardhat_stopImpersonatingAccount` | `anvil_stopImpersonatingAccount` |
| Mine | `hardhat_mine` | `evm_mine` |
| Reset | `hardhat_reset` | N/A |

## See Also

* [Anvil Methods](https://voltaire.tevm.sh/jsonrpc-provider/anvil-methods)
* [Error Handling](https://voltaire.tevm.sh/jsonrpc-provider/error-handling.md)
* [Hardhat Network Docs](https://hardhat.org/hardhat-network/docs)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.