This README provides step-by-step instructions to set up Foundry and run tests on Solidity contracts. The tests include checking ownership and minting functionalities for an ERC721 token.
- Foundry installed on your machine.
- Basic knowledge of Solidity and smart contract testing.
-
Install Foundry:
- Use the following command to install Foundry using
foundryup
:curl -L https://foundry.paradigm.xyz | bash
- Add Foundry to your PATH:
source $HOME/.foundry/env
- Use the following command to install Foundry using
-
Verify Installation:
- Check that Foundry is installed correctly by running:
forge --version
- You should see the version information for Foundry.
- Check that Foundry is installed correctly by running:
-
Create a Solidity Project Directory:
- Create a directory for your Solidity project. For example:
mkdir -p ~/foundry-project cd ~/foundry-project
- Create a directory for your Solidity project. For example:
-
Initialize Foundry:
- Run the following command to initialize a new Foundry project:
forge init
- Run the following command to initialize a new Foundry project:
-
Add Your Solidity Code:
- Save the following Solidity code in a file named
MyERC721.sol
under thesrc
directory:
- Save the following Solidity code in a file named
- Create Test File:
- Save the following test code in a file named
MyERC721_1.t.sol
under thetest
directory: - Save the following test code in a file named
MyERC721_2.t.sol
under thetest
directory:
- Save the following test code in a file named
-
Compile the Contract:
- Compile the smart contracts by running:
forge build
- Compile the smart contracts by running:
-
Execute Tests:
- Run the tests using the following command:
forge test
- Run the tests using the following command:
-
Verify Test Results:
- Review the output to ensure all tests pass.
Please refer to the provided screenshots for each step:
- Failures: If any test fails, review the specific failure message to debug the issue in the contract or test logic.The tests failed due to the following reasons:
-
testMintAfterOwnershipTransfer
: This test failed because after transferring ownership tonewOwner
, minting a token should succeed. However, the test attempts to assert ownership of token ID 1, which does not exist, leading to anERC721NonexistentToken(1)
error. This indicates that the token was either not minted or the token ID used in the assertion was incorrect. -
testMultipleOwnershipTransfers
: This test failed because it attempts multiple ownership transfers. The failure was due to a revert with the message "Caller is not the owner," indicating that the contract's current owner was not the expected owner when attempting to transfer ownership for the second or third time. This suggests a logic error in the test setup or the contract's ownership transfer functionality.
Foundry is a comprehensive suite of Ethereum development tools, including a Solidity compiler, a testing framework, and a deployment tool. It is designed to improve the developer experience by providing fast and efficient testing and deployment capabilities for Solidity smart contracts.