Deploying a Smart Contract with Foundry
This guide will walk you through setting up a new project using Foundry, a blazing fast toolkit for Ethereum application development written in Rust.
Installing Foundry
First, you'll need to install Foundry. Run this command in your terminal:
curl -L https://foundry.paradigm.xyz | bashThen run:
foundryupThis will install forge, cast, and anvil - the core tools of Foundry. You can also use foundryup to update the tools to the latest version.
Creating a New Project
To create a new project, navigate to the directory where you want to create your project and use the forge init command:
forge init my_project
cd my_projectThis will create a new directory with the following structure:
my_project/
βββ lib/
βββ src/
β βββ Counter.sol
βββ test/
β βββ Counter.t.sol
βββ script/
βββ .gitignore
βββ foundry.tomlWriting Your First Contract
Remove the default Counter example contract:
rm -rf src/Counter.sol script/Counter.s.sol test/Counter.t.solCreate a new contract and put it in the file src/InkContract.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract InkContract {
string public greeting = "Hello, Ink!";
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}Create the tests for this contract in the file test/InkContract.t.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {Test} from "forge-std/Test.sol";
import {InkContract} from "../src/InkContract.sol";
contract InkContractTest is Test {
InkContract public ink;
function setUp() public {
ink = new InkContract();
}
function test_DefaultGreeting() public view {
assertEq(ink.greeting(), "Hello, Ink!");
}
function test_SetGreeting() public {
string memory newGreeting = "New greeting!";
ink.setGreeting(newGreeting);
assertEq(ink.greeting(), newGreeting);
}
function testFuzz_SetGreeting(string memory randomGreeting) public {
ink.setGreeting(randomGreeting);
assertEq(ink.greeting(), randomGreeting);
}
}Building and Testing
Build your project:
forge buildRun tests:
forge testDeployment
- First, create a
.envfile in your project root:
PRIVATE_KEY=your_private_key_here
RPC_URL=https://rpc-gel-sepolia.inkonchain.com/
BLOCKSCOUT_API_KEY=your_blockscout_api_key_here- Create a deployment script in
script/Deploy.s.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "forge-std/Script.sol";
import "../src/InkContract.sol";
contract DeployScript is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
new InkContract();
vm.stopBroadcast();
}
}- Deploy your contract:
# Load environment variables
source .env
# Deploy to InkSepolia Testnet
forge script script/Deploy.s.sol:DeployScript --rpc-url $RPC_URL --broadcast --verifyVerifying Your Contract
If you want to verify your contract on Etherscan:
forge verify-contract <DEPLOYED_CONTRACT_ADDRESS> src/InkContract.sol:InkContract \
--chain-id 763373 \
--etherscan-api-key $BLOCKSCOUT_API_KEYAdditional Configuration
You can customize your Foundry setup in foundry.toml:
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
solc = "0.8.19"
optimizer = true
optimizer_runs = 200
[rpc_endpoints]
inksepolia = "${INKSEPOLIA_RPC_URL}"Next Steps
- Check out Get Foundry Book for more information on Foundry.