Skip to content

joaquimafn/hello-world-smart-contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hello World Solidity + Foundry with Security

This project demonstrates a simple "Hello World" smart contract implemented in Solidity using the Foundry framework, with a focus on basic security techniques.

Implemented Security Techniques

The HelloWorld.sol contract has been enhanced with the following security techniques:

  1. Access Control: Implementation of the Ownable pattern to restrict critical functions to the contract owner only.

  2. Pausability: Ability to pause the contract in case of emergency, preventing state modifications.

  3. Input Validation: Verification of input data to avoid invalid states (for example, empty strings).

  4. Events: Emission of events for all important actions, allowing off-chain monitoring and auditing.

  5. Custom Errors: Use of custom errors for clearer error messages and gas efficiency.

  6. Proper Visibility: Appropriate use of visibility modifiers (private, public) for state variables.

  7. NatSpec Documentation: Complete code documentation using the NatSpec format.

Project Structure

  • src/HelloWorld.sol: Main contract with security implementations
  • test/HelloWorld.t.sol: Comprehensive tests for all functionalities and error cases
  • script/HelloWorld.s.sol: Deployment script that demonstrates the functionalities

How to Use

Prerequisites

Compile

forge build

Test

forge test -vv

Deploy

# 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 --verify

Features

  • getGreeting(): Returns the current greeting
  • setGreeting(string): Sets a new greeting (owner only, when not paused)
  • transferOwnership(address): Transfers ownership of the contract
  • pause(): Pauses the contract (owner only)
  • unpause(): Unpauses the contract (owner only)
  • isPaused(): Checks if the contract is paused
  • getOwner(): Returns the address of the current owner

Additional Information

Installation

# Clone the repository
git clone <repository-url>
cd <repository-directory>

# Install dependencies
forge install

Local Deployment

  1. Start a local Ethereum node using Anvil:
anvil
  1. Deploy the contract to the local network:
forge script script/HelloWorld.s.sol --fork-url http://localhost:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcast -vvv

Interacting with the Contract

After 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:8545

Deployment to a Testnet or Mainnet

To deploy to a testnet or mainnet, you'll need to:

  1. Set up environment variables for your RPC URL and private key
  2. 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 -vvv

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Hello world smart contract with security rules

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors