Skip to content

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.

Notifications You must be signed in to change notification settings

rionnaldi/BaseLearn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

BaseLearn - Solidity Learning Exercises πŸŽ“

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.

πŸ“š Table of Contents

🎯 Overview

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 new keyword
  • ERC-20 Tokens with voting mechanisms
  • ERC-721 NFTs with unique content validation
  • Custom Token implementations

⚑ Prerequisites

  • Node.js (v16 or higher)
  • Foundry toolkit
  • Git
  • Basic understanding of Solidity and Ethereum

Install Foundry

curl -L https://foundry.paradigm.xyz | bash
foundryup

πŸš€ Quick Start

  1. Clone the repository

    git clone https://github.com/rionnaldi/BaseLearn.git
    cd BaseLearn
  2. Install dependencies

    forge install
  3. Build all contracts

    forge build
  4. Run tests

    forge test
  5. Deploy a contract (example)

    forge script script/DeployBasicMath.s.sol --rpc-url <your_rpc_url> --private-key <your_private_key> --broadcast

πŸ“‹ Exercises & Contracts

1. BasicMath.sol - Math Operations with Safety

// 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


2. EmployeeStorage.sol - Storage Optimization

// Optimized storage with variable packing
uint16 private shares;     // Packed with salary in same slot
uint24 private salary;     // Up to 16M salary range

Key Features: Variable packing, custom errors, access control


3. ArraysExercise.sol - Array Manipulation

// 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


4. FavoriteRecords.sol - Mapping Usage

// Nested mappings for user preferences
mapping(address => mapping(string => bool)) public userFavorites;

Key Features: Nested mappings, approval systems, custom errors


5. GarageManager.sol - Structs & Storage

struct Car {
    string make;
    string model; 
    string color;
    uint8 numberOfDoors;  // Gas-optimized
}

Key Features: Struct optimization, CRUD operations, index validation


6. InheritanceExercise.sol - Advanced Inheritance

// Multiple inheritance patterns
contract EngineeringManager is Salaried, Manager

Key Features: Abstract contracts, virtual/override, multiple inheritance


7. ImportsExercise.sol - Library Integration

// External library usage
using SillyStringUtils for string;

Key Features: Library imports, struct returns, external dependencies


8. ErrorTriageExercise.sol - Debugging & Error Handling

// Corrected problematic functions
function diffWithNeighbor() // Fixed underflow issues
function applyModifier()    // Fixed type conversion
function popWithReturn()    // Fixed array bounds

Key Features: Bug fixes, type safety, boundary checks


9. AddressBook.sol + AddressBookFactory.sol - Factory Pattern

// 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


10. UnburnableToken.sol - Custom Token

// Minimal token with safety checks
function safeTransfer(address _to, uint256 _amount) public

Key Features: Custom token logic, ETH balance validation, claim system


11. WeightedVoting.sol - ERC-20 Governance

// Token-weighted voting system
function vote(uint256 _issueId, Vote _vote) public

Key Features: ERC-20 compliance, governance, quorum systems


12. HaikuNFT.sol - ERC-721 with Validation

// Unique content NFTs
function mintHaiku(string memory _line1, string memory _line2, string memory _line3)

Key Features: ERC-721 compliance, uniqueness validation, sharing system

πŸš€ Deployment Scripts

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

Example Deployment

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

πŸ”§ Development Workflow

Building Contracts

forge build

Running Tests

forge test
forge test -vvv  # Verbose output

Code Formatting

forge fmt

Gas Analysis

forge snapshot

Local Development

# Start local node
anvil

# Deploy to local node
forge script script/DeployBasicMath.s.sol --rpc-url http://localhost:8545 --private-key <anvil_key> --broadcast

πŸ“ Project Structure

BaseLearn/
β”œβ”€β”€ 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

🌟 Key Learning Concepts Covered

  • βœ… 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

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“– Additional Resources

πŸ“„ License

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.

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published