Foundry
Learn how to develop smart contracts on the Lens Network using Foundry.
This guide is focused on migrating an existing Solidity Foundry project to the Lens Network.
Limitations
Before deciding to use Foundry for development on the Lens Network, it's important to note that Foundry's ZKsync support comes from an open-source Foundry fork currently in alpha stage.
Be mindful of the following limitations, among others:
Compile Time: Compilation may be slower for some users.
Specific Foundry Features: Features like --gas-report or --verify might not function as expected. Efforts are underway to fully support these features.
Compiling Libraries: Non-inlinable libraries require deployment and configuration adjustments.
Configuration
Follow the steps below to configure Foundry for development on the Lens Network.
Clone the Foundry ZKsync repository:
git clone [email protected]:matter-labs/foundry-zksync.git
Install Foundry ZKsync:
cd foundry-zksync./install-foundry-zksync
Upon successful installation, you will receive a confirmation message similar to the following:
foundryup-zksync: installed - forge 0.0.2 (6e1c282 2024-07-03T00:22:11.972797000Z)foundryup-zksync: installed - cast 0.0.2 (6e1c282 2024-07-03T00:22:22.891893000Z)foundryup-zksync: done!Verifying installation...Forge version 0.0.2 is successfully installed.
After installation, the forge and cast commands will switch to using the Foundry ZKsync fork.
To revert to the original Foundry version, use the foundryup command. To switch back to the Foundry ZKsync fork, run foundryup-zksync.
You can verify the active Foundry version at any time by comparing the version hash with those provided in the confirmation message.
Add a dedicated profile to your foundry.toml file for the Lens Network:
foundry.toml
[profile.default]src = 'src'out = 'out'libs = ['lib']# other options your default profile might have
[profile.zksync]src = 'src'libs = ['lib']solc-version = "0.8.24"fallback_oz = trueis_system = falsemode = "3"
To deploy contracts on the Lens Network, you'll need $GRASS tokens. Ask the Lens Team for testnet tokens.
Then, create a Foundry keystore:
FOUNDRY_PROFILE=zksync cast wallet import myKeystore --interactive
You will be prompted to enter your Private Key and a password. Upon successful import, you will receive a confirmation message similar to the following:
`myKeystore` keystore was saved successfully. Address: 0x1234567890abcdef1234567890abcdef12345678
myKeystore is the name of the keystore you created. You can replace it with a name of your choice.
Usage
Compile Contracts
To compile your contracts for the Lens Network, use the following command:
FOUNDRY_PROFILE=zksync forge build --zksync
Upon successful compilation, you will receive a confirmation message similar to the following:
[⠃] Compiling (zksync)...Compiler run successful with warnings:┌──────────────────────────────────────────────────────────────────────────────────────────────────┐│ Warning: Your code or one of its dependencies uses the 'extcodesize' instruction, which is ││ usually needed in the following cases: ││ 1. To detect whether an address belongs to a smart contract. ││ 2. To detect whether the deploy code execution has finished. ││ ZKsync Era comes with native account abstraction support (so accounts are smart contracts, ││ including private-key controlled EOAs), and you should avoid differentiating between contracts ││ and non-contract addresses. │└──────────────────────────────────────────────────────────────────────────────────────────────────┘--> lib/forge-std/src/StdCheats.sol
...
Address any warnings as necessary. For more information, consult the ZKsync Best Practices.
Deploy Contracts
Use the forge create --zksync command to deploy your contracts to the Lens Network.
forge create --zksync
FOUNDRY_PROFILE=zksync forge create <CONTRACT> [OPTIONS] \ --rpc-url <RPC-URL> \ --chain <CHAIN-ID> \ --account <KEYSTORE_NAME> \ --from <KEYSTORE_ADDRESS> \ --zksync
For example:
Lens Network Sepolia Testnet
FOUNDRY_PROFILE=zksync forge create src/Lock.sol:Lock \ --constructor-args "42" \ --account myKeystore \ --from <KEYSTORE_ADDRESS> \ --rpc-url https://rpc.testnet.lens.dev \ --chain 37111 \ --zksync
After entering your keystore password twice, a successful deployment will generate a confirmation message as shown below:
Deployment Confirmation
[⠊] Compiling (zksync)...Enter keystore password:Enter keystore password:Deployer: 0x00A58BA275E6BFC004E2bf9be121a15a2c543e71Deployed to: 0x5CbF18d3379a7FE3cFFcA34801EDc700eAe49a92Transaction hash: 0xd3eda207aa6930b3e6b271ff77997921570c41e525689ce1b62c50013b6226fb
That's it—you have successfully deployed your contract to the Lens Network.
Running Tests
Use the forge test --zksync command to run tests.
forge test --zksync
FOUNDRY_PROFILE=zksync forge test --zksync
Troubleshooting
Library Not Loaded
If you encounter an error similar to the following when running ./install-foundry-zksync:
dyld[10022]: Library not loaded: /opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib Referenced from: <93EBBD45-018B-39DE-8009-A2662BD3CFE4> /Users/brainjammer/.foundry/bin/forge Reason: tried: '/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file), '/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file)foundryup-zksync: command failed: /Users/brainjammer/.foundry/bin/forge --versionfoundryup-zksync: installed -dyld[10025]: Library not loaded: /opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib Referenced from: <2E658B56-FE34-30BF-A410-8E6D46C6B1C5> /Users/brainjammer/.foundry/bin/cast Reason: tried: '/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file), '/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file)foundryup-zksync: command failed: /Users/brainjammer/.foundry/bin/cast --versionfoundryup-zksync: installed -foundryup-zksync: done!Verifying installation...dyld[10027]: Library not loaded: /opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib Referenced from: <93EBBD45-018B-39DE-8009-A2662BD3CFE4> /Users/brainjammer/.foundry/bin/forge Reason: tried: '/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file), '/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib' (no such file)Installation verification failed. Forge is not properly installed.
You can resolve this issue by installing libusb:
brew install libusb