> For the complete documentation index, see [llms.txt](https://validium.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://validium.gitbook.io/docs/tooling/foundary/getting-started.md).

# Getting Started

***

### Basic Usage <a href="#basic-usage" id="basic-usage"></a>

#### Testing <a href="#testing" id="testing"></a>

Use `forge test --zksync` to run tests written for your smart contracts.

For an overview of how to write tests using `foundry-zksync` please refer to Foundry testing [here](https://docs.zksync.io/build/test-and-debug/foundry).

### Deploying Smart Contracts with `forge` <a href="#deploying-smart-contracts-with-forge" id="deploying-smart-contracts-with-forge"></a>

#### Compilation with `forge build --zksync` <a href="#compilation-with-forge-build-zksync" id="compilation-with-forge-build-zksync"></a>

`forge build --zksync` is used for compiling smart contracts into Validium VM bytecode. The compiled files are stored in a structured directory at `<PROJECT-ROOT>/zkout/`.

**Command:**

```bash
forge build [OPTIONS] --zksync
```

**Options:**

To get the list of all `build` options run:

```bash
forge build --help
```

Besides standard options from original Foundry, there are Validium specific set of options:

* `--zk-startup[=<ENABLE_ZKVM_AT_STARTUP>]` Enable zkEVM at startup.
* `--zk-compile[=<COMPILE_FOR_ZKVM>]` Compile for zkEVM.
* `--zk-solc-path <ZK_SOLC_PATH>` Solc compiler path to use when compiling with zksolc.
* `--zk-enable-eravm-extensions[=<ENABLE_ERAVM_EXTENSIONS>]` Enable the system contract compilation mode.
* `--zk-force-evmla[=<FORCE_EVMLA>]` Forcibly switch to the EVM legacy assembly pipeline.
* `--zk-llvm-options <LLVM_OPTIONS>` ZkSolc extra LLVM options.
* `--zk-fallback-oz[=<FALLBACK_OZ>]` Try to recompile with -Oz if the bytecode is too large.
* `--zk-detect-missing-libraries` Detect missing libraries, instead of erroring.
* `-O, --zk-optimizer-mode <LEVEL>` Set the LLVM optimization parameter `-O[0 | 1 | 2 | 3 | s | z]`. Use `3` for best performance and `z` for minimal size.
* `--zk-optimizer` Enables optimizations.
* `--zk-avoid-contracts <AVOID_CONTRACTS>` Contracts to avoid compiling on Validium.

**Examples:**

Compile with default settings or specify `zksolc` version:

```bash
forge build --zksync
```

#### Deployment with `forge create --zksync` <a href="#deployment-with-forge-create-zksync" id="deployment-with-forge-create-zksync"></a>

**Command:**

```bash
forge create <CONTRACT> [OPTIONS] --zksync
```

The following commands make use of Foundry keystore instead of private keys. [Learn how to create a foundry keystore](/docs/tooling/foundary/installation.md).

`forge create --zksync` deploys smart contracts to Validium.

**Usage:**

{% code overflow="wrap" %}

```bash
forge create <CONTRACT> [OPTIONS] --rpc-url <RPC-URL> --chain <CHAIN-ID> --account myKeystore --sender <KEYSTORE_ADDRESS> --zksync
```

{% endcode %}

**Options:** To get the list of all `create` options run:

```bash
forge create --help
```

**Examples:** Deploy `Greeter.sol` to ZKsync Sepolia Testnet:

<details>

<summary>Click to view the `Greeter.sol` contract</summary>

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

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        greeting = _greeting;
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        greeting = _greeting;
    }
}
```

</details>

{% code overflow="wrap" %}

```bash
forge create src/Greeter.sol:Greeter --constructor-args "Hello ZKsync" --account myKeystore --sender <KEYSTORE_ADDRESS> --rpc-url https://sepolia.era.zksync.dev --chain 300 --zksync
```

{% endcode %}

#### Deploying Factory Contracts <a href="#deploying-factory-contracts" id="deploying-factory-contracts"></a>

To deploy contracts like `GreeterFactory.sol`, use the `--zk-enable-eravm-extensions` flag.

<details>

<summary>Click to view the `GreeterFactory.sol` contract</summary>

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

import "./Greeter.sol";

contract Factory {
    Greeter[] public GreeterArray;

    function CreateNewGreeter(string memory _greeting) public {
        Greeter greeter = new Greeter(_greeting);
        GreeterArray.push(greeter);
    }

    function gfSetter(uint256 _greeterIndex, string memory _greeting) public {
        Greeter(address(GreeterArray[_greeterIndex])).setGreeting(_greeting);
    }

    function gfGetter(uint256 _greeterIndex) public view returns (string memory) {
        return Greeter(address(GreeterArray[_greeterIndex])).greet();
    }
}
```

</details>

**Compile `GreeterFactory.sol`:**

```bash
forge build --zk-enable-eravm-extensions --zksync
```

**Deploy `GreeterFactory.sol`:**

{% code overflow="wrap" %}

