Beginning Solidity - Alexandros Dolgov - E-Book

Beginning Solidity E-Book

Alexandros Dolgov

0,0
42,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Unlock the future of programming on the Ethereum blockchain with Solidity smart contracts

Explore and learn smart contract development on the Ethereum blockchain with Beginning Solidity: Learn to Program Smart Contracts with Solidity by Alexandros Dolgov.

This book is a guide to taking your first steps and becoming comfortable with Solidity programming, providing accessible learning material for existing and aspiring programmers who wish to build decentralised applications on the Ethereum platform. This book provides insights into the creation, compilation and deployment of smart contracts and decentralised applications.

Beginning Solidity demystifies the complexities of the Ethereum blockchain and the Solidity language. From understanding the origins and use of money to basic blockchain concepts such as accounts, transactions, block explorers, wallets and consensus mechanisms, to applications like understanding and creating fungible (ERC-20) and Non-fungible tokens (NFTs) or developing a decentralized auction platform, Alexandros Dolgov covers it all. Through practical examples and real-world scenarios, this book equips you with the knowledge to design, develop, and deploy smart contracts and decentralized apps, positioning you at the forefront of the blockchain revolution. You'll also:

  • Learn Solidity programming through the Foundry framework making Solidity programming incredibly accessible for those with or without prior coding experience
  • Become comfortable with the development of Ethereum smart contracts and the deployment of decentralized applications across various sectors
  • Stay up to date in the rapidly evolving field of blockchain technology with cutting-edge practices and adaptable learning strategies

For both practicing and aspiring programmers and developers eager to explore the possibilities of the Ethereum blockchain and Solidity programming, Beginning Solidity is an essential read. Embark on an exciting journey to become proficient in creating blockchain-based applications that can transform the digital world. Grab your copy today and take the first step towards mastering the future of decentralized technology.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 776

Veröffentlichungsjahr: 2025

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Cover

Table of Contents

Title Page

Introduction

WHAT THIS BOOK COVERS

USING AN AI TOOL

1 What Is Money and a Brief History of It?

WHAT IS MONEY?

THE HISTORY AND EVOLUTION OF MONEY

CRYPTOCURRENCIES

ETHEREUM AND SMART CONTRACTS

DECENTRALIZED AUTONOMOUS ORGANIZATIONS

DECENTRALIZED EXCHANGES

CRYPTOCURRENCIES AND SMART CONTRACTS: CONCLUSION

CHAPTER 1 QUESTIONS

2 An Introduction to Ethereum’s Architecture

BASICS OF ETHEREUM

THE BLOCKCHAIN TRILEMMA

SMART CONTRACTS

THE ETHEREUM VIRTUAL MACHINE

THE ETHER COIN

THE BYZANTINE GENERAL’S PROBLEM AND ETHEREUM’S CONSENSUS MECHANISM

GAS FEES

THE SCALING PROBLEM OF ETHEREUM

LAYER 2 SOLUTIONS

LAYER 3 SOLUTIONS

ETHEREUM: TOWARD FINALIZATION

CHAPTER 2 QUESTIONS

3 Wallets, MetaMask, and Block Explorers

UNDERSTANDING WALLETS

INSTALLING METAMASK

LOGGING IN AGAIN WITH THE SEED PHRASE

CHANGING NETWORKS

ENGAGING WITH FAUCETS

SENDING YOUR FIRST TRANSACTION

BLOCK EXPLORERS

CHAPTER 3 QUESTIONS

4 Remix, Data Types, Visibility, and HelloWorld

WHAT IS PROGRAMMING?

STARTING WITH SOLIDITY, REMIX, AND HELLOWORLD

SPDX-LICENSE-IDENTIFIER

SOLIDITY VERSIONS AND THE PRAGMA LINE

CONTRACT HELLOWORLD {}

DATA TYPES AND VARIABLES IN SOLIDITY

FUNCTION VISIBILITY LEVELS

FUNCTION ANATOMY

VISIBILITY LEVELS

VIEW AND PURE KEYWORDS

HELLOWORLD CONTRACT

CHAPTER 4 QUESTIONS

5 ZooManagement

SETTING UP THE ZooManagement CONTRACT

STRUCTS

ARRAYS

DYNAMIC ARRAYS

FIXED-SIZE ARRAYS

MAPPINGS

CONTRACT IMPORTING

INHERITANCE

DEPLOYING AND RUNNING A CONTRACT FROM ANOTHER CONTRACT

CHAPTER 5 QUESTIONS

6 Installing Microsoft Visual Studio Code and Foundry

WHAT IS MICROSOFT VS CODE?

MICROSOFT VISUAL STUDIO CODE LAYOUT

VS CODE KEYBOARD SHORTCUTS

WORKING ON DIFFERENT FILES AT THE SAME TIME

MIRROR/MINI-MAP

ZEN MODE

VS CODE TERMINAL

INSTALLING FOUNDRY

STARTING A FOUNDRY PROJECT

CHAPTER 6 QUESTIONS

7 Foundry ZooManagement

THE FOUNDRY PROJECT FILES

COMPILING A CONTRACT

INTRODUCTION TO ANVIL

LOCAL SMART CONTRACT DEPLOYMENT

USING SCRIPTING TO DEPLOY A CONTRACT

CONTRACT INTERACTION WITH FOUNDRY

DEPLOYING A SMART CONTRACT TO A TEST NETWORK THROUGH FOUNDRY

CHAPTER 7 QUESTIONS

8 Fundraising Contract

SETTING UP A FUNDRAISING CONTRACT

ORACLES

DEPLOYING THE PRICE FEED CONTRACT THROUGH REMIX

SOLIDITY INTERFACES

CREATING LIBRARIES

WITHDRAW FUNCTION

CONSTRUCTOR

MODIFIERS

TESTING THE CONTRACT BY DEPLOYING IT ON A TEST NETWORK

IMMUTABILITY AND CONSTANTS

CUSTOM ERRORS

RECEIVE AND FALLBACK FUNCTIONS

CHAPTER 8 QUESTIONS

9 Building an ERC-20 Cryptocurrency

INTRODUCTION TO ERC-20

THE PROCESS OF CREATING AN ETHEREUM IMPROVEMENT PROPOSAL

DEPLOY YOUR ERC-20 CRYPTOCURRENCY

CHAPTER 9 QUESTIONS

10 Borrowing and Lending Protocol

WHAT IS A STABLECOIN?

TYPES OF STABLECOINS

CREATING THE STABLECOIN

THE STABLECOIN SKELETON

CHAPTER 10 QUESTIONS

11 Building an ERC-721 Nonfungible Token

WHAT IS AN NFT?

SETTING UP THE NFT PROJECT

ERC-721 CONTRACT BREAKDOWN

INTRODUCTION TO IPFS

CREATING THE ERC-721 CONTRACT

WRITING A DEPLOYMENT SCRIPT AND DEPLOYING ON SEPOLIA

CHAPTER 11 QUESTIONS

12 Upgradable Smart Contracts

INTRODUCING UPGRADABLE CONTRACTS

USING DELEGATECALL

OpenZeppelin UUPS PROXIES

INITIALIZER

DEPLOYING THE PROXY CONTRACT

CHAPTER 12 QUESTIONS

13 Decentralized Autonomous Organizations

WHAT IS A DAO?

EXPLORING THE AAVE PROTOCOL

POTENTIAL VOTING ARCHITECTURES FOR A DAO

THE DAO TOOLKIT

