SlideShare a Scribd company logo
1 of 28
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
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

What's hot

Blockchain basics
Blockchain basicsBlockchain basics
Blockchain basicsRomit Bose
 
The best smart contract platforms in 2021
The best smart contract platforms in 2021The best smart contract platforms in 2021
The best smart contract platforms in 2021OliviaJune1
 
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...Edureka!
 
ERC20 Step-by-Step - Creating Your First Ethereum Token
ERC20 Step-by-Step - Creating Your First Ethereum TokenERC20 Step-by-Step - Creating Your First Ethereum Token
ERC20 Step-by-Step - Creating Your First Ethereum TokenCodeOps Technologies LLP
 
Ethereum in a nutshell
Ethereum in a nutshellEthereum in a nutshell
Ethereum in a nutshellDaniel Chan
 
Blockchain Study(1) - What is Blockchain?
Blockchain Study(1) - What is Blockchain?Blockchain Study(1) - What is Blockchain?
Blockchain Study(1) - What is Blockchain?Fermat Jade
 
Blockchain Tutorial For Beginners - 2 | Blockchain Technology | Blockchain Tu...
Blockchain Tutorial For Beginners - 2 | Blockchain Technology | Blockchain Tu...Blockchain Tutorial For Beginners - 2 | Blockchain Technology | Blockchain Tu...
Blockchain Tutorial For Beginners - 2 | Blockchain Technology | Blockchain Tu...Simplilearn
 
Blockchain Presentation
Blockchain PresentationBlockchain Presentation
Blockchain PresentationZied GUESMI
 
Intro to Web3
Intro to Web3Intro to Web3
Intro to Web3asasdasd5
 
Smart Contract & Ethereum
Smart Contract & EthereumSmart Contract & Ethereum
Smart Contract & EthereumAkshay Singh
 
Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...
Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...
Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...Edureka!
 
Ethereum Solidity Fundamentals
Ethereum Solidity FundamentalsEthereum Solidity Fundamentals
Ethereum Solidity FundamentalsEno Bassey
 
Blockchain Interview Questions And Answers | Blockchain Technology Interview ...
Blockchain Interview Questions And Answers | Blockchain Technology Interview ...Blockchain Interview Questions And Answers | Blockchain Technology Interview ...
Blockchain Interview Questions And Answers | Blockchain Technology Interview ...Simplilearn
 
How Blockchain & Cryptocurrencies Redefining Financial Instruments in Finance
How Blockchain & Cryptocurrencies Redefining Financial Instruments in Finance How Blockchain & Cryptocurrencies Redefining Financial Instruments in Finance
How Blockchain & Cryptocurrencies Redefining Financial Instruments in Finance Kellton Tech Solutions Ltd
 
Blockchain Introduction Presentation
Blockchain Introduction PresentationBlockchain Introduction Presentation
Blockchain Introduction PresentationAmr Alaa Yassen
 
Brand New Web3 Wallet
Brand New Web3 WalletBrand New Web3 Wallet
Brand New Web3 Walletssuser7259e6
 

What's hot (20)

Blockchain basics
Blockchain basicsBlockchain basics
Blockchain basics
 
DeFi PPT.pptx
DeFi PPT.pptxDeFi PPT.pptx
DeFi PPT.pptx
 
The best smart contract platforms in 2021
The best smart contract platforms in 2021The best smart contract platforms in 2021
The best smart contract platforms in 2021
 
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
 
ERC20 Step-by-Step - Creating Your First Ethereum Token
ERC20 Step-by-Step - Creating Your First Ethereum TokenERC20 Step-by-Step - Creating Your First Ethereum Token
ERC20 Step-by-Step - Creating Your First Ethereum Token
 
Ethereum in a nutshell
Ethereum in a nutshellEthereum in a nutshell
Ethereum in a nutshell
 
Blockchain Study(1) - What is Blockchain?
Blockchain Study(1) - What is Blockchain?Blockchain Study(1) - What is Blockchain?
Blockchain Study(1) - What is Blockchain?
 
How NFT Works
How NFT WorksHow NFT Works
How NFT Works
 
