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

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.chainstack.com/feedback

```json
{
  "path": "/docs/hyperliquid-forking-evm-foundry",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# Fork Hyperliquid EVM with Foundry

> Fork the Hyperliquid EVM chain locally using Foundry Anvil for offline smart contract development, testing, and debugging with a Chainstack endpoint.

This guide demonstrates how to fork the Hyperliquid EVM chain using Foundry's Anvil to create a local development environment for testing smart contracts and dApps.

## Prerequisites

Before starting, ensure you have:

* [Foundry](https://book.getfoundry.sh/getting-started/installation) installed
* A [reliable Hyperliquid RPC endpoint](https://chainstack.com/build-better-with-hyperliquid/) from Chainstack
* Basic knowledge of Foundry and smart contract development

## Why fork Hyperliquid EVM?

Forking allows you to:

* Test smart contracts against the actual Hyperliquid EVM state
* Simulate transactions without spending real funds
* Debug and develop dApps in a controlled environment
* Test interactions with deployed contracts on Hyperliquid

## Start a forked instance

Use Anvil to fork the Hyperliquid EVM mainnet:

```bash theme={"system"}
anvil --fork-url YOUR_CHAINSTACK_HYPERLIQUID_ENDPOINT
```

Replace `YOUR_CHAINSTACK_HYPERLIQUID_ENDPOINT` with your actual Chainstack Hyperliquid EVM endpoint.

<Note>
  The Hyperliquid HyperEVM mainnet uses chain ID 999. Anvil will automatically inherit this when forking.
</Note>

## Configure fork options

### Fork from a specific block

To fork from a specific block height:

```bash theme={"system"}
anvil --fork-url YOUR_CHAINSTACK_HYPERLIQUID_ENDPOINT --fork-block-number 13276400
```

### Set custom chain ID

Override the chain ID if needed:

```bash theme={"system"}
anvil --fork-url YOUR_CHAINSTACK_HYPERLIQUID_ENDPOINT --chain-id 31337
```

### Adjust block time

Set custom block time (default is instant mining):

```bash theme={"system"}
anvil --fork-url YOUR_CHAINSTACK_HYPERLIQUID_ENDPOINT --block-time 1
```

## Test the fork

### Connect with cast

Verify your fork is running:

```bash theme={"system"}
cast block-number --rpc-url http://localhost:8545
```

Or use curl to retrieve the HYPE token total supply off the local fork:

```bash theme={"system"}
curl --request POST \
  --url 127.0.0.1:8545 \
  --header 'Content-Type: application/json' \
  --data '{
  "jsonrpc": "2.0",
  "method": "eth_call",
  "params": [
    {
      "to": "0x5555555555555555555555555555555555555555",
      "data": "0x18160ddd"
    },
    "latest"
  ],
  "id": 1
}'
```

### Check account balances

View the balance of any Hyperliquid address:

```bash theme={"system"}
cast balance 0xYourAddress --rpc-url http://localhost:8545
```

### Impersonate accounts

Impersonate any account for testing:

```bash theme={"system"}
cast rpc anvil_impersonateAccount 0xAccountToImpersonate --rpc-url http://localhost:8545
```

<Warning>
  Remember that forked state is ephemeral. Any transactions or state changes exist only in your local fork and don't affect the actual Hyperliquid network.
</Warning>

## Best practices

<Steps>
  <Step>
    Always fork from a recent block to ensure state relevance
  </Step>

  <Step>
    Use deterministic testing by forking from specific blocks
  </Step>

  <Step>
    Save fork state when testing complex scenarios
  </Step>

  <Step>
    Monitor your Chainstack endpoint usage to avoid rate limits
  </Step>
</Steps>

## Conclusion

Forking Hyperliquid EVM with Foundry provides a powerful development environment for testing smart contracts and dApps. This approach allows you to interact with actual on-chain state without risking real funds or affecting the live network.

## Next steps

* Explore [Hyperliquid smart contract development](/docs/hyperliquid-development)
* Learn about [Hyperliquid authentication](/docs/hyperliquid-authentication-guide)
* [Foundry docs](https://book.getfoundry.sh/)
