This repository contains a comprehensive collection of Solidity smart contract examples designed for learning and reference purposes. The contracts are organized sequentially, starting from basic concepts and progressing to advanced topics such as security patterns, token standards, and low-level operations. Each file demonstrates a specific Solidity feature or best practice, with clear code comments for educational value.
- Solidity compiler (solc) version 0.8.20 or higher
- Node.js and npm (for testing with Hardhat or Truffle, if applicable)
- A development environment like Remix IDE, Visual Studio Code with Solidity extensions, or a local Ethereum development setup
-
Clone the repository:
git clone <repository-url> cd solidity-learning -
Ensure you have the Solidity compiler installed. You can install it via npm:
npm install -g solc -
For testing and deployment, consider setting up a development framework like Hardhat:
npm install --save-dev hardhat npx hardhat init
The following is a detailed list of all Solidity contract files in this repository, along with brief descriptions of the concepts they demonstrate:
- 01_HelloWorld.sol - Basic contract structure with state variables, constructor, functions, and events.
- 02_DataTypes.sol - Overview of Solidity's built-in data types (uint, int, bool, address, etc.).
- 03_Variables.sol - Declaration and usage of state and local variables.
- 04_ConstantsImmutables.sol - Use of constants and immutable variables for gas optimization.
- 05_Functions.sol - Function definitions, parameters, return values, and modifiers.
- 06_Visibility.sol - Public, private, internal, and external function and variable visibility.
- 07_Modifiers.sol - Custom function modifiers for access control and validation.
- 08_Constructors.sol - Contract initialization using constructors.
- 09_Inheritance.sol - Single inheritance between contracts.
- 10_MultiInheritanceOverride.sol - Multiple inheritance and function overriding.
- 11_Interface.sol - Defining and implementing interfaces.
- 12_Abstract.sol - Abstract contracts and their usage.
- 13_Events.sol - Emitting and handling events in contracts.
- 14_ErrorsRequireRevert.sol - Error handling using require, revert, and assert.
- 15_Mapping.sol - Usage of mapping data structures.
- 16_Arrays.sol - Fixed and dynamic arrays, array operations.
- 17_Structs.sol - Defining and using custom data structures (structs).
- 18_Enums.sol - Enumeration types and their applications.
- 19_DataLocations.sol - Storage, memory, and calldata data locations.
- 20_ViewPure.sol - View and pure function modifiers for read-only operations.
- 21_Payable.sol - Functions that can receive Ether.
- 22_FallbackReceive.sol - Fallback and receive functions for handling Ether transfers.
- 23_Units.sol - Working with Ether units (wei, gwei, ether) and time units.
- 24_Gas.sol - Gas optimization techniques and considerations.
- 25_Library.sol - Creating and using library contracts.
- 26_ImportDemo.sol - Importing contracts and libraries.
- 27_Overloading.sol - Function and operator overloading.
- 28_Pausable.sol - Implementing pausable functionality in contracts.
- 29_Keccak.sol - Using Keccak256 hashing functions.
- 30_ECDSA.sol - Elliptic Curve Digital Signature Algorithm implementation.
- 31_Ownable.sol - Ownership pattern for contract access control.
- 32_Reentrancy_Unsafe.sol - Example of vulnerable reentrancy attack.
- 33_Reentrancy_Safe.sol - Secure implementation with reentrancy guard.
- 34_PullPayments.sol - Pull payment pattern to prevent reentrancy.
- 35_Auction.sol - Simple auction contract implementation.
- 36_ERC20_Min.sol - Minimal ERC20 token standard implementation.
- 37_ERC20_Mintable.sol - ERC20 token with minting functionality.
- 38_ERC721_Min.sol - Minimal ERC721 (NFT) token standard implementation.
- 39_SafeMathBuiltIn.sol - Using Solidity's built-in SafeMath operations.
- 40_TryCatch.sol - Try-catch error handling in external calls.
- 41_Create2.sol - Using CREATE2 opcode for deterministic contract deployment.
- 42_Delegatecall.sol - Delegatecall for calling functions in other contracts.
- 43_LowLevelCall.sol - Low-level call, staticcall, and delegatecall operations.
- 44_Assembly.sol - Inline assembly for advanced optimizations and operations.
This repository provides examples that can be run and tested using various Solidity development environments. Below are detailed instructions for using Remix IDE and Hardhat.
Remix is a web-based IDE suitable for quick testing and learning.
-
Open your web browser and navigate to remix.ethereum.org.
-
Create a new file for each contract:
- Click the "+" icon in the File Explorers panel.
- Name the file (e.g.,
HelloWorld.sol). - Copy and paste the contract code from the corresponding file in this repository.
-
Compile the contract:
- Select the Solidity compiler tab.
- Choose the appropriate compiler version (0.8.20 or higher).
- Click "Compile" for the selected file.
-
Deploy the contract:
- Switch to the "Deploy & Run Transactions" tab.
- Select "JavaScript VM" for local testing or connect to a testnet like Sepolia.
- Click "Deploy" to deploy the contract to the selected environment.
-
Interact with the deployed contract:
- Use the deployed contract interface to call functions and view state variables.
- Observe transaction results and event logs in the console.
Hardhat is a development environment for compiling, testing, and deploying Solidity contracts.
-
Ensure Node.js and npm are installed on your system.
-
Set up a Hardhat project in the repository directory:
npm init -y npm install --save-dev hardhat npx hardhat init- Choose "Create a JavaScript project" when prompted.
-
Copy the Solidity files from this repository into the
contracts/directory of your Hardhat project. -
Compile the contracts:
npx hardhat compile -
Create a deployment script (e.g.,
scripts/deploy.js):const { ethers } = require("hardhat"); async function main() { const Contract = await ethers.getContractFactory("ContractName"); const contract = await Contract.deploy(/* constructor arguments */); await contract.deployed(); console.log("Contract deployed to:", contract.address); } main().catch((error) => { console.error(error); process.exitCode = 1; });
Replace "ContractName" with the actual contract name (e.g., "HelloWorld").
-
Start a local Hardhat network:
npx hardhat node -
Deploy the contract in a new terminal:
npx hardhat run scripts/deploy.js --network localhost -
For testing, create test files in the
test/directory using Mocha and Chai, or interact with the deployed contract using Hardhat console.
-
Command Line Compilation: Use the Solidity compiler directly:
solc --bin --abi 01_HelloWorld.sol -
Integration: These contracts can be integrated into larger projects or used as templates for your own smart contract development. For production use, always conduct thorough testing and security audits.
Contributions are welcome! Please follow these guidelines:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Add clear, educational comments to any new code.
- Ensure all contracts compile without warnings.
- Test your changes thoroughly.
- Submit a pull request with a detailed description of your changes.
This project is licensed under the MIT License. See the LICENSE file for details.