Blockchain Basics
Blockchain BasicsBlockchain Basics
Blockchain Basics
 
Blockchain Tutorial For Beginners - 2 | Blockchain Technology | Blockchain Tu...
Blockchain Tutorial For Beginners - 2 | Blockchain Technology | Blockchain Tu...Blockchain Tutorial For Beginners - 2 | Blockchain Technology | Blockchain Tu...
Blockchain Tutorial For Beginners - 2 | Blockchain Technology | Blockchain Tu...
 
Blockchain Presentation
Blockchain PresentationBlockchain Presentation
Blockchain Presentation
 
Intro to Web3
Intro to Web3Intro to Web3
Intro to Web3
 
Smart Contract & Ethereum
Smart Contract & EthereumSmart Contract & Ethereum
Smart Contract & Ethereum
 
Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...
Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...
Blockchain Explained | Blockchain Simplified | Blockchain Technology | Blockc...
 
Ethereum Solidity Fundamentals
Ethereum Solidity FundamentalsEthereum Solidity Fundamentals
Ethereum Solidity Fundamentals
 
Ethereum-Cryptocurrency (All about Ethereum)
Ethereum-Cryptocurrency (All about Ethereum) Ethereum-Cryptocurrency (All about Ethereum)
Ethereum-Cryptocurrency (All about Ethereum)
 
Blockchain Interview Questions And Answers | Blockchain Technology Interview ...
Blockchain Interview Questions And Answers | Blockchain Technology Interview ...Blockchain Interview Questions And Answers | Blockchain Technology Interview ...
Blockchain Interview Questions And Answers | Blockchain Technology Interview ...
 
How Blockchain & Cryptocurrencies Redefining Financial Instruments in Finance
How Blockchain & Cryptocurrencies Redefining Financial Instruments in Finance How Blockchain & Cryptocurrencies Redefining Financial Instruments in Finance
How Blockchain & Cryptocurrencies Redefining Financial Instruments in Finance
 
Blockchain Introduction Presentation
Blockchain Introduction PresentationBlockchain Introduction Presentation
Blockchain Introduction Presentation
 
Brand New Web3 Wallet
Brand New Web3 WalletBrand New Web3 Wallet
Brand New Web3 Wallet
 

Viewers also liked

Introduction to Ethereum
Introduction to EthereumIntroduction to Ethereum
Introduction to EthereumArnold Pham
 
The Ethereum Geth Client
The Ethereum Geth ClientThe Ethereum Geth Client
The Ethereum Geth ClientArnold Pham
 
Ethereum Smart Contract Tutorial
Ethereum Smart Contract TutorialEthereum Smart Contract Tutorial
Ethereum Smart Contract TutorialArnold Pham
 
A tour of ethereum ecosystem
A tour of ethereum ecosystemA tour of ethereum ecosystem
A tour of ethereum ecosystemChang-Wu Chen
 
Blockchain &amp; the Future of Democracy
Blockchain &amp; the Future of DemocracyBlockchain &amp; the Future of Democracy
Blockchain &amp; the Future of DemocracyShermin Voshmgir
 
History of Distributed Computing
History of Distributed ComputingHistory of Distributed Computing
History of Distributed ComputingShermin Voshmgir
 
Dapps for Web Developers Aberdeen Techmeetup
Dapps for Web Developers Aberdeen TechmeetupDapps for Web Developers Aberdeen Techmeetup
Dapps for Web Developers Aberdeen TechmeetupJames Littlejohn
 
Etherem ~ agvm
Etherem ~ agvmEtherem ~ agvm
Etherem ~ agvmgha sshee
 
日本のIT市場のトピックス
日本のIT市場のトピックス日本のIT市場のトピックス
日本のIT市場のトピックスHiroyasu NOHATA
 
Etherisc Versicherung neu erfinden
Etherisc Versicherung neu erfindenEtherisc Versicherung neu erfinden
Etherisc Versicherung neu erfindenStephan Karpischek
 
Vision for a health blockchain
Vision for a health blockchainVision for a health blockchain
Vision for a health blockchainJames Littlejohn
 
