src
directory is where the smart contracts are placed.out
directory will generate a JSON file containing compilation data, such as ABI.
127.0.0.1:8545
, and you can add it to MetaMask for ease of testing.forge create
. Forge defaults to the Anvil local blockchain, but other RPCs can be specified using the --rpc-url
flag.--interactive
flag for a prompt to add a private key:
.s.sol
.SimpleStorage.sol
.deploySimpleStorage.s.sol
with the following content:
pragma solidity ^0.8.19;
: Specifies that the script is compatible with Solidity version 0.8.19 or any newer version of the 0.8 series but not version 0.9 or above.import {Script} from "forge-std/Script.sol";
: Imports the Script
class from the forge-std
library, which is a part of Foundry, a development environment for Ethereum smart contracts.import {SimpleStorage} from "../src/SimpleStorage.sol";
: Imports the SimpleStorage
contract, presumably a custom contract located in the src
directory.contract DeploySimpleStorage is Script
: Defines a new contract named DeploySimpleStorage
that inherits from the Script
class. This setup is typical for deployment scripts in Foundry.function run() external returns (SimpleStorage)
: The run
function is the main entry point for the deployment script. It’s marked external
as it’s intended to be called externally, and it returns an instance of SimpleStorage
.vm.startBroadcast();
: Initiates a transaction broadcast. The vm
object is a special component in Foundry, providing various functionalities related to the Ethereum Virtual Machine (EVM).SimpleStorage simpleStorage = new SimpleStorage();
: Instantiates the SimpleStorage
contract.vm.stopBroadcast();
: Ends the transaction broadcast.return simpleStorage;
: Returns the deployed instance of SimpleStorage
.SimpleStorage
contract, which is not detailed here, would contain the actual business logic or data storage mechanisms.
cast send
:
cast call
for reading view functions:
lib
directory.foundry.toml
file for syntax convenience:
-vv
flag outputs detailed logs for better insight.// SPDX-License-Identifier: MIT
: This is a comment specifying the license under which this file is released, in this case, the MIT License.pragma solidity ^0.8.18;
: This line specifies the compiler version. The file is compatible with Solidity version 0.8.18 and above within the 0.8.x range.import {Test, console} from "forge-std/Test.sol";
: This line imports two elements from the Forge standard library (forge-std
):
Test
: A base contract that provides testing functionalities.console
: A utility to log output to the console. This is particularly useful for debugging and tracking variable values during test execution.contract FundMeTest is Test {
: This line declares a new contract FundMeTest
which inherits from the Test
contract. In the context of Forge, this means FundMeTest
is a test suite.uint256 number = 33;
: A state variable number
of type uint256
(unsigned integer of 256 bits) is declared and initialized to 33. This variable is used to demonstrate state manipulation and assertion in the test.function setUp() external { number = 3333; }
: The setUp()
function is a special function in the Forge framework that runs before each test function. It’s used for initializing or resetting the state. Here, it sets the number
variable to 3333.function testDemo() public { ... }
: This is the actual test function. In Forge, any function with a name starting with test
is considered a test case.
console.log("The saved number is", number);
: This line logs the value of number
to the console, which is useful for debugging or verifying the test state.assertEq(number, 3333);
: This is an assertion statement provided by the Test
contract. It checks whether the value of number
is equal to 3333. If the assertion fails (i.e., if number
is not 3333), the test will fail.forge coverage
to analyze how much of your contracts are tested: