Learning Solidity
-
Upload
arnold-pham -
Category
Engineering
-
view
114 -
download
2
Transcript of Learning Solidity
![Page 1: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/1.jpg)
Ethereum Developers Community
Learning Solidity
Arnold PhamLunyr 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/)
![Page 2: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/2.jpg)
How to compile
• Use Browser-Solidity
• Use geth console
![Page 3: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/3.jpg)
![Page 4: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/4.jpg)
Basic Features (similar to Java, Javascript, Python)• Comments• Primitive Types• Strings• Arrays• Statements• Boolean, Conditional, and Arithmetic Expressions• Loops• Variables• Literals• Methods
![Page 5: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/5.jpg)
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
![Page 6: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/6.jpg)
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}
![Page 7: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/7.jpg)
Natspec documentationProduced 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
![Page 8: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/8.jpg)
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.
![Page 9: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/9.jpg)
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
![Page 10: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/10.jpg)
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;
![Page 11: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/11.jpg)
Conditional ExpressionsUses control structures that are typical in other languages
1. if2. else3. while4. do5. for6. break7. continue8. return9. ? :
![Page 12: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/12.jpg)
Warning with loops
Operations cost gas so it is best to construct loops to repeat a known number of times if possible
![Page 13: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/13.jpg)
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; }
![Page 14: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/14.jpg)
Variables
State and local variables are in storage by default
contract Products { mapping (address->uint) owned}
![Page 15: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/15.jpg)
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
![Page 16: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/16.jpg)
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
![Page 17: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/17.jpg)
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
![Page 18: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/18.jpg)
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
![Page 19: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/19.jpg)
Use “is” to inherit from another contract
![Page 20: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/20.jpg)
Multiple inheritance is possible
![Page 21: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/21.jpg)
“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
![Page 22: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/22.jpg)
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
![Page 23: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/23.jpg)
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.)
![Page 24: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/24.jpg)
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.
![Page 25: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/25.jpg)
Function modifiers
modifier onlyOwner() { if (msg.sender != owner) throw; _}
![Page 26: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/26.jpg)
Constructor Functions
Called once during contract creation
![Page 27: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/27.jpg)
Calling a method from another contract
Either import the contract or use an abstract contract/interface
![Page 28: Learning Solidity](https://reader035.fdocuments.us/reader035/viewer/2022070512/5899b9f01a28aba11e8b6cab/html5/thumbnails/28.jpg)
Example Code