Skip to main content

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
advertencia

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
información

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:

  1. Option A (CLI): Automated verification using Foundry.
  2. Option B (Web UI): Manual verification via the XFI Scan website.

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!")
información

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:

Step-by-Step Guide​

To successfully verify, all parameters must match your foundry.toml settings exactly.

  1. Contract Address: Paste the address of your deployed contract.

  2. 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.
  3. Compiler Version:

    • Check solc_version in your foundry.toml or running forge build --info.
    • Select the exact version (e.g., v0.8.20+commit...).
  4. EVM Version:

    • Check evm_version in foundry.toml.
    • Default: Often paris or shanghai depending on the solc version.
  5. Optimization / Runs:

    • Optimization: Yes (Foundry enables it by default). Check optimizer = true in config.
    • Runs: Check optimizer_runs in foundry.toml. Default is usually 200.
  6. Upload Source (Single File):

    • Generate a flattened code file:
      forge flatten src/Greeting.sol > Flat.sol
    • Copy the entire content of Flat.sol and paste it into the Contract code box.
  7. 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 flatten includes all your imports. If you have multiple licenses, you might need to clean up SPDX-License-Identifier lines (keep only top-level one), though the verifier is usually lenient.
  • Optimization Runs: If you didn't specify optimizer_runs in foundry.toml, try the default 200.