> ## Documentation Index
> Fetch the complete documentation index at: https://monadfoundation-40611fb6-devops-1498-direct-udp-implementat.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy a smart contract on Monad using Foundry

This guide uses the official [Foundry](/tooling-and-infra/toolkits/foundry) v1.8 or later to compile and deploy a contract on Monad Testnet.

## 1. Install Foundry

Install the official Foundry installer:

```sh theme={null}
curl -L https://foundry.paradigm.xyz | bash
```

Follow the installer instructions to add `foundryup` to your shell, then install the latest stable release:

```sh theme={null}
foundryup
```

Confirm that `forge --version` reports v1.8.0 or later. See the [Foundry toolkit page](/tooling-and-infra/toolkits/foundry#installation) for more installation and migration guidance.

## 2. Create a new foundry project

<Tip>
  You can use `foundry-monad` template to create a new project.

  *[Foundry-Monad](https://github.com/monad-developers/foundry-monad) is a Foundry template with Monad configuration.*
</Tip>

The below command uses `foundry-monad` to create a new foundry project:

```sh theme={null}
forge init --template monad-developers/foundry-monad [project_name]
```

Alternatively, you can create a foundry project using the command below:

```sh theme={null}
forge init [project_name]
```

## 3. Modify Foundry configuration

Update `foundry.toml` to select the Monad execution environment and configure Monad Testnet:

```toml lines title="foundry.toml" theme={null}
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
network = "monad"
# Monad Testnet Configuration
eth-rpc-url = "https://testnet-rpc.monad.xyz"
chain_id = 10143
```

The `network = "monad"` setting is the persistent equivalent of passing `--network monad`. It makes Forge use Monad's execution rules for local compilation, testing, scripts, and transaction simulation. Foundry selects the active hardfork automatically when interacting with Monad Testnet.

## 4. Write a smart contract

You can write your smart contracts under the `src` folder. There is already a `Counter` contract in the project located at `src/Counter.sol`.

```solidity lines title="src/Counter.sol" theme={null}
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

contract Counter {
    uint256 public number;

    function setNumber(uint256 newNumber) public {
        number = newNumber;
    }

    function increment() public {
        number++;
    }
}
```

## 5. Compile the smart contract

```sh theme={null}
forge compile
```

Compilation process output can be found in the newly created `out` directory, which includes contract ABI and bytecode.

## 6. Deploy the smart contract

<Note>
  For deploying contracts, we recommend using keystores instead of private keys.
</Note>

### Get testnet funds

Deploying smart contracts requires testnet funds. Claim testnet funds via a [faucet](https://testnet.monad.xyz).

### Deploy smart contract

<Tabs>
  <Tab title="Using a Keystore (Recommended)">
    Using a keystore is much safer than using a private key because keystore encrypts the private key and can later be referenced in any commands that require a private key.

    Create a new keystore by importing a newly generated private key with the command below.

    ```sh theme={null}
    cast wallet import monad-deployer --private-key $(cast wallet new | grep 'Private key:' | awk '{print $3}')
    ```

    Here is what the command above does, step by step:

    * Generates a new private key
    * Imports the private key into a keystore file named `monad-deployer`
    * Prints the address of the newly created wallet to the console

    After creating the keystore, you can read its address using:

    ```sh theme={null}
    cast wallet address --account monad-deployer
    ```

    Provide a password to encrypt the keystore file when prompted and do not forget it.

    Run the below command to deploy your smart contracts

    ```sh theme={null}
    forge create src/Counter.sol:Counter --account monad-deployer --broadcast
    ```
  </Tab>

  <Tab title="Using a Private Key (Not Recommended)">
    Use the below command to deploy a smart contract by directly pasting the private key in the terminal.

    <Warning>
      Using a private key is not recommended. You should not be copying and pasting private keys into your terminal. Please use a keystore instead.
    </Warning>

    ```sh theme={null}
    forge create --private-key <your_private_key> src/Counter.sol:Counter --broadcast
    ```
  </Tab>
</Tabs>

On successful deployment of the smart contract, the output should be similar to the following:

```sh theme={null}
[⠊] Compiling...
Deployer: 0xB1aB62fdFC104512F594fCa0EF6ddd93FcEAF67b
Deployed to: 0x67329e4dc233512f06c16cF362EC3D44Cdc800e0
Transaction hash: 0xa0a40c299170c9077d321a93ec20c71e91b8aff54dd9fa33f08d6b61f8953ee0
```

### Next Steps

Check out [how to verify the deployed smart contract on MonadVision](/guides/verify-smart-contract/foundry).
