This project demonstrates a simple "Hello World" smart contract implemented in Solidity using the Foundry framework, with a focus on basic security techniques.
The HelloWorld.sol contract has been enhanced with the following security techniques:
-
Access Control: Implementation of the Ownable pattern to restrict critical functions to the contract owner only.
-
Pausability: Ability to pause the contract in case of emergency, preventing state modifications.
-
Input Validation: Verification of input data to avoid invalid states (for example, empty strings).
-
Events: Emission of events for all important actions, allowing off-chain monitoring and auditing.
-
Custom Errors: Use of custom errors for clearer error messages and gas efficiency.
-
Proper Visibility: Appropriate use of visibility modifiers (private, public) for state variables.
-
NatSpec Documentation: Complete code documentation using the NatSpec format.
src/HelloWorld.sol: Main contract with security implementationstest/HelloWorld.t.sol: Comprehensive tests for all functionalities and error casesscript/HelloWorld.s.sol: Deployment script that demonstrates the functionalities
- Foundry installed
forge buildforge test -vv# Configure your private key in the .env file
echo "PRIVATE_KEY=your_private_key_here" > .env
# Deploy to a test network
forge script script/HelloWorld.s.sol --rpc-url <RPC_URL> --broadcast --verifygetGreeting(): Returns the current greetingsetGreeting(string): Sets a new greeting (owner only, when not paused)transferOwnership(address): Transfers ownership of the contractpause(): Pauses the contract (owner only)unpause(): Unpauses the contract (owner only)isPaused(): Checks if the contract is pausedgetOwner(): Returns the address of the current owner
# Clone the repository
git clone <repository-url>
cd <repository-directory>
# Install dependencies
forge install- Start a local Ethereum node using Anvil:
anvil- Deploy the contract to the local network:
forge script script/HelloWorld.s.sol --fork-url http://localhost:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcast -vvvAfter deployment, you can interact with the contract using Cast:
# Get the current greeting
cast call <CONTRACT_ADDRESS> "getGreeting()" --rpc-url http://localhost:8545 | cast --to-ascii
# Set a new greeting
cast send <CONTRACT_ADDRESS> "setGreeting(string)" "Your New Greeting!" --private-key <PRIVATE_KEY> --rpc-url http://localhost:8545
# Check if contract is paused
cast call <CONTRACT_ADDRESS> "isPaused()" --rpc-url http://localhost:8545
# Get the contract owner
cast call <CONTRACT_ADDRESS> "getOwner()" --rpc-url http://localhost:8545To deploy to a testnet or mainnet, you'll need to:
- Set up environment variables for your RPC URL and private key
- Run the deployment script with the appropriate network
forge script script/HelloWorld.s.sol --rpc-url <YOUR_RPC_URL> --private-key <YOUR_PRIVATE_KEY> --broadcast -vvvThis project is licensed under the MIT License - see the LICENSE file for details.