# With Foundry

### Prerequisites <a href="#prerequisites" id="prerequisites"></a>

* ETH on Ethernity:&#x20;
* Rust:
* Foundry:

### 1. Set up the project <a href="#id-1.-setting-up-the-project" id="id-1.-setting-up-the-project"></a>

1.1 Initialize a new Foundry project:

Forge is the command-line interface (CLI) tool for Foundry, allowing developers to execute operations directly from the terminal.

Open up a terminal and run this command:

```
forge init my-project
```

1.2. Install the OpenZeppelin contracts library inside your project, which provides a tested and community-audited implementation of ERC20:

```
forge install OpenZeppelin/openzeppelin-contracts
```

### 2. Write the ERC20 token contract <a href="#id-2.-writing-the-erc20-token-contract" id="id-2.-writing-the-erc20-token-contract"></a>

2.1. In **/src** of your project directory, create a text file named **MyERC20.sol** and add:

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

import "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";

contract MyERC20 is ERC20 {
    constructor() ERC20("MyToken", "MTK") {}
}
```

This is a simple ERC20 token named "MyToken" with the symbol "MTK". You can name it any way you want and also write any other contracts. Just remember that if your file name is called differently, the commands to deploy will be slightly different.

### 3. Build the contract <a href="#id-3.-building-the-contract" id="id-3.-building-the-contract"></a>

3.1. Use Foundry to compile your smart contract running the following command:

```
forge build
```

### 4. Deploy the ERC20 token contract <a href="#id-4.-deploying-the-erc20-token-contract" id="id-4.-deploying-the-erc20-token-contract"></a>

4.1. To deploy your contract you will need to run the following command and replace \<YOUR\_PRIVATE\_KEY> with your credentials:

Never share or expose your private key publicly. This will grant complete control to whoever has it. Please store it safely.

```
forge create --rpc-url https://testnet.ethernitychain.io --private-key <YOUR_PRIVATE_KEY> src/MyERC20.sol:MyERC20
```

You can add the --verify flag and use blockscout to verify your contract while deploying.

The following is the command to deploy and verify the contract. If you already deployed your contract but still want to verify it, don't panic! Next step will walk you through that case.

```
forge create --rpc-url https://testnet.ethernitychain.io --private-key <YOUR_PRIVATE_KEY> src/MyERC20.sol:MyERC20 --verify --verifier blockscout --verifier-url https://sepolia.explorer.mode.network/api\?
```

Copy the “Deployed to” value and store it somewhere to use later. This is the address of your contract.

### 5. Verify the contract <a href="#id-5.-verifying-the-erc20-token-contract-after-deployment" id="id-5.-verifying-the-erc20-token-contract-after-deployment"></a>

5.1. For already deployed contracts you can use the verify-contract command:

```
forge verify-contract <CONTRACT_ADDRESS> src/MyERC20.sol:MyERC20  --verifier blockscout --verifier-url https://sepolia.explorer.mode.network/api\?
```

### 6. Exploring and interacting with your deployed contract <a href="#id-6.-exploring-and-interacting-with-your-deployed-contract" id="id-6.-exploring-and-interacting-with-your-deployed-contract"></a>

Use [ERNScan](https://testnet.ernscan.io) to view your contract's details. Paste your contract's address you saved from the deployment output into blockscout's search bar.

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ethernity.io/tutorials/deploy-a-smart-contract/with-foundry.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