"Performance Analysis of In-Network Caching in Content-Centric Advanced Meter...
"Performance Analysis of In-Network Caching in Content-Centric Advanced Meter..."Performance Analysis of In-Network Caching in Content-Centric Advanced Meter...
"Performance Analysis of In-Network Caching in Content-Centric Advanced Meter...Khaled Ben Driss
 
The Ethereum ÐApp IDE: Mix
The Ethereum ÐApp IDE: MixThe Ethereum ÐApp IDE: Mix
The Ethereum ÐApp IDE: Mixgavofyork
 
NodeJS Blockchain.info Wallet
NodeJS Blockchain.info WalletNodeJS Blockchain.info Wallet
NodeJS Blockchain.info WalletSjors Provoost
 
Ingredients for creating dapps
Ingredients for creating dappsIngredients for creating dapps
Ingredients for creating dappsStefaan Ponnet
 

Viewers also liked (20)

Introduction to Ethereum
Introduction to EthereumIntroduction to Ethereum
Introduction to Ethereum
 
The Ethereum Geth Client
The Ethereum Geth ClientThe Ethereum Geth Client
The Ethereum Geth Client
 
Ethereum Smart Contract Tutorial
Ethereum Smart Contract TutorialEthereum Smart Contract Tutorial
Ethereum Smart Contract Tutorial
 
Mist and parity
Mist and parityMist and parity
Mist and parity
 
A tour of ethereum ecosystem
A tour of ethereum ecosystemA tour of ethereum ecosystem
A tour of ethereum ecosystem
 
Smart contracts & dApps
Smart contracts & dAppsSmart contracts & dApps
Smart contracts & dApps
 
Blockchain &amp; the Future of Democracy
Blockchain &amp; the Future of DemocracyBlockchain &amp; the Future of Democracy
Blockchain &amp; the Future of Democracy
 
History of Distributed Computing
History of Distributed ComputingHistory of Distributed Computing
History of Distributed Computing
 
Ethereum @ descon 2016
Ethereum @ descon 2016Ethereum @ descon 2016
Ethereum @ descon 2016
 
Dapps for Web Developers Aberdeen Techmeetup
Dapps for Web Developers Aberdeen TechmeetupDapps for Web Developers Aberdeen Techmeetup
Dapps for Web Developers Aberdeen Techmeetup
 
Etherem ~ agvm
Etherem ~ agvmEtherem ~ agvm
Etherem ~ agvm
 
日本のIT市場のトピックス
日本のIT市場のトピックス日本のIT市場のトピックス
日本のIT市場のトピックス
 
Etherisc Versicherung neu erfinden
Etherisc Versicherung neu erfindenEtherisc Versicherung neu erfinden
Etherisc Versicherung neu erfinden
 
Vision for a health blockchain
Vision for a health blockchainVision for a health blockchain
Vision for a health blockchain
 
Introduction to Idea
Introduction to IdeaIntroduction to Idea
Introduction to Idea
 
"Performance Analysis of In-Network Caching in Content-Centric Advanced Meter...
"Performance Analysis of In-Network Caching in Content-Centric Advanced Meter..."Performance Analysis of In-Network Caching in Content-Centric Advanced Meter...
"Performance Analysis of In-Network Caching in Content-Centric Advanced Meter...
 
Solidity intro
Solidity introSolidity intro
Solidity intro
 
The Ethereum ÐApp IDE: Mix
The Ethereum ÐApp IDE: MixThe Ethereum ÐApp IDE: Mix
The Ethereum ÐApp IDE: Mix
 
NodeJS Blockchain.info Wallet
NodeJS Blockchain.info WalletNodeJS Blockchain.info Wallet
NodeJS Blockchain.info Wallet
 
Ingredients for creating dapps
Ingredients for creating dappsIngredients for creating dapps
Ingredients for creating dapps
 

Similar to Learn Solidity Basics Like Comments, Variables, Structs

Ethereum
EthereumEthereum
EthereumV C
 
