# Deploy Your Smart Contracts

To deploy your smart contracts to a live network, there are a few things you need to adjust.

## 1. Configure your network

Scaffold-ETH 2 comes with a selection of predefined networks. To add your custom network:

<Tabs groupId="dev-tool">
  <TabItem value="hardhat" label="Hardhat" default>
    Go to `packages/hardhat/hardhat.config.ts` and add your network to the `networks` object.

    ```typescript title="packages/hardhat/hardhat.config.ts"
    networks: {
        // ... other networks
        base: {
            url: "https://mainnet.base.org",
            accounts: [deployerPrivateKey]
        },
    }
    ```
  </TabItem>

  <TabItem value="foundry" label="Foundry">
    Go to `packages/foundry/foundry.toml` and add your network to the `rpc_endpoints` object.

    ```toml title="packages/foundry/foundry.toml"
    [rpc_endpoints]
    ...other chains
    base = "https://mainnet.base.org"
    ```
  </TabItem>
</Tabs>

Here are the [Alchemy docs](https://www.alchemy.com/docs/ethereum) for information on specific networks.

You can also add your custom network by following the recipe [here](/recipes/add-custom-chain).

## 2. Generate a new account or add one to deploy the contract(s) from

The deployer account is the account that will deploy your contracts. Additionally, the deployer account will be used to execute any function calls that are part of your deployment script.

<Tabs groupId="dev-tool">
  <TabItem value="hardhat" label="Hardhat" default>
    You can **generate a random account / private key** by running:

    ```bash
    yarn generate
    ```

    It will automatically add the encrypted private key (`DEPLOYER_PRIVATE_KEY_ENCRYPTED`) in your `.env` file.

    You will be prompted to enter a password which will be used to encrypt your private key. <u>**Make sure to remember this password as you'll need it for future deployments and account queries.**</u>

    :::info
    We are only storing the plain private key in memory, it's never stored in disk files for security reasons. Checkout the code [here](https://github.com/scaffold-eth/create-eth/blob/main/templates/solidity-frameworks/hardhat/packages/hardhat/scripts/generateAccount.ts).
    :::

    If you prefer to **import your private key**, run:

    ```bash
    yarn account:import
    ```

    You will get prompted to paste your private key and set the encryption password. It will store your encrypted private key in your `.env` file.

    You can **check the configured (generated or imported) account and balances** with:

    ```bash
    yarn account
    ```

    You will need to enter your password to decrypt the private key and view the account information and balances.

    <details>
      <summary><strong>Reveal the private key of your deployer account (security risk)</strong></summary>

      To reveal the private key of your currently configured deployer account (from `.env`), you can run:

      ```bash
      yarn account:reveal-pk
      ```

      You will be prompted to enter the password you used during the `yarn generate` or `yarn account:import` process. The command will then display your private key in the terminal.

      This command is especially useful if you need to **export your deployer account to another tool, wallet, or environment**.

      :::warning
      Revealing your private key can be risky. Ensure you are in a secure environment and understand the implications. Never share your private key or commit it to version control.
      :::
    </details>
  </TabItem>

  <TabItem value="foundry" label="Foundry">
    :::note
    If you already have a foundry keystore account, you can skip the following steps.
    :::

    You can **generate a new keystore account** by running:

    ```bash
    yarn generate
    ```

    It will automatically generate a new [keystore](https://book.getfoundry.sh/reference/cast/cast-wallet-import) with random private key.

    You will be prompted to enter keystore name and a password which will be used to encrypt your keystore. <u>**Make sure to remember this password as you'll need it for future deployments and account queries.**</u>

    If you prefer to **import your private key**, run:

    ```bash
    yarn account:import
    ```

    You will get prompted to enter [keystore](https://book.getfoundry.sh/reference/cast/cast-wallet-import#cast-wallet-import) name, private key and set the encryption password. It will create a new keystore in `~/.foundry/keystore` directory.

    You can **check the configured (generated or imported) account and balances** with:

    ```bash
    yarn account
    ```

    You will need to enter your password to decrypt the private key and view the account information and balances.

    <details>
      <summary><strong>Reveal the private key of a keystore account (security risk)</strong></summary>

      To reveal the private key of a specific keystore account, you can run:

      ```bash
      yarn account:reveal-pk
      ```

      You will be prompted to select the keystore account and then enter the password for that keystore. The command will then display the private key in the terminal.

      This command is especially useful if you need to **export your deployer account to another tool, wallet, or environment**.

      :::warning
      Revealing your private key can be risky. Ensure you are in a secure environment and understand the implications. Never share your private key or commit it to version control.
      :::
    </details>
  </TabItem>
</Tabs>

## 3. Deploy your smart contract(s)

<Tabs groupId="dev-tool">
  <TabItem value="hardhat" label="Hardhat" default>
    By default `yarn deploy` will deploy all the contracts from your `packages/hardhat/contracts` folder to the local network. You can change `defaultNetwork` in `packages/hardhat/hardhat.config.ts` file.

    To deploy to a specific network, you can also use the `--network` flag:

    ```bash
    yarn deploy --network network_name
    ```

    eg: `yarn deploy --network sepolia`
  </TabItem>

  <TabItem value="foundry" label="Foundry">
    By default, `yarn deploy` runs `Deploy.s.sol`, deploying to the local network using Anvil's 9th account (configured via `LOCALHOST_KEYSTORE_ACCOUNT` in `packages/foundry/.env`).

    Change the `default_network` in `packages/foundry/foundry.toml` to change default deployment network.

    To deploy to a specific network, you can also use the `--network` flag:

    ```bash
    yarn deploy --network network_name
    ```

    eg: `yarn deploy --network sepolia`

    :::note
    When deploying to a live network, you'll be prompted to create or select a keystore account and enter its password.
    :::

    You can also bypass the keystore selection prompt and use a specific keystore account with the `--keystore` flag:

    ```bash
    yarn deploy --network sepolia --keystore my-account
    ```

    The keystore must exist in `~/.foundry/keystores/`.
  </TabItem>
</Tabs>

#### Deploy Specific Contracts

<Tabs groupId="dev-tool">
  <TabItem value="hardhat" label="Hardhat" default>
    To deploy specific contracts, add tags to the deploy scripts in `packages/hardhat/deploy`. For example, in `01_deploy_my_contract.ts`:

    ```ts
    deployMyContract.tags = ["tagExample"];
    ```

    Then run:

    ```bash
    yarn deploy --tags tagExample
    ```
  </TabItem>

  <TabItem value="foundry" label="Foundry">
    For individual contracts, create a separate deployment script in `packages/foundry/script`, e.g., `DeployMyContract.s.sol`. Run the script with:

    ```bash
    yarn deploy --file DeployMyContract.s.sol
    ```

    If `--file` parameter is not specified, `Deploy.s.sol` file is used.
  </TabItem>
</Tabs>

## 4. Verify your smart contract

You can verify your smart contract on Etherscan by running:

```bash
yarn verify --network network_name
```

eg: `yarn verify --network sepolia`

This command **works in both Hardhat and Foundry**, verifying all the deployed contracts. However, the verification method differs depending on the Solidity framework you're using...

<Tabs groupId="dev-tool">
  <TabItem value="hardhat" label="Hardhat" default>
    Hardhat uses [etherscan-verify from hardhat-deploy](https://www.npmjs.com/package/hardhat-deploy#4-hardhat-etherscan-verify).

    Additionally, there's an alternative method for contract verification. You can use [hardhat-verify](https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify) to verify your contracts, passing in the network name, contract address and constructor arguments (if any):

    ```bash
    yarn hardhat-verify --network network_name contract_address "Constructor arg 1"
    ```

    If the chain you're using is not supported by any of the verifying methods, you can add new supported chains to your chosen method, either [etherscan-verify](https://www.npmjs.com/package/hardhat-deploy#options-2) or [hardhat-verify](https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify#adding-support-for-other-networks).
  </TabItem>

  <TabItem value="foundry" label="Foundry">
    Foundry uses `VerifyAll.s.sol` script located in `packages/foundry/script`.
  </TabItem>
</Tabs>

## Configuration of Third-Party Services for Production-Grade Apps

By default, Scaffold-ETH 2 provides predefined API keys for popular services such as Alchemy and Etherscan. This allows you to begin developing and testing your applications more easily, avoiding the need to register for these services.

For production-grade applications, it's recommended to obtain your own API keys (to prevent rate limiting issues). You can configure these at:

<Tabs groupId="dev-tool">
  <TabItem value="hardhat" label="Hardhat" default>
    * `ALCHEMY_API_KEY` variable in `packages/hardhat/.env` and `packages/nextjs/.env.local`. You can create API keys from the [Alchemy dashboard](https://dashboard.alchemy.com/).
    * `ETHERSCAN_API_KEY` variable in `packages/hardhat/.env` using your generated API key. You can get your key [here](https://etherscan.io/myapikey).
  </TabItem>

  <TabItem value="foundry" label="Foundry">
    * `ALCHEMY_API_KEY` variable in `packages/nextjs/.env.local`. You can create API keys from the [Alchemy dashboard](https://dashboard.alchemy.com/).
    * `ETHERSCAN_API_KEY` variable in `packages/foundry/.env` using your generated API key. You can get your key [here](https://etherscan.io/myapikey).
  </TabItem>
</Tabs>

:::tip
It's recommended to store envs for nextjs in Vercel/system env config for live apps and use .env.local for local testing.
:::