SETTING UP THE DAO PROJECT

ADDING THE RETRIEVABLENUMBER.SOL CONTRACT

VOTING TOKEN CONTRACT

OPENZEPPELIN'S CONTRACTS WIZARD

THE GOVERNANCE CONTRACT

TIMELOCK CONTRACT

CHAPTER 13 QUESTIONS

14 Introduction to Smart Contract Security

THE IMPORTANCE OF SMART CONTRACT SECURITY

SMART CONTRACT AUDITING AND SECURITY BEST PRACTICES

SECURITY TECHNIQUES USED FOR AUDITING

HAVING HIGH CONFIDENCE AND ASSURANCE THAT YOUR SMART CONTRACT IS SAFE

CHAPTER 14 QUESTIONS

15 The First (or One of the First) Stepping Stones

UPLOADING PROJECTS TO GitHub

FINDING A JOB IN THE CRYPTO INDUSTRY

CONTINUING WITH SOLIDITY EDUCATION

Appendix: Answers to Chapter Questions

CHAPTER 1 WHAT IS MONEY AND A BRIEF HISTORY OF IT?

CHAPTER 2 AN INTRODUCTION TO ETHEREUM’S ARCHITECTURE

CHAPTER 3 WALLETS, METAMASK, AND BLOCK EXPLORERS

CHAPTER 4 REMIX, DATA TYPES, VISIBILITY, AND HELLOWORLD

CHAPTER 5 ZOOMANAGEMENT

CHAPTER 6 INSTALLING MICROSOFT VISUAL STUDIO CODE AND FOUNDRY

CHAPTER 7 FOUNDRY ZOOMANAGEMENT

CHAPTER 8 FUNDRAISING CONTRACT

CHAPTER 9 BUILDING AN ERC-20 CRYPTOCURRENCY

CHAPTER 10 BORROWING AND LENDING PROTOCOL

CHAPTER 11 BUILDING AN ERC-721 NONFUNGIBLE TOKEN

CHAPTER 12 UPGRADABLE SMART CONTRACTS

CHAPTER 13 DECENTRALIZED AUTONOMOUS ORGANIZATIONS

CHAPTER 14 INTRODUCTION TO SMART CONTRACT SECURITY

Index

Copyright

Dedication

About the Author

About the Technical Editor

Acknowledgments

End User License Agreement

List of Tables

Chapter 2

TABLE 2.1: Pros and cons of smart contracts

Chapter 6

TABLE 6.1: VS Code shortcuts

List of Illustrations

Chapter 2

FIGURE 2.1: Gas fees demonstration

FIGURE 2.2: Gas price over time

FIGURE 2.3: Sharding example

Chapter 3

FIGURE 3.1: Creating your own seed phrase

FIGURE 3.2: MetaMask browser extension's front end

FIGURE 3.3: MetaMask's logo

FIGURE 3.4: Uninstalling MetaMask's browser extension

FIGURE 3.5: MetaMask importing seed phrase

FIGURE 3.6: MetaMask front-end browser window

FIGURE 3.7: MetaMask's front end

FIGURE 3.8: Changing networks using MetaMask

FIGURE 3.9: Showing MetaMask's test networks

FIGURE 3.10: MetaMask's SepoliaETH testnet

FIGURE 3.11: Ethereum faucet at

sepolia-faucet.pk910.de

FIGURE 3.12: MetaMask's wallet address

FIGURE 3.13: Gitcoin passport message

FIGURE 3.14: Social and professional platform Gitcoin passport confirmation...

FIGURE 3.15: Ethereum faucet loading screen while mining SepoliaETH

FIGURE 3.16: Sending transactions with MetaMask

FIGURE 3.17: Address to which to send the transaction on MetaMask

FIGURE 3.18: Sending SepoliaETH on MetaMask

FIGURE 3.19: Confirmation on MetaMask on sending SepoliaETH

FIGURE 3.20: Transaction summary and last confirmation of the transaction on...

FIGURE 3.21: Transaction is pending the network's confirmation in MetaMask

FIGURE 3.22: Transaction is confirmed by the network on MetaMask

FIGURE 3.23: Transactions in MetaMask

FIGURE 3.24: Viewing transactions on the block explorer in MetaMask

FIGURE 3.25: MetaMask's transaction viewed on block explorer

FIGURE 3.26: Transaction on Ethereum's main network

FIGURE 3.27: A block of transactions on Ethereum's mainnet

FIGURE 3.28: Contract composability example

FIGURE 3.29: Uniswap protocol landing page

FIGURE 3.30: Uniswap protocol landing page of decentralized application

FIGURE 3.31: Uniswap connecting MetaMask wallet

FIGURE 3.32: Uniswap wallet connected and balance shown

FIGURE 3.33: Uniswap swapping token availability screen

FIGURE 3.34: Uniswap changing blockchain screen

FIGURE 3.35: Uniswap value swap overview

FIGURE 3.36: Uniswap swap confirmation

FIGURE 3.37: Confirm swap loading screen

FIGURE 3.38: Uniswap swap overview on MetaMask

FIGURE 3.39: Uniswap swap submission screen

FIGURE 3.40: Uniswap swap confirmation

FIGURE 3.41: Uniswap UNI swap back to ETH overview

FIGURE 3.42: ETH address block explorer

FIGURE 3.43: ETH ERC-20 tokens

FIGURE 3.44: ETH ERC-20 tokens part 2

FIGURE 3.45: Block explorer NFT tokens

FIGURE 3.46: On-chain transactions

FIGURE 3.47: Internal transactions

FIGURE 3.48: Token transfers

FIGURE 3.49: NFT Transfers

FIGURE 3.50: Produced blocks

FIGURE 3.51: Smart contract address

FIGURE 3.52: Smart contract code

FIGURE 3.53: Smart contract code #2

FIGURE 3.54: Read contract

FIGURE 3.55: Write contract

FIGURE 3.56: Read As Proxy

FIGURE 3.57: Write As Proxy

Chapter 4

FIGURE 4.1: Remix interface

FIGURE 4.2: Remix interface deleting files

FIGURE 4.3: Remix interface creating a new file

FIGURE 4.4: Remix interface naming a new file

FIGURE 4.5: Remix workspaces with a newly created file

FIGURE 4.6: Remix working on a new file

FIGURE 4.7:

pragma solidity

version

FIGURE 4.8: Contract with opening and closing brackets

FIGURE 4.9: MetaMask Ethereum address illustration

FIGURE 4.10: Showcasing

myname1

as string and

myname2

as bytes

FIGURE 4.11: Showcasing

myname1

FIGURE 4.12: Showcasing

myname2

FIGURE 4.13: Deploying HelloWorld #1

FIGURE 4.14: Contract deployed

FIGURE 4.15:

HelloWorld

#1 reflected

Chapter 5

FIGURE 5.1: Solidity compiler icon

FIGURE 5.2: Solidity version error

FIGURE 5.3: Solidity version error solved

FIGURE 5.4: Solidity version

FIGURE 5.5: Solidity version list #1

FIGURE 5.6: Previous Solidity version 0.8.9 chosen

FIGURE 5.7: Deploy & Run Transactions tab

FIGURE 5.8:

ZooManagement

contract result

FIGURE 5.9:

ZooManagement

contract, update visitors

FIGURE 5.10: Visual explanation of a struct

FIGURE 5.11:

ZooManagement

contract result #2

FIGURE 5.12:

ZooManagement

contract animals

FIGURE 5.13:

