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

# Hardhat - Get Started

> Get started with Abstract by deploying your first smart contract using Hardhat.

This document outlines the end-to-end process of deploying a smart contract on Abstract using Hardhat.
It’s the ideal guide if you’re building a new project from scratch.

<Card title="YouTube Tutorial: Get Started with Hardhat" icon="youtube" href="https://www.youtube.com/watch?v=Jr_Flw-asZ4">
  Watch a step-by-step tutorial on how to get started with Hardhat.
</Card>

## 1. Create a new project

<Accordion title="Prerequisites">
  Ensure you have the following installed on your machine:

  * [Node.js](https://nodejs.org/en/download/) v18.0.0 or later.
  * If you are on Windows, we strongly recommend using [WSL 2](https://learn.microsoft.com/en-us/windows/wsl/about) to follow this guide.
</Accordion>

Inside an empty directory, initialize a new Hardhat project using
the [Hardhat CLI](https://hardhat.org/getting-started/):

Create a new directory and navigate into it:

```bash theme={null}
mkdir my-abstract-project && cd my-abstract-project
```

Initialize a new Hardhat project within the directory:

```bash theme={null}
npx hardhat init
```

Select your preferences when prompted by the CLI, or use the recommended setup below.

<Accordion title="Recommended Hardhat setup">
  We recommend selecting the following options when prompted by the Hardhat CLI:

  ```bash theme={null}
  ✔ What do you want to do? · Create a TypeScript project
  ✔ Hardhat project root: · /path/to/my-abstract-project
  ✔ Do you want to add a .gitignore? (Y/n) · y
  ✔ Do you ... install ... dependencies with npm ... · y
  ```
</Accordion>

## 2. Install the required dependencies

Abstract smart contracts use [different bytecode](/how-abstract-works/evm-differences/overview)
than the Ethereum Virtual Machine (EVM).

Install the required dependencies to compile, deploy and interact with smart contracts on Abstract:

* [@matterlabs/hardhat-zksync](https://github.com/matter-labs/hardhat-zksync):
  A suite of Hardhat plugins for working with Abstract.
* [zksync-ethers](/build-on-abstract/applications/ethers):
  Recommended package for writing [Hardhat scripts](https://hardhat.org/hardhat-runner/docs/advanced/scripts)
  to interact with your smart contracts.

```bash theme={null}
npm install -D @matterlabs/hardhat-zksync zksync-ethers@6 ethers@6
```

## 3. Modify the Hardhat configuration

Update your `hardhat.config.ts` file to include the following options:

```typescript [expandable] theme={null}
import { HardhatUserConfig } from "hardhat/config";
import "@matterlabs/hardhat-zksync";

const config: HardhatUserConfig = {
  zksolc: {
    version: "latest",
    settings: {
      // find all available options in the official documentation
      // https://docs.zksync.io/build/tooling/hardhat/hardhat-zksync-solc#configuration
    },
  },
  defaultNetwork: "abstractTestnet",
  networks: {
    abstractTestnet: {
      url: "https://api.testnet.abs.xyz",
      ethNetwork: "sepolia",
      zksync: true,
      chainId: 11124,
    },
    abstractMainnet: {
      url: "https://api.mainnet.abs.xyz",
      ethNetwork: "mainnet",
      zksync: true,
      chainId: 2741,
    },
  },
  etherscan: {
    apiKey: {
      abstractTestnet: "TACK2D1RGYX9U7MC31SZWWQ7FCWRYQ96AD",
      abstractMainnet: "IEYKU3EEM5XCD76N7Y7HF9HG7M9ARZ2H4A",
    },
    customChains: [
      {
        network: "abstractTestnet",
        chainId: 11124,
        urls: {
          apiURL: "https://api-sepolia.abscan.org/api",
          browserURL: "https://sepolia.abscan.org/",
        },
      },
      {
        network: "abstractMainnet",
        chainId: 2741,
        urls: {
          apiURL: "https://api.abscan.org/api",
          browserURL: "https://abscan.org/",
        },
      },
    ],
  },
  solidity: {
    version: "0.8.24",
  },
};

export default config;
```

## 4. Write a smart contract

Rename the existing `contracts/Lock.sol` file to `contracts/HelloAbstract.sol`:

```bash theme={null}
mv contracts/Lock.sol contracts/HelloAbstract.sol
```

Write a new smart contract in the `contracts/HelloAbstract.sol` file, or use the example smart contract below:

```solidity theme={null}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract HelloAbstract {
    function sayHello() public pure virtual returns (string memory) {
        return "Hello, World!";
    }
}
```

## 5. Compile the smart contract

Clear any existing artifacts:

```bash theme={null}
npx hardhat clean
```

Use the [zksolc compiler](https://docs.zksync.io/zk-stack/components/compiler/toolchain/solidity)
(installed in the above steps) to compile smart contracts for Abstract:

<CodeGroup>
  ```bash Testnet theme={null}
  npx hardhat compile --network abstractTestnet
  ```

  ```bash Mainnet theme={null}
  npx hardhat compile --network abstractMainnet
  ```
</CodeGroup>

You should now see the compiled smart contracts in the generated `artifacts-zk` directory.

## 6. Deploy the smart contract

<Steps>
  <Step title="Add the deployer account private key" icon="key">
    Create a new [configuration variable](https://hardhat.org/hardhat-runner/docs/guides/configuration-variables)
    called `DEPLOYER_PRIVATE_KEY`.

    ```bash theme={null}
    npx hardhat vars set DEPLOYER_PRIVATE_KEY
    ```

    Enter the private key of a new wallet you created for this step.

    ```bash theme={null}
    ✔ Enter value: · ****************************************************************
    ```

    <Warning>Do NOT use a private key associated with real funds. Create a new wallet for this step.</Warning>
  </Step>

  <Step title="Get ETH in the deployer account">
    The deployer account requires ETH to deploy a smart contract.

    * **Testnet**: Claim ETH via a [faucet](/tooling/faucets), or
      [bridge](/tooling/bridges) ETH from Sepolia.
    * **Mainnet**: Use a [bridge](/tooling/bridges) to transfer ETH to Abstract mainnet.
  </Step>

  <Step title="Write the deployment script" icon="code">
    Create a new [Hardhat script](https://hardhat.org/hardhat-runner/docs/advanced/scripts) located at `/deploy/deploy.ts`:

    ```bash theme={null}
    mkdir deploy && touch deploy/deploy.ts
    ```

    Add the following code to the `deploy.ts` file:

    ```typescript theme={null}
    import { Wallet } from "zksync-ethers";
    import { HardhatRuntimeEnvironment } from "hardhat/types";
    import { Deployer } from "@matterlabs/hardhat-zksync";
    import { vars } from "hardhat/config";

    // An example of a deploy script that will deploy and call a simple contract.
    export default async function (hre: HardhatRuntimeEnvironment) {
      console.log(`Running deploy script`);

      // Initialize the wallet using your private key.
      const wallet = new Wallet(vars.get("DEPLOYER_PRIVATE_KEY"));

      // Create deployer object and load the artifact of the contract we want to deploy.
      const deployer = new Deployer(hre, wallet);
      // Load contract
      const artifact = await deployer.loadArtifact("HelloAbstract");

      // Deploy this contract. The returned object will be of a `Contract` type,
      // similar to the ones in `ethers`.
      const tokenContract = await deployer.deploy(artifact);

      console.log(
        `${
          artifact.contractName
        } was deployed to ${await tokenContract.getAddress()}`
      );
    }
    ```
  </Step>

  <Step title="Deploy your smart contract" icon="rocket">
    Run the following command to deploy your smart contracts:

    <CodeGroup>
      ```bash Testnet theme={null}
      npx hardhat deploy-zksync --script deploy.ts --network abstractTestnet
      ```

      ```bash Mainnet theme={null}
      npx hardhat deploy-zksync --script deploy.ts --network abstractMainnet
      ```
    </CodeGroup>

    If successful, your output should look similar to the following:

    ```bash {2} theme={null}
    Running deploy script
    HelloAbstract was deployed to YOUR_CONTRACT_ADDRESS
    ```
  </Step>

  <Step title="Verify your smart contract on the block explorer" icon="check">
    Verifying your smart contract is helpful for others to view the code and
    interact with it from a [block explorer](/tooling/block-explorers).
    To verify your smart contract, run the following command:

    <CodeGroup>
      ```bash Testnet theme={null}
      npx hardhat verify --network abstractTestnet YOUR_CONTRACT_ADDRESS
      ```

      ```bash Mainnet theme={null}
      npx hardhat verify --network abstractMainnet YOUR_CONTRACT_ADDRESS
      ```
    </CodeGroup>

    **Note**: Replace `YOUR_CONTRACT_ADDRESS` with the address of your deployed smart contract.
  </Step>
</Steps>