```bash
forge create src/GreeterFactory.sol:Factory --factory-deps src/Greeter.sol:Greeter --account myKeystore --sender <KEYSTORE_ADDRESS> --rpc-url https://sepolia.era.zksync.dev --chain 300 --zksync
```

{% endcode %}

**Deploy `Greeter.sol` via `GreeterFactory.sol`:**

{% code overflow="wrap" %}

```bash
cast send <FACTORY_ADDRESS> "CreateNewGreeter(string)" "ZKsync Rules"  --account myKeystore --sender <KEYSTORE_ADDRESS> --rpc-url https://sepolia.era.zksync.dev --chain 300
```

{% endcode %}

**Interact with `Greeter.sol`**

{% code overflow="wrap" %}

```bash
cast call <CONTRACT_ADDRESS> "greet()(string)" --rpc-url https://sepolia.era.zksync.dev --chain 300
```

{% endcode %}

**Output:**

{% code overflow="wrap" %}

```bash
0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c7a6b53796e632052756c65730000000000000000000000000000000000000000
```

{% endcode %}

**To decode the output to a readable string:**

{% code overflow="wrap" %}

```bash
cast to-ascii  0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c7a6b53796e632052756c65730000000000000000000000000000000000000000
```

{% endcode %}

**Output:**

```bash
ZKsync Rules
```

### Basic Validium Chain Interactions with `cast` <a href="#basic-zksync-chain-interactions-with-cast" id="basic-zksync-chain-interactions-with-cast"></a>

#### Introduction <a href="#introduction" id="introduction"></a>

This guide introduces you to fundamental interactions within the ZKsync chain using `cast`, a component of the `foundry-zksync` toolkit. Learn how to query chain IDs, retrieve client versions, check L2 ETH balances, obtain gas prices, and more.

**Command:**

```bash
cast <subcommand>
```

#### Chain ID Retrieval <a href="#chain-id-retrieval" id="chain-id-retrieval"></a>

* **Local Node:**\
  Retrieve the Chain ID for a local Validium node with:

  ```bash
  cast chain-id --rpc-url http://localhost:3050
  ```

  \
  Expected Output: `270`, indicating the Chain ID of your local Validium node.
* **Validium Sepolia Testnet:**\
  For the ZKsync Sepolia Testnet, use:

  ```bash
  cast chain-id --rpc-url https://sepolia.era.zksync.dev
  ```

  \
  Expected Output: `300`, the Chain ID for the Validium Sepolia Testnet.

#### Client Version Information <a href="#client-version-information" id="client-version-information"></a>

Knowing the client version is vital for compatibility checks and debugging:

```bash
cast client --rpc-url https://sepolia.era.zksync.dev
```

Expected Output: `ZKsync/v2.0`, denoting the client version.

#### L2 Balance Check <a href="#l2-balance-check" id="l2-balance-check"></a>

Verify the Layer 2 (L2) balance of an account:

{% code overflow="wrap" %}

```bash
cast balance 0x8b1d48a69ACEbC6eb201e2F4d162A002203Bfe8E --rpc-url https://sepolia.era.zksync.dev
```

{% endcode %}

Expected Output: A numerical value, e.g., `774909739323110932`, representing the account's L2 balance.

#### Current Gas Price <a href="#current-gas-price" id="current-gas-price"></a>

Fetch the current gas price on the network for transaction cost estimations:

```bash
cast gas-price --rpc-url https://sepolia.era.zksync.dev
```

Expected Output: A value such as `100000000`, indicating the current gas price.

#### Latest Block Details <a href="#latest-block-details" id="latest-block-details"></a>

Gain insights into the latest block on the Validium chain:

```bash
cast block latest --rpc-url https://sepolia.era.zksync.dev
```

Expected Output: Detailed information about the latest block, including base fee per gas, gas limit, block hash, and more.

#### Sending Transactions <a href="#sending-transactions" id="sending-transactions"></a>

Initiate transactions, such as contract function calls, using `cast`:

{% code overflow="wrap" %}

```bash
cast send <CONTRACT_ADDRESS> <FUNCTION_SIGNATURE> <ARGUMENTS> --rpc-url <RPC-URL> --account myKeystore --sender <KEYSTORE_ADDRESS> --chain <CHAIN-ID>
```

{% endcode %}

Example:

{% code overflow="wrap" %}

```bash
cast send 0xe34E488C1B0Fb372Cc4a5d39219261A5a6fc7996 "setGreeting(string)" "Hello, Validium!" --rpc-url https://sepolia.era.zksync.dev --account myKeystore --sender <KEYSTORE_ADDRESS> --chain 300
```

{% endcode %}

This command calls the `setGreeting` function of a contract, updating the greeting to "Hello, Validium!".

### Support <a href="#support" id="support"></a>

If you're having an issues creating Foundry Validium project, please check out list of issues in [`foundry-zksync` github issues page](https://github.com/matter-labs/foundry-zksync/issues) or reach out to us by [creating a GitHub discussion](https://github.com/ZKsync-Community-Hub/zksync-developers/discussions/).
