Wei-Jen Liu
Write Smart Contract with
Solidity on Ethereum
whoami
• dennisliuu

• 魔術⽅塊、爬⼭、社群仔

• NTHU EE graduate student

• 25sprout

• SurveyCake frontend team
Mindset
• Jserv: ⾼度分⼯的社會,不⽤每個⼈都是 developer

• 非 workshop

• 單純分享會
出去線
• Basic of solidity

• Remix: web browser IDE

• Ganache: local blockchain
How Blockchain works
Ethereum Blockchain framework
• Agreements are written as Smart
Contracts. 

• Smart Contracts are business
logic 

• Transactions are validated
against Smart Contract
Smart Contract
• Digitized and codi
fi
ed rules of
transaction between accounts 

• In Ethereum world smart
contracts are written in language
called Solidity and deployed into
Ethereum network
Smart Contract
Smart Contract
How smart contract works
Solidity – Language to write Smart Contract
• Solidity is a contract-oriented, high-level language for implementing smart
contracts. 

• It was in
fl
uenced by C++, Python and JavaScript and is designed to target the
Ethereum Virtual Machine (EVM). 

• Solidity is statically typed, supports inheritance, libraries and complex user-
de
fi
ned types among other features. 

• Written in .sol
fi
les
Solidity Compiler
Application Binary Interface
Web browser based IDE
• Write Solidity smart contracts 

• Test smart contract 

• Deploy 

• Run the smart contracts.
Remix web tool
Write & compile
HelloWorld
program in Remix
Use memory storage For return struct or array
types of data is good practice and it improves
the performance of code and from solidity
version 0.5.0, it is compulsory to use memory
storage for this type of data types.
Deploy & run HelloWorld program in Remix
Deploy & run HelloWorld program in Remix
Comments
• Single-line comments are denoted by a double forward slash //, 

• Multiline comments are denoted using /* and */. 

• Natspec has two formats: ///
Import
• import <<
fi
lename>>;

• import ‘CommonLibrary.sol';
State variables
• Variables in programming refer to storage location that can contain values.
These values can be changed during runtime. 

• State variables that are permanently stored in a blockchain/Ethereum ledger
by miners. 

• State variables store the current values of the contract. 

• The allocated memory for a state variable is statically assigned and it cannot
change (the size of memory allocated) during the lifetime of the contract.
State variable access
• internal: This variable can only be used within current contract functions and
any contract that inherits from them.

• int internal StateVariable;

• private
• int public stateIntVariable;

• constant: This quali
fi
er makes state variables immutable.

• bool constant hasIncome = true;
Data types
• bool 

• uint/int 

• bytes 

• address
• mapping

• key-value table
(keccak256 hash)

• enum

• user-de
fi
ned type

• struct 

• Bytes/String
Functions
Function Types
• public - Anyone can call this function 

• private - Only this contract can call this function. 

• view - This function returns data and does not modify the contract's data 

• constant - This function returns data and does not modify the contract's data 

• pure - Function will not modify or even read the contract's data 

• payable - When someone call this function they might send ether along
• Solidity supports declaring a
constructor within a contract. 

• Constructors are optional in
Solidity and the compiler induces
a default constructor when no
constructor is explicitly de
fi
ned.

• The constructor is executed
once while deploying the
contract.
Constructors
• In Solidity, a modi
fi
er is always
associated with a function. 

• A modi
fi
er in programming
languages refers to a construct
that changes the behavior of the
executing code.
Modifier
Conditions & Loop
Inheritance
• Solidity supports inheritance between smart contracts. 

• Inheritance is the process of de
fi
ning multiple contracts that are related to
each other through parent-child relationships.

• Solidity supports multiple types of inheritance, including multiple inheritance.
Polymorphism
Polymorphism means having multiple forms of functions.
Wallet – Install Metamask to Chrome Browser
Deploy HelloWorld to local ganache
Deploy HelloWorld to local ganache
Get free ether for Ropsten Ethereum test network
• https://faucet.ropsten.be/
Deploy HelloWorld to Ropsten TestNet
Deploy HelloWorld to Ropsten TestNet
View your Contract
Initialization
Project
Create local blockchain
Project
Migrations
• Automatically deploy contracts
to blockchain
Project
Migrations
• Automatically deploy contracts
to blockchain
Project
Write your contract
• https://github.com/
dappuniversity/dstorage/blob/
master/src/contracts/
DStorage.sol
Project
Project
Dropbox Clone with Blockchain
• https://www.youtube.com/watch?v=1KwaUyjLa4Q

• https://github.com/dappuniversity/dstorage
Hardhat
Ethereum development
environment for
professionals
https://hardhat.org/

Write Smart Contract with Solidity on Ethereum