Skip to content

Latest commit

 

History

History
223 lines (164 loc) · 6.31 KB

File metadata and controls

223 lines (164 loc) · 6.31 KB
title Foundry - Get Started
sidebarTitle Get Started
description 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 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.

1. Install the foundry-zksync fork

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.

Install the foundryup-zksync fork:

curl -L https://raw.githubusercontent.com/matter-labs/foundry-zksync/main/install-foundry-zksync | bash

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

foundryup-zksync

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

Restart your terminal session. To add the `foundry` binary to your PATH, run the following command: ``` export PATH="$PATH:/Users//.foundry/bin" ``` Replace `` with the correct path to your home directory. The [libusb](https://libusb.info/) library may need to be installed manually on macOS. Run the following command to install the library: ```bash brew install libusb ```
A helpful command to check if the installation was successful is:

```bash
forge build --help | grep -A 20 "ZKSync configuration:"
```

If installed successfully, 20 lines of `--zksync` options will be displayed.

2. Create a new project

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

forge init my-abstract-project && cd my-abstract-project

3. Modify Foundry configuration

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

[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.etherscan.io/v2/api?chainid=11124", key = ""}
abstractMainnet = { chain = "2741", url = "", key = ""} # You can replace these values or leave them blank to override via CLI
To use [system contracts](/how-abstract-works/system-contracts/overview), set the `enable_eravm_extensions` flag in `[profile.default.zksync]` to **true**.

4. Write a smart contract

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

// 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 (installed in the above steps) to compile smart contracts for Abstract:

forge build --zksync

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

6. Deploy the smart contract

Create a new [wallet keystore](https://book.getfoundry.sh/reference/cast/cast-wallet-import).
```bash
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>
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.

Follow the Abscan documentation to get an API key to verify your smart contracts on the block explorer. This is recommended; but not required.

Run the following command to deploy your smart contracts:

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.etherscan.io/v2/api?chainid=11124 \
    --etherscan-api-key <your-etherscan-api-key> \
    --broadcast
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.etherscan.io/v2/api?chainid=2741 \
    --etherscan-api-key <your-etherscan-api-key> \
    --broadcast

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.

Deployer: 0x9C073184e74Af6D10DF575e724DC4712D98976aC
Deployed to: 0x85717893A18F255285AB48d7bE245ddcD047dEAE
Transaction hash: 0x2a4c7c32f26b078d080836b247db3e6c7d0216458a834cfb8362a2ac84e68d9f

Contract successfully verified