The document summarizes key concepts in Solidity, including:
1. Solidity is a programming language for smart contracts with influences from Python, C++ and JavaScript. Remix is a good tool for trying out Solidity.
2. The layout of a Solidity smart contract includes pragmas, imports, the contract name, state variables, events, functions, modifiers, and other optional elements like structs.
3. Solidity supports basic datatypes like booleans, integers, bytes, strings, and addresses. Mappings and arrays can also be used to store and access data.
The document summarizes key concepts in Solidity, including:
1. Solidity is a programming language for smart contracts with influences from Python, C++ and JavaScript. Remix is a good tool for trying out Solidity.
2. The layout of a Solidity smart contract includes pragmas, imports, the contract name, state variables, events, functions, modifiers, and other optional elements like structs.
3. Solidity supports basic datatypes like booleans, integers, bytes, strings, and addresses. Mappings and arrays can also be used to store and access data.
The document summarizes key concepts in Solidity, including:
1. Solidity is a programming language for smart contracts with influences from Python, C++ and JavaScript. Remix is a good tool for trying out Solidity.
2. The layout of a Solidity smart contract includes pragmas, imports, the contract name, state variables, events, functions, modifiers, and other optional elements like structs.
3. Solidity supports basic datatypes like booleans, integers, bytes, strings, and addresses. Mappings and arrays can also be used to store and access data.
The document summarizes key concepts in Solidity, including:
1. Solidity is a programming language for smart contracts with influences from Python, C++ and JavaScript. Remix is a good tool for trying out Solidity.
2. The layout of a Solidity smart contract includes pragmas, imports, the contract name, state variables, events, functions, modifiers, and other optional elements like structs.
3. Solidity supports basic datatypes like booleans, integers, bytes, strings, and addresses. Mappings and arrays can also be used to store and access data.
Version 0.4.25 f u n c t i o n f u n c t i o n 1 ( a d d r e s s p1 , u i n t p2 ) 4. Enums
{ Enums are one way to create a user-defined type in Solidity where 1. Introduction / / body o f f u n c t i o n h e r e } a keyword can be mapped to an integer value. Solidity is a high-level programming language, to implement // default function Example smart contracts. function ( ) { throw ; c o n t r a c t Enums { It has Python, C++ and JavaScript influences and is used for } } enum Ac { G o L e f t , G o R i g h t , G o S t r a i g h t , S i t S t i l l Ethereum Virtual Machine ( EVM ). } Ac c h o i c e ; For the purposes of learning, Remix is currently the most optimal A _ c o n s t a n t = Ac . G o S t r a i g h t ; 3. Datatypes way of trying out Solidity. function s e t S i t S t i l l () { Link: https://remix.ethereum.org/ c h o i c e = Ac . S i t S t i l l ; Booleans } The possible values are constants true and false. } Example 2. Layout of a Solidity- Smart Contract bool foo = true; Integers 5. Operators pragma s o l i d i t y 0 . 4 . 8 ; / / import s e c t i o n • int - signed integer Booleans import " filename " ; / / begin the c o n t r a c t • uint256 - unsigned integer • ! -> (logical negation) c o n t r a c t ContractName { • ufixed - unsigned fixed-point number / / T h i s i s a s i n g l e − l i n e comment . • -> (logical conjunction, âĂIJandâĂİ) /* Note - floating-point numbers are not supported by Solidity yet. • || -> (logical disjunction, âĂIJorâĂİ) This is a Bytes m u l t i − l i n e comment . Bytes is essentially a dynamic array of bytes. The maximum bytes • == -> (equality) */ array is ‘bytes32‘. // State Variables • != - > (inequality) bytes32 foo = address public stateVariable1 ; 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF uint public stateVariable2 ; FFFFFFFFF; uint private stateVariable3 ; 6. Arrays Strings s t r i n g p u b l i c c o n s t a n t ID = 0 x 1 2 3 4 ; String literals are written with either double or single-quotes (foo Operators / / Events or bar). Index access: x[k] for 0 <= k < len(x) returns the k-th element. event _Event1 ( address par1 , u i n t par2 ) ; Example: Members e v e n t _ E v e n t 2 ( a d d r e s s pa ram1 ) ; string text = "Hello World"; .length returns size of the array. event _Event3 ( ) ; / / Function Modifiers Address .push works only for dynamic storage arrays and bytes (not string). modifier onlyIfOwnerModifier ( ) { Special data type that holds a 20 byte value . It appends an element at the end of the array. The function i f ( msg . s e n d e r ! = owner ) t h r o w ; Address also comes with data types members which are ‘balance‘ returns the new length. _; and ‘transfer‘. Example } Example How t o d e c l a r e a r r a y : / / S t r u c t , a r r a y s o r Enum i f any h e r e pragma s o l i d i t y ^ 0 . 4 . 1 1 ; Patient [] patients ; enum enum1 { v a l 1 , v a l 2 , v a l 3 } c o n t r a c t Address { How t o push d a t a i n t o t h e a r r a y : mapping ( a d d r e s s = > u i n t ) b a l a n c e s ; function getBalance ( address_address ) function addPatient ( uint_id , string_name ) { / / Define c o n s u t r u c t here public constant returns ( uint256 ) { p a t i e n t s . push ( new P a t i e n t ( _ i d , _name ) ) ; } function ContractN ( uint i n i t i a l C o i n s ) return_address . balance ; How t o a c c e s s i n d e x v a l u e : { } for ( uint i =0; i < patients . length ; i ++){ / / I n i t i a l i z e s t a t e v a r i a b l e s here } i f ( patients [ i ] . getId ()== id ) Loops pure r e t u r n s ( u i n t ) { for (uint i = 0; i < a.length; i ++) a[i] = i; 9. Exception Handling in Solidity return a * (b + 42); Since version 0.4.13 we have a new Exception handling in Solidity. } Instead of a simple throw we have now revert(), require() and } 7. Different Types assert() Mapping This line: 12. Fallback Function contract MappingExample { i f ( msg . s e n d e r ! = owner ) { t h r o w ; } A contract can have exactly one unnamed function. This function mapping ( address => u i n t ) p u b l i c balances ; cannot have arguments and cannot return anything. It is executed function update ( u i n t newBalance ) p u b l i c { currently behaves exactly the same as all of the following: on a call to the contract if none of the other functions matches the balances [ msg . s e n d e r ] = n e w B a l a n c e ; } } i f ( msg . s e n d e r ! = owner ) { r e v e r t ( ) ; } given function identifier (or if no data was supplied at all). a s s e r t ( msg . s e n d e r == owner ) ; Example Structure r e q u i r e ( msg . s e n d e r == owner ) ; contract Sink { contract Voting { function ( ) public payable { } mapping ( b y t e s 3 2 = > u i n t 8 ) p u b l i c vR ; } s t r u c t cL { uint id ; 10. Events contract Caller { b y t e s 3 2 name ; } Events are convenience interfaces with the EVM logging facilities. function c a l l T e s t ( Test test ) public { uint length ; Example t e s t . c a l l (0 x a b c d e f 0 1 ) ; / / hash doe c L i s t [ ] public candidates ; pragma s o l i d i t y ^ 0 . 4 . 2 1 ; } } F u n c t i o n V o t i n g ( b y t e s 3 2 [ ] cn ) contract SimpleAuction { p u b l i c { l e n g t h = cn . l e n g t h ; event HighestBidIncreased ( for ( uint i =0; i < length ; i ++) 13. How Structure, Loop and Self Destruction Works a d d r e s s b i d d e r , u i n t amount ) ; / / E v e n t { c a n d i d a t e s . push ( c L ( { i d : i , name : c n [ i ] } ) ) ; Example function bid ( ) public payable { } } // . . . Function Modifiers emit H i g h e s t B i d I n c r e a s e d ( struct Tile Modifiers can automatically check a condition prior to executing msg . s e n d e r , msg . v a l u e ) ; / / T r i g g e r i n g e v e n t { a d d r e s s owner ; the function. } address descriptorContract ; Example } uint8 elevation ; m o d i f i e r onlyOwner { } require ( f o r ( u i n t 8 y = 0; y < mapsize ; y ++) 11. Pure Functions msg . s e n d e r == owner , { Functions can be declared pure in which case they promise not to f o r ( u i n t 8 x = 0; x < mapsize ; x ++) " O n l y owner c a n c a l l t h i s f u n c t i o n . " read from or modify the state. { t i l e s [ x ] [ y ] . e l e v a t i o n = mapsize * y ); _; } } } suicide ( creator ); • Reading from state variables. // k i l l s this contract f u n c t i o n c l o s e ( ) p u b l i c onlyOwner { s e l f d e s t r u c t ( owner ) ; • Accessing this.balance or <address>.balance. } • Accessing any of the members of the block, tx, msg (with the exception of msg.sig and msg.data). 8. Library • Calling any function not marked pure. Example pragma s o l i d i t y ^ 0 . 4 . 1 6 ; • Using inline assembly that contains certain opcodes. library BigInt { struct bigint { Example uint [ ] limbs ; } } pragma s o l i d i t y ^ 0 . 4 . 1 6 ; contract C { contract C { using BigInt for BigInt . bigint ; } function f ( uint a , uint b) public