How to Start Building in Web3 – Smart Contract Design & Development Part 1
How to Start Building in Web3 – Smart Contract Design & Development Part 1How to Start Building in Web3 – Smart Contract Design & Development Part 1
How to Start Building in Web3 – Smart Contract Design & Development Part 1Zeeve
 
solidity programming.pptx
solidity programming.pptxsolidity programming.pptx
solidity programming.pptxRohiniBagul4
 
Advanced Qtp Book
Advanced Qtp BookAdvanced Qtp Book
Advanced Qtp BookG.C Reddy
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping newaprilyyy
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)Koen Metsu
 
Workshop: .NET Code Contracts
Workshop: .NET Code ContractsWorkshop: .NET Code Contracts
Workshop: .NET Code ContractsRainer Stropek
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.gerrell
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and loopingaprilyyy
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kimkimberly_Bm10203
 
Best Coding Practices in Java and C++
Best Coding Practices in Java and C++Best Coding Practices in Java and C++
Best Coding Practices in Java and C++Nitin Aggarwal
 
Kotlin programming language
Kotlin programming languageKotlin programming language
Kotlin programming languageDSCMESCOE
 
Advanced+qtp+open+order
Advanced+qtp+open+orderAdvanced+qtp+open+order
Advanced+qtp+open+orderRamu Palanki
 
Write Smart Contract with Solidity on Ethereum
Write Smart Contract with Solidity on EthereumWrite Smart Contract with Solidity on Ethereum
Write Smart Contract with Solidity on Ethereum劉 維仁
 

Similar to Learn Solidity Basics Like Comments, Variables, Structs (20)

Ethereum
EthereumEthereum
Ethereum
 
How to Start Building in Web3 – Smart Contract Design & Development Part 1
How to Start Building in Web3 – Smart Contract Design & Development Part 1How to Start Building in Web3 – Smart Contract Design & Development Part 1
How to Start Building in Web3 – Smart Contract Design & Development Part 1
 
solidity programming.pptx
solidity programming.pptxsolidity programming.pptx
solidity programming.pptx
 
Advanced Qtp Book
Advanced Qtp BookAdvanced Qtp Book
Advanced Qtp Book
 
Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
.NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010).NET 4.0 Code Contracts (2010)
.NET 4.0 Code Contracts (2010)
 
My final requirement
My final requirementMy final requirement
My final requirement
 
VB Script Overview
VB Script OverviewVB Script Overview
VB Script Overview
 
Workshop: .NET Code Contracts
Workshop: .NET Code ContractsWorkshop: .NET Code Contracts
Workshop: .NET Code Contracts
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
Ecma script
Ecma scriptEcma script
Ecma script
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Ethereum.pptx
Ethereum.pptxEthereum.pptx
Ethereum.pptx
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
 
Best Coding Practices in Java and C++
Best Coding Practices in Java and C++Best Coding Practices in Java and C++
Best Coding Practices in Java and C++
 
Kotlin programming language
Kotlin programming languageKotlin programming language
Kotlin programming language
 
Ethereum
EthereumEthereum
Ethereum
 
Advanced+qtp+open+order
Advanced+qtp+open+orderAdvanced+qtp+open+order
Advanced+qtp+open+order
 
Write Smart Contract with Solidity on Ethereum
Write Smart Contract with Solidity on EthereumWrite Smart Contract with Solidity on Ethereum
Write Smart Contract with Solidity on Ethereum
 

Recently uploaded

welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Steel Structures - Building technology.pptx
Steel Structures - Building technology.pptxSteel Structures - Building technology.pptx
Steel Structures - Building technology.pptxNikhil Raut
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Steel Structures - Building technology.pptx
Steel Structures - Building technology.pptxSteel Structures - Building technology.pptx
Steel Structures - Building technology.pptx
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 

Learn Solidity Basics Like Comments, Variables, Structs

  • 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
  • 3.
  • 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

  1. Solidity is a statically typed language. The type of every variable must be specified at compile-time
  2. Allows changes that do not modify the left-most non-zero digit
  3. 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.
  4. Solidity is a statically typed language. The type of every variable must be specified at compile-time
  5. you can check the