How to Deploy a Smart Contract on Conflux eSpace Testnet Using Foundry
Are you ready to explore the power of Foundry while deploying smart contracts on the Conflux eSpace Testnet? Whether you’re just starting or transitioning from other toolchains like Hardhat, this step-by-step guide will walk you through the complete process — from setup to deployment.
What You’ll Learn
- How to install Foundry
- How to write and compile a smart contract
- How to configure for the Conflux eSpace Testnet
- How to deploy the contract using Foundry
Prerequisites
- Node.js (v16 or above)
- Git installed
- Basic knowledge of Solidity
- A funded wallet on the Conflux eSpace Testnet
Installation
1. Install Foundry
First, install Foundry using the official installer:
curl -L https://foundry.paradigm.xyz | bash
2. Update Foundry
After installation, update Foundry to the latest version:
foundryup
Verify installation:
forge --version
Usage
Building the Project
Compile your smart contracts:
forge build
Running Tests
Execute the test suite:
forge test
For verbose output:
forge test -vv
Testing
The project includes comprehensive tests for the Counter contract:
- test_Increment(): Tests the increment functionality
- testFuzz_SetNumber(): Fuzz testing for the setNumber function
Run tests with:
forge test
Deployment
Local Development
- Start a local Anvil instance:
anvil
- Deploy to local network:
forge script script/Counter.s.sol --rpc-url http://localhost:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcast
Conflux Testnet Deployment
- Set your private key as an environment variable:
export PRIVATE_KEY="your_private_key_here"
- Deploy to Conflux testnet:
forge script script/Counter.s.sol --rpc-url https://evmtestnet.confluxrpc.com --private-key $PRIVATE_KEY --broadcast -g 200
Mainnet Deployment
For mainnet deployment, use the appropriate RPC URL and ensure you have sufficient funds:
forge script script/Counter.s.sol --rpc-url https://evm.confluxrpc.com --private-key $PRIVATE_KEY --broadcast -g 200
Overview
This project demonstrates a basic counter smart contract with the following features:
- Increment the counter value
- Set the counter to a specific value
- View the current counter value
The contract is built using Foundry, a fast Ethereum development toolkit.
Contract Functions
Counter Contract
The Counter
contract provides the following functions:
-
number()
: View function that returns the current counter value -
setNumber(uint256 newNumber)
: Sets the counter to a specific value -
increment()
: Increments the counter by 1
Example Usage
// Set the counter to 10
counter.setNumber(10);
// Increment the counter
counter.increment(); // Now the value is 11
// Get the current value
uint256 currentValue = counter.number();
Configuration
The project uses the default Foundry configuration in foundry.toml
:
[profile.default]
src = "src"
out = "out"
libs = ["lib"]