ZooManagement

contract result #3

FIGURE 5.14:

ZooManagement AddAnimal

FIGURE 5.15:

ZooManagement listofAnimals

FIGURE 5.16:

ZooManagement

mappings

FIGURE 5.17:

ContractInheritance.sol

creation

FIGURE 5.18:

ZooManagement

name demonstration

FIGURE 5.19: SampleFolder with

ZooManagement.sol

added

FIGURE 5.20: Error on

ContractInheritance

FIGURE 5.21:

ContractInheritance.sol

importation

FIGURE 5.22: Changing the contract path

FIGURE 5.23:

ContractInheritance.sol

incorrect path error

FIGURE 5.24:

ContractInheritance.sol

new path importation

FIGURE 5.25:

ContractInheritance.sol

changing paths

FIGURE 5.26:

CreateNumber addNumber

FIGURE 5.27:

AddSubtractionToAddition AddNumber

FIGURE 5.28:

AdditionContract AddNumber

FIGURE 5.29:

Contract3 defineNumber

and

showNumber

FIGURE 5.30: Visualization of the different types of Inheritance contracts

FIGURE 5.31:

ZooManagementFactory

FIGURE 5.32:

ZooManagementFactory DeployZooManagement

FIGURE 5.33:

ZooManagementFactory zooManagement

#1

FIGURE 5.34:

ZooManagementFactory zooManagement

#2

FIGURE 5.35:

ZooManagementFactory

array #1

FIGURE 5.36: ZooManagementFactory array #2

FIGURE 5.37: ZooManagementFactory array #3

FIGURE 5.38:

ZooManagementFactory

array #4

FIGURE 5.39:

ZooManagementFactory

final deployment

Chapter 6

FIGURE 6.1: VS Code website

FIGURE 6.2: The VS Code Insiders Edition link

FIGURE 6.3: VS Code's Welcome page

FIGURE 6.4: Explorer pane

FIGURE 6.5: Exploring the Explorer pane

FIGURE 6.6: Magnifying glass icon and Search pane

FIGURE 6.7: Including and excluding certain files

FIGURE 6.8: Searching for the Night Owl theme

FIGURE 6.9: Night Owl extension window

FIGURE 6.10: Installed Night Owl theme

FIGURE 6.11: Polacode

FIGURE 6.12: Bookmarks

FIGURE 6.13: Cloak

FIGURE 6.14: Solidity

FIGURE 6.15: Even Better TOML

FIGURE 6.16: GitHub Copilot

FIGURE 6.17: VS Code shortcuts

FIGURE 6.18: VS Code File: Save shortcut

FIGURE 6.19: VS Code changing the File: Save shortcut

FIGURE 6.20: Two files,

Example1.sol

and

Example2.sol

, open at the same time...

FIGURE 6.21: Dragging

Example2.sol

into

Example1.sol

FIGURE 6.22: Split screen with

Example2.sol

at the top and

Example1.sol

at t...

FIGURE 6.23: Split screen with

Example1.sol

and

Example2.sol

beside each oth...

FIGURE 6.24: Split screen with

Example1.sol

,

Example2.sol

, and

Example3.sol

...

FIGURE 6.25: Split screen with

Example1.sol

atop

Example3.sol

and both side-...

FIGURE 6.26: Split screen with

Example1.sol

,

Example2.sol

,

Example3.sol

, and...

FIGURE 6.27: Mini-map

FIGURE 6.28: Command palette

FIGURE 6.29: Zen Mode on

FIGURE 6.30: VS Code terminal

FIGURE 6.31: Split Terminal button

FIGURE 6.32: Two split terminals

FIGURE 6.33: Closing the split screen

FIGURE 6.34: Maximize Panel Size button

FIGURE 6.35: Maximized panel demonstration

FIGURE 6.36: Landing page

getfoundry.sh

FIGURE 6.37: Script copy pasted into the terminal

FIGURE 6.38: libusb missing

FIGURE 6.39: Homebrew landing page

FIGURE 6.40: Homebrew link pasted into terminal

FIGURE 6.41: Terminal asking for password

FIGURE 6.42: Terminal showcasing the installation and new directories/folder...

FIGURE 6.43: Homebrew downloading

FIGURE 6.44: Running scripts to add Homebrew to path

FIGURE 6.45: Foundry being downloaded in the terminal

FIGURE 6.46: Installing Foundry

FIGURE 6.47:

forge --version

command

FIGURE 6.48: VS Code terminal

FIGURE 6.49: Typing

ls

in the terminal

FIGURE 6.50: File locations

FIGURE 6.51: The

beginning-solidity

folder has been created.

FIGURE 6.52: Using the

cd

command to enter the

beginning-solidity

folder

FIGURE 6.53: Opening the File menu

FIGURE 6.54: Clicking Open Folder

FIGURE 6.55: Clicking the

beginning-solidity

folder

FIGURE 6.56: Clicking the

ZooManagement-Foundry

folder and the blue Open but...

FIGURE 6.57: “Do you trust the authors?” window

FIGURE 6.58:

forge init:

example

FIGURE 6.59: Foundry project created

FIGURE 6.60:

ZooManagement-Foundry

folder after

forge init

installation

Chapter 7

FIGURE 7.1:

ZooManagement-Foundry

project overview

FIGURE 7.2:

Counter.sol

contract

FIGURE 7.3:

forge build

FIGURE 7.4:

forge compile

FIGURE 7.5: Compiler run successful!

FIGURE 7.6:

Counter.sol

without closing curly brace

FIGURE 7.7: Compilation failed

FIGURE 7.8: Compilation is successful again

FIGURE 7.9: Anvil terminal

FIGURE 7.10: Anvil running

FIGURE 7.11: First part of the process of adding Anvil to MetaMask

FIGURE 7.12: Second part of the process of adding Anvil to MetaMask

FIGURE 7.13: First part of the process of adding a private key of an Anvil a...

FIGURE 7.14: Second part of the process of adding a private key to an Anvil ...

FIGURE 7.15: Adding a new terminal

FIGURE 7.16: The two terminals

FIGURE 7.17:

Counter.sol

FIGURE 7.18: Entering private key

FIGURE 7.19: Private keys and accounts

FIGURE 7.20: Contract deployed

FIGURE 7.21:

ZooManagement

contract

FIGURE 7.22:

ZooManagement.s.sol

FIGURE 7.23: Deployed contract with script

FIGURE 7.24: Contract deployed

FIGURE 7.25:

cast wallet import wallet1 ––interactive

in termina...

FIGURE 7.26: Enter private key

FIGURE 7.27: Having pasted the private key

FIGURE 7.28: Entering password

FIGURE 7.29: Do not store the keystore address in a comment in the contract ...

FIGURE 7.30: Deployed contract address

FIGURE 7.31: Contract address

FIGURE 7.32:

updateVisitorCount

function

FIGURE 7.33: Keystore password

FIGURE 7.34: Transaction executed

FIGURE 7.35:

updateVisitorCount

on Remix

FIGURE 7.36:

getTotalVisitors

on Remix

FIGURE 7.37:

TotalVisitors HexNumber

FIGURE 7.38: Alchemy sign-in landing page

FIGURE 7.39: Alchemy: create new app

FIGURE 7.40: Blockchain options

FIGURE 7.41: Service selection

FIGURE 7.42: Beginning Solidity test deployment landing page

FIGURE 7.43: Blockchain networks

FIGURE 7.44: Sepolia Testnet ETH

