A collection of Solidity smart contracts implementing solutions to the Base Learn exercises. This repository contains progressive learning exercises covering fundamental to advanced Solidity concepts.
- Overview
- Prerequisites
- Quick Start
- Exercises & Contracts
- Deployment Scripts
- Development Workflow
- Testing
- Contributing
This codebase contains solutions to Base's Solidity learning curriculum, covering:
- Basic Math Operations with overflow/underflow protection
- Control Structures and conditional logic
- Storage Optimization and variable packing
- Arrays & Mappings manipulation
- Structs and complex data types
- Inheritance patterns and abstract contracts
- Import Systems and library usage
- Error Handling and debugging techniques
- Factory Patterns with the
newkeyword - ERC-20 Tokens with voting mechanisms
- ERC-721 NFTs with unique content validation
- Custom Token implementations
- Node.js (v16 or higher)
- Foundry toolkit
- Git
- Basic understanding of Solidity and Ethereum
curl -L https://foundry.paradigm.xyz | bash
foundryup-
Clone the repository
git clone https://github.com/rionnaldi/BaseLearn.git cd BaseLearn -
Install dependencies
forge install
-
Build all contracts
forge build
-
Run tests
forge test -
Deploy a contract (example)
forge script script/DeployBasicMath.s.sol --rpc-url <your_rpc_url> --private-key <your_private_key> --broadcast
// Handles addition/subtraction with overflow/underflow detection
function adder(uint256 _a, uint256 _b) external returns (uint256 sum, bool error)
function subtractor(uint256 _a, uint256 _b) external returns (uint256 difference, bool error)Key Features: Overflow protection, error flags, safe arithmetic
// Optimized storage with variable packing
uint16 private shares; // Packed with salary in same slot
uint24 private salary; // Up to 16M salary rangeKey Features: Variable packing, custom errors, access control
// Dynamic array operations
function appendToNumbers(uint[] calldata _toAppend) public
function afterY2K() public view returns (uint[] memory, address[] memory)Key Features: Dynamic arrays, filtering, timestamp handling
// Nested mappings for user preferences
mapping(address => mapping(string => bool)) public userFavorites;Key Features: Nested mappings, approval systems, custom errors
struct Car {
string make;
string model;
string color;
uint8 numberOfDoors; // Gas-optimized
}Key Features: Struct optimization, CRUD operations, index validation
// Multiple inheritance patterns
contract EngineeringManager is Salaried, ManagerKey Features: Abstract contracts, virtual/override, multiple inheritance
// External library usage
using SillyStringUtils for string;Key Features: Library imports, struct returns, external dependencies
// Corrected problematic functions
function diffWithNeighbor() // Fixed underflow issues
function applyModifier() // Fixed type conversion
function popWithReturn() // Fixed array boundsKey Features: Bug fixes, type safety, boundary checks
// Factory deployment pattern
function deploy() public returns (address) {
AddressBook newAddressBook = new AddressBook(msg.sender);
return address(newAddressBook);
}Key Features: Factory pattern, Ownable integration, dynamic deployment
// Minimal token with safety checks
function safeTransfer(address _to, uint256 _amount) publicKey Features: Custom token logic, ETH balance validation, claim system
// Token-weighted voting system
function vote(uint256 _issueId, Vote _vote) publicKey Features: ERC-20 compliance, governance, quorum systems
// Unique content NFTs
function mintHaiku(string memory _line1, string memory _line2, string memory _line3)Key Features: ERC-721 compliance, uniqueness validation, sharing system
Each contract has a corresponding deployment script in the script/ directory:
| Contract | Deploy Script | Purpose |
|---|---|---|
| BasicMath | DeployBasicMath.s.sol |
Math operations with safety |
| EmployeeStorage | DeployEmployeeStorage.s.sol |
Storage optimization demo |
| ArraysExercise | DeployArraysExercise.s.sol |
Array manipulation |
| FavoriteRecords | DeployFavoriteRecords.s.sol |
Mapping systems |
| GarageManager | DeployGarageManager.s.sol |
Struct management |
| InheritanceExercise | DeployInheritance.s.sol |
Inheritance patterns |
| ImportsExercise | DeployImportsExercise.s.sol |
Library integration |
| ErrorTriageExercise | DeployErrorTriageExercise.s.sol |
Debugging solutions |
| AddressBookFactory | DeployAddressBookFactory.s.sol |
Factory pattern |
| UnburnableToken | DeployUnburnableToken.s.sol |
Custom token |
| WeightedVoting | DeployWeightedVoting.s.sol |
ERC-20 governance |
| HaikuNFT | DeployHaikuNFT.s.sol |
ERC-721 NFTs |
# Deploy to Base Sepolia testnet
forge script script/DeployWeightedVoting.s.sol \
--rpc-url https://sepolia.base.org \
--private-key $PRIVATE_KEY \
--broadcast \
--verify \
--etherscan-api-key <YOUR-ETHERSCAN-API>forge buildforge test
forge test -vvv # Verbose outputforge fmtforge snapshot# Start local node
anvil
# Deploy to local node
forge script script/DeployBasicMath.s.sol --rpc-url http://localhost:8545 --private-key <anvil_key> --broadcastBaseLearn/
βββ src/ # Smart contracts
β βββ BasicMath.sol
β βββ EmployeeStorage.sol
β βββ ArraysExercise.sol
β βββ FavoriteRecords.sol
β βββ GarageManager.sol
β βββ InheritanceExercise.sol
β βββ ImportsExercise.sol
β βββ SillyStringUtils.sol
β βββ ErrorTriageExercise.sol
β βββ AddressBook.sol
β βββ AddressBookFactory.sol
β βββ UnburnableToken.sol
β βββ WeightedVoting.sol
β βββ HaikuNFT.sol
βββ script/ # Deployment scripts
βββ test/ # Test files
βββ lib/ # Dependencies
βββ README.md # This file
- β Gas Optimization: Variable packing, efficient storage patterns
- β Security: Overflow/underflow protection, access controls
- β Design Patterns: Factory, inheritance, library usage
- β Standards: ERC-20, ERC-721 implementations
- β Error Handling: Custom errors, debugging techniques
- β Advanced Features: Governance systems, uniqueness validation
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source (UNLICENSED) - see individual contract files for details.
Happy Learning! π This repository represents a journey through Solidity development, from basic concepts to advanced patterns. Each contract builds upon previous knowledge and introduces new concepts essential for blockchain development.