# With Hardhat

Let's go through the process of deploying a smart contract on Ethernity Chain using Hardhat and TypeScript.

**Requirements**

* Node.js and npm: download and install both packages [here](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
* Ethereum Wallet: [Metamask](https://metamask.io) or any other non-custodial Ethereum wallet, since you will need the [private key](https://support.metamask.io/managing-my-wallet/secret-recovery-phrase-and-private-keys/how-to-export-an-accounts-private-key/)
* ETH: You can get some [Sepolia ETH](https://www.infura.io/faucet/sepolia) and then bridge it to Ethernity&#x20;
* Solidity and CLI knowledge

### 1. Initialize a Hardhat TypeScript project <a href="#id-1.-initialize-a-hardhat-typescript-project" id="id-1.-initialize-a-hardhat-typescript-project"></a>

Open your terminal and create a new directory for your project, then navigate into it:

```
mkdir hello && cd hello
```

Initialize an npm project:

```
npm init -y
```

Install the necessary packages for Hardhat and TypeScript:

```
npm install --save-dev hardhat ts-node typescript @nomiclabs/hardhat-ethers ethers
```

Start a new Hardhat project with TypeScript:

```
npx hardhat init
```

When prompted, make the following selections:

* Choose "Create a TypeScript project".
* For the `.gitignore` prompt, select "Yes" (or `y`).
* For installing the projects dependencies select "Yes" (or `y`).

```
npx hardhat

888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888      88b 888P   d88  888 888  88b      88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888  Y888888 888      Y88888 888  888  Y888888  Y888

👷 Welcome to Hardhat v2.18.2 👷‍

✔ What do you want to do? · Create a TypeScript project
✔ Hardhat project root: · /Users/Ethernity/hello
✔ Do you want to add a .gitignore? (Y/n) · y
✔ Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)? (Y/n) · y
```

### 2. Write a Smart Contract <a href="#id-2.-drafting-the-smart-contract" id="id-2.-drafting-the-smart-contract"></a>

In the `contracts` directory, delete the sample smart contract `Lock.sol` and then create a new file named `Hello.sol`:

Copy

```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Hello {
    function hi() public view returns (string memory) {
        return "Hey there!";
    }
}
```

### 3. Configure Harhdat for Ethernity <a href="#id-3.-configuring-harhdat-for-mode" id="id-3.-configuring-harhdat-for-mode"></a>

Edit the `hardhat.config.ts` file to include Ethernity Chain Testnet settings:

```
import "@nomiclabs/hardhat-ethers";

import { HardhatUserConfig } from "hardhat/config";

const config: HardhatUserConfig = {
  networks: {
    ethernity-testnet: {
      url: "https://testnet.ethernitychain.io",
      chainId: 233,
      accounts: ["PRIVATE_KEY"] // DO NOT SHARE THIS!
    }
  },
  solidity: "0.8.0",
};

export default config;
```

Replace `PRIVATE_KEY` with your Ethereum wallet private key.

**IMPORTANT:** Do not push your `hardhat.config.ts` file to github or share your private key with anyone.&#x20;

### 4. Compile <a href="#id-4.-compilation" id="id-4.-compilation"></a>

Compile the smart contract:

<pre><code><strong>npx hardhat compile
</strong></code></pre>

### 5. Deploy <a href="#id-5.-deployment" id="id-5.-deployment"></a>

In the `scripts` directory, create a new file named `deploy.ts`:

```
import { ethers } from "hardhat";

async function main() {
    const Hello = await ethers.getContractFactory("Hello");
    const hello = await Hello.deploy();
    await hello.deployed();
    console.log("Hello deployed to:", hello.address);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });
```

Now you can deploy the smart contract to Ethernity Testnet:

```
npx hardhat run scripts/deploy.ts --network ethernity-testnet
```

### 6. Check <a href="#id-6.-check-your-contract-in-a-block-explorer" id="id-6.-check-your-contract-in-a-block-explorer"></a>

See your smart contract's deployment on the Ethernity Testnet block explorer (ERNScan): [https://testnet.ernscan.io](https://sepolia.explorer.mode.network/).  Enter the contract address from the command line in the search bar to see the details.
