> ## 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.

# Foundry - Get Started

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

To use Foundry to build smart contracts on Abstract, use the [foundry-zksync](https://github.com/matter-labs/foundry-zksync) fork.

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

## 1. Install the foundry-zksync fork

<Note>
  This installation overrides any existing forge and cast binaries in
  `~/.foundry`. To revert to the standard foundry installation, follow the
  [Foundry installation
  guide](https://book.getfoundry.sh/getting-started/installation#using-foundryup).
  You can swap between the two installations at any time.
</Note>

<Steps>
  <Step title="Install foundry-zksync">
    Install the `foundryup-zksync` fork:

    ```bash theme={null}
    curl -L https://raw.githubusercontent.com/matter-labs/foundry-zksync/main/install-foundry-zksync | bash
    ```

    Run `foundryup-zksync` to install `forge`, `cast`, and `anvil`:

    ```bash theme={null}
    foundryup-zksync
    ```

    You may need to restart your terminal session after installation to continue.

    <Accordion title="Common installation issues" icon="circle-exclamation">
      <AccordionGroup>
        <Accordion title="foundryup-zksync: command not found">
          Restart your terminal session.
        </Accordion>

        <Accordion title="Could not detect shell">
          To add the `foundry` binary to your PATH, run the following command:

          ```
          export PATH="$PATH:/Users/<your-username-here>/.foundry/bin"
          ```

          Replace `<your-username-here>` with the correct path to your home directory.
        </Accordion>

        <Accordion title="Library not loaded: libusb">
          The [libusb](https://libusb.info/) library may need to be installed manually on macOS.
          Run the following command to install the library:

          ```bash theme={null}
          brew install libusb
          ```
        </Accordion>
      </AccordionGroup>
    </Accordion>
  </Step>

  <Step title="Verify installation">
    A helpful command to check if the installation was successful is:

    ```bash theme={null}
    forge build --help | grep -A 20 "ZKSync configuration:"
    ```

    If installed successfully, 20 lines of `--zksync` options will be displayed.
  </Step>
</Steps>

## 2. Create a new project

Create a new project with `forge` and change directory into the project.

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

## 3. Modify Foundry configuration

Update your `foundry.toml` file to include the following options:

```toml theme={null}
[profile.default]
src = 'src'
libs = ['lib']
fallback_oz = true

[profile.default.zksync]
enable_eravm_extensions = true # Note: System contract calls (NonceHolder and ContractDeployer) can only be called with this set to true

[etherscan]
abstractTestnet = { chain = "11124", url = "https://api-sepolia.abscan.org/api", key = "TACK2D1RGYX9U7MC31SZWWQ7FCWRYQ96AD"}
abstractMainnet = { chain = "2741", url = "", key = ""} # You can replace these values or leave them blank to override via CLI
```

<Note>
  To use [system contracts](/how-abstract-works/system-contracts/overview),
  set the `enable_eravm_extensions` flag in `[profile.default.zksync]` to **true**.
</Note>

## 4. Write a smart contract

Modify the `src/Counter.sol` file to include the following smart contract:

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

contract Counter {
    uint256 public number;

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

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

## 5. Compile the smart contract

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:

```bash theme={null}
forge build --zksync
```

You should now see the compiled smart contracts in the generated `zkout` directory.

## 6. Deploy the smart contract

<Steps>
  <Step title="Add your private key" icon="key">
    Create a new [wallet keystore](https://book.getfoundry.sh/reference/cast/cast-wallet-import).

    ```bash theme={null}
    cast wallet import myKeystore --interactive
    ```

    Enter your wallet's private key when prompted and provide a password to encrypt it.

    <Warning>We recommend not to 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="Get an Abscan API key (Mainnet only)">
    Follow the [Abscan documentation](https://docs.abscan.org/getting-started/viewing-api-usage-statistics) to get an API key to verify your smart contracts on the block explorer.
    This is recommended; but not required.
  </Step>

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

    <CodeGroup>
      ```bash Testnet theme={null}
      forge create src/Counter.sol:Counter \
          --account myKeystore \
          --rpc-url https://api.testnet.abs.xyz \
          --chain 11124 \
          --zksync \
          --verify \
          --verifier etherscan \
          --verifier-url https://api-sepolia.abscan.org/api \
          --etherscan-api-key TACK2D1RGYX9U7MC31SZWWQ7FCWRYQ96AD \
          --broadcast
      ```

      ```bash Mainnet theme={null}
      forge create src/Counter.sol:Counter \
          --account myKeystore \
          --rpc-url https://api.mainnet.abs.xyz \
          --chain 2741 \
          --zksync \
          --verify \
          --verifier etherscan \
          --verifier-url https://api.abscan.org/api \
          --etherscan-api-key <your-abscan-api-key> \
          --broadcast
      ```
    </CodeGroup>

    ***Note**: Replace the contract path, address, and Abscan API key with your own.*

    If successful, the output should look similar to the below output, and you can view your contract on a [block explorer](/tooling/block-explorers).

    ```bash {2} theme={null}
    Deployer: 0x9C073184e74Af6D10DF575e724DC4712D98976aC
    Deployed to: 0x85717893A18F255285AB48d7bE245ddcD047dEAE
    Transaction hash: 0x2a4c7c32f26b078d080836b247db3e6c7d0216458a834cfb8362a2ac84e68d9f

    Contract successfully verified
    ```
  </Step>
</Steps>
