Foundry Template
The CrossFi Foundry Template is a minimal starter project for developers who prefer using Foundry for smart contract development on the CrossFi Chain.
It provides a streamlined setup with a "Hello World" contract and deployment scripts configured for CrossFi networks.
Prerequisites
- Foundry installed
- A CrossFi account with testnet tokens (get them from the faucet)
- A private key with testnet XFI tokens
Quick Start
Follow these steps to deploy your first contract to the CrossFi Testnet using Foundry.
1. Clone the Repository
git clone https://github.com/crossfichain/foundry-template.git
cd foundry-template
2. Install Dependencies
forge install
3. Configure Environment
Create a .env file in the root directory by copying the example:
cp .env.example .env
Edit the .env file and add your private key.
Note: The .env.example file already contains the default RPC URLs (CROSSFI_TEST_RPC, etc.) and Verifier URLs needed for the commands below. You can leave them as is or update them if needed.
PRIVATE_KEY=your_private_key_here
Never commit your .env file to version control. It contains sensitive information.
4. Build Contracts
Build the project to ensure everything is correct:
forge build
5. Deploy to CrossFi Testnet
Run the deployment script using forge script. Make sure to load your environment variables first:
source .env
forge script script/Deploy.s.sol:DeployScript \
--rpc-url $CROSSFI_TEST_RPC \
--private-key $PRIVATE_KEY \
--broadcast
If successful, Foundry will broadcast the transaction and output the contract address. You can verify the deployment on the CrossFi Testnet Explorer.
Network Configuration
The template includes a foundry.toml file pre-configured with CrossFi RPC endpoints.
Testnet
- Chain ID: 4157
- RPC URL:
https://rpc.testnet.ms
Mainnet
- Chain ID: 4158
- RPC URL:
https://rpc.mainnet.ms
Gas Settings
CrossFi uses a fixed gas price strategy. The template suggests using a gas price of 1 gwei for transactions.
You can specify the gas price in your deployment command if needed:
--gas-price 1000000000
1 gwei is a recommended value for standard transactions. Adjust as necessary for your specific needs.
Contract Verification
CrossFi supports two methods for contract verification:
- Option A (CLI): Automated verification using Foundry.
- Option B (Web UI): Manual verification via the XFI Scan website.
Option A: CLI Verification (Recommended)
CrossFi uses custom verifier endpoints (xfiscan.com) instead of the standard Etherscan API. The foundry.toml is pre-configured with these settings.
Configuration
The foundry.toml file includes specific [etherscan] configurations for CrossFi networks:
[etherscan]
crossfi_testnet = { key = "ABCDEFG", url = "${CROSSFI_TEST_VERIFIER_URL}", chain = 4157 }
crossfi_mainnet = { key = "no-key", url = "${CROSSFI_VERIFIER_URL}", chain = 4158 }
Ensure your .env file contains the verifier URLs:
CROSSFI_VERIFIER_URL=https://xfiscan.com/api/1.0/verify-contract
CROSSFI_TEST_VERIFIER_URL=https://test.xfiscan.com/api/1.0/verify-contract
Verify Command
Use the forge verify-contract command to verify your deployed contract.
For Testnet:
forge verify-contract <CONTRACT_ADDRESS> \
src/Greeting.sol:Greeting \
--chain crossfi_testnet \
--rpc-url $CROSSFI_TEST_RPC \
--etherscan-api-key ABCDEFG \
--constructor-args $(cast abi-encode "constructor(string)" "Hello, CrossFi!")
For Mainnet:
forge verify-contract <CONTRACT_ADDRESS> \
src/Greeting.sol:Greeting \
--chain crossfi_mainnet \
--rpc-url $CROSSFI_RPC \
--etherscan-api-key no-key \
--constructor-args $(cast abi-encode "constructor(string)" "Hello, CrossFi!")
The API key for Mainnet is "no-key" and for Testnet is "ABCDEFG". These are required placeholders for the verification process.
Option B: Verify via XFI Scan Web UI
If you prefer using the graphical interface or need to troubleshoot CLI issues, use the Web UI.
Links:
- Testnet: test.xfiscan.com/contract/verify
- Mainnet: xfiscan.com/contract/verify
Step-by-Step Guide
To successfully verify, all parameters must match your foundry.toml settings exactly.
-
Contract Address: Paste the address of your deployed contract.
-
Compiler Type:
- Single file: Recommended for Foundry users (using
forge flatten). - Multi-Part files: Not recommended for Foundry unless you manually reconstruct the import structure.
- Single file: Recommended for Foundry users (using
-
Compiler Version:
- Check
solc_versionin yourfoundry.tomlor runningforge build --info. - Select the exact version (e.g.,
v0.8.20+commit...).
- Check
-
EVM Version:
- Check
evm_versioninfoundry.toml. - Default: Often
parisorshanghaidepending on the solc version.
- Check
-
Optimization / Runs:
- Optimization: Yes (Foundry enables it by default). Check
optimizer = truein config. - Runs: Check
optimizer_runsinfoundry.toml. Default is usually200.
- Optimization: Yes (Foundry enables it by default). Check
-
Upload Source (Single File):
- Generate a flattened code file:
forge flatten src/Greeting.sol > Flat.sol - Copy the entire content of
Flat.soland paste it into the Contract code box.
- Generate a flattened code file:
-
Click Verify.
Troubleshooting Web UI Verification
- Metadata: Foundry includes metadata hash by default. If verification fails, ensure the EVM version is correct.
- Flattening: Make sure
forge flattenincludes all your imports. If you have multiple licenses, you might need to clean upSPDX-License-Identifierlines (keep only top-level one), though the verifier is usually lenient. - Optimization Runs: If you didn't specify
optimizer_runsinfoundry.toml, try the default200.