Upload
arnold-pham
View
119
Download
2
Embed Size (px)
Citation preview
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/)
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 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
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 ExpressionsUses control structures that are typical in other languages
1. if2. else3. while4. do5. for6. break7. continue8. return9. ? :
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