FIGURE 7.45: Account Details QR code

FIGURE 7.46: Hold to reveal the key button.

FIGURE 7.47: Private key

FIGURE 7.48: Storing MetaMask's private key

FIGURE 7.49: Keystore Password ETHSepolia

FIGURE 7.50: Smart contract deployed on test network

FIGURE 7.51: Contract deployment transaction hash

FIGURE 7.52: Etherscan contract transaction hash pasted

FIGURE 7.53: Transaction of creation of the testnet smart contract

FIGURE 7.54: To address

FIGURE 7.55:

ZooManagement

contract on Sepolia test network

Chapter 8

FIGURE 8.1: The result of typing

ls

FIGURE 8.2: The result of typing

ls

for the second time

FIGURE 8.3: The result of typing

ls

for a third time

FIGURE 8.4: Typing

forge init

to install a new Foundry project

FIGURE 8.5: Fundraising-contract pane

FIGURE 8.6: Creating

Fundraising.sol

FIGURE 8.7:

https://data.chain.link

landing page

FIGURE 8.8: Price feeds list

FIGURE 8.9: Oracle answer in ETH price in USD

FIGURE 8.10: Market statistics, product information, and addresses

FIGURE 8.11: ETH/USD data feed Chainlink nodes

FIGURE 8.12: ETH/USD data feed Chainlink nodes list

FIGURE 8.13: Landing page of

https://docs.chain.link

FIGURE 8.14: Chainlink documentation: going to the Price Feed Addresses page...

FIGURE 8.15: Price feeds availability in different networks

FIGURE 8.16: List of price feeds on the Sepolia testnet

FIGURE 8.17: Chainlink documentation: going to the Getting Started page

FIGURE 8.18: Sample contract example

FIGURE 8.19: BTC/USD price feed

FIGURE 8.20: BTC/USD price feed web page

FIGURE 8.21: Deploying and running transactions on Remix

FIGURE 8.22: Changing environment and deploying the sample contract

FIGURE 8.23: Contract deployment confirmation on MetaMask

FIGURE 8.24: The contract deployed

FIGURE 8.25: Price of BTC

FIGURE 8.26: ETH/USD price feed

FIGURE 8.27: Chainlink GitHub

FIGURE 8.28: Downloading Chainlink brownie contracts

FIGURE 8.29: Contracts being downloaded

FIGURE 8.30: Path of

AggregatorV3Interface

FIGURE 8.31: Import of

AggregatorV3Interface

FIGURE 8.32: Library creation

FIGURE 8.33:

Fundraiser.sol

contract status

FIGURE 8.34:

Library.sol

library status

FIGURE 8.35: Defining a constructor

FIGURE 8.36: Compilation successful

FIGURE 8.37:

Fundraising.sol

and

ETHtoUSDConverter.sol

in Remix

FIGURE 8.38:

ETHtoUSDConverter.sol

library

FIGURE 8.39:

Fundraising.sol

contract

FIGURE 8.40: Changed import path

FIGURE 8.41: Demonstration of correct Environment and Deploy

FIGURE 8.42: Contract functions

FIGURE 8.43: Value: 10000000000000000 wei

FIGURE 8.44: MetaMask confirmation

FIGURE 8.45: 0.01 ETH in the contract address

FIGURE 8.46: MetaMask address to be copied

FIGURE 8.47: Lifetime wei sent by address

FIGURE 8.48: Lifetime wei sent by address doubled

FIGURE 8.49: Sepolia address balance before withdrawal

FIGURE 8.50: MetaMask confirmation

FIGURE 8.51: Contract balance and amount sent of address

FIGURE 8.52: MetaMask address balance after withdrawal

FIGURE 8.53: Transaction of the 0.02 ETH withdrawal

FIGURE 8.54: Smart contract address and transactions

FIGURE 8.55: Remix VM Cancun

FIGURE 8.56: Successful deployment of contract message

FIGURE 8.57: Gas costs of deploying the contract in its current state

FIGURE 8.58: Gas costs with

constant

keyword

FIGURE 8.59: Current gas costs in Gwei via Etherscan

FIGURE 8.60: Gwei into wei conversion

FIGURE 8.61: Gas price without the

constant

keyword

FIGURE 8.62: Gas price with

constant

keyword

FIGURE 8.63: Gas costs with immutable keyword

FIGURE 8.64: Gas price with

constant

and

immutable

together

FIGURE 8.65:

error NotTheOwner();

example

Chapter 9

FIGURE 9.1: ERC-20 EIP example #1

FIGURE 9.2: ERC-20 EIP example #2

FIGURE 9.3: ERC-20 EIP example #3

FIGURE 9.4: OpenZeppelin landing page

FIGURE 9.5: Contracts Library on OpenZeppelin

FIGURE 9.6: Choosing the programming language

FIGURE 9.7: OpenZeppelin documentation

FIGURE 9.8: Installing OpenZeppelin contracts:

ls

command

FIGURE 9.9: Installing OpenZeppelin contracts:

mkdir beginning-solidity-toke

...

FIGURE 9.10: Installing OpenZeppelin contracts: second ls command

FIGURE 9.11: Installing OpenZeppelin contracts:

cd beginning-solidity-token

...

FIGURE 9.12: Installing OpenZeppelin contracts:

forge init

command

FIGURE 9.13: Opening the

beginning-solidity-token

folder

FIGURE 9.14: “Yes, I trust the authors” pop-up

FIGURE 9.15: Deleting

Counter.s.sol

FIGURE 9.16: OpenZeppelin smart contracts version

FIGURE 9.17: OpenZeppelin smart contracts installed

FIGURE 9.18: File path to

ERC20.sol

FIGURE 9.19:

ERC20.sol

FIGURE 9.20: ERC-20 constructor

FIGURE 9.21:

forge build

for contract

FIGURE 9.22: Contract compilation successful

FIGURE 9.23:

BeginningSolidityToken.s.sol

FIGURE 9.24: Anvil launched

FIGURE 9.25: Anvil private keys

FIGURE 9.26: Adding a password to protect private key

FIGURE 9.27: Private key successfully stored

FIGURE 9.28: Keystore password before deployment

FIGURE 9.29: Contract deployment on Anvil successful

Chapter 10

FIGURE 10.1: The combined value of all fiat-backed stablecoins worldwide

FIGURE 10.2: The three biggest USD-backed stablecoins—Tether (USDT), USD Coin...

FIGURE 10.3: Market capitalization of only USD-pegged stablecoins

FIGURE 10.4: The combined value of all fiat-backed stablecoins worldwide minu...

FIGURE 10.5: Price fluctuations of USDT when compared to the USD from 2015 to...

FIGURE 10.6: Stablecoin logos: USDT, USDC, BUSD, GUSD, TUSD, USDP

FIGURE 10.7: Price fluctuations of DAI when compared to the USD from 2020 to ...

FIGURE 10.8: Additional stablecoin logos: DAI, LUSD, sUSD, GHO, RAI, MIM

FIGURE 10.9: Price fluctuations of Ampleforth when compared to the USD from 2...

FIGURE 10.10: Stablecoin visualization

FIGURE 10.11: Fundraising contract directory

FIGURE 10.12:

beginning-solidity

directory

FIGURE 10.13:

ls

after creating directory

FIGURE 10.14: Opening a folder

FIGURE 10.15:

beginning-solidity

and

beginning-solidity-stablecoin

folders

FIGURE 10.16:

Stablecoin.sol

FIGURE 10.17: Burn function of

ERC20Burnable.sol

FIGURE 10.18: Health factor equation

Chapter 11

FIGURE 11.1:

beginning-solidity-NFT-ERC721

folder created and entered

FIGURE 11.2: Opening the

beginning-solidity-NFT-ERC721

folder

FIGURE 11.3: Yes, I trust the authors!

FIGURE 11.4: Foundry project installation pending

FIGURE 11.5:

foundry init

installed

FIGURE 11.6: Files in the pane after

forge init

installation

FIGURE 11.7: GitHub OpenZeppelin

FIGURE 11.8: Downloading OpenZeppelin contracts from GitHub

FIGURE 11.9: Installed smart contracts

FIGURE 11.10:

lib

and

openzeppelin-contracts

folders

FIGURE 11.11: Path to

ERC721.sol

FIGURE 11.12:

AddNumber

contract

FIGURE 11.13: Function being reverted error message

FIGURE 11.14:

AddNumber

contract with unchecked keyword

FIGURE 11.15: uint8 at 255

FIGURE 11.16: Clicking on unchecked

addnumber

will revert the 255 to 0

FIGURE 11.17: IPFS landing page

FIGURE 11.18: “Get Started” section on IPFS landing page

FIGURE 11.19: IPFS install guide

FIGURE 11.20: IPFS downloads page clickable text

FIGURE 11.21: The EXE file one has to download and run to start downloading ...

FIGURE 11.22: The DMG file has to download and run in order to start downloa...

FIGURE 11.23: IPFS client

FIGURE 11.24: The block representing IPFS running in the background

FIGURE 11.25: The choices available upon clicking the IPFS block

FIGURE 11.26: Install the IPFS browser extension

FIGURE 11.27: Availability of browsers to download IPFS browser extension

FIGURE 11.28: Firefox IPFS extension download landing page

FIGURE 11.29: Chrome Web Store IPFS Extension Download Landing page

FIGURE 11.30: Potential pop-up warning

FIGURE 11.31: Puzzle icon for extensions

FIGURE 11.32: Pinning the IPFS extension

FIGURE 11.33: IPFS pinned

FIGURE 11.34: Browser Extension's pop-up

FIGURE 11.35: Importing a file

FIGURE 11.36: Importing a file (continued)

FIGURE 11.37: IPFS application landing page

FIGURE 11.38: IPFS Files section

FIGURE 11.39: Import menu

FIGURE 11.40: File

Girl.webp

uploaded

FIGURE 11.41: File

Girl.webp

revealed

FIGURE 11.42: Table after clicking More

FIGURE 11.43: CID pasted

FIGURE 11.44: Protocol added

FIGURE 11.45: Opening the IPFS Desktop

FIGURE 11.46: Girl picture demonstration appears on browser through IPFS

FIGURE 11.47: Using the browser to access an IPFS file through a gateway

FIGURE 11.48: Accessing the file through an IPFS gateway

FIGURE 11.49: OpenSea landing page

FIGURE 11.50: Mutant Ape Yacht Club Landing Page

FIGURE 11.51: Token Details of one of the Apes on OpenSea

FIGURE 11.52: Mutant Ape Yacht Club contract on Etherscan

FIGURE 11.53: Contract tab

FIGURE 11.54: Read contract

FIGURE 11.55:

tokenURI

function

FIGURE 11.56: Token ID

FIGURE 11.57: String link to token information

FIGURE 11.58: Token information

FIGURE 11.59: Ape picture on IPFS

FIGURE 11.60:

NFT.sol

contract

FIGURE 11.61:

Girl.png

in the picture folder

FIGURE 11.62:

NFT.s.sol

file creation

FIGURE 11.63: Cyfrin DevOps installed

FIGURE 11.64:

Foundry-DevOps

in Library

FIGURE 11.65:

ContractForInteractions.s.sol

file creation

FIGURE 11.66: Private key pasting

FIGURE 11.67: MetaMask account arrow

FIGURE 11.68: MetaMask account information

FIGURE 11.69: MetaMask account details

FIGURE 11.70: MetaMask showing private key

FIGURE 11.71: MetaMask entering your password

FIGURE 11.72: MetaMask Hold to reveal password

FIGURE 11.73: MetaMask private key displayed

FIGURE 11.74: MetaMask entering private key

FIGURE 11.75: MetaMask entering password

FIGURE 11.76: Keystore address

FIGURE 11.77: Contract deployment keystore password request

FIGURE 11.78: Contract successfully deployed

FIGURE 11.79: Interactions contract successfully deployed

FIGURE 11.80: Hash of contract deployment transaction

FIGURE 11.81: Adding hash of transaction to Sepolia Etherscan

FIGURE 11.82: Deployed contract address on Sepolia Etherscan

FIGURE 11.83: Copying address of deployed contract

FIGURE 11.84: NFT section of MetaMask wallet

FIGURE 11.85: Importing NFT pane

FIGURE 11.86: NFT imported

Chapter 12

FIGURE 12.1: Typing the name of a project on CertiK and clicking it

FIGURE 12.2: General security score of Uniswap on CertiK

FIGURE 12.3: CertiK code overview

FIGURE 12.4: Issues in code report by Cyfrin

FIGURE 12.5: CertiK project fundamentals

FIGURE 12.6: CertiK operational information

FIGURE 12.7: CertiK governance information

FIGURE 12.8: CertiK market information

FIGURE 12.9: CertiK community overview

FIGURE 12.10: Proxy and logic contract example

FIGURE 12.11: Proxy and logic contracts diamond implementation example 1

FIGURE 12.12: Proxy and logic contracts diamond implementation example 2

FIGURE 12.13: Proxy and logic contracts diamond implementation example 3

FIGURE 12.14:

SoliditybyExample.sol

FIGURE 12.15: Copy-pasting the code of

solidity-by-example.org

FIGURE 12.16: Deploying Contract B

FIGURE 12.17:

setVars

to 255 in contract B

FIGURE 12.18: Result of setting

setVars

in contract B to 255

FIGURE 12.19: Remix's pseudo-wallet address

FIGURE 12.20: Calling

setVars

from contract A by referencing contract B's ad...

FIGURE 12.21: Copying the contract address of contract B

FIGURE 12.22: Result of calling

setVars

from contract A by referencing the a...

FIGURE 12.23: List (

ls

) command #1

FIGURE 12.24: List (

ls

) command #2

FIGURE 12.25:

forge init

FIGURE 12.26: Opening

beginning-solidity-proxy-contracts

#1

FIGURE 12.27: Opening

beginning-solidity-proxy-contracts

#2

FIGURE 12.28: Zoo1 and Zoo2

FIGURE 12.29: GitHub

OpenZeppelin-contracts-upgradable

FIGURE 12.30: Script to download OpenZeppelin GitHub contracts

FIGURE 12.31: Message upon successful completion of downloading the GitHub

O

...

FIGURE 12.32: Abstract keyword

FIGURE 12.33:

UUPSupgradeable.sol

path

FIGURE 12.34:

UUPSUpgradeable.sol

contract

FIGURE 13.35:

upgradeToAndCall

function for

UUPSUpgradeable.sol

FIGURE 12.36:

_authorizeUpgrade

function

FIGURE 12.37:

Initializable.sol

file path

FIGURE 12.38:

Initializable.sol

contract

FIGURE 12.39:

_disableInitializers

function

FIGURE 12.40: Successfully installed package with

ERC1967Proxy

contract

FIGURE 12.41:

ERC1967Proxy

contract constructor

FIGURE 12.42:

forge build

successful

Chapter 13

FIGURE 13.1: Approximate representation of the governance of a traditional pu...

FIGURE 13.2: CoinCarp UNI token distribution

FIGURE 13.3: Aave landing page

FIGURE 13.4: Aave Ethereum Market landing page

FIGURE 13.5: Connecting MetaMask to Aave

FIGURE 13.6: Aave Ethereum Market landing page with wallet connected

FIGURE 13.7: Aave Governance forum

FIGURE 13.8: Aave proposal example

FIGURE 13.9: Aave proposal lifecycle

FIGURE 13.10: Aave forum for DAO proposal discussion

FIGURE 13.11: Example of discussions on Aave's forum

FIGURE 13.12: Snapshot landing page

FIGURE 13.13: Connecting to Snapshot through MetaMask

FIGURE 13.14: Aave Snapshot mock proposal landing page

FIGURE 13.15: Aave mock proposal example

FIGURE 13.16: Directory location

FIGURE 13.17:

beginning-solidity-DAO

directory creation

FIGURE 13.18: Migrating to the DAO folder created

FIGURE 13.19: File options

FIGURE 13.20: Choosing the DAO folder to open

FIGURE 13.21: Installing a new project

FIGURE 13.22: Deleting pre-installed contracts

FIGURE 13.23: Creation of

RetrievableNumber.sol

FIGURE 13.24: OpenZeppelin GitHub contracts

FIGURE 13.25: Contract package being installed

FIGURE 13.26: Contract package installed

FIGURE 13.27:

Ownable.sol

path

FIGURE 13.28: VotingToken file creation

FIGURE 13.29: The Contracts Wizard

FIGURE 13.30: Ticker example

FIGURE 13.31: OpenZeppelin's Governance contract generator settings

FIGURE 13.32: OpenZeppelin's Governance contract generator settings part 2

FIGURE 13.33: Copying the

DAOGovernanceContract

FIGURE 13.34: Creating a

DAOGovernanceContract.sol

in VS Code

FIGURE 13.35: Copied

DAOGovernanceContract

into the

DAOGovernanceContract.sol

FIGURE 13.36:

TimelockContract.sol

contract created

Chapter 14

FIGURE 14.1: Total value in hacks

FIGURE 14.2: Cover of Uniswap V3 audit report by Cyfrin

FIGURE 14.3: Executive summary of report with issues found and their severit...

FIGURE 14.4: Each issue found reported in detail and its status

FIGURE 14.5: The report of each issue with description of the issue, the imp...

Chapter 15

FIGURE 15.1: GitHub profile

FIGURE 15.2: GitHub overview

FIGURE 15.3: New GitHub page

FIGURE 15.4: Naming the repository

FIGURE 15.5: Creating a repository

FIGURE 15.6: Instructions to add the project to the GitHub repository

FIGURE 15.7: Project added to the GitHub repository

Appendix

FIGURE A.1 Shiba Inu token holdings at the zero address and their value in U...

FIGURE A.2 Number of tokens Vitalik sent (and their value in USD, at the tim...

FIGURE A.3 Block height and block confirmations, at the time of writing

Guide

Cover

Table of Contents

Title Page

Copyright

Dedication

About the Author

About the Author

Acknowledgments

Introduction

Begin Reading

Answers to Chapter Questions

Index

End User License Agreement

Pages

v

xix

xx

xxi

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

vi

vii

viii

ix

x

555

BEGINNING Solidity

LEARN TO PROGRAM SMART CONTRACTS WITH SOLIDITY

 

 

Alexandros Dolgov

 

 

 

 

 

 

 

 

INTRODUCTION

AT ITS CORE, THIS BOOK IS ABOUT LEARNING Solidity programming, specifically on the Ethereum blockchain, using the Foundry development framework. The goal is to create a resource for seasoned, aspiring, and beginning programmers to make their first steps in mastering the Solidity programming language. Foundry as a framework was selected over Hardhat and Truffle because Foundry is a straightforward framework when compared to those development frameworks. It is written in Rust, and it excels in speed, performance, and being lightweight. Additionally, when using Hardhat and Truffle, you need to be familiar with another programming language, JavaScript, to run tests and deployments. In Foundry, all this is done only through Solidity and cheat codes. The Foundry does not need any additional setup or plugins to work.

Foundry comes with its own native set of tools, such as Anvil, which is Foundry's own local Ethereum development node that helps simulate Ethereum's blockchain environment for testing and debugging. Another tool native to Foundry is Cast, which is a tool that uses the command line to interact with the Ethereum network and allows its command to interact with the different smart contracts that have been deployed on Ethereum's network. Additional interactions using Cast include sending transactions and retrieving blockchain data. The final component in Foundry's arsenal of tools is called the Forge. Forge is Foundry's smart contract development tool, which allows for smart contract compilation, testing, and deployment in Solidity without the need to use JavaScript or any other language.

WHAT THIS BOOK COVERS

When planning and writing this book, my aim was to go beyond simply teaching Solidity in isolation. I wanted to ensure you will gain an understanding of the broader context, including the implications, potential, and history of this technology. To do that, I begin the book by covering topics that, while not directly focused on Solidity, are crucial for building a well-rounded understanding of Ethereum and the underlying technology. Later chapters cover specific tools and techniques you need to know to begin to master Solidity programming.

The first chapter briefly discusses the history of money and how different forms of money have been perceived and used by the cultures that gave birth to each specific type of money. It provides brief overviews from barter and primitive money to today's cryptocurrencies while giving different use case examples of Ethereum's technology.

The second chapter goes briefly through Ethereum's architecture without doing a deep dive into it. It covers the basics of Ethereum's architecture, how it works, and why some decisions about its architecture were taken in the way they were taken. It also explores Ethereum's future developments to help you stay up-to-date with the state of Ethereum and with the planned future developments of the technology.

The third chapter is an introduction and tutorial on installing and using MetaMask, one of the most well-known wallets in the cryptocurrency industry. It covers using Ethereum faucets to get testnet ether—a fake version of Ethereum's cryptocurrency used for testing—and how to use block explorers to view and analyze transactions that happen on the Ethereum blockchain. This chapter also explains how developers can understand the smart contracts they interact with and interpret the information provided by block explorers. It also includes tips on avoiding being scammed by malicious parties and an overview of the different wallets currently in cryptocurrency. This chapter gives developers an understanding of the differences, advantages, and disadvantages of each type of wallet.

The fourth chapter of the book introduces Remix, a native online—and now available offline as well—Ethereum integrated development environment (IDE) that developers and aspiring developers can use to quickly test their smart contracts or take their first steps in Solidity programming. Within the fourth chapter, you will build your first simple smart contract.

In the fifth chapter, while still using and exploring new features the Remix IDE has to offer, you will start building more complex smart contracts, in this case, a zoo management contract that manages the animals of a small zoo as well as the number of visitors to the zoo.

The sixth chapter covers installing a professional IDE called Visual Studio Code (VS Code), which existing programmers know, but aspiring Solidity programmers might not. It also covers installing the Foundry framework to VS Code itself.

The seventh chapter of the book goes over the zoo management contract again, this time in a VS Code and Foundry environment. The main aim is to go through the Foundry framework and showcase its tools, how to use the Foundry tools, how to compile the contract within the framework, and how to deploy the contract both on Anvil's local integrated blockchain that comes with Foundry and on the Ethereum blockchain. It also covers how to manage your private keys when using Foundry and working on smart contracts.

The eighth chapter teaches you how to create a new smart contract. This time, instead of using Remix, you use VS Code and Foundry. The smart contract is a fundraising contract that allows someone to send money to the contract and for the owner of the contract to withdraw the money. The chapter also introduces blockchain oracles, a tool that allows blockchains to communicate data to and receive data from the real world, such as the prices of different currencies and assets.

The ninth chapter explains what an ERC-20 cryptocurrency is, its characteristics, and what ERC itself stands for. It also explains how proposals to change something in Ethereum are created, what they consist of, and, finally, how you can, step-by-step, create an ERC-20 cryptocurrency on your own.

The tenth chapter explains what stablecoins are, the types of stablecoins, how they work, and the mechanisms each uses to keep its price relatively stable. Finally, it explains how to create a stablecoin and a protocol step-by-step that allows the protocol user to borrow that stablecoin.

In the eleventh chapter, you will learn what nonfungible tokens (NFTs) are and how to create one. The chapter also introduces the Interplanetary File System database, a way of storing files worldwide in a decentralized manner to ensure censorship resistance for the NFT collection a developer might create.

The twelfth chapter explains upgradeable smart contracts—ways of upgrading and updating already developed and deployed smart contracts—and teaches you how to implement them.

The thirteenth chapter explains decentralized autonomous organizations (DAOs) in the context of blockchain and cryptocurrencies and teaches you how to create a DAO.

The fourteenth chapter discusses methods of keeping a smart contract secure, explains what a smart contract audit is, and details the process for conducting one.

USING AN AI TOOL

While reading the book, you can benefit from buying a subscription to an AI tool such as ChatGPT, Claude, or other tool to ask questions, request additional information, and enhance your learning. Questions and requests you might ask in AI prompts include but are not limited to asking the AI to break down complex concepts from ERC-20 to ERC-721 to DAOs, upgradable contracts, and others. In addition, you can ask the AI tool to provide analogies for complicated concepts to make it easier to understand something, which can be especially useful for complete beginners to programming, Solidity, and blockchain. Moreover, you can copy-paste into an LLM error messages and the code of a contract from this book and ask it to identify problems and how to fix them. Asking for clarification is crucial for learning and finding solutions.

Every book has limits in terms of scope, and this is no different. In addition, it is impossible to work through all the smart contracts imported and inherited through the different libraries you learn about in this book. A beyond-the-book exercise you can do is to ask the AI to explain snippets of code that are not examined in the book to enhance your understanding and deepen your knowledge and skill of not only writing code but also reading it and comprehending what a piece of code likely does.

Another way of using an AI companion is to ask it to create additional questions based on the book's material or something closely related to it and answer the questions to ensure a deep understanding of the material. Finally, the best way of using AI is not in the way that I am suggesting you use it—although this has its uses, too—but in how you come up with using it to enhance your learning. Think and be creative with how you use AI; try your ideas with prompts and see how effective they are.

To conclude this introduction, it is crucial to point out another limitation of books: they are usually slower to come up with new editions and updates than the industry and subject matter they delve into, making them a bit out-of-date after a couple of years. To minimize this as much as possible, it could be useful to go through the documentation of the tools and frameworks used in this book such as Foundry and Solidity itself to ensure that you stay up-to-date with any updates that might render some parts of the codebase in this book obsolete.

Foundry documentation link

:

https://book.getfoundry.sh

Solidity documentation link

:

https://docs.soliditylang.org/en/latest

Finally, on this book's website you'll find all the code available for download so you can work and experiment with the live code. The files are at:

https://www.wiley.com/en-us/Beginning+Solidity%3A+Learn+to+Program+Smart+Contracts+with+Solidity-p-9781394290611

1What Is Money and a Brief History of It?

This chapter explores the fundamental concept of money and its evolution through human history, providing essential context for understanding blockchain-based financial systems and smart contract development. By examining how money functions as both a technology and a language for value exchange, you'll gain insight into why decentralized systems like Bitcoin and Ethereum represent the next evolution in monetary and contractual relationships.

The chapter addresses key questions: What makes something valuable enough to serve as money? How have societies historically solved the coordination problems of value exchange? How do cryptocurrencies and smart contracts build upon these historical patterns while introducing new capabilities?

Through detailed analysis of primitive money systems, modern banking, and emerging crypto networks, you'll learn how money's core properties—medium of exchange, store of value, and unit of account—manifest across different technological implementations. The chapter connects these concepts directly to smart contract development by examining real-world applications ranging from tokenized real estate to decentralized autonomous organizations (DAOs).

By working through historical examples and modern case studies, you'll develop a crucial foundation for understanding the problems that smart contracts aim to solve and the new possibilities they enable. These insights are essential for designing effective decentralized applications and financial protocols that align with fundamental economic principles while leveraging blockchain's unique capabilities.

WHAT IS MONEY?

This, first and foremost, is a Solidity book. However, money is integral to cryptocurrencies, to blockchains such as Ethereum, to Solidity, and to smart contracts. Thus, a short chapter on money in this book is essential for you to fully and deeply understand and appreciate the bigger picture of what it is that people are, in essence, building when they use Solidity to create smart contracts. In this chapter, we'll cover the history of money for a deeper understanding of Ethereum, Solidity, smart contracts, and cryptocurrencies in general.

Money has many meanings and definitions. Money has been called a medium of exchange. This commodity represents the time and energy given to an employer to build or do something. In exchange, we get money. Another way of defining money is as energy—spent in service of an employer or on creating a service or a product—that takes physical form as coins and paper notes that can then be used in exchange for someone else's energy, who was spending their energy building a car, organizing a vacation, working at a hotel we visit, preparing the meal we would eat, and so forth. These are just a few definitions of money throughout history.

Since this is a technology book, we will look at money not as a commodity but as a language and technology, seeing the current global economy—and those before it—as a protocol built on this language and technology. It is estimated that there are around 7,000 languages in the world that people use to communicate with each other every day. There are around 700 programming languages in the world. When it comes to computers, they fundamentally understand only binary sequences of 0s and 1s, with each separate sequence of 0s and 1s meaning something entirely different from the others. Money, on the other hand, no matter the currency, is a language humans use to communicate how much they value a product or a service. On the most basic level, money is saying, “Oh, you have a great kitchen knife for sale there. I value it at 50 amounts of money. Will you sell it to me?” (Replace “amounts of money” with dollars or euro or the currency of your country.) If the person selling the kitchen knife values the labor and time that went into creating the kitchen knife to 50 amounts of money, they will say “yes,” and the kitchen knife will be sold. In this example, money acts as a language between two parties, conveying the value they assign to a specific item or service—in this case, a kitchen knife. Before finalizing the transaction, the parties use this shared “language” to communicate and agree upon the knife's value. Once agreed, the transaction proceeds: the buyer transfers the agreed-upon value to the seller, and the seller transfers ownership of the knife to the buyer.

Money as a form of technology—and the economic systems built upon it as protocols derived from this “language”—is a complex and expansive subject. It provides material spanning bachelor's, master's, and PhD programs, as well as countless books and even entire series of books, depending on the depth of exploration. In this chapter, however, we will take a condensed approach, focusing on the major milestones in the evolution of money as a technology. This will involve simplifying and omitting many details to provide a high-level overview.

Money as a Technology

Money as a technology is a contract in and of itself in two ways:

A contract is an agreement among a community of people to use a specific type of money as a medium of exchange to transact with one another for goods and services. This agreement establishes their willingness to treat this form of money as the community's transactional tool.

A contract between two parties that are not part of the same community needs a standard transactional tool to exchange goods and services. This ensures the transactional tool will be valuable to the party selling the goods and services. The following are two examples of that, one historical and one contemporary:

Historical:

Members of two different tribes engaging in commerce with one another—with each tribe using its type of money that may not be valuable to the other—agreeing to use another intermediary for commerce, such as coins, belts, animal skins, or anything else that may be of value to both tribes.

Contemporary:

Modern countries have agreed to use the US dollar (USD) as a medium of exchange for international business transactions. Before USD became the world's reserve currency, countries agreed to use the British Pound Sterling as a medium of exchange for international transactions. Before the pound, it was the Dutch guilder; before that, the Spanish Real; and before even that, the Italian Florin.

When it comes to using money as technology, money has three properties, each having a separate set of subproperties to fulfill for that property to be considered viable as use for money. Those three properties and their subproperties are discussed next.

Medium of Exchange

A medium of exchange is an intermediary technological tool between parties that can facilitate value communication in exchange for products and services. In turn, the party that exchanged their service or product for the medium of exchange can use that medium of exchange to buy the products and services that they need from someone else in an endless, circular way until the specific medium of exchange used, be it coins, paper money, or digital money, is destroyed by being taken out of circulation or is damaged irreparably to the point that no party would accept it as a medium of exchange. In the modern era, the currency issued by a country's central bank, also known as the national currency of a country, is the primary medium of exchange for participating in an economy and exchanging goods and services.

For a medium of exchange to be reliable, it must have certain subproperties:

Durability

:

This is the extent to which a currency can be used without significantly being damaged or torn.

Transportability

:

How easily can a currency be transferred from one place to another in terms of speed and weight?

Divisibility

: Can the money be divided into smaller chunks? Can a 100 USD bill be divided into smaller chunks of 50, 20, 10, 5, 2, and 1 USD and then into cents?

Fungibility

:

Is money fungible? Following the earlier dollar example, is a 100 USD bill equal to another 100 USD bill?

Noncounterfeit ability

: How easily can the currency be counterfeited?

Scarcity

:

Is the medium of exchange scarce enough in supply to ensure that there is value in it? In other words, is it rare enough to come by so that people want it? When a medium of exchange is available in abundant supply, it loses value. With a “reasonable” decrease in scarcity and consequent loss of value, people raise the price of their goods. When the surplus increases significantly, the money loses its value to the extent that it becomes useless, and nobody wants to use it anymore.

Acceptability

: The more acceptability a medium of exchange has, the more effective it becomes. Its acceptability can vary from one person to another or one store or merchant to another. The acceptability of a medium of exchange can be as narrow as only two parties agreeing to use something as a medium of exchange and transact with it. This would be extremely weak acceptability, but it is still nonetheless something that can be considered as acceptability of a medium of exchange. That said, when a medium of exchange is desired, its acceptability becomes much more potent and much more widely acceptable than just two parties, making it a more effective medium.

Store of Value

Another significant property of money is as a store of value. A store of value is a tool or property of money that allows for money's value to be saved and retrieved in the future with a degree of predictability about the value remaining, or at the very least, the same in the future. It is worth noting that a store of value is not a property of only money but also of assets, as there are different stores of value, such as gold, silver, diamonds, reserve currencies, government bonds, stocks, real estate, and other assets.

The property of being a good store of value is derived from the following:

Stability

:

Current predictions of a stable or predictable demand for an asset, be it money or any previously mentioned assets.

Predictability

:

An asset's stable or shrinking future supply. As all assets and currencies have a certain degree of unpredictability in value, today there is no perfect store of value.

Cultural and societal values

:

The perceived cultural value of certain currencies or assets that may inflate their price beyond the apparent use in industrial or other uses.

Liquidity

:

The ease with which a store of value can be converted into a medium of exchange without loss—or significant loss—in value. High liquidity means that an asset can be quickly sold or at least converted for cash (or its equivalents) when required, which enhances its utility as a store of value. For example, suppose you buy real estate and, after a few years or months, want to sell it. It could take months or possibly years to sell the real estate and convert it into money or cash. On the other hand, stocks can be sold much faster, increasing their liquidity and conversion into cash.

Portability and transferability

:

Like the transportability of a medium of exchange mentioned earlier, how easy it is for someone to transfer the assets to someone else, from one party to another, or from one geographical location to another, without any compromise in value and reduced costs. This is, of course, much easier to do when a proof of ownership document—whether digital or physical—is available. In contrast, transporting 100 bars of gold poses numerous challenges, such as moving them from one US state to another or, even worse, from one continent to another. Such a scenario increases costs and exposes the stored value to risks including damage, theft, or other unforeseen issues.

Unit of Account

The final significant property of money is that of a unit of account. For a type of money to be considered a unit of account, prices must be communicated in terms of money and currency rather than other goods. For example, “I paid 10.000 USD for a trip to Europe” instead of “I paid four cows, or 66 shares of Alphabet (Google) stock for a trip to Europe.” In money's language, prices in a unit of account properly communicate the value of goods and services, assets, liabilities, and other economic activity. In each country, the main currency tends to be the unit of account; certain exceptions apply, such as the European Union, a union of countries bound by a single currency, the euro.

For money to be considered a practical unit of account and used to communicate value for goods and services to others, it must be stable in price and not volatile. In the short term, national currencies such as the US dollar and the euro are much more stable than cryptocurrencies.

However, in the long term, the current structure of the global economy integrates inflation and the practice of printing money mainly due to money's endless supply as a natural aspect of annual economic operations. Inflation and the practice of money printing are seen as unavoidable in the current economy and desirable in activating the economy and stimulating spending by aiming for a 2 percent annual inflation. However, actual rates frequently exceed this target. Increasing the money supply through printing money and thus creating inflation impacts the currency's scarcity, decreasing its value over time.

The preceding discussions of money's properties explain why no form of money today can be considered perfect: none fulfills all the previously mentioned properties. When some of these properties are taken to the extreme, they tend to negate some of the other properties, which suggests that currently the ideal economic state is a balance between these properties while maintaining periodic adjustments toward higher or lower inflation based on the condition of the economy at a given time.

THE HISTORY AND EVOLUTION OF MONEY

Now that you have an understanding of what money is, let's pivot to gaining an understanding of its history and evolution.

Money is a fundamental part of human civilization, evolving alongside societies to meet the needs of trade, value storage, and wealth exchange. This chapter explores the history and evolution of money, from its earliest form—barter—to the development of primitive money, highlighting how humans transitioned from simple exchanges to complex economic systems.

Before currency was invented, people relied on barter within small, self-sufficient communities. Over time, the limitations of barter, such as the “double coincidence of wants,” led to the creation of primitive forms of money, including shells, wampum belts, cattle, and even salt. These items were shaped by their cultural and environmental contexts, serving as both mediums of exchange and symbols of social value.

Through this exploration, we see that money is not just a tangible object but also a psychological and social construct. Its evolution reflects humanity's drive to create tools and systems that enable peaceful trade and cooperation, ultimately shaping the economic frameworks we rely on today.

Barter

Before any currency or form of money was invented, people lived in tiny, agrarian, self-sufficient communities where everyone practically knew each other and engaged in the communication of value, shaping of contracts, and trade of services and goods they found valuable through an activity called barter