SlideShare a Scribd company logo
Ethereum Developers
Community
Learning Solidity
Arnold Pham
Lunyr Inc.
https://www.linkedin.com/in/arnoldpham/
Unless otherwise stated, these slides are licensed under the Creative Commons Attribution-
NonCommercial 3.0 License (https://creativecommons.org/licenses/by-nc/3.0/us/)
How to compile
• Use Browser-Solidity
• Use geth console
Learning Solidity
Basic Features (similar to Java, Javascript, Python)
• Comments
• Primitive Types
• Strings
• Arrays
• Statements
• Boolean, Conditional, and Arithmetic Expressions
• Loops
• Variables
• Literals
• Methods
Version Pragma
• Annotates source files with a prerequisite
• Comes from semantic versioning which is widely used in the JavaScript
community (https://docs.npmjs.com/misc/semver)
^pragma solidity ^0.4.0 means >= compiler version 0.4.0 but <0.5.0
Comments
Use // or /*...*/
pragma solidity ^0.4.0;
/*
Multisignature Wallet for requiring multiple owners to approve a transaction
*/
contract MultiSigWallet{
address[] public owners;
uint public required; // the number of owner approval required
}
Natspec documentation
Produced as an object when you call a contract object from eth.compile.solidity(source)
Contract objects have the following properties
• code
• info
• source
• language
• languageVersion
• compilerVersion
• abiDefinition
• userDoc
• the Natspec Doc for users
• developerDoc
• the Natspec Doc for developers
Natspec
@title: This is a title that should describe the contract and go above the contract
definition
@author: The name of the author of the contract. Should also go above the
contract definition.
@notice: Represents user documentation. This is the text that will appear to the
user to notify him of what the function he is about to execute is doing
@dev: Represents developer documentation. This is documentation that would
only be visible to the developer.
@param: Documents a parameter just like in doxygen. Has to be followed by the
parameter name.
@return: Documents the return type of a contract's function.
Structs
• Advantageous for describing a set of variables that will be repeatedly used to
describe something
• Defines a new type
• Cheaper to use than abstract contracts which require paying gas for contract
creation
Structs example
Imagine you have a contract that registers people, and will do that repeatedly
struct Person{
string name;
uint birthdate;
enum gender;
}
mapping (uint => Person) people;
uint personID;
Conditional Expressions
Uses control structures that are typical in other languages
1. if
2. else
3. while
4. do
5. for
6. break
7. continue
8. return
9. ? :
Warning with loops
Operations cost gas so it is best to construct loops to repeat a known number of
times if possible
Boolean Expressions
Widely used for throw, which reverses all side effects
function transfer(address _to, uint256 _value) returns (bool) {
var senderBalance = balances[msg.sender];
if (senderBalance >= _value && _value > 0) {
senderBalance -= _value;
balances[msg.sender] = senderBalance;
balances[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
}
return false;
}
Variables
State and local variables are in storage by default
contract Products {
mapping (address->uint) owned
}
Mappings
Only allowed for state variables
Mappings can be seen as hashtables which are virtually initialized such that
every possible key exists and is mapped to a value
Global variables
1. msg.sender
a. the address of the sender in the current call
2. msg.value
a. the amount of wei sent with the message
3. now
a. the current unix timestamp in seconds
Visibility for functions and state variables
• Public
• can be called either internally or from messages
• default for functions
• Private
• can only be called from the contract that it is defined in and not from
derived contracts
• Internal
• can be called from the contract it is defined in or in derived contracts
• default for state variables
• External
• can only be called from other contracts and via transactions
• cannot be called internally
Inheritance
• For contracts inheriting from multiple other contracts, only a single contract is
created on the blockchain
• The code from the base contracts is copied into the final contract
Use “is” to inherit from another contract
Multiple inheritance is possible
“super”
• Use “super” to call the next position in line in the inheritance hierarchy
• If Base1 calls a function of super, then it will call it on Base2 rather than on
Base1
destinationAddress.send or Currentaddress.balance
Use destinationAddress.send to send wei to a destination address from the
current contract
Use currentAddress.balance to check the balance of currentAddress
Bytes32
• Each bytes32 can store up to 32 letters (ASCII): each character is a byte.
• The EVM has a word-size of 32 bytes, so it is "optimized" for dealing with data
in chunks of 32 bytes. (Compilers, such as Solidity, have to do more work and
generate more bytecode when data isn't in chunks of 32 bytes, which
effectively leads to higher gas cost.)
Gas costs
• ~20,000 gas when a value is set to non-zero from zero
• ~5,000 gas when writing to existing storage or setting a value to zero
• ~15,000 gas refund when a non-zero value is set to zero.
Function modifiers
modifier onlyOwner() {
if (msg.sender != owner) throw;
_
}
Constructor Functions
Called once during contract creation
Calling a method from another contract
Either import the contract or use an abstract contract/interface
Example Code

More Related Content

PDF
Ethereum-Cryptocurrency (All about Ethereum)
PPTX
Write smart contract with solidity on Ethereum
PPTX
Hyperledger Fabric
PPTX
Ethereum Tutorial - Ethereum Explained | What is Ethereum? | Ethereum Explain...
PPTX
Solidity
PPTX
Hyperledger
Ethereum-Cryptocurrency (All about Ethereum)
Write smart contract with solidity on Ethereum
Hyperledger Fabric
Ethereum Tutorial - Ethereum Explained | What is Ethereum? | Ethereum Explain...
Solidity
Hyperledger

What's hot (20)

PPTX
Blockchain Consensus Protocols
PDF
Smart contracts & dApps
PPTX
Bitcoin Script
PPTX
BLOCKCHAIN
PDF
Programming smart contracts in solidity
PDF
PDF
Ethereum Solidity Fundamentals
PDF
Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...
PDF
Blockchain 101 | Blockchain Tutorial | Blockchain Smart Contracts | Blockchai...
PDF
Blockchain Study(1) - What is Blockchain?
PPTX
Solidity programming Language for beginners
PPTX
Ethereum (Blockchain Network)
PDF
Blockchain and Cryptocurrency for Dummies
PDF
Hyperledger Fabric Technical Deep Dive 20190618
PDF
Introduction to Blockchain
PPTX
Bitcoin, Ethereum, Smart Contract & Blockchain
PPSX
Blockchain HyperLedger Fabric Internals - Clavent
PPTX
Smart Contract & Ethereum
PPTX
Blockchain Smart Contract v5
PDF
Blockchain, cryptography, and consensus
 
Blockchain Consensus Protocols
Smart contracts & dApps
Bitcoin Script
BLOCKCHAIN
Programming smart contracts in solidity
Ethereum Solidity Fundamentals
Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...
Blockchain 101 | Blockchain Tutorial | Blockchain Smart Contracts | Blockchai...
Blockchain Study(1) - What is Blockchain?
Solidity programming Language for beginners
Ethereum (Blockchain Network)
Blockchain and Cryptocurrency for Dummies
Hyperledger Fabric Technical Deep Dive 20190618
Introduction to Blockchain
Bitcoin, Ethereum, Smart Contract & Blockchain
Blockchain HyperLedger Fabric Internals - Clavent
Smart Contract & Ethereum
Blockchain Smart Contract v5
Blockchain, cryptography, and consensus
 
Ad

Viewers also liked (20)

PPTX
Introduction to Ethereum
PPTX
The Ethereum Geth Client
PPTX
Ethereum Smart Contract Tutorial
PPTX
Mist and parity
PDF
A tour of ethereum ecosystem
PDF
Blockchain &amp; the Future of Democracy
PDF
History of Distributed Computing
PDF
Ethereum @ descon 2016
ODP
Dapps for Web Developers Aberdeen Techmeetup
PDF
Etherem ~ agvm
PDF
日本のIT市場のトピックス
PDF
Etherisc Versicherung neu erfinden
PPTX
Vision for a health blockchain
ODP
Introduction to Idea
PDF
"Performance Analysis of In-Network Caching in Content-Centric Advanced Meter...
PPTX
Solidity intro
PPTX
The Ethereum ÐApp IDE: Mix
PDF
Ingredients for creating dapps
PDF
Build dapps 1:3 dev tools
PDF
The future of Blockchain
Introduction to Ethereum
The Ethereum Geth Client
Ethereum Smart Contract Tutorial
Mist and parity
A tour of ethereum ecosystem
Blockchain &amp; the Future of Democracy
History of Distributed Computing
Ethereum @ descon 2016
Dapps for Web Developers Aberdeen Techmeetup
Etherem ~ agvm
日本のIT市場のトピックス
Etherisc Versicherung neu erfinden
Vision for a health blockchain
Introduction to Idea
"Performance Analysis of In-Network Caching in Content-Centric Advanced Meter...
Solidity intro
The Ethereum ÐApp IDE: Mix
Ingredients for creating dapps
Build dapps 1:3 dev tools
The future of Blockchain
Ad

Similar to Learning Solidity (20)

PPTX
Ethereum
 
PDF
How to Start Building in Web3 – Smart Contract Design & Development Part 1
PPTX
solidity programming.pptx
DOC
Advanced Qtp Book
PPTX
Blockchain Blockchain Blockchain Lec 2.1.pptx
PPTX
Switch case and looping jam
PPTX
Switch case and looping new
PPTX
.NET 4.0 Code Contracts (2010)
PPTX
C programming language:- Introduction to C Programming - Overview and Importa...
PPTX
My final requirement
PPT
VB Script Overview
PPTX
Workshop: .NET Code Contracts
PPTX
Macasu, gerrell c.
PPTX
Ecma script
PPTX
Switch case and looping
PPTX
Ethereum.pptx
PPTX
Switch case and looping kim
PPTX
Best Coding Practices in Java and C++
PPTX
Kotlin programming language
PPTX
Ethereum
Ethereum
 
How to Start Building in Web3 – Smart Contract Design & Development Part 1
solidity programming.pptx
Advanced Qtp Book
Blockchain Blockchain Blockchain Lec 2.1.pptx
Switch case and looping jam
Switch case and looping new
.NET 4.0 Code Contracts (2010)
C programming language:- Introduction to C Programming - Overview and Importa...
My final requirement
VB Script Overview
Workshop: .NET Code Contracts
Macasu, gerrell c.
Ecma script
Switch case and looping
Ethereum.pptx
Switch case and looping kim
Best Coding Practices in Java and C++
Kotlin programming language
Ethereum

Recently uploaded (20)

PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Sustainable Sites - Green Building Construction
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Digital Logic Computer Design lecture notes
DOCX
573137875-Attendance-Management-System-original
PPT
Project quality management in manufacturing
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Welding lecture in detail for understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
additive manufacturing of ss316l using mig welding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Sustainable Sites - Green Building Construction
Mechanical Engineering MATERIALS Selection
CH1 Production IntroductoryConcepts.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
R24 SURVEYING LAB MANUAL for civil enggi
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Digital Logic Computer Design lecture notes
573137875-Attendance-Management-System-original
Project quality management in manufacturing
CYBER-CRIMES AND SECURITY A guide to understanding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Welding lecture in detail for understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
additive manufacturing of ss316l using mig welding

Learning Solidity

  • 1. Ethereum Developers Community Learning Solidity Arnold Pham Lunyr Inc. https://www.linkedin.com/in/arnoldpham/ Unless otherwise stated, these slides are licensed under the Creative Commons Attribution- NonCommercial 3.0 License (https://creativecommons.org/licenses/by-nc/3.0/us/)
  • 2. How to compile • Use Browser-Solidity • Use geth console
  • 4. Basic Features (similar to Java, Javascript, Python) • Comments • Primitive Types • Strings • Arrays • Statements • Boolean, Conditional, and Arithmetic Expressions • Loops • Variables • Literals • Methods
  • 5. Version Pragma • Annotates source files with a prerequisite • Comes from semantic versioning which is widely used in the JavaScript community (https://docs.npmjs.com/misc/semver) ^pragma solidity ^0.4.0 means >= compiler version 0.4.0 but <0.5.0
  • 6. Comments Use // or /*...*/ pragma solidity ^0.4.0; /* Multisignature Wallet for requiring multiple owners to approve a transaction */ contract MultiSigWallet{ address[] public owners; uint public required; // the number of owner approval required }
  • 7. Natspec documentation Produced as an object when you call a contract object from eth.compile.solidity(source) Contract objects have the following properties • code • info • source • language • languageVersion • compilerVersion • abiDefinition • userDoc • the Natspec Doc for users • developerDoc • the Natspec Doc for developers
  • 8. Natspec @title: This is a title that should describe the contract and go above the contract definition @author: The name of the author of the contract. Should also go above the contract definition. @notice: Represents user documentation. This is the text that will appear to the user to notify him of what the function he is about to execute is doing @dev: Represents developer documentation. This is documentation that would only be visible to the developer. @param: Documents a parameter just like in doxygen. Has to be followed by the parameter name. @return: Documents the return type of a contract's function.
  • 9. Structs • Advantageous for describing a set of variables that will be repeatedly used to describe something • Defines a new type • Cheaper to use than abstract contracts which require paying gas for contract creation
  • 10. Structs example Imagine you have a contract that registers people, and will do that repeatedly struct Person{ string name; uint birthdate; enum gender; } mapping (uint => Person) people; uint personID;
  • 11. Conditional Expressions Uses control structures that are typical in other languages 1. if 2. else 3. while 4. do 5. for 6. break 7. continue 8. return 9. ? :
  • 12. Warning with loops Operations cost gas so it is best to construct loops to repeat a known number of times if possible
  • 13. Boolean Expressions Widely used for throw, which reverses all side effects function transfer(address _to, uint256 _value) returns (bool) { var senderBalance = balances[msg.sender]; if (senderBalance >= _value && _value > 0) { senderBalance -= _value; balances[msg.sender] = senderBalance; balances[_to] += _value; Transfer(msg.sender, _to, _value); return true; } return false; }
  • 14. Variables State and local variables are in storage by default contract Products { mapping (address->uint) owned }
  • 15. Mappings Only allowed for state variables Mappings can be seen as hashtables which are virtually initialized such that every possible key exists and is mapped to a value
  • 16. Global variables 1. msg.sender a. the address of the sender in the current call 2. msg.value a. the amount of wei sent with the message 3. now a. the current unix timestamp in seconds
  • 17. Visibility for functions and state variables • Public • can be called either internally or from messages • default for functions • Private • can only be called from the contract that it is defined in and not from derived contracts • Internal • can be called from the contract it is defined in or in derived contracts • default for state variables • External • can only be called from other contracts and via transactions • cannot be called internally
  • 18. Inheritance • For contracts inheriting from multiple other contracts, only a single contract is created on the blockchain • The code from the base contracts is copied into the final contract
  • 19. Use “is” to inherit from another contract
  • 21. “super” • Use “super” to call the next position in line in the inheritance hierarchy • If Base1 calls a function of super, then it will call it on Base2 rather than on Base1
  • 22. destinationAddress.send or Currentaddress.balance Use destinationAddress.send to send wei to a destination address from the current contract Use currentAddress.balance to check the balance of currentAddress
  • 23. Bytes32 • Each bytes32 can store up to 32 letters (ASCII): each character is a byte. • The EVM has a word-size of 32 bytes, so it is "optimized" for dealing with data in chunks of 32 bytes. (Compilers, such as Solidity, have to do more work and generate more bytecode when data isn't in chunks of 32 bytes, which effectively leads to higher gas cost.)
  • 24. Gas costs • ~20,000 gas when a value is set to non-zero from zero • ~5,000 gas when writing to existing storage or setting a value to zero • ~15,000 gas refund when a non-zero value is set to zero.
  • 25. Function modifiers modifier onlyOwner() { if (msg.sender != owner) throw; _ }
  • 26. Constructor Functions Called once during contract creation
  • 27. Calling a method from another contract Either import the contract or use an abstract contract/interface

Editor's Notes

  • #3: Solidity is a statically typed language. The type of every variable must be specified at compile-time
  • #6: Allows changes that do not modify the left-most non-zero digit
  • #10: http://ethereum.stackexchange.com/questions/8615/child-contract-vs-struct Under most circumstances, data structures, even complicated ones, should be structs. Here are some reasons to choose structs: Contracts are more expensive. You'll have to pay for the contract's creation initially, and every time you access it, you'll need to pay for a call to another contracts. This is much, much more expensive than a sha3 for a lookup inside the contract's own storage. Contracts must replicate code. Every contract must contain the logic for setting and altering values, which you must pay for in gas. A struct needs only set of functions. Contracts are exposed. Anyone can send a message to a contract. If you use a contract for storing data structures, you'll have to manage access manually. Libraries might be what you're actually looking for. If you find yourself looking for functions on a data structure (i.e. foo.bar()), you can use a library contract to do it without the additional complexity of creating contracts for every instance. Here are some reasons where contracts would be superior: Contracts can be polymorphic. A contract could potentially contain arbitrary code. This allows multiple types to be intermingled, or even to have users bring their own logic. The logic will be split. In this registrar contract each Deed could have been a struct. By making Deeds their own contracts, there is less of an attack surface for the Deeds themselves, reducing the chance of another TheDAO-scale disaster. Contracts are exposed. If users have to configure their data structures, having a unique address they can interface with directly may prove simpler. Contracts are contracts. A child contract can do anything a contract can do. If the data structures, for some reason, would own things as an address would, then having a contract would be far superior. A contract can directly hold Ether, as opposed to a struct sharing the main contract's balance with other structs. However, these are less common. My advice: try it with structs first, and use data structure contracts as a last resort.
  • #15: Solidity is a statically typed language. The type of every variable must be specified at compile-time
  • #23: you can check the