Foundry
Learn how to use Foundry to build and deploy smart contracts on Abstract.
To use Foundry to build smart contracts on Abstract, use the foundry-zksync fork.
YouTube Tutorial: Get Started with Foundry
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.
You can swap between the two installations at any time.
Clone the repository
From any directory, clone the foundry-zksync
repository.
git clone https://github.com/matter-labs/foundry-zksync.git
Run the installer
Change directory into the foundry-zksync
directory.
cd foundry-zksync
Run the installer:
./install-foundry-zksync
A successful installation will output the following message:
Forge version 0.0.2 is successfully installed.
Verify installation
A helpful command to check if the installation was successful is:
forge build --help | grep -A 20 "ZKSync configuration:"
If successful, it will output 20
lines of the available
options included in the foundry-zksync
fork.
2. Create a new project
First, change directory out of the foundry-zksync
directory.
cd ..
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 forge.toml
file to include the following options:
[profile.default]
src = 'src'
libs = ['lib']
fallback_oz = true
is_system = false # Note: NonceHolder and the ContractDeployer system contracts can only be called with a special isSystem flag as true
mode = "3"
To use system contracts,
set the is_system
flag 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
Add your private key
Create a new wallet keystore.
cast wallet import myKeystore --interactive
Enter your wallet’s private key when prompted & provide a password to encrypt it.
Deploy your smart contract
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
Ensure myKeystore
(in the above command) is the name of the keystore file you created in the previous step.
If successful, your output should look similar to the following:
Deployer: 0x8e729E23CDc8bC21c37a73DA4bA9ebdddA3C8B6d
Deployed to: 0x1590522E5758a7A8DD00f212fe487AE94fE69dcf
Transaction hash: 0xa7e2ecf1611fc01221678ac2c8919fe9c338b2aad9ac1ff4ae6f0d1711a62cf0
Verify smart contract on the block explorer
Contract verification is not supported with Foundry yet.
You can verify your smart contracts manually on the Abstract block explorer.
Was this page